Ethereum: Low-level call cancellation error
The Ethereum blockchain is a programmable blockchain in which smart contracts can be deployed and executed. However, when executing low-level calls to external functions, such errors as execution cancellation errors may occur.
In this article, we will look at the cause of the execution cancellation error in Ethereum and provide steps to eliminate it.
What causes the abort error?
An execution cancellation error occurs when a smart contract attempts to execute a function call that results in an invalid state or value. This can happen for several reasons:
Example Gas Oracle API Call
In Ethereum, the GasOracle
API is used to obtain an estimated gas price for a transaction. This API can be called from another procedure or smart contract using the following syntax:
`solidity
function estimateGas() public returns (uint256) {
// GasOracle API call and result analysis
uint256 estimateGas = 0; // Initialize the variable
GasOracle gasOracle = new GasOracle(address);
gasOracle.estimateGas(); // GasOracle API call
estimateGas = gasOracle.getEstimatedGas(); // Analysis of the result
return estimateGas;
}
estimatedGas
In this example, the
function calls the
GasOracleAPI to get the estimated gas value and returns it.
Example of undo execution errorConsider an example where we have a simple smart contract that tries to call a function:
solidity
contract Example {
function foo() public {
// Call bar()
callBar();
}
function bar() public pure {
// Modeling of some calculations
uint256 result = 0;
for (uint256 i = 0; i < 100000; i++) {
result += i;
}
return result;
}
}
Suppose we have a GasOracleAPI call in the second procedure, the
foo()function tries to call the
bar()function:
solidity
pragma solidity ^0.8.0;
contract Example {
// Call the Gas Oracle API
uint256 public gasCost;
function estimateGas() public returns (uint256) {
gasCost = 0; // Initialize the variable
GasOracle gasOracle = new GasOracle(address);
gasOracle.estimateGas(); // GasOracle API call
gasCost = gasOracle.getEstimatedGas(); // Analysis of the result
return gasCost;
}
}
bar()
When trying to call
, an execution cancellation error occurs because the function signature is invalid (a required argument is missing). The Ethereum blockchain does not allow calling a function that is not declared "pure" or does not have the correct signatures.
bar()
SolutionTo solve this problem, we need to make sure that the functions of our smart contract are clearly defined and correspond to the expected function signature. In this case, we can simply change the
function to pure:
solidity
contract Example {
function foo() public {
// Call bar()
callBar();
}
function bar() pure public returns (uint256) {
// Modeling of some calculations
uint256 result = 0;
for (uint256 i = 0; i < 100000; i++) {
result += i;
}
return result;
}
}
With this change, we can safely call foo()without encountering a cancellation error. In addition, we need to make sure that the
GasOracle` API calls are executed correctly and do not exceed the permissible gas limit.