Skip to content

Commit

Permalink
Latest gas price endpoint (FuelLabs#1649)
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner authored Feb 8, 2024
1 parent afe109f commit 08cccf4
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 10,7 @@ Description of the upcoming release here.

### Changed

- [#1649](https://github.com/FuelLabs/fuel-core/pull/1649): Add api endpoint for getting latest gas price
- [#1600](https://github.com/FuelLabs/fuel-core/pull/1640): Upgrade to fuel-vm 0.45.0
- [#1633](https://github.com/FuelLabs/fuel-core/pull/1633): Notify services about importing of the genesis block.
- [#1625](https://github.com/FuelLabs/fuel-core/pull/1625): Making relayer independent from the executor and preparation for the force transaction inclusion.
Expand Down
6 changes: 6 additions & 0 deletions crates/client/assets/schema.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 518,11 @@ type InputMessage {
}


type LatestGasPrice {
gasPrice: U64!
blockHeight: U32!
}

type LightOperation {
base: U64!
unitsPerGas: U64!
Expand Down Expand Up @@ -806,6 811,7 @@ type Query {
contractBalance(contract: ContractId!, asset: AssetId!): ContractBalance!
contractBalances(filter: ContractBalanceFilterInput!, first: Int, after: String, last: Int, before: String): ContractBalanceConnection!
nodeInfo: NodeInfo!
latestGasPrice: LatestGasPrice!
message(nonce: Nonce!): Message
messages(owner: Address, first: Int, after: String, last: Int, before: String): MessageConnection!
messageProof(transactionId: TransactionId!, nonce: Nonce!, commitBlockId: BlockId, commitBlockHeight: U32): MessageProof
Expand Down
6 changes: 6 additions & 0 deletions crates/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 12,7 @@ use crate::client::{
TransactionId,
},
types::{
gas_price::LatestGasPrice,
message::MessageStatus,
primitives::{
Address,
Expand Down Expand Up @@ -348,6 349,11 @@ impl FuelClient {
self.query(query).await.map(|r| r.node_info.into())
}

pub async fn latest_gas_price(&self) -> io::Result<LatestGasPrice> {
let query = schema::gas_price::QueryLatestGasPrice::build(());
self.query(query).await.map(|r| r.latest_gas_price.into())
}

pub async fn connected_peers_info(&self) -> io::Result<Vec<PeerInfo>> {
let query = schema::node_info::QueryPeersInfo::build(());
self.query(query)
Expand Down
2 changes: 2 additions & 0 deletions crates/client/src/client/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 33,8 @@ pub mod coins;
pub mod contract;
pub mod message;
pub mod node_info;

pub mod gas_price;
pub mod primitives;
pub mod tx;

Expand Down
30 changes: 30 additions & 0 deletions crates/client/src/client/schema/gas_price.rs
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)
}
}
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
}
}


2 changes: 2 additions & 0 deletions crates/client/src/client/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 4,8 @@ pub mod chain_info;
pub mod coins;
pub mod contract;
pub mod gas_costs;

pub mod gas_price;
pub mod merkle_proof;
pub mod message;
pub mod node_info;
Expand Down
16 changes: 16 additions & 0 deletions crates/client/src/client/types/gas_price.rs
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(),
}
}
}
3 changes: 3 additions & 0 deletions crates/fuel-core/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 28,8 @@ pub mod dap;
pub mod health;
pub mod message;
pub mod node_info;

pub mod gas_price;
pub mod scalars;
pub mod tx;

Expand All @@ -43,6 45,7 @@ pub struct Query(
contract::ContractQuery,
contract::ContractBalanceQuery,
node_info::NodeQuery,
gas_price::LatestGasPriceQuery,
message::MessageQuery,
);

Expand Down
54 changes: 54 additions & 0 deletions crates/fuel-core/src/schema/gas_price.rs
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(),
})
}
}
18 changes: 18 additions & 0 deletions tests/tests/gas_price.rs
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);
}
2 changes: 2 additions & 0 deletions tests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 11,8 @@ mod dap;
mod debugger;
mod deployment;
mod fee_collection_contract;

mod gas_price;
mod health;
mod helpers;
mod messages;
Expand Down

0 comments on commit 08cccf4

Please sign in to comment.