0

I'm using a method in which transferfrom method of an ERC20 token is called. In test enn I use my own ERC20 token and ganache and everything works fine. but in Rinkeby testnet when I use interface of ERC20 and address of Deplyoed tokens in Rinkeby I get the error.

here is my deposite function :

function deposit(uint _value, IERC20 _currency) public {              
        require(_currency.transferFrom(msg.sender , address(this), _value),  "transferFrom failed");    
    }
}

in my web3 I call approve function first(and wait for get mined) and then call deposit, every thing works fine in Ganache, but in Rinkeby, I always get my deposit reverted.

web3 code:

ERCInstance.methods.approve('deposit contract address', token amount).send({from: 
accounts[0]}).on('transactionHash', (hash) => {
        dipositeContractInstance.methods.deposit(amount, 'ERC address').
        send({from: accounts[0]}).on('receipt', function(receipt){
           
        });

note that I used pre created ERC20 tokens in rinkeby and even created my own one, but still get revert. for example rinkeby dai address is :'0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735'

1

There are several reasons why this might be leading to an error:

  1. According to the web3js docs The transactionHash event is fired automatically as soon as the transaction hash is available. On local ganache blockchains transactions are usually confirmed and mined instantly after being sent (unless configured otherwise). On testnets and on mainnet this event will usually always fire before the transaction is really confirmed. If you then send another transaction they may be confirmed in the wrong order or simply rejected. (Although the nonce should prevent this). To be sure always use the receipt event or simply wait for the promise to resolve.
  2. You do not own sufficient tokens to complete the transfer. In order to successfully call transferFrom the sender must not only have approved the operator but also must have sufficient tokens.

Hope I could help.

10
  • Thanks for the help, It worked fine. the problem was on transactionHash. I was using Remix GUI for testing the deposit function. and it is still failing like here. any clue how to fix that? – Amin Jan 19 at 15:16
  • How are you running remix? Javascript VM, rpc, web3 provider? – MrClottom Jan 19 at 15:21
  • Javascrips VM is fine. web 3 "rinkeby" throws the same error like in this one before. and withdrawing Ether from rcp ganache is not working for some reason. – Amin Jan 19 at 15:27
  • ethereum.stackexchange.com/questions/92459/… . check this question for ether withdraw problem in ganache – Amin Jan 19 at 15:28
  • Is your tx actually reverting or is remix throwing an error? – MrClottom Jan 19 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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