Skip to content

Commit

Permalink
Removed the unnecessary size check of the hexadecimal balance.
Browse files Browse the repository at this point in the history
  • Loading branch information
THLO committed Sep 9, 2024
1 parent e2382e7 commit 148ba4e
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions rust/basic_ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub async fn get_balance(address: String) -> Nat {

let hex_balance = match response {
RequestResult::Ok(balance_result) => {
// The response to a successful `eth_getBalance` call has the format
// The response to a successful `eth_getBalance` call has the following format:
// { "id": "[ID]", "jsonrpc": "2.0", "result": "[BALANCE IN HEX]" }
let response: serde_json::Value = serde_json::from_str(&balance_result).unwrap();
response
Expand All @@ -73,14 +73,8 @@ pub async fn get_balance(address: String) -> Nat {
RequestResult::Err(e) => panic!("Received an error response: {:?}", e),
};

// Make sure that the number of digits is even and remove the "0x" prefix.
let hex_balance = if hex_balance.len() % 2 != 0 {
format!("0{}", &hex_balance[2..])
} else {
hex_balance[2..].to_string()
};

Nat(BigUint::from_str_radix(&hex_balance, 16).unwrap())
// Remove the "0x" prefix before converting to a decimal number.
Nat(BigUint::from_str_radix(&hex_balance[2..], 16).unwrap())
}

#[update]
Expand Down

0 comments on commit 148ba4e

Please sign in to comment.