从主网以太坊上的其他智能合约调用已部署合约的功能就天然气成本而言是否可行?

问题描述

我需要在需要在主网上部署的新合同中调用已部署的ERC20令牌的传递函数。我想知道这将是可行的,还是会需要大量的汽油,从而导致高昂的交易费用。

解决方法

是的,这是可行的-这是合同彼此交互的方式。通常,我们在调用合同中使用一个接口:

pragma solidity ^0.5.0;

// Define an interface to the contract you want to call. Only need function signatures

interface YourErc20Contract {
        function transfer(address recipient,uint amount) external;
    }

contract CallingContract {
    
    address your_erc20_address = 0x.....  // called contract address here
    address recipient = 0x....  // arguments for the function call
    uint amount = 2  


    // Call the function on the other contract using the interface
    function remoteCall(address recipient,uint amount) public {
        YourErc20Contract(your_erc20_address).transfer(recipient,amount)
        }
    }