Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add :height param to balance endpoint and use in test #2335

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,40 @@ impl SequencerClient {
Some(block) => block,
None => self.get_height().await?,
};

// As of block zero the state is empty, and the balance will be zero.
if block == 0 {
return Ok(0.into());
}

// Block is non-zero, we can safely decrement to query the state as of the previous block.
block -= 1;

tracing::debug!(%address, block, "fetching Espresso balance");
dbg!(&address, &block);

let balance = self
.0
.get::<Option<FeeAmount>>(&format!("fee-state/fee-balance/{block}/{address:#x}"))
.send()
.await
.unwrap()
.unwrap();

Ok(balance)
}

/// Get the balance for a given account at a given block height, defaulting to current balance.
pub async fn get_espresso_balance_legacy(
&self,
address: Address,
block: Option<u64>,
) -> anyhow::Result<FeeAmount> {
// Get the block height to query at, defaulting to the latest block.
let mut block = match block {
Some(block) => block,
None => self.get_height().await?,
};
// As of block zero the state is empty, and the balance will be zero.
if block == 0 {
return Ok(0.into());
Expand Down
3 changes: 2 additions & 1 deletion sequencer/api/merklized_state.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[route.getfeebalance]
PATH = ["fee-balance/latest/:address"]
PATH = ["fee-balance/:height/:address"]
":height" = "Integer"
":address" = "Literal"
DOC = "Get current balance in fee state. Expected parameter is an Ethereum address in hex format."
22 changes: 16 additions & 6 deletions sequencer/src/api/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,28 @@ where
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)?;
let mut api = merklized_state::define_api::<
State,
SeqTypes,
FeeMerkleTree,
Ver,
{ FeeMerkleTree::ARITY },
>(&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 height = req.string_param("height")?.parse::<u64>().map_err(|e| {
merklized_state::Error::Custom {
message: format!("failed to parse block height: {e}"),
status: StatusCode::BAD_REQUEST,
}
})?;
let snapshot = Snapshot::Index(height);
let key = address
.parse()
.map_err(|_| merklized_state::Error::Custom {
message: "failed to parse address".to_string(),
.map_err(|e| merklized_state::Error::Custom {
message: format!("failed to parse address: {e}"),
status: StatusCode::BAD_REQUEST,
})?;
let path = state.get_path(snapshot, key).await?;
Expand Down
Loading