Skip to content

Commit

Permalink
implement balance query using view client
Browse files Browse the repository at this point in the history
  • Loading branch information
avahowell committed Feb 21, 2024
1 parent 9084197 commit e939d0f
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions crates/relayer/src/chain/penumbra/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use penumbra_proto::{
view::v1::{
broadcast_transaction_response::Status as BroadcastStatus,
view_service_client::ViewServiceClient, view_service_server::ViewServiceServer,
BalancesRequest, GasPricesRequest,
GasPricesRequest,
},
};
use penumbra_transaction::gas::GasCost;
Expand Down Expand Up @@ -434,6 +434,35 @@ impl PenumbraChain {

Ok(id)
}

async fn query_balance(
&self,
address_index: AddressIndex,
denom: &str,
) -> Result<crate::account::Balance, anyhow::Error> {
let mut view_client = self.view_client.lock().await.clone();
let assets = ViewClient::assets(&mut view_client).await?;
let asset_id = assets
.get_unit(denom)
.ok_or_else(|| anyhow::anyhow!("denom not found"))?
.id();

let balances =
ViewClient::balances(&mut view_client, address_index, Some(asset_id)).await?;

for (id, amount) in balances {
if id != asset_id {
continue; // should never happen
}

return Ok(crate::account::Balance {
amount: amount.to_string(),
denom: denom.to_string(),
});
}

Err(anyhow::anyhow!("denom not found"))
}
}

impl ChainEndpoint for PenumbraChain {
Expand Down Expand Up @@ -670,37 +699,14 @@ impl ChainEndpoint for PenumbraChain {

fn query_balance(
&self,
key_name: Option<&str>,
_key_name: Option<&str>,
denom: Option<&str>,
) -> Result<crate::account::Balance, Error> {
let mut view_client = self.rt.block_on(self.view_client.lock()).clone();

/* TODO: implement balance query
let balances = self
.rt
.block_on(
view_client
.balances(BalancesRequest {
account_filter: None,
asset_id_filter: None,
})
.boxed(),
)
.map_err(|_| Error::temp_penumbra_error("error querying balances".to_string()))?;
for balance in balances.next()
if balance.denom == "upenumbra" {
return Ok(crate::account::Balance {
amount: balance.amount,
denom: balance.denom,
});
}
} */
let denom = denom.unwrap_or("upenumbra");

Ok(crate::account::Balance {
amount: "100000".to_string(),
denom: "upenumbra".to_string(),
})
self.rt
.block_on(self.query_balance(AddressIndex::new(0), denom))
.map_err(|e| Error::temp_penumbra_error(e.to_string()))
}

fn query_all_balances(
Expand Down

0 comments on commit e939d0f

Please sign in to comment.