使用合约 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 本身? 请告诉我我在这里做错了什么,谢谢

解决方法

ContractB 调用 ContractA 时,msg.sender 中的 ContractAContractB 地址。

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

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

很可能您有一个可以使用的 approve() 函数(令牌标准中的 defined)。在链接的示例中,函数的调用者将是 ownerspender 将是 ContractB,而 value 将是等于或大于金额的任何值您要发送的令牌数(注意小数)。