基于区块链的 DAPP 开发笔记 (4)-EOS 从 helloworld 到 hello 红包 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
duimi
V2EX    程序员

基于区块链的 DAPP 开发笔记 (4)-EOS 从 helloworld 到 hello 红包

  •  
  •   duimi 2018-09-03 12:05:57 +08:00 2121 次点击
    这是一个创建于 2610 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我们不定期更新我们的开发心得,汇总在 github 上,欢迎 star: https://github.com/rrtoken/DAPP_Blog

    从 helloworld 到 hello 红包之一

    今天,我们来看看如何在 EOS 智能合约内部发起转帐(发红包)。在下面第一部分的例子代码里,我们在官方 Helloworld 标准的打招呼 hi()接口的基础上,增加新的参数“金额”,实现在打招呼的同时,通过合约内部转帐给对方账户发红包。第二部分的测试中,合约账号 eosonetest1 通过新的打招呼接口,给 eosonetest2 发送转帐。 使用合约内转帐,和标准转帐相比,可以在合约里加上各种条件和控制逻辑,实现丰富多彩的合约玩法。

    从零开始

    创建 helloworld 智能合约

    $ eosiocpp -n helloworld created helloworld from skeleton 

    得到的 helloworld.cpp 源代码是这个样子的

    #include <eosiolib/eosio.hpp> using namespace eosio; class hello : public eosio::contract { public: using contract::contract; /// @abi action void hi( account_name user ) { print( "Hello, ", name{user} ); } }; EOSIO_ABI( hello, (hi) ) 

    现在我们开始把它改造成“红包合约” 添加必要的头文件

    #include <eosiolib/asset.hpp> 

    给 hi() 函数添加通证

    void hi( account_name to, const asset& quantity ) { 

    加上一些安全检查

     require_auth( _self ); eosio_assert( quantity.is_valid(), "invalid token" ); eosio_assert( quantity.amount > 0, "must be positive quantity" ); require_recipient( _self ); require_recipient( to ); 

    添加合约里转账

     action( permission_level{ _self, N(active) }, N(eosio.token), N(transfer), std::make_tuple(_self, to, quantity, std::string("hello money")) ).send(); 

    改造好的 hi()函数变成这个样子:

     /// @abi action hi void hi( account_name to, const asset& quantity ) { require_auth( _self ); eosio_assert( quantity.is_valid(), "invalid token" ); eosio_assert( quantity.amount > 0, "must be positive quantity" ); require_recipient( _self ); require_recipient( to ); action( permission_level{ _self, N(active) }, N(eosio.token), N(transfer), std::make_tuple(_self, to, quantity, std::string("hello money")) ).send(); print( "Hello, here is some money for ", name{to} ); } 

    在 Jungle testnet 上实测

    给合约账号购买适当的 RAM

    cleos -u http://jungle.cryptolions.io:18888 system buyram -k eosonetest2 eosonetest2 1000 

    把合约布署到合约账号

    cleos -u http://jungle.cryptolions.io:18888 set contract eosonetest2 ../helloworld -p eosonetest2 

    合约里转账需要添加的 eosio.code permission

    cleos -u http://jungle.cryptolions.io:18888 set account permission eosonetest2 active '{"threshold": 1,"keys": [{"key": "EOS8xxxxxxxxxxxxxxx","weight": 1}],"accounts": [{"permission":{"actor":"eosonetest2","permission":"eosio.code"},"weight":1}]}' owner -p eosonetest2@owner 

    测试网上的输出

    测试前的余额

    @joel-Lenovo-Y520:helloworld $ cleos -u https://jungle.eosio.cr:443 get currency balance eosio.token eosonetest1 EOS 7970.3411 EOS @joel-Lenovo-Y520:helloworld $ cleos -u https://jungle.eosio.cr:443 get currency balance eosio.token eosonetest2 EOS 6733.2414 EOS 

    通过合约发红包

    @joel-Lenovo-Y520:helloworld $ cleos -u http://jungle.cryptolions.io:18888 push action eosonetest2 hi '["eosonetest1", "103.0000 EOS"]' -p eosonetest2 executed transaction: 71fxxxxxxxxxab706fxx3ea1cxxxxx1f70f05cxxxxx 120 bytes 2483 us # eosonetest2 <= eosonetest2::hi {"to":"eosonetest1","quantity":"103.0000 EOS"} >> Hello, here is some money for eosonetest1 # eosonetest1 <= eosonetest2::hi {"to":"eosonetest1","quantity":"103.0000 EOS"} # eosio.token <= eosio.token::transfer {"from":"eosonetest2","to":"eosonetest1","quantity":"103.0000 EOS","memo":"hello money"} # eosonetest2 <= eosio.token::transfer {"from":"eosonetest2","to":"eosonetest1","quantity":"103.0000 EOS","memo":"hello money"} # eosonetest1 <= eosio.token::transfer {"from":"eosonetest2","to":"eosonetest1","quantity":"103.0000 EOS","memo":"hello money"} warning: transaction executed locally, but may not be confirmed by the network yet ] 

    测试后的余额

    @joel-Lenovo-Y520:helloworld $ cleos -u https://jungle.eosio.cr:443 get currency balance eosio.token eosonetest1 EOS 8073.3411 EOS @joel-Lenovo-Y520:helloworld $ cleos -u https://jungle.eosio.cr:443 get currency balance eosio.token eosonetest2 EOS 6630.2414 EOS 

    欢迎对 DAPP 开发感兴趣的开发者加入我们,一起探讨学习: https://github.com/rrtoken/DAPP_Blog

    目前尚无回复
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     836 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 20:10 PVG 04:10 JFK 16:10
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86