0

I am new to solidity. I have written the below contract which holds some USDC in it. I want to withdraw it but on calling executeOperation() gas limit is being exceed. I have even tried it with high gas limit like 555555 but still it don't work.

// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.8.10;
pragma experimental ABIEncoderV2;

import { SafeMath } from "https://github.com/aave/aave-v3-core/contracts/dependencies/openzeppelin/contracts/SafeMath.sol";

interface IFaucet {
    function mint(
        address _token,
        uint256 _amount
    ) external;
}



/** 
 Faucet address:
    0x1ca525Cd5Cb77DB5Fa9cBbA02A0824e283469DBe

    Assest address (USDC):
    0xA2025B15a1757311bfD68cb14eaeFCc237AF5b43
 */
contract MySimpleFlashLoanV3  {
    using SafeMath for uint256;
    address payable owner;
    IFaucet FAUCET;

    constructor(IFaucet _faucet) {
        owner = payable(msg.sender);
        FAUCET= _faucet;
    }
    function widthdrawAmount() internal {
        // require(owner == msg.sender,"Only for owner");
        owner.transfer(10);
    }
    function executeOperation(
        address asset,
        uint256 amount
    )
        public
        returns (bool)
    {

        FAUCET.mint(asset,10);
        widthdrawAmount();
        return true;
    }

}

May anyone tell me what's I am doing wrong ? why it's costing so much that even high gas fee is not enough.

1
  • 1
    as DereK mentions, there is a built in gas limit to .send and .transfer method which was intended as a protection to reentrancy. They are now deprecated anyway, and .call should be used in preference
    – sola24
    Commented Aug 23, 2022 at 14:36

3 Answers 3

2

Well try using call it will work, you are running out of gas as there is an limit in .transfer().

function widthdrawAmount() internal {
    // require(owner == msg.sender,"Only for owner");
    (bool success,)= owner.call{value: 10}("");
    // updated
    require(success,"tx failed");
}

Here is an explanation enter image description here

11
  • may you explain me this syntax owner.call{value:10}("") ? what does the second parenthesis with empty string mean? Commented Aug 23, 2022 at 19:10
  • in the curly braces go the parameters for the call. easy to look up the possible params. inside the parentheses is the data payload for the transaction. In this case there isn't any, just a transfer of value. In other cases, there might be data crafted with a function selector and function arguments
    – sola24
    Commented Aug 23, 2022 at 19:29
  • @sola24 ohk. Got the point. I made the changes as explained by Derek but the executeOperation() is not calling the widthdrawAmount(). What's the issue here ? Commented Aug 23, 2022 at 19:33
  • I have updated the code it must check weather the transaction is success or not, but where is the contract getting eth from to transfer it to someone else ???
    – DereK
    Commented Aug 24, 2022 at 5:07
  • are you trying to withdraw ERC20 token ?
    – DereK
    Commented Aug 24, 2022 at 5:17
1

You are calling faucet to mint 10 ERC20 tokens to your smart contract with FAUCET.mint(asset,10);, and trying to withdraw the ERC20 token with owner.transfer(10);.

However, payable(address).transfer() only works with transferring the gas token (ETH), and is not working when you are trying to transfer ERC20 token.

If you want to withdraw ERC20, you could try

function widthdrawAmount(IERC20 token) internal {
    token.transfer(msg.sender, 10);
}

instead of your withdrawAmount() function.

If you have some gas token in the smart contract, you would prefer to use owner.transfer(10).

0

You can withdraw your funds via function like

function withdraw(address someAddress) public onlyOwner {
        uint256 balance = address(this).balance;
        someAddress.transfer(balance );
    }

onlyOwner to allow call only for person who deployed contract to test withdraw you can remove onlyOwner - just for testing purpose

To support onlyOwner use Ownable from openzeppelin https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol. So you need to inhert it

MySimpleFlashLoanV3 is Ownable

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.