首先,目前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代码如下
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
编译
请参考我的博文【EOS编写HelloWorld智能合约】
测试
//cleos create account只适合本地私有链,其他网络需要使用cleos system newaccount命令
$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
源码一键实践
从以下github网页下载源码,即可一键实践执行该智能合约
https://github.com/itleaks/eos-contract/tree/master/callcontract-exp/
/********************************
* 本文来自CSDN博主"爱踢门"
* 转载请标明出处:http://blog.csdn.net/itleaks
******************************************/
如果你对EOS,ETH技术及开发感兴趣,请入QQ群讨论: 829789117
如需实时查看最新文章,请关注公众号"区块链斜杠青年",一起探索区块链未来