Skip to content

Commit

Permalink
Fix: balance error. (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
chinyuchan authored Oct 22, 2024
1 parent ce48f34 commit 8f3409c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
30 changes: 19 additions & 11 deletions evm-exporter/src/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,16 @@ impl Getter for PgGetter {
.get::<&str, i64>("lowest_height") as u32)
}
fn get_balance(&self, height: u32, address: H160) -> Result<U256> {
Ok(U256::from_str(
self.conn
.get()?
.query_one(
"SELECT balance FROM balance WHERE address = $1 ORDER BY height DESC LIMIT 1",
&[&format!("{:?}", address).to_lowercase()],
)?
.get("balance"),
)?)
match self.conn.get()?.query_one(
"SELECT balance FROM balance WHERE address = $1 ORDER BY height DESC LIMIT 1",
&[&format!("{:?}", address).to_lowercase()],
) {
Ok(row) => {
let b = row.try_get("balance")?;
Ok(U256::from_str(b)?)
}
_ => Ok(U256::zero()),
}
}
fn get_nonce(&self, height: u32, address: H160) -> Result<U256> {
Ok(U256::from_str(
Expand Down Expand Up @@ -143,7 +144,11 @@ impl Getter for PgGetter {
.get()?
.query_one(
"SELECT value FROM state WHERE idx = $1 AND address = $2 AND height = $3",
&[&format!("{:?}", index), &format!("{:?}", address).to_lowercase(), &(height as i64)],
&[
&format!("{:?}", index),
&format!("{:?}", address).to_lowercase(),
&(height as i64),
],
)?
.get("value"),
)?)
Expand Down Expand Up @@ -259,7 +264,10 @@ impl Getter for PgGetter {
.get()?
.query_one(
"SELECT value FROM pending_state WHERE address = $1 AND idx = $2",
&[&format!("{:?}", address).to_lowercase(), &format!("{:?}", index)],
&[
&format!("{:?}", address).to_lowercase(),
&format!("{:?}", index),
],
)?
.get("value"),
)?))
Expand Down
12 changes: 6 additions & 6 deletions web3-service/src/rpc/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl EthApi for EthService {
.await
.map_err(|e| {
internal_err(format!(
"eth api balance get_pending_balance error: {:?}",
"eth api balance get_pending_balance error1: {:?}",
e.to_string()
))
})?;
Expand All @@ -253,7 +253,7 @@ impl EthApi for EthService {
Ok(None) => {}
Err(e) => {
return Err(internal_err(format!(
"eth api balance get_pending_balance error: {:?}",
"eth api balance get_pending_balance error2: {:?}",
e.to_string()
)))
}
Expand All @@ -267,13 +267,13 @@ impl EthApi for EthService {
.await
.map_err(|e| {
internal_err(format!(
"eth api balance block_number_to_height error: {:?}",
"eth api balance block_number_to_height error1: {:?}",
e.to_string()
))
})?
.map_err(|e| {
internal_err(format!(
"eth api balance block_number_to_height error: {:?}",
"eth api balance block_number_to_height error2: {:?}",
e.to_string()
))
})?;
Expand All @@ -282,15 +282,15 @@ impl EthApi for EthService {
.await
.map_err(|e| {
internal_err(format!(
"eth api balance get_balance error: {:?}",
"eth api balance get_balance error1: {:?}",
e.to_string()
))
})?;

match result {
Ok(balance) => Ok(balance),
Err(e) => Err(internal_err(format!(
"eth api balance get_balance error: {:?}",
"eth api balance get_balance error2: {:?}",
e.to_string()
))),
}
Expand Down

0 comments on commit 8f3409c

Please sign in to comment.