Introduction to EVM Archive Node Queries
HIP-584 enhances Hedera Mirror Nodes with extended EVM execution capabilities, allowing developers to perform gas-free smart contract queries, estimate gas usage, and simulate EVM transactions without committing state changes. These enhancements empower developers to:- Perform Gas-Free Smart Contract Queries: Retrieve data from smart contracts without incurring gas costs.
- Estimate Gas Usage: Determine the gas required for executing specific contract functions.
- Simulate EVM Transactions: Test transactions that involve state changes without committing those changes to the blockchain.
This guide uses real examples of interactions with smart contracts, including SaucerSwap’s DeFi contracts on the Hedera network:
- SaucerSwap’s HashScan verified DeFi smart contract:
0x00000000000000000000000000000000002e7a5d
- The HashScan link to the verified smart contract can be found here.
API Endpoint Overview
The API endpoint and parameters used for all operations described in this guide is:Key Parameters
estimate(boolean): Determines the operation type.true: Performs gas estimation.false: Executes a query or simulation.
block(string): Specifies the block for the operation (e.g., “latest” or a specific block number).data(string): Encoded function call data in hexadecimal format following the ABI specifications.from(string, optional): Address initiating the call. Required for simulations involving state changes.to(string): Target smart contract address (SaucerSwap’s DeFi contract for this guide).value(number, optional): Amount of tinybars to send with the transaction. Relevant for simulations involving value transfers.
Prerequisites
Before proceeding, ensure you have:- Basic knowledge of EVM smart contracts:
- Understanding of ABI (Application Binary Interface) and function encoding.
- Essential tools installed and configured:
- cURL for making HTTP requests.
- Ethers.js (v6 or later or equivalent libraries) for interacting with the EVM-compatible networks.
- Function data encoding:
- Familiarity with encoding function data into the required EVM-compatible hexadecimal format.
Gas Estimation
Estimating gas usage helps determine the cost required to execute a smart contract function without actually performing the transaction.Example Request
Here’s how to estimate gas usage using the/api/v1/contracts/call endpoint:
Expected Response
The API returns an estimated gas value in hexadecimal format:Decoding the Result:
To interpret the hexadecimal gas estimate, follow these steps:-
Extract the
result: the field from the API response containing the gas estimate in hexadecimal format. -
Convert Hexadecimal to BigInt:
Use JavaScript’s
BigIntto convert the hexadecimal string to a numerical value.Output:
The gas estimate result represents the value in tinybars.
- Hexadecimal Representation: Smart contracts and blockchain APIs often use hexadecimal strings to represent numerical values, ensuring precise and compact data transmission.
- Conversion to BigInt: JavaScript’s
BigIntis used to handle large integers that exceed the safe integer limit of the standardNumbertype, ensuring accuracy in calculations. - Interpretation: The numerical value (
32525in this case) represents the estimated gas required to execute the specified smart contract function.
Contract Queries
Contract queries allow developers to retrieve data from smart contracts without altering the blockchain state. This is particularly useful for reading data such as token balances, contract states, and more.Example Request
Retrieve the token balance using a contract’s view function:Expected Response
The API returns the result of the contract’s view function in hexadecimal format:Decoding the Result
To interpret the hexadecimal result (e.g., token balance), follow these steps:-
Extract the
result: Theresultfield contains the data returned by the smart contract function. -
Convert Hexadecimal to BigInt:
Output:
- Hexadecimal Representation: The smart contract’s
balanceOffunction returns the token balance in hexadecimal format. - Conversion to BigInt: Using
BigIntensures accurate representation of potentially large token balances. - Interpretation: The numerical value (
32in this case) represents the token balance of the specified address
EVM Transaction Simulations
Simulate EVM transactions (non-view functions) that involve state changes to test contract interactions without committing them. This can be useful for testing token transfers, approvals, and other state-altering functions without changing the blockchain state.Example Request
This example simulates a token transfer by testing contract interactions without altering the state.Expected Response
The empty result indicates that the simulation ran successfully without errors:Decoding the Results with Ethers.js
To decode the data returned by the Mirror Node, you need the ABI of the smart contract you are interacting with. The ABI defines the structure of inputs and outputs for the contract’s functions.Step 1: Install Ethers.js
If you haven’t already, install Ethers.js using npm:Step 2: Decode the Hexadecimal Result
Below is a detailed explanation of how to extract and interpret theresult from the API response.
Example Scenario
You have made an API request to retrieve a token balance, and received the following response:
-
Extracting the
result:- Purpose: Assign the
resultfrom the API response to the variablehexString. - Content: The
hexStringcontains the ABI-encoded data returned by the smart contract function.
- Purpose: Assign the
-
Converting Hexadecimal to BigInt:
- Purpose: Convert the hexadecimal string to a
BigIntfor numerical operations. - Explanation:
BigInt: A JavaScript data type that can represent integers with arbitrary precision, suitable for handling large numbers often used in blockchain applications.- Conversion: The
BigIntconstructor automatically parses the hexadecimal string (prefixed with0x) and converts it to its numerical equivalent.
- Purpose: Convert the hexadecimal string to a
-
Displaying the Token Balance:
- Purpose: Output the numerical value of the token balance to the console.
- Explanation:
.toString(): Converts theBigIntto a string for readable output.
result based on the contract’s ABI.
-
Define the ABI:
- The ABI specifies the
getAmountsInfunction which returns an array ofuint256values.
- The ABI specifies the
-
Create an Interface:
ethers.Interfaceuses the ABI to understand how to decode the data.
-
Decode the Result:
decodeFunctionResultinterprets theresultbased on the function’s return type.
-
Access the Decoded Data:
- The decoded result is accessed as
decodedResult[0]and converted to a string for readability.
- The decoded result is accessed as