'How to call a smart contract function from another smart contract function?

Some background first. I have two smart contracts:

  • ERC20 for fungible tokens where I minted my own token/crypto currency;
  • ERC1155 for non-fungible tokens (NFTs);
  • The user will buy the NFT minted in the ERC1155 with the token I minted in the ERC20 smart contract.

The user calls the function buyNFT() in the ERC1155 that call the function transfer() inside the ERC20 smart contract. My problem relies in making the "bridge" between these two contracts. I've tried using the low-level function delegatecall() but with no success since it always returns a tuple whose first value is false.

enter image description here

This is my code so far:

abstract ERC20 {
    function _transfer(
        address _from,
        address _to,
        uint256 _value
    ) internal {}

    function transfer(address _to, uint256 _value)
        public
        returns (bool success)
    {
        _transfer(msg.sender, _to, _value);
        return true;
    }
}

contract ERC1155 {
    function buyNFT(uint256 _NFTPrice, address _to)
        public
        returns (bytes memory)
    {
        (bool success, bytes memory returnData) = 0x00...00.delegatecall(
            abi.encodeWithSelector(HRC20.transfer.selector, _to, _NFTPrice)
        );

        if (!success) {
            if (returnData.length == 0) revert("here");

            assembly {
                revert(add(32, returnData), mload(returnData))
            }
        }

        return returnData;
    }

}

Sorry for no code highlight, it seems stack overflow doesn't support the .sol extension.

Also, I've tried using abi.encodePacked(), abi.encode(), abi.encodeWithSignature().



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source