官网教程
https://developers.eos.io/eosio-home/docs/your-first-contract
编译
实例1
https://www.cnblogs.com/tokenpai/p/9175959.html
//@abi action 不能省略,否正数据不能存储
编译和部署
$ eosiocpp -o farm.wast farm.cpp
$ eosiocpp -g farm.abi farm.cpp
$ cleos create account eosio farm.code EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4
$ cleos set contract farm.code ../farm -p farm.code@active
$ cleos push action farm.code insert '[123, "fashion-farm", "2018-08-15", "hangzhouxihu"]' -p farm.code@active
$ cleos get table farm.code farm.code tfarm; #查询表数据
智能合约编译器安装(CDT)
git clone --recursive https://github.com/eosio/eosio.cdt --branch v1.3.2 --single-branch
以下为当前版本,不指定版本号,在/root/目录下,在共享目录没有成功。你可以自己试试不同目录,编译时间有点长。
git clone --recursive https://github.com/eosio/eosio.cdt --single-branch
cd eosio.cdt
sudo ./build.sh
sudo ./install.sh
部署合约hello
cleos create key --to-console
Private key: 5HxKu3XVpVS6oFUq1dchzbpLPM4FYsRbkzaHCbUeGCcAzeR4tdo
Public key: EOS5Yn38kT1FYD5z2C7BC2BLWnhi8Ltac1g3J3MhyYdSLAUGgCn8A
先解锁钱包,才能导入私钥,解锁钱包这里省略
cleos wallet import -n eosio.wallet --private-key 5HxKu3XVpVS6oFUq1dchzbpLPM4FYsRbkzaHCbUeGCcAzeR4tdo
imported private key for: EOS5Yn38kT1FYD5z2C7BC2BLWnhi8Ltac1g3J3MhyYdSLAUGgCn8A
先创建合约账户
cleos system newaccount eosio hello.code EOS5Yn38kT1FYD5z2C7BC2BLWnhi8Ltac1g3J3MhyYdSLAUGgCn8A --stake-net '50.00 SYS' --stake-cpu '50.00 SYS' --buy-ram-kbytes 8
买内存
cleos system buyram eosio hello.code -b 1024000
部署合约
cleos set contract hello.code ./
Reading WASM from /root/eosio.cdt/examples/hello/hello.wasm...
Publishing contract...
executed transaction: b1f7bd797ab536186c7159d5f32f3b4a2dd117b28b3f4d130e4c3715b18d87fd 3712 bytes 1109 us
# eosio <= eosio::setcode {"account":"hello.code","vmtype":0,"vmversion":0,"code":"0061736d0100000001390b60027f7e0060027f7f006...
# eosio <= eosio::setabi {"account":"hello.code","abi":"0e656f73696f3a3a6162692f312e31000102686900010475736572046e616d6501000...
warning: transaction executed locally, but may not be confirmed by the network yet ]
执行合约
cleos push action hello.code hi '["hangzhouxihu"]' -p hello.code@active
executed transaction: 47b730efb54baf616b1d4b5843a7794a460fc8324caef2b3cbed9c1b7bc26dd4 104 bytes 465 us
# hello.code <= hello.code::hi {"user":"hangzhouxihu"}
warning: transaction executed locally, but may not be confirmed by the network yet ]
发现上面执行合约没有打印信息,是因为合约执行在各个节点,把本节点合约终端打开即可
配置文件中选项 contracts-console = true
再执行合约
cleos push action hello.code hi '["hangzhouxihu"]' -p hello.code@active
executed transaction: d6759d5fda638391539e2dd704f1bace02fdec788c8612b23c42c8286f8bdfc2 104 bytes 763 us
# hello.code <= hello.code::hi {"user":"hangzhouxihu"}
>> Hello hangzhouxihu from hello
warning: transaction executed locally, but may not be confirmed by the network yet ]