0

When i deploy a smartcontract with remix, i can call functions in order to test it.

I do not really know if this is remix or metamask which is doing this job: Before validating the transaction, i have a popup which tells me if the execution of the function will cause an error. And i know how much gas the function will consume.

So i suppose there is a kind of "pre-execution" of the smartcontract's code, before sending the transaction.

What i want to know if it is possible to do the same thing inside a solidity smartcontract.

Is it possible to know how much gas is necessary before calling an external contract inside a solidity function ? And is it possible to pre-execute it in order to see if it will fails ?

Thanks

0

Is it possible to pre-execute it in order to see if it fails?

Not really:

  • Yes: in a contract function, you can call another contract function without reverting, get a status of whether or not it has failed, and handle that as desired
  • No: if it hasn't failed, then it means that it had already executed

For example:

pragma solidity 0.6.12;

contract A {
    uint256 public x;

    function func(uint256 _x) public {
        require(_x < 42);
        x = _x;
    }
}

contract B {
    uint256 public successCount;

    function run(address a, uint256 x) public {
        bytes memory input = abi.encodeWithSelector(bytes4(keccak256("func(uint256)")), x);
        (bool success, bytes memory output) = a.call(input);
        if (success)
            successCount++;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.