Skip to content

Commit

Permalink
feat(gclient): impl at_block for query at specific block (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
vobradovich authored Jan 20, 2025
1 parent b312938 commit f163640
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions rs/src/gclient/calls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
calls::Remoting,
calls::{Query, Remoting},
errors::{Result, RtlError},
events::Listener,
prelude::*,
Expand All @@ -16,13 +16,19 @@ use gear_core_errors::ReplyCode;
#[derive(Debug, Default)]
pub struct GClientArgs {
voucher: Option<(VoucherId, bool)>,
at_block: Option<H256>,
}

impl GClientArgs {
pub fn with_voucher(mut self, voucher_id: VoucherId, keep_alive: bool) -> Self {
self.voucher = Some((voucher_id, keep_alive));
self
}

fn at_block(mut self, hash: H256) -> Self {
self.at_block = Some(hash);
self
}
}

#[derive(Clone)]
Expand Down Expand Up @@ -127,7 +133,7 @@ impl Remoting for GClientRemoting {
payload: impl AsRef<[u8]>,
#[cfg(not(feature = "ethexe"))] gas_limit: Option<GasUnit>,
value: ValueUnit,
_args: GClientArgs,
args: GClientArgs,
) -> Result<Vec<u8>> {
let api = self.api;
// Get Max gas amount if it is not explicitly set
Expand All @@ -143,7 +149,14 @@ impl Remoting for GClientRemoting {
let payload = payload.as_ref().to_vec();

let reply_info = api
.calculate_reply_for_handle(Some(origin), target, payload, gas_limit, value)
.calculate_reply_for_handle_at(
Some(origin),
target,
payload,
gas_limit,
value,
args.at_block,
)
.await?;

match reply_info.code {
Expand Down Expand Up @@ -202,3 +215,17 @@ async fn get_events_from_block(
.await?;
Ok(vec)
}

pub trait QueryAtBlock {
/// Query at a specific block.
fn at_block(self, hash: H256) -> Self;
}

impl<T> QueryAtBlock for T
where
T: Query<Args = GClientArgs>,
{
fn at_block(self, hash: H256) -> Self {
self.with_args(GClientArgs::default().at_block(hash))
}
}

0 comments on commit f163640

Please sign in to comment.