首页 > 解决方案 > 使用合约 A 将代币从合约 B 转移到用户地址 X

问题描述

我已经部署了合约 A。现在我正在创建网关合约 B,我想使用所有者地址将合约 A 的一些代币发送到用户地址 X。值得一提的是,合同 A 的所有者与合同 B 相同。我执行以下操作

contract A is Ownable { // this one already deployed by owner
  constructor() {
    owner = msg.sender; // owner address is 0x123
    approve(msg.sender, totalSupply); // let's approve all tokens for owner
  }      

  function transferFrom(address from, address to, uint256 value) public returns (bool) {
    require(value <= allowed[from][msg.sender], "Not allowed!");
    // let's skip other logic
  }
}    

contract B is Ownable { // gateway contract will be deployed and executed by same owner
  A contractA = ETC20(0x111);
  address payable X = 0x333;

  constructor() {
    owner = msg.sender; // owner address is 0x123
  }

  function giveAwayTokens(uint256 value) {
    contractA.transferFrom(owner, X, value);
  }
}

当我从所有者地址 (0x123) 执行“giveAwayTokens”函数时,我收到错误“不允许!”。所以我现在有点困惑,因为所有者的所有者津贴是最大供应量。或者 msg.sender 可能是 contractB 本身?请赐教我在这里做错了什么,谢谢

标签: blockchainethereumsoliditysmartcontracts

解决方案


ContractB调用时ContractAmsg.senderinContractAContractB地址。

根据代码和错误消息,owner( 0x123) 不允许ContractB花费他们的代币。

您需要将 的值设置allowed[<owner>][<ContractB>]为至少要发送的令牌数量。

很可能您有一个可以使用的approve()函数(在令牌标准中定义)。在链接的示例中,函数的调用者将是 the owner, thespender将是 theContractB并且 thevalue将是等于或高于您要发送的令牌数量的任何值(注意小数)。


推荐阅读