1

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

 2 years ago
source link: https://learnblockchain.cn/question/2454
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
如何在我的合约内调用一个已经部署的erc20合约的转账方法。给某个地址转账呢 | 登链社区 | 技术问答

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

cj 4天前

我用了两种方法,分别是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;
    }
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK