【发布时间】:2021-07-26 18:15:48
【问题描述】:
我有一个 erc20 代币,在另一个合约中我想创建一个代币交换功能。 非常容易,发送一个 usdc 代币并以 1:1 的比例交换我的 erc20 代币。 问题是如何批准使用我的 erc20 代币。试了好几次都没找到办法。
interface IERC20 {...}
contract AnotherContract {
function approve(address _spender, uint256 _amount) public returns(bool) {
return IERC20(MyToken).approve(_spender, _amount);
}
我部署了另一个合同,当我从中调用批准功能时。因此,当我将“_spender”设置为此合约地址时。结果很奇怪。所以这个合同既是所有者又是支出者。我认为用户应该是所有者,而这个合同应该是支出者。但是从链上调用函数。 msg.sender 将是这个合约地址。
我不明白,也很困惑。有人知道或有一些资源吗?谢谢。
【问题讨论】:
标签: blockchain ethereum solidity smartcontracts binance-smart-chain