near_jsonrpc_client/methods/gas_price.rs
1//! Returns the gas price for a specific block height or block hash.
2//!
3//! ## Examples
4//!
5//! Returns the gas fees for this block:
6//! <https://explorer.near.org/blocks/6atGq4TUTZerVHU9qWoYfzXNBg3K4C4cca15TE6KfuBr>
7//!
8//! - `BlockId::Height`
9//!
10//! ```
11//! # use near_jsonrpc_client::{methods, JsonRpcClient};
12//! use near_primitives::types::BlockId;
13//!
14//! # #[tokio::main]
15//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
16//! let client = JsonRpcClient::connect("https://archival-rpc.mainnet.fastnear.com");
17//!
18//! let request = methods::gas_price::RpcGasPriceRequest {
19//! block_id: Some(BlockId::Height(61512623)),
20//! };
21//!
22//! let response = client.call(request).await?;
23//!
24//! assert!(matches!(
25//! response,
26//! methods::gas_price::RpcGasPriceResponse { .. }
27//! ));
28//! # Ok(())
29//! # }
30//! ```
31//!
32//! - `BlockId::Hash`
33//!
34//! ```
35//! # use near_jsonrpc_client::{methods, JsonRpcClient};
36//! use near_primitives::types::BlockId;
37//!
38//! # #[tokio::main]
39//! # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
40//! let client = JsonRpcClient::connect("https://archival-rpc.mainnet.fastnear.com");
41//!
42//! let request = methods::gas_price::RpcGasPriceRequest {
43//! block_id: Some(BlockId::Hash("6atGq4TUTZerVHU9qWoYfzXNBg3K4C4cca15TE6KfuBr".parse()?)),
44//! };
45//!
46//! let response = client.call(request).await?;
47//!
48//! assert!(matches!(
49//! response,
50//! methods::gas_price::RpcGasPriceResponse { .. }
51//! ));
52//! # Ok(())
53//! # }
54//! ```
55use super::*;
56
57pub use near_jsonrpc_primitives::types::gas_price::{RpcGasPriceError, RpcGasPriceRequest};
58
59pub type RpcGasPriceResponse = near_primitives::views::GasPriceView;
60
61impl RpcHandlerResponse for RpcGasPriceResponse {}
62
63impl RpcHandlerError for RpcGasPriceError {
64 fn parse(value: serde_json::Value) -> Result<Self, serde_json::Error> {
65 common::parse_unknown_block!(value => Self)
66 }
67}
68
69impl RpcMethod for RpcGasPriceRequest {
70 type Response = RpcGasPriceResponse;
71 type Error = RpcGasPriceError;
72
73 fn method_name(&self) -> &str {
74 "gas_price"
75 }
76
77 fn params(&self) -> Result<serde_json::Value, io::Error> {
78 Ok(json!([self.block_id]))
79 }
80}
81
82impl private::Sealed for RpcGasPriceRequest {}