Estimate required step
It is important to set a proper stepLimit value in your transaction to make the submitted transaction executed successfully. This document explains how to systemically estimate the required Step.

Intended Audience

Anyone who sends transactions to the ICON Network

Purpose

To learn how to estimate the required Step for their transactions using ICON SDKs, T-Bears CLI or JSON-RPC API calls.

Prerequisite

    Understand how ICON JSON-RPC APIs work.
    Need to know how to submit a transaction to the ICON Network using various tools.

How-To

The mechanism of requesting Step estimation is very similar to sending transactions.

Request and Response

Transaction Request
The following example is a transaction request message that will invoke the transfer method on the IRC2 token contract.
1
{
2
"jsonrpc": "2.0",
3
"method": "icx_sendTransaction",
4
"id": 1234,
5
"params": {
6
"version": "0x3",
7
"from": "hxcced4d83a03098f5976b5ed339b0cab3e51ca1f9",
8
"to": "cx65fc79aa09e867fd51d0c8361f42cb38bc1f08f3",
9
"stepLimit": "0x30d40",
10
"timestamp": "0x583f58f62eae0",
11
"nid": "0x3",
12
"nonce": "0x1",
13
"dataType": "call",
14
"data": {
15
"method": "transfer",
16
"params": {
17
"_to": "hx25cd4028f055457b8fbdae1dbd8b5fe465248e55",
18
"_value": "0x8ac7230489e80000"
19
}
20
},
21
"signature": "rBaH0U6y85y1CWp/JbalUbzLVGjtGYG+hut/G5o30vBxhWoxPYtSYBQu6X0Tak1SdcnlZSCJL7DeOeKmI4y+5wE="
22
}
23
}
Step Estimate Request
The step estimation request is the same as the above example except the stepLimit and signature. The stepLimit and signature fields are unnecessary because the step estimation process does not verify the sender, nor limit the execution until the cumulative step reaches the stepLimit.
The following example is the Step estimation request for the above token transfer transaction. You can simply remove the stepLimit and signature fields from the original transaction request.
1
{
2
"jsonrpc": "2.0",
3
"method": "debug_estimateStep",
4
"id": 1234,
5
"params": {
6
"version": "0x3",
7
"from": "hxcced4d83a03098f5976b5ed339b0cab3e51ca1f9",
8
"to": "cx65fc79aa09e867fd51d0c8361f42cb38bc1f08f3",
9
"timestamp": "0x583f58f62eae0",
10
"nid": "0x3",
11
"nonce": "0x1",
12
"dataType": "call",
13
"data": {
14
"method": "transfer",
15
"params": {
16
"_to": "hx25cd4028f055457b8fbdae1dbd8b5fe465248e55",
17
"_value": "0x8ac7230489e80000"
18
}
19
}
20
}
21
}
Response - Success
If there are no exceptions, the response will return the estimated Step usage as follows.
1
{
2
"jsonrpc": "2.0",
3
"id": 1234,
4
"result": "0x23f8c"
5
}
Response - Fail
If there is an exception, the response will show the error message.
1
{
2
"jsonrpc": "2.0",
3
"id": 1234,
4
"error": {
5
"code": -32602,
6
"message": "JSON schema validation error: 'version' is a required property"
7
}
8
}

JSON-RPC API call

Debug API Endpoint : :///api/debug/v3
You can make a step estimation request using curl from the CLI as follows. Note that in the below command example, stepEstimationRequest.json is the file that contains the request message.
1
$ curl -H "Content-Type: application/json" -d @stepEstimationRequest.json https://bicon.net.solidwallet.io/api/debug/v3
2
{"jsonrpc": "2.0", "result": "0x23f8c", "id": 1234}

Java

Below is a code snippet for Java. For the detailed ICON Java SDK usage guideline, please read Java SDK.
1
// make a raw transaction without the stepLimit
2
Transaction transaction = TransactionBuilder.newBuilder()
3
.nid(networkId)
4
.from(fromAddress)
5
.to(toAddress)
6
.nonce(BigInteger.valueOf(1))
7
.call("transfer")
8
.params(params)
9
.build();
10
​
11
// get an estimated step value
12
BigInteger estimatedStep = iconService.estimateStep(transaction).execute();
13
​
14
// set some margin
15
BigInteger margin = BigInteger.valueOf(10000);
16
​
17
// make a signed transaction with the same raw transaction and the estimated step
18
SignedTransaction signedTransaction = new SignedTransaction(transaction, wallet, estimatedStep.add(margin));
19
Bytes txHash = iconService.sendTransaction(signedTransaction).execute();
20
...

Python

Step estimation code snippet for Python. For the detailed ICON Python SDK usage guideline, please read Python SDK.
1
# Generates a raw transaction without the stepLimit
2
transaction = TransactionBuilder()\
3
.from_(wallet.get_address())\
4
.to("cx00...02")\
5
.value(150000000)\
6
.nid(3)\
7
.nonce(100)\
8
.build()
9
​
10
# Returns an estimated step value
11
estimate_step = icon_service.estimate_step(transaction)
12
​
13
# Adds some margin to the estimated step
14
step_limit = estimate_step + 10000
15
​
16
# Returns the signed transaction object having a signature with the same raw transaction and the estimated step
17
signed_transaction = SignedTransaction(transaction, wallet, step_limit)
18
​
19
# Sends the transaction
20
tx_hash = icon_service.send_transaction(signed_transaction)

Summary

With the debug_estimateStep JSON-RPC API, developers can estimate how much Step will be required for a particular transaction under the current block state. Using various ICON SDKs, you can integrate the functionality in your DApp.
Please be aware that the returned value is just an ESTIMATION. The block state may not remain the same between the Step estimation and the actual transaction execution time. The required Step can be different if the block state changes.

References