【Add Inline Actions】
1、为了使用 inline action,需要给 eosio.code 账号添加active权限。
To use the 'activeauthority inline you will need your contract's to give active authority to
eosio.code` pseudo-authority
In order for the inline actions to be sent from addressbook
, add the eosio.code
permission to the contract's account's active permission.
cleos set account permission addressbook active '{"threshold": 1,"keys": [{"key": "EOS5NKJziczzVQefMxp3n1cp1X3FUegS7jw8LtxLvubZehqBKGiWT","weight": 1}], "accounts": [{"permission":{"actor":"addressbook","permission":"eosio.code"},"weight":1}]}' -p addressbook@owner
The eosio.code
authority is a pseudo authority implemented to enhance security, and enable contracts to execute inline actions.
2、Notify方法
[[eosio::action]] void notify(name user, std::string msg) { require_auth(get_self()); require_recipient(user); }
get_self:Get this contract name. 对于本例,就是addressbook。
require_recipient:对 name 账号调用本方法。
3、send_summary()方法
private: void send_summary(name user, std::string message){ action( //permission_level, //code, //action, //data ); }
The action constructor requires a number of parameters.
- A permission_level struct
- The contract to call (initialised using
eosio::name
type) - The action (initialised using
eosio::name
type) - The data to pass to the action, a tuple of positionals that correlate to the actions being called.
4、实现 send_summary
private: void send_summary(name user, std::string message){ action( permission_level{get_self(),"active"_n}, get_self(), "notify"_n, std::make_tuple(user, name{user}.to_string() + message) ).send(); }
make_tuple:
a function available through std
C++ library. Data passed in the tuple is positional, and determined by the order of the parameters accepted by the action that being called.
5、合约发布后,我们测试一下。
1)尝试插入数据
cleos push action addressbook upsert '["alice", "alice", "liddell", 21, "123 drink me way", "wonderland", "amsterdam"]' -p alice@active
我们发现会触发3个action。下图,第二个#表明 addressbook 触发了 notify, 第三个#表明 alice 触发了 notify。
executed transaction: e9e30524186bb6501cf490ceb744fe50654eb393ce0dd733f3bb6c68ff4b5622 160 bytes 9810 us # addressbook <= addressbook::upsert {"user":"alice","first_name":"alice","last_name":"liddell","age":21,"street":"123 drink me way","cit... # addressbook <= addressbook::notify {"user":"alice","msg":"alicesuccessfully emplaced record to addressbook"} # alice <= addressbook::notify {"user":"alice","msg":"alicesuccessfully emplaced record to addressbook"}
2)查看alice动作
cleos get actions alice
结果如下,有两个动作