[EOS源码分析] EOS智能合约开发实践之合约调用合约(inline action)
首先,目前dawn-4.1, dawn-4.2使用inline action是会报如下错误
transaction declares authority '{"actor":"hello.code","permission":"active"}', but does not have signatures for it under a provided delay of 0 ms
这个问题是4.0以后inline action的权限发生变化导致的。这个改动在eos官网的#3013这个issue讨论BM有提到过
核心是为智能合约账号添加eosio.code permission,比如hello.code调用hello.target智能合约,需要添加如下permission
cleos set account permission args.user active '{"threshold": 1,"keys": [{"key":"EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV", "weight":1}],"accounts": [{"permission":{"actor":"hello.code","permission":"eosio.code"},"weight":1}]}' owner -p args.user@owner
代码
新建两个contract: hello.code和hello.target
hello.target代码如下
#include <eosiolib/eosio.hpp> #include <eosiolib/print.hpp> using namespace eosio; class target : public eosio::contract { public: using contract::contract; /// @abi action void callme( account_name user ) { require_auth(user); print( "Call me from, ", name{user} ); } }; EOSIO_ABI( target, (callme) )
hello.code的代码:
class hello : public eosio::contract { public: using contract::contract; /// @abi action void hi( account_name from, account_name to) { require_auth(from); print( "Hello, from:", name{from}, ", to:", name{to}); action( //这里{to, active}必须授权给{_self, eosio.code} permission_level{to, N(active)}, //调用 hello.target合约 的'callme' action N(hello.target), N(callme), std::make_tuple(to) ).send(); } };
核心就是上面的红色字体的内容action(xx).send(),具体参数的含义是:
Action(permssion_level, other_contract_account_name, method, args)
所以:
action(permission_level{to, N(active)},
N(hello.target), N(callme),
std::make_tuple(to)
).send();
这个等价于如下命令
$cleos push action hello.target callme '["to"]' -p to
测试
$cleos create account eosio hello.code $KEY_PUB_1 $KEY_PUB_1
$cleos set contract hello.code ./hello -p hello.code
$cleos create account eosio args.user $KEY_PUB_2 $KEY_PUB_2
$cleos create account eosio hello.target $KEY_PUB_3 $KEY_PUB_3
$cleos create account eosio args.user1 $KEY_PUB_4 $KEY_PUB_4
$cleos set contract hello.target ./hello.target -p hello.target
$cleos set account permission args.user1 active '{"threshold": 1,"keys": [],"accounts": [{"permission":{"actor":"hello.code","permission":"eosio.code"},"weight":1}]}' owner -p args.user1@owner
$cleos push action hello.code hi '["args.user", "args.user1"]' -p args.user
源码一键实践
https://github.com/itleaks/eos-contract/tree/master/callcontract-exp
转载自:http://blog.csdn.net/itleaks
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »