[EOS源码分析]7.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有提到过
[EOS源码分析]7.EOS智能合约开发实践之合约调用合约(inline action)
[EOS源码分析]7.EOS智能合约开发实践之合约调用合约(inline action)          
核心是为智能合约账号添加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 [email protected]
  

代码

    新建两个contract:  hello.code和hello.target
        hello.target代码如下
    [EOS源码分析]7.EOS智能合约开发实践之合约调用合约(inline action)
       
        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 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 [email protected]

$cleos push action hello.code hi '["args.user", "args.user1"]' -p args.user


源码一键实践

    从以下github网页下载源码,即可一键实践执行该智能合约

/********************************
* 本文来自****博主"爱踢门"
* 转载请标明出处:http://blog.****.net/itleaks
******************************************/

EOS技术交流群,EOS开发群,以太坊技术群:787804520

    [EOS源码分析]7.EOS智能合约开发实践之合约调用合约(inline action)    

    公众号:

[EOS源码分析]7.EOS智能合约开发实践之合约调用合约(inline action)