准备�
上小节我们创建了user用户, 那么本小节我们来使用user用户部署我们直接的智能合约
eosiocpp构建合约文件系统
Eosiocpp 是智能合约的引导工具, 简单说需要生成智能合约就需要使用它
wast文件生成方式:
eosiocpp -o {contract}.cpp
abi是一个json格式的,用来描述智能合约如何在action和二进制程序中进行转变的方法,也用来描述数据库状态。有了abi来描述你的智能合约,开发者和用户都可以通过JSON无缝地与合约进行交互。
abi文件生成时源文件语法包括:
typedef,可以自定义类型,供合约使用。在源文件中可通过typedef关键字导出类型。
///@abi table,注解,标在一个类或者结构上,会通过eosiocpp工具生成abi文件中包含数据库表table的名字。
///@abi action,注解,标在一个函数或方法上,这是合约暴露给外部的功能接口,外部可以调用这些动作。(可以给同一个功能函数定义多个action名字,例如//@abi action action1 action2大家都调用一个。)
abi文件生成方式:
eosiocpp -g ${contract}.abi ${contract}.hpp
abi文件生成以后,我们可以找一个打开看一下,里面包含的内容很多,有各种属性,数据,方法功能的描述。
Helloworld
1: hello.cpp
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
//用eosio命名空间
using namespace eosio;
//所有的智能合约都继承自contract类
class Hello : public eosio::contract {
public:
using contract::contract;
/// @abi action
void hi( account_name user ) {
print( "Hello World , ", name{user} );
}
};
EOSIO_ABI( Hello, (hi) )
2: 编译wast 文件
eosiocpp -o hello.wast hello.cpp
root@aries-virtual-machine:/home/aries/tmp/contracts/hello# pwd
/home/aries/tmp/contracts/hello
root@aries-virtual-machine:/home/aries/tmp/contracts/hello# ls
hello.cpp
root@aries-virtual-machine:/home/aries/tmp/contracts/hello# eosiocpp -o hello.wast hello.cpp
root@aries-virtual-machine:/home/aries/tmp/contracts/hello# ls
hello.cpp hello.wasm hello.wast
root@aries-virtual-machine:/home/aries/tmp/contracts/hello#
3: 编译abi
eosiocpp -g hello.abi hello.cpp
2018-08-31T03:51:21.317 thread-0 abi_generator.hpp:68 ricardian_contracts ] Warning, no ricardian clauses found for Hello
2018-08-31T03:51:21.318 thread-0 abi_generator.hpp:75 ricardian_contracts ] Warning, no ricardian contract found for hi
Generated hello.abi ...
�
eosiocpp -o hello.abi hello.cpp
root@aries-virtual-machine:/home/aries/tmp/contracts/hello# ls
hello.abi hello.cpp hello.wasm hello.wast
部署智能合约
root@aries-virtual-machine:/home/aries/tmp/contracts# cleos set contract user hello
Reading WASM from hello/hello.wasm...
Publishing contract...
executed transaction: 8c066acae4dc535b666ac43e5d6f3fcae040818cda65325723e0ab7ca3d1e6ec 1792 bytes 4242 us
# eosio <= eosio::setcode {"account":"user","vmtype":0,"vmversion":0,"code":"0061736d01000000013b0c60027f7e006000017e60027e7e0...
# eosio <= eosio::setabi {"account":"user","abi":"0e656f73696f3a3a6162692f312e30000102686900010475736572046e616d6501000000000...
warning: transaction executed locally, but may not be confirmed by the network yet ]
执行智能合约
cleos push action user hi '{"user":"aries"}' -p user
executed transaction: 88b7e9f0ef6a3f3ec6a0642c68049d124843ed1f1e160b9044b467d0b1391f3c 104 bytes 385 us
# user <= user::hi {"user":"aries"}
>> Hello, aries
warning: transaction executed locally, but may not be confirmed by the network yet ]
网友评论