目录
准备三步曲
本地环境
账户
基础文件生成
开始三步曲
结构编写
逻辑.cpp
abi编写
成功三步曲
智能合约文件生成
智能合约发布
智能合约使用
准备三步曲
这一节我们要讲的是智能合约前期的准备工作。
本地环境
本地环境部署这个就不在多说本论坛已经有很多的教程,这里列出链接:
在MacOS和Ubuntu上搭建EOS V2.0 ---爬过好多坑后的总结
除了部署本地环境以外,你还需要以下额外的操作:
cdbuild make install
编译完以后就可以使用eos的插件了:eosc/eos-wallet/eoscpp/eosd
eosd: 用来开启本地测试网络。
eosc: eos的命令行接口。
eoscpp: eos智能合约相关的功能,生成模版智能合约等等。
eos-wallet: eos钱包功能。
账户
新建一个或多个账户,部署智能合约,测试智能合约都能用得到。
本论坛有关账户的创建教程已经很完善,例如:
基础文件生成
利用eoscpp可以快速的生成模版智能合约:
eoscpp -n
执行完以后会在本地生成一个contract name的文件夹,里面包含智能合约需要的基础文件。执行截图如下:
会生成.cpp/.abi/.hpp三个必要的文件,文件中包含生成智能合约的基本内容。
开始三步曲
准备工作完成以后,开始写智能合约。
结构编写
/** * @file * @copyright definedineos/LICENSE.txt */#include #include using namespace eosio; //使用eosio命名空间namespace eos_fans{ //数据库 struct PACKED(love) { account_name lover; //被表白的人 account_name author; //表白的人 uint8_ttimes; //表白次数 }; //对谁表白action struct say { account_name lover; //被表白的人 account_name author; //表白的人 };//table 参数依次为:该表所在的默认帐户名范围,代码帐户名称具有对此表的写入权限,表名,存储在每一行中的数据类型, 记录中存储的第一个字段的类型。 using Lovers = eosio::table; }
逻辑.cpp
#include using namespace eosio;namespace eos_fans{ //代码逻辑 void say_love(const say& s) { //判断用户权限 require_auth(s.author); love exited_love; bool love_exiting = Lovers::get(s.lover, exited_love, s.author); //判断是否已经存在,如果存在times加1,如果不存在则创建数据。if(love_exiting ==true){ exited_love.times += 1; Lovers::update(exited_love, s.author); }else{ love love_to_say; love_to_say.lover = s.lover; love_to_say.author = s.author; love_to_say.times = 1; Lovers::store(love_to_say, s.author); } }}extern"C"{ voidinit() { } /// The apply method implements the dispatch of events to this contract void apply( uint64_t code, uint64_t action ) {if( code == N(eosfans) ){if(action == N(say)){ eos_fans::say_love(current_message()); } }}}// extern"C"
abi编写
abi文件根据官网描述是可以通过eoscpp自动生成的,但是笔者尝试过,生成的文件的各个字段都是空的,所以这里就需要笔者自己编写,当然如果有人生成成功,可以告诉笔者正确生成方法,在这里提前感谢。
abi文件内容如下:
{"types": [],"structs": [{"name":"love",//所存储的数据类型"base":"","fields": {"lover":"account_name","author":"account_name","times":"uint8"} },{"name":"say",//action"base":"","fields": {"lover":"account_name","author":"account_name"} }],"actions": [{"action_name":"say","type":"say"}],"tables": [{"table_name":"loves",//数据表"type":"love","index_type":"i64","key_names": ["lover"],//索引字段"key_types": ["account_name"] }]}
成功三步曲
智能合约写完以后,成功就近在眼前了。
智能合约文件生成
智能合约的各个组件编写完毕以后,下一步就是编译成.wast文件,eos有专门的工具:eoscpp,执行命令如下:
eoscpp-geosfans.wasteosfans.cpp
执行完以后如果没有报错,就说明智能合约文件生成完毕,如果有错误可以根据错误对智能合约进行更改。
智能合约发布
智能合约文件生成完以后我们就要发布智能合约,命令如下:
eoscsetcontracteosfanseosfans.wasteosfans.abi
执行完以后你会看到智能合约生成的交易:
Reading WAST...Assembling WASM...Publishing contract...{"transaction_id":"e59fac3876441bce2e65c6d20294523f6e597af71e0e7699b2d6d436a6f24444","processed": {"ref_block_num":64936,"ref_block_prefix":4035039255,"expiration":"2018-03-03T07:59:50","scope": ["eos","lome"],"signatures": ["1f030f69ef5e5f3c3f90835c04cd5ee9756a78ba22f7227ffee88dd33d3bc138450dd1e8caceb3c11bfe318389f772593f53f6103316d0c1650b514aab680a2c8d"],"messages": [{"code":"eos","type":"setcode","authorization": [{"account":"lome","permission":"active"} ],"data": {"account":"lome","vm_type":0,"vm_version":0,"code":"0061736d01000000012d0860017e0060057e7e7e7f7f017f60047e7e7f7f017f60017f0060027f7f017f60027f7f0060000060027e7e0002710703656e7606617373657274000503656e76086c6f61645f693634000103656e76067072696e7473000303656e760c726561645f6d657373616765000403656e760c726571756972655f61757468000003656e760973746f72655f693634000203656e760a7570646174655f69363400020304030306070404017000000503010001073c04066d656d6f72790200205f5a4e38656f735f66616e73387361795f6c6f766545524b4e535f3373617945000704696e69740008056170706c7900090afd0503bf0102017e017f4100410028020441306b220236020420002903081004200220002903003703180240024020002903084280808080f0e9ac98d50042808080808080ab9b8d7f200241186a411110014111470d00200220022d002841016a3a0028200041086a29030042808080808080ab9b8d7f200241186a411110061a0c010b200241013a0010200220002903003703002002200041086a2903002201370308200142808080808080ab9b8d7f2002411110051a0b4100200241306a3602040b02000bb60403027f047e017f4100410028020441c0006b220836020442002105423b2104411021034200210603400240024002400240024020054206560d0020032c00002202419f7f6a41ff017141194b0d01200241a5016a21020c020b420021072005420b580d020c030b200241d0016a41002002414f6a41ff01714105491b21020b2002ad42388642388721070b2007421f83200442ffffffff0f838621070b200341016a2103200542017c2105200720068421062004427b7c2204427a520d000b024020062000520d0042002105423b2104412021034200210603400240024002400240024020054202560d0020032c00002202419f7f6a41ff017141194b0d01200241a5016a21020c020b420021072005420b580d020c030b200241d0016a41002002414f6a41ff01714105491b21020b2002ad42388642388721070b2007421f83200442ffffffff0f838621070b200341016a2103200542017c2105200720068421062004427b7c2204427a520d000b20062001520d0041301002200841101003410f4b41c00010002008290308100420082008290300370328024020082903084280808080f0e9ac98d50042808080808080ab9b8d7f200841286a411110014111470d00200820082d003841016a3a0038200841086a29030042808080808080ab9b8d7f200841286a411110061a0c010b200841013a0020200820082903003703102008200841086a2903002205370318200542808080808080ab9b8d7f200841106a411110051a0b4100200841c0006a3602040b0b52050041040b04604000000041100b08656f7366616e73000041200b04736179000041300b09736179206c6f7665000041c0000b1e6d6573736167652073686f72746572207468616e2065787065637465640000ad01046e616d650a06617373657274020000086c6f61645f693634050000000000067072696e747301000c726561645f6d6573736167650200000c726571756972655f6175746801000973746f72655f69363404000000000a7570646174655f6936340400000000205f5a4e38656f735f66616e73387361795f6c6f766545524b4e535f33736179450301300131013204696e697400056170706c7909013001310132013301340135013601370138","code_abi": {"types": [],"structs": [{"name":"love","base":"","fields": {"lover":"account_name","author":"account_name","times":"uint8"} },{"name":"say","base":"","fields": {"lover":"account_name","author":"account_name"} } ],"actions": [{"action_name":"say","type":"say"} ],"tables": [{"table_name":"loves","index_type":"i64","key_names": ["lover"],"key_types": ["account_name"],"type":"love"} ] } },"hex_data":"0000000000a0248d0000fd090061736d01000000012d0860017e0060057e7e7e7f7f017f60047e7e7f7f017f60017f0060027f7f017f60027f7f0060000060027e7e0002710703656e7606617373657274000503656e76086c6f61645f693634000103656e76067072696e7473000303656e760c726561645f6d657373616765000403656e760c726571756972655f61757468000003656e760973746f72655f693634000203656e760a7570646174655f69363400020304030306070404017000000503010001073c04066d656d6f72790200205f5a4e38656f735f66616e73387361795f6c6f766545524b4e535f3373617945000704696e69740008056170706c7900090afd0503bf0102017e017f4100410028020441306b220236020420002903081004200220002903003703180240024020002903084280808080f0e9ac98d50042808080808080ab9b8d7f200241186a411110014111470d00200220022d002841016a3a0028200041086a29030042808080808080ab9b8d7f200241186a411110061a0c010b200241013a0010200220002903003703002002200041086a2903002201370308200142808080808080ab9b8d7f2002411110051a0b4100200241306a3602040b02000bb60403027f047e017f4100410028020441c0006b220836020442002105423b2104411021034200210603400240024002400240024020054206560d0020032c00002202419f7f6a41ff017141194b0d01200241a5016a21020c020b420021072005420b580d020c030b200241d0016a41002002414f6a41ff01714105491b21020b2002ad42388642388721070b2007421f83200442ffffffff0f838621070b200341016a2103200542017c2105200720068421062004427b7c2204427a520d000b024020062000520d0042002105423b2104412021034200210603400240024002400240024020054202560d0020032c00002202419f7f6a41ff017141194b0d01200241a5016a21020c020b420021072005420b580d020c030b200241d0016a41002002414f6a41ff01714105491b21020b2002ad42388642388721070b2007421f83200442ffffffff0f838621070b200341016a2103200542017c2105200720068421062004427b7c2204427a520d000b20062001520d0041301002200841101003410f4b41c00010002008290308100420082008290300370328024020082903084280808080f0e9ac98d50042808080808080ab9b8d7f200841286a411110014111470d00200820082d003841016a3a0038200841086a29030042808080808080ab9b8d7f200841286a411110061a0c010b200841013a0020200820082903003703102008200841086a2903002205370318200542808080808080ab9b8d7f200841106a411110051a0b4100200841c0006a3602040b0b52050041040b04604000000041100b08656f7366616e73000041200b04736179000041300b09736179206c6f7665000041c0000b1e6d6573736167652073686f72746572207468616e2065787065637465640000ad01046e616d650a06617373657274020000086c6f61645f693634050000000000067072696e747301000c726561645f6d6573736167650200000c726571756972655f6175746801000973746f72655f69363404000000000a7570646174655f6936340400000000205f5a4e38656f735f66616e73387361795f6c6f766545524b4e535f33736179450301300131013204696e697400056170706c79090130013101320133013401350136013701380002046c6f76650003056c6f7665720c6163636f756e745f6e616d6506617574686f720c6163636f756e745f6e616d650574696d65730575696e7438037361790002056c6f7665720c6163636f756e745f6e616d6506617574686f720c6163636f756e745f6e616d6501000000000000bcc103736179010000000000ac368d0369363401056c6f766572010c6163636f756e745f6e616d65046c6f7665"} ],"output": [{"notify": [],"deferred_trxs": [] } ] }}
现在我们可以来看一下我们智能合约的table内容,通过如下命令察看:
eosc get table eosfans eosfans loves //table 拥有者,智能合约名称, table名称
我们可以看到如下的显示:
智能合约使用
根据abi文件我们可以轻松使用智能合约,命令如下:
eosc push message eosfans say '{"lover": "eosfans", "author": "lome2"}' -S lome2 -p lome2@active
执行完以后我们可以看到交易的详情:
现在我们来看一下执行完几次后笔者的数据表,如下所示:
该智能合约我已经发布在测试网络,合约名称为eosfans欢迎各位一起玩。
说明: 最近一直在学习智能合约其中遇到很多困难,譬如本地用的是2.0,官网的接口文档是3.0。这次使用的是2.0,以后将会把本地版本部署为3.0,这片文章以后若是在未说明的情况下所用的版本都为3.0。
可到http://eosmonitor.io查看你的表白。