forked from FuelLabs/fuel-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Latest gas price endpoint (FuelLabs#1649)
Closes: FuelLabs#1647
- Loading branch information
1 parent
afe109f
commit 08cccf4
Showing
12 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,30 @@ | ||
use crate::client::schema::{ | ||
schema, | ||
U32, | ||
U64, | ||
}; | ||
|
||
#[derive(cynic::QueryFragment, Debug)] | ||
#[cynic(schema_path = "./assets/schema.sdl")] | ||
pub struct LatestGasPrice { | ||
pub gas_price: U64, | ||
pub block_height: U32, | ||
} | ||
|
||
#[derive(cynic::QueryFragment, Debug)] | ||
#[cynic(schema_path = "./assets/schema.sdl", graphql_type = "Query")] | ||
pub struct QueryLatestGasPrice { | ||
pub latest_gas_price: LatestGasPrice, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn latest_gas_price_query_gql_output() { | ||
use cynic::QueryBuilder; | ||
let operation = QueryLatestGasPrice::build(()); | ||
insta::assert_snapshot!(operation.query) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...uel_core_client__client__schema__gas_price__tests__latest_gas_price_query_gql_output.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,12 @@ | ||
--- | ||
source: crates/client/src/client/schema/gas_price.rs | ||
expression: operation.query | ||
--- | ||
query { | ||
latestGasPrice { | ||
gasPrice | ||
blockHeight | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,16 @@ | ||
use crate::client::schema; | ||
|
||
pub struct LatestGasPrice { | ||
pub gas_price: u64, | ||
pub block_height: u32, | ||
} | ||
|
||
// GraphQL Translation | ||
impl From<schema::gas_price::LatestGasPrice> for LatestGasPrice { | ||
fn from(value: schema::gas_price::LatestGasPrice) -> Self { | ||
Self { | ||
gas_price: value.gas_price.into(), | ||
block_height: value.block_height.into(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,54 @@ | ||
use super::scalars::{ | ||
U32, | ||
U64, | ||
}; | ||
use crate::{ | ||
fuel_core_graphql_api::{ | ||
database::ReadView, | ||
Config as GraphQLConfig, | ||
}, | ||
query::BlockQueryData, | ||
}; | ||
use async_graphql::{ | ||
Context, | ||
Object, | ||
}; | ||
use fuel_core_types::blockchain::block::Block; | ||
|
||
pub struct LatestGasPrice { | ||
pub gas_price: U64, | ||
pub block_height: U32, | ||
} | ||
|
||
#[Object] | ||
impl LatestGasPrice { | ||
async fn gas_price(&self) -> U64 { | ||
self.gas_price | ||
} | ||
|
||
async fn block_height(&self) -> U32 { | ||
self.block_height | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct LatestGasPriceQuery {} | ||
|
||
#[Object] | ||
impl LatestGasPriceQuery { | ||
async fn latest_gas_price( | ||
&self, | ||
ctx: &Context<'_>, | ||
) -> async_graphql::Result<LatestGasPrice> { | ||
let config = ctx.data_unchecked::<GraphQLConfig>(); | ||
|
||
let query: &ReadView = ctx.data_unchecked(); | ||
let latest_block: Block<_> = query.latest_block()?; | ||
let block_height = u32::from(*latest_block.header().height()); | ||
|
||
Ok(LatestGasPrice { | ||
gas_price: config.min_gas_price.into(), | ||
block_height: block_height.into(), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,18 @@ | ||
use fuel_core::service::{ | ||
Config, | ||
FuelService, | ||
}; | ||
use fuel_core_client::client::{ | ||
types::gas_price::LatestGasPrice, | ||
FuelClient, | ||
}; | ||
|
||
#[tokio::test] | ||
async fn latest_gas_price() { | ||
let node_config = Config::local_node(); | ||
let srv = FuelService::new_node(node_config.clone()).await.unwrap(); | ||
let client = FuelClient::from(srv.bound_address); | ||
|
||
let LatestGasPrice { gas_price, .. } = client.latest_gas_price().await.unwrap(); | ||
assert_eq!(gas_price, node_config.txpool.min_gas_price); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters