0

I have been trying to make my code work but I couldn't and wanted to know if you could help me or give me some advice :(

Here is my code in Remix: enter image description here

And here written:


pragma solidity ^0.8.18;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol";

contract TokenTransfer {
    IERC20 _token;

    // token = MyToken's contract address
    constructor(address token) {
        _token = IERC20(token);
    }

    // Modifier to check token allowance
    modifier checkAllowance(uint amount) {
        require(_token.allowance(msg.sender, address(this)) >= amount, "Error");
        _;
    }

    function checkAllowanceRemaining() public view returns(uint256) {
        return _token.allowance(msg.sender, address(this));
    }

    function checkAllowancePass(uint amount) public view returns(bool) {
        return _token.allowance(msg.sender, address(this)) >= amount;
    }

    // In your case, Account A must to call this function and then deposit an amount of tokens 
    function depositTokens(uint _amount) public checkAllowance(_amount) {
        _token.transferFrom(msg.sender, address(this), _amount);
    }
    
    // to = Account B's address
    function stake(address to, uint amount) public {
        _token.transfer(to, amount);
    }

    // Allow you to show how many tokens owns this smart contract
    function getSmartContractBalance() external view returns(uint256) {
        return _token.balanceOf(address(this));
    }
    
    function checkSenderBalance() public view returns(uint256) {
        return _token.balanceOf(msg.sender);
    }
} 

When I call the depositTokens function it shows me that error message you see in the console. See that first I called the checkSenderBalance, and checkAllowanceRemaining, and also the checkAllowancePass to make sure everything is okay. Here the image: enter image description here

By the way that's the Remix blockchain, but I also tried with the Goerli error and it shows me a similar error "Gas estimation failed: execution reverted".

And I'm using TetherToken as ERC20 token, from here: https://goerli.etherscan.io/token/0x509ee0d083ddf8ac028f2a56731412edd63223b9#code

That I deployed an instance locally in Remix in Remix/Goerli blockchain.

Thank you for reading this, and your advice would be a great help.

11
  • It worked perfectly in my remix. Are you sure you used the right token address? Feb 13, 2023 at 4:48
  • In order to call depositTokens you need to approve the contract. Did you do it?
    – Ismael
    Feb 13, 2023 at 5:29
  • @shubhamskatel yes I'm using the tethertoken that finishes with "39318", as you see in the image that's the instance I created of this TetherToken: goerli.etherscan.io/token/… Feb 13, 2023 at 12:14
  • @Ismael yes, I forgot to say that but I went to the instance I created from TetherToken contract and called the approve function sending as "spender" the address of my TokenTransfer contract instance, and 50000 as "value". And you can see in the image that my checkAllowanceRemaining returns me 50000 Feb 13, 2023 at 12:20
  • @shubhamskatel I'm surprised it worked on your machine, I only imagine I'm not using correctly the token because the TokenTransfer contract is really simple. Are you using the same TetherToken I sent, or are you using a different contract? Feb 13, 2023 at 12:35

2 Answers 2

0

The checkAllowance modifier is not necessary, the erc20 contract will already check that for you. Also make sure that the token has been approved.

0

I found the issue, the TetherToken I was using: https://goerli.etherscan.io/token/0x509ee0d083ddf8ac028f2a56731412edd63223b9#code

Has a bug, and doesn't allow to use the transfer() or transferFrom(). I changed to USDC ERC20 token in Goerli network and everything works perfect now, also another guy tried with some others ERC20 tokens and it works. Thank you guys!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.