0

My smart contract holds a few WETH. I have this function in my contract:

    function getFunds(address tokenAddress) external onlyOwner{
        IERC20 token = IERC20(tokenAddress);
        uint balance = token.balanceOf(address(this)); 
        uint balanceOwner = token.balanceOf(owner);

        assert(token.transfer(owner, balance));

        assert(token.balanceOf(owner) == balanceOwner + balance);
    }

I call getFunds with the correct WETH address and from the correct owner address.

But still, the tx reverts with 20% gas used, and I don't know why. What can be the reason?

I tested everything via hardhat and there it works.

EDIT: This is the way I call the contract in production via web3js:

        let myContract = new web3.eth.Contract(myContractAbi, CONTRACT_ADDRESS)
    
        const tx = myContract.methods.getFunds(WETH) //WETH constant with correct address 
        const data = tx.encodeABI()
      
        let currentGasPriceInWei = parseInt(await web3.eth.getGasPrice())
      
        const txData = {
          from: OWNER_ADDRESS,
          to: CONTRACT_ADDRESS,
          data: data,
          gasPrice: web3.utils.toBN(currentGasPriceInWei),
          gas: 35000  
        }         
        
        web3.eth.sendTransaction(txData)
        ...
8
  • Can you share a tx hash of the failing transaction. Also the address of WETH (there are multiple, but I would assume you use WETH9 and what network ;)
    – Richard
    Commented Jan 10, 2022 at 16:07
  • Sorry, I just wrote WETH to simplify things, but actually its WFTM on the Fantom network. WFTM adrress is correct, I prefer to keep the tx private, but I can share tx details of course :)
    – flo
    Commented Jan 10, 2022 at 16:10
  • So this one ftmscan.com/address/… ?
    – Richard
    Commented Jan 10, 2022 at 16:13
  • Exactly, yes that is the address :)
    – flo
    Commented Jan 10, 2022 at 16:16
  • Are you sure about the gas parameter? Looking at fmtscan I can see wfmt transfers that take quite some more gas, so I would probably try setting this to 100k just to test it.
    – Richard
    Commented Jan 10, 2022 at 16:17

0

Your Answer

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

Browse other questions tagged or ask your own question.