Skip to content

Commit

Permalink
Adding API point to get fee balance per account
Browse files Browse the repository at this point in the history
  • Loading branch information
fkrell committed Oct 11, 2024
1 parent 26f6d6e commit 4401ce7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
48 changes: 42 additions & 6 deletions sequencer/src/api/endpoints.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
//! Sequencer-specific API endpoint handlers.
use std::{
collections::{BTreeSet, HashMap},
env,
};

use anyhow::Result;
use committable::Committable;
use espresso_types::{NamespaceId, NsProof, PubKey, Transaction};
use espresso_types::{FeeMerkleTree, NamespaceId, NsProof, PubKey, Transaction};
use futures::{try_join, FutureExt};
use hotshot_query_service::merklized_state::Snapshot;
use hotshot_query_service::{
availability::{self, AvailabilityDataSource, CustomSnafu, FetchBlockSnafu},
data_source::storage::ExplorerStorage,
Expand All @@ -27,6 +23,10 @@ use hotshot_types::{
};
use serde::{de::Error as _, Deserialize, Serialize};
use snafu::OptionExt;
use std::{
collections::{BTreeSet, HashMap},
env,
};
use tagged_base64::TaggedBase64;
use tide_disco::{method::ReadState, Api, Error as _, StatusCode};
use vbs::version::StaticVersionType;
Expand All @@ -46,6 +46,42 @@ pub struct NamespaceProofQueryData {
pub transactions: Vec<Transaction>,
}

pub(super) fn get_balance<State, Ver>() -> Result<Api<State, merklized_state::Error, Ver>>
where
State: 'static + Send + Sync + ReadState,
Ver: 'static + StaticVersionType,
<State as ReadState>::State: Send
+ Sync
+ MerklizedStateDataSource<SeqTypes, FeeMerkleTree, 256>
+ MerklizedStateHeightPersistence,
//for<'a> <<UniversalMerkleTree<FeeAmount,Sha3Digest,FeeAccount,256,Sha3Node> as hotshot_query_service::merklized_state::MerklizedState<Types, ARITY>>::Commit as TryFrom<&'a TaggedBase64>>::Error: Display,
{
let mut options = merklized_state::Options::default();
let extension = toml::from_str(include_str!("../../api/merklized_state.toml"))?;
options.extensions.push(extension);

let mut api =
merklized_state::define_api::<State, SeqTypes, FeeMerkleTree, Ver, 256>(&options)?;

api.get("getfeebalance", move |req, state| {
async move {
let address = req.string_param("address")?;
let height = state.get_last_state_height().await?;
let snapshot = Snapshot::Index(height as u64);
let key = address
.parse()
.map_err(|_| merklized_state::Error::Custom {
message: "failed to parse Key param".to_string(),
status: StatusCode::INTERNAL_SERVER_ERROR,
})?;
let path = state.get_path(snapshot, key).await?;
Ok(path.elem().copied())
}
.boxed()
})?;
Ok(api)
}

pub(super) type AvailState<N, P, D, ApiVer> = ApiState<StorageState<N, P, D, ApiVer>>;

type AvailabilityApi<N, P, D, V, ApiVer> = Api<AvailState<N, P, D, V>, availability::Error, ApiVer>;
Expand Down
4 changes: 2 additions & 2 deletions sequencer/src/api/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use async_std::sync::Arc;
use clap::Parser;
use espresso_types::{
v0::traits::{EventConsumer, NullEventConsumer, SequencerPersistence},
BlockMerkleTree, FeeMerkleTree, PubKey,
BlockMerkleTree, PubKey,
};
use futures::{
channel::oneshot,
Expand Down Expand Up @@ -356,7 +356,7 @@ impl Options {
// Initialize merklized state module for fee merkle tree
app.register_module(
"fee-state",
endpoints::merklized_state::<N, P, _, FeeMerkleTree, _, 256>()?,
endpoints::get_balance::<_, SequencerApiVersion>()?,
)?;

let state = state.clone();
Expand Down

0 comments on commit 4401ce7

Please sign in to comment.