如何在我的合约内调用一个已经部署的erc20合约的转账方法。给某个地址转账呢

请先 登录 后评论

2 个回答

i am duck - 划水

我用了两种方法,分别是test和test1

// SPDX-License-Identifier: MIT

pragma solidity 0.6.12;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.0.0/contracts/token/ERC20/ERC20.sol";
contract MockERC20 is ERC20{

    constructor(string memory _name, string memory _symbol) public ERC20(_name, _symbol) {
        _mint(msg.sender, 100000000 * 10 ** 18);

    }
    
}

contract Test{
    ERC20 erc20;
    constructor(ERC20 _erc20) public{
        erc20 = _erc20;
    }
    function transferFrom(address _to,uint256 _amount) public{
        erc20.transferFrom(msg.sender,_to,_amount);
    }
}

contract Test1{
    address erc20;
    constructor(address _erc20) public{
        erc20 = _erc20;
    }
    function transferFrom(address _to,uint256 _amount) public returns(bool){
      bytes32 a =  keccak256("transferFrom(address,address,uint256)");
      bytes4 methodId = bytes4(a);
      bytes memory b =  abi.encodeWithSelector(methodId,msg.sender,_to,_amount);
      (bool result,) = erc20.call(b);
      return result;
    }
}

请先 登录 后评论
Ethereal - Solidity智能合约开发工程师

就是1楼的方法,在你的合约X的方法里写入调用ERC20合约的授权转账transferFrom接口,你得先在ERC20合约里给你的合约X的地址授权一定额度,再去调用包含transferFrom接口的方法就可以任意转账了

请先 登录 后评论
  • 2 关注
  • 1 收藏,3911 浏览
  • xmenzp 提出于 2021-08-18 13:33