如何在EOS合约中调用其它合约的action

目的

在omes.user合约的reguser(account_name user)这个action中调用eosio.token合约的issue action.

配置

  1. 创建部署eosio.token账号和合约
cleos create account eosio eosio.token EOS73D1s3ie2M8WdSDefQp4ZCYX99sda9wmkVvJ3GBLUbNyBeXnTe EOS5pJ1hEb6YhNVwE6EsJWYk88fYwYnEzYGEMwbwsZeGZx1XvT1u6
cleos set contract eosio.token ~/eos-master/eos/build/contracts/eosio.token -p eosio.token
  1. 创建omes.token账号。创建代币OMES。
cleos create account eosio omes.token EOS73D1s3ie2M8WdSDefQp4ZCYX99sda9wmkVvJ3GBLUbNyBeXnTe EOS5pJ1hEb6YhNVwE6EsJWYk88fYwYnEzYGEMwbwsZeGZx1XvT1u6
#use eosio.token contract to create OMES. omes.token is the issuer.
cleos push action eosio.token create '[ "omes.token", "1000000.0000 OMES", 0, 0, 0]' -p eosio.token
  1. 验证omes.token可以给其它账号发代币OMES
cleos create account eosio user EOS73D1s3ie2M8WdSDefQp4ZCYX99sda9wmkVvJ3GBLUbNyBeXnTe EOS5pJ1hEb6YhNVwE6EsJWYk88fYwYnEzYGEMwbwsZeGZx1XvT1u6
cleos push action eosio.token issue '[ "user", "100.0000 OMES", "memo" ]' -p omes.token
  1. 创建omes.user账号。授权omes.user。注意这里用的是eosio.code.
cleos create account eosio omes.user EOS73D1s3ie2M8WdSDefQp4ZCYX99sda9wmkVvJ3GBLUbNyBeXnTe EOS5pJ1hEb6YhNVwE6EsJWYk88fYwYnEzYGEMwbwsZeGZx1XvT1u6
cleos set account permission omes.token airdrop  '{"threshold":1, "accounts":[{"permission": {"actor":"omes.user", "permission":"eosio.code"}, "weight": 1}]}' -p omes.token
cleos set action permission omes.token eosio.token issue airdrop
  1. 调用eosio.token issue action
    在omes.user合约的reguser(account_name user)里,如下调用代码:
INLINE_ACTION_SENDER(eosio::token, issue)( N(eosio.token), {N(omes.token),N(airdrop)},
       { user, asset(1000, S(4, OMES)), std::string("bid name ") } );

EOS在dispatch这个action时会附加上当前合约omes.user的eosio.code权限。

详情请参考apply_context.cpp中的

void apply_context::execute_inline( action&& a ) {
      control.get_authorization_manager()
             .check_authorization( {a},
                                   {},
                                   {{receiver, config::eosio_code_name}},
                                   control.pending_block_time() - trx_context.published,
                                   std::bind(&transaction_context::checktime, &this->trx_context),
                                   false
                                 );
}

这里的receiver就是当前合约账户,config::eosio_code_name就是eosio.code

  1. 部署omes.user合约,验证
cleos set contract omes.user ~/omes-contract/omes.user -p omes.user
cleos push action omes.user reguser '["user"]' -p user@active
cleos get table eosio.token user accounts
{
  "rows": [{
      "balance": "100.1000 OMES"
    }
  ],
  "more": false
}

你可能感兴趣的:(如何在EOS合约中调用其它合约的action)