celestia_grpc/grpc/
gas_estimation.rs

1use celestia_proto::celestia::core::v1::gas_estimation::{
2    EstimateGasPriceAndUsageRequest, EstimateGasPriceAndUsageResponse, EstimateGasPriceRequest,
3    EstimateGasPriceResponse,
4};
5use serde::{Deserialize, Serialize};
6
7use crate::Result;
8use crate::grpc::{FromGrpcResponse, IntoGrpcParam};
9
10/// TxPriority is the priority level of the requested gas price.
11#[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize)]
12#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
13#[cfg_attr(
14    all(feature = "wasm-bindgen", target_arch = "wasm32"),
15    wasm_bindgen::prelude::wasm_bindgen
16)]
17#[repr(i32)]
18pub enum TxPriority {
19    /// Estimated gas price is the value at the end of the lowest 10% of gas prices from the last 5 blocks.
20    Low = 1,
21    /// Estimated gas price is the mean of all gas prices from the last 5 blocks.
22    #[default]
23    Medium = 2,
24    /// Estimated gas price is the price at the start of the top 10% of transactions’ gas prices from the last 5 blocks.
25    High = 3,
26}
27
28/// Result of gas price and usage estimation
29#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
30#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
31#[cfg_attr(
32    all(feature = "wasm-bindgen", target_arch = "wasm32"),
33    wasm_bindgen::prelude::wasm_bindgen
34)]
35pub struct GasEstimate {
36    /// Gas price estimated based on last 5 blocks
37    pub price: f64,
38    /// Simulated transaction gas usage
39    pub usage: u64,
40}
41
42impl IntoGrpcParam<EstimateGasPriceRequest> for TxPriority {
43    fn into_parameter(self) -> EstimateGasPriceRequest {
44        EstimateGasPriceRequest {
45            tx_priority: self as i32,
46        }
47    }
48}
49
50impl FromGrpcResponse<f64> for EstimateGasPriceResponse {
51    fn try_from_response(self) -> Result<f64> {
52        Ok(self.estimated_gas_price)
53    }
54}
55
56impl IntoGrpcParam<EstimateGasPriceAndUsageRequest> for (TxPriority, Vec<u8>) {
57    fn into_parameter(self) -> EstimateGasPriceAndUsageRequest {
58        EstimateGasPriceAndUsageRequest {
59            tx_priority: self.0 as i32,
60            tx_bytes: self.1,
61        }
62    }
63}
64
65impl FromGrpcResponse<GasEstimate> for EstimateGasPriceAndUsageResponse {
66    fn try_from_response(self) -> Result<GasEstimate> {
67        Ok(GasEstimate {
68            price: self.estimated_gas_price,
69            usage: self.estimated_gas_used,
70        })
71    }
72}