Gas
The RPC API enables you to query the gas price for a specific block or hash.
Gas Price
Returns gas price for a specific
block_height
orblock_hash
.
- Using
[null]
will return the most recent block's gas price.
- method:
gas_price
- params:
[block_height]
,["block_hash"]
, or[null]
[block_height]
- JSON
- JavaScript
- HTTPie
{
"jsonrpc": "2.0",
"id": "dontcare",
"method": "gas_price",
"params": [17824600]
}
const response = await near.connection.provider.gasPrice(17824600);
http post https://rpc.testnet.near.org jsonrpc=2.0 method=gas_price params:='[17824600]' id=dontcare
["block_hash"]
- JSON
- JavaScript
- HTTPie
{
"jsonrpc": "2.0",
"id": "dontcare",
"method": "gas_price",
"params": ["AXa8CHDQSA8RdFCt12rtpFraVq4fDUgJbLPxwbaZcZrj"]
}
const response = await near.connection.provider.gasPrice(
"AXa8CHDQSA8RdFCt12rtpFraVq4fDUgJbLPxwbaZcZrj"
);
http post https://rpc.testnet.near.org jsonrpc=2.0 method=gas_price params:='["AXa8CHDQSA8RdFCt12rtpFraVq4fDUgJbLPxwbaZcZrj"]' id=dontcare
[null]
- JSON
- JavaScript
- HTTPie
{
"jsonrpc": "2.0",
"id": "dontcare",
"method": "gas_price",
"params": [null]
}
const response = await near.connection.provider.gasPrice(null);
http post https://rpc.testnet.near.org jsonrpc=2.0 method=gas_price params:='[null]' id=dontcare
Example response:
{
"jsonrpc": "2.0",
"result": {
"gas_price": "100000000"
},
"id": "dontcare"
}
What could go wrong?
When API request fails, RPC server returns a structured error response with a limited number of well-defined error variants, so client code can exhaustively handle all the possible error cases. Our JSON-RPC errors follow verror convention for structuring the error response:
{
"error": {
"name": <ERROR_TYPE>,
"cause": {
"info": {..},
"name": <ERROR_CAUSE>
},
"code": -32000,
"data": String,
"message": "Server error",
},
"id": "dontcare",
"jsonrpc": "2.0"
}
Heads up
The fields
code
,data
, andmessage
in the structure above are considered legacy ones and might be deprecated in the future. Please, don't rely on them.
Here is the exhaustive list of the error variants that can be returned by gas_price
method:
ERROR_TYPE | ERROR_CAUSEerror.cause.name | Status Code | Reason | Solution |
---|---|---|---|---|
HANDLER_ERROR | UNKNOWN_BLOCK | 200 | The requested block has not been produced yet or it has been garbage-collected (cleaned up to save space on the RPC node) |
|
REQUEST_VALIDATION_ERROR | PARSE_ERROR | 400 | Passed arguments can't be parsed by JSON RPC server (missing arguments, wrong format, etc.) |
|
INTERNAL_ERROR | INTERNAL_ERROR | 500 | Something went wrong with the node itself or overloaded |
|