You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/sdk-and-tools/rest-api/transactions.mdx
+40-4Lines changed: 40 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -347,7 +347,36 @@ Example response for cross-shard transactions:
347
347
348
348
`https://gateway.multiversx.com/transaction/cost`
349
349
350
-
This endpoint allows one to estimate the cost of a transaction.
350
+
This endpoint is used to estimate the gas cost of a given transaction.
351
+
352
+
It performs a read-only simulation of the transaction against the current on-chain state, returning the number of gas units the transaction would consume if executed in that exact state.
353
+
354
+
#### How it works:
355
+
- The endpoint takes all transaction input fields (value, sender, receiver, data, chainID, etc.).
356
+
- It executes the transaction in a sandboxed, non-persistent environment, meaning it simulates execution without affecting the actual blockchain.
357
+
- It uses the current state of the network (including smart contract storage, balances, etc.).
358
+
- It returns the estimated gas (txGasUnits) and may also return smart contract results and events triggered by the simulation.
359
+
360
+
#### How does it apply to smart contracts?
361
+
362
+
For smart contracts, the endpoint simulates the contract call exactly as if it were executed live, including processing all logic, branches, and emitted events.
363
+
The gas estimate reflects the computation and storage impact that would occur if the state remained unchanged at the time of actual execution.
364
+
365
+
#### How does it approximate the cost if the contract logic has variable gas usage?
366
+
367
+
If a smart contract’s logic has branches or conditional execution that result in variable gas usage (e.g., depending on internal storage state, previous executions, etc.),
368
+
the estimation will only reflect the gas used by the path taken during this particular simulation.
369
+
370
+
#### Why is providing the correct nonce important?
371
+
372
+
Because the simulation engine mirrors real transaction behavior, it requires a valid and correct nonce to properly simulate the transaction. Using an outdated or incorrect
373
+
nonce may lead to simulation failure.
374
+
375
+
#### Can the estimated gas differ from the actual cost?
376
+
377
+
Yes. Since the blockchain state may change between the moment you call /transaction/cost and when the transaction is actually sent to the network, the real gas usage can differ.
378
+
This is especially true for smart contract calls that depend on dynamic or mutable state.
379
+
351
380
352
381
<Tabs
353
382
defaultValue="Request"
@@ -364,10 +393,12 @@ Body Parameters
364
393
| value | <spanclass="text-danger">REQUIRED</span> |`string`| The Value to transfer, as a string representation of a Big Integer (can be "0"). |
365
394
| receiver | <spanclass="text-danger">REQUIRED</span> |`string`| The Address (bech32) of the Receiver. |
366
395
| sender | <spanclass="text-danger">REQUIRED</span> |`string`| The Address (bech32) of the Sender. |
367
-
| data | <spanclass="text-normal">OPTIONAL</span> |`string`| The base64 string representation of the Transaction's message (data). |
368
396
| chainID | <spanclass="text-danger">REQUIRED</span> |`string`| The Chain identifier. |
369
397
| version | <spanclass="text-danger">REQUIRED</span> |`number`| The Version of the Transaction (e.g. 1). |
370
-
| nonce | <spanclass="text-normal">OPTIONAL</span> |`number`| The Sender nonce. |
398
+
| nonce | <spanclass="text-danger">REQUIRED</span> |`number`| The Sender nonce. |
399
+
| options | <spanclass="text-normal">OPTIONAL</span> |`number`| The Options of the Transaction (e.g. 1). |
400
+
| data | <spanclass="text-normal">OPTIONAL</span> |`string`| The base64 string representation of the Transaction's message (data). |
401
+
371
402
372
403
</TabItem>
373
404
<TabItemvalue="Response">
@@ -390,7 +421,12 @@ The cost is estimated successfully.
390
421
</Tabs>
391
422
392
423
:::tip
393
-
This endpoint returns the cost on the transaction in **gas units**. The returned value can be used to fill in **gasLimit** field of the transaction.
424
+
- Use the returned `txGasUnits` value as the `gasLimit` in your actual transaction.
425
+
- Make sure to provide the correct `nonce` of the transaction
426
+
:::
427
+
428
+
:::tip
429
+
**Best practice:** when sending the transaction, add ~10% extra gas to the estimated value to avoid underestimation and failure due to insufficient gas.
0 commit comments