1

I am trying to call a function from my smart contract. The function i am trying to call is convertRTKIntoRTKLX

(Full code :- swapContract)

Although, the necessary code for this question is only this much:-

function convertRTKIntoRTKLX(address to, uint256 amount, uint256 X) public returns (bool) {
    require (
        (X >= 2 && X <= 5), 
        "Invalid value of X. X can only be 2, 3, 4, 5"
    );
    
    require (
        amount > 0,
        "Cannot convert 0"
    );
    
    ERC20 AMMOToken = ERC20(_Token_AMMO);
    ERC20 RTKToken = ERC20(_Token_RTKL[0]);
    ERC20 RTKLXToken = ERC20(_Token_RTKL[X-1]);
    
    require (
        RTKLXToken.balanceOf(address(this)) >= amount,
        "Insufficeint RTKLX Token balance in the contract for the given value of X"
    );
    
    require (
        RTKToken.allowance(msg.sender, address(this)) >= amount,
        "Allowance Lower than required for RTKToken"
    );
    
    require (
        AMMOToken.allowance(msg.sender, address(this)) >= ((X-1)*amount),
        "Allowance Lower than required for bulletToken"
    );
    

    if(AMMOToken.transferFrom(msg.sender, address(this), ((X-1)*amount))) {
        if(RTKToken.transferFrom(msg.sender, address(this), amount)) {
            if(RTKLXToken.transfer(to, amount)) {
                _RTKLX_ExtCirculation[X-1] += amount;
                return true;
            } else {
                return false;
            }
        } else {
            AMMOToken.transfer(msg.sender, (X-1)*amount);
            return false;
        }
    } else {
        return false;
    }
}

Now i call this function with X = 2, and some valid address. Consider these 2 cases --

a) amount = 10 x (10^18) <--- mentioned in this format for easier understanding b) amount = 150 x (10^18)

Now, for case a, the gas limit is fine and comes less than 200,000 But for case b, the gas limit comes around 11,874,898

(NOTE :- Before executing this function, i have approved 1000 x (10^18) RTK and AMMO token.)

Can anyone tell me why is gas limit going so high ?

When i test it on remix, the gas limit only comes around 175,000

2
  • 1
    The gas limit is so high likely because one of the statements in your code reverts. The fact that you get a reasonable gas limit when passing an amount 10 tokens but not when passing an amount 150 tokens, implies that it is likely because you don't have a balance of (at least) 150 tokens in your account. Commented Dec 15, 2020 at 12:39
  • 1
    Yes, That was the issue. Thanks for help. I realised that too late. This was a really stupid question to ask. Commented Dec 15, 2020 at 13:33

1 Answer 1

1

As answered by @goodvibration in the comments, the reason for this issue is the fact that the contract's balance was not sufficient and the transaction was getting reverted.

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.