0

I'm having a trouble trying to solve this task: I created a very simple smart contract that should call a function in the external smart contract and transfer tokens. Here's my contract code:

pragma solidity >=0.7.0 <0.9.0;


contract TestContract {

    address owner;
    
    modifier onlyowner {
        require(owner == msg.sender);
        _;
    }

     function withdraw(address usdtContractAddress, address receiver, uint256 amount) public {
        bytes memory payload = abi.encodeWithSignature("transfer(address, uint256)", receiver, amount);
        (bool success, ) = usdtContractAddress.call(payload);
        require(success);
     }
}

So basically this contract calls transfer function in the external contract (USDT).

I've deployed my contract and sent some usdt tokens to this contract address. So since the contract address is the owner of that tokens in usdt contract I assume I can call transfer method from my contract and send these tokens to any address.

The whole idea is to pay gas from the address which calls withdraw method in my contract and contract as the owner of tokens will transfer tokens to another address.

The issue is once I'm trying to run this withdraw function the transaction is being reverted.

Just an example: https://ropsten.etherscan.io/tx/0x4e0e782dee6ee256f8038dd249970ccd5bb5328333bc4c481649ec730644cdf2

I'm not a solidity developer so perhaps my mistake is obvious, but I can't find the reason why it doesn't work. Any advice?

By the way, when I'm trying to debug this transaction in remix I see that success is true, so It means the function call was success? remix debug screen

5
  • USDT in a not an ERC-20 compliant token. You have to use OpenZeppelin's SafeERC20.sol to get around this issue. Aug 13 at 14:55
  • Read about the missing return value bug. Aug 13 at 14:56
  • ethereum.stackexchange.com/questions/101564/… This doesn't answer my question (I actually saw this topic recently) The original purpose of my contract is to send specific tokens to it. But the OpenZeppelin's SafeERC20.sol could be the solution. I'll check that out. Thanks a lot Aug 13 at 15:09
  • Welcome, @VladimirSarkisyan. We could possibly help you debug that failing TX if you could get the source code of the following contract verified on Etherscan: ropsten.etherscan.io/address/… Aug 13 at 17:53
  • Hi, @scorpion9979, Thanks, but I think for now I can handle this. Looks like everything works fine. The OpenZeppelin's implementation did work. I'll post some examples once it's finished. Hope this will be helpful for someone Aug 14 at 14:15