Skip to content

Commit

Permalink
fix u256 parsing + to_rounded using wad logic
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetbout committed Nov 14, 2023
1 parent 7cb2b70 commit fce99df
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
13 changes: 4 additions & 9 deletions crates/starkwhale-alert/src/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bigdecimal::{FromPrimitive, ToPrimitive};
use bigdecimal::ToPrimitive;
use num_bigint::BigUint;
use num_format::{Locale, ToFormattedString};
use starknet::core::types::{EmittedEvent, FieldElement};
Expand All @@ -13,7 +13,7 @@ pub async fn get_formatted_text(emitted_event: EmittedEvent, token: &Token) -> S
emitted_event.data[2].try_into().expect("Error: low"),
emitted_event.data[3].try_into().expect("Error: high"),
);
// TODO No need to use this one yet?
// TODO This whole rounding logic and * 10000_f64 seems odd
amount = to_rounded(amount, token.decimals);
let amount_string = amount.to_u128().unwrap().to_formatted_string(&Locale::en);
let rate = api::fetch_coin(token.rate_api_id).await.unwrap();
Expand Down Expand Up @@ -49,13 +49,8 @@ pub async fn get_formatted_text(emitted_event: EmittedEvent, token: &Token) -> S

fn to_rounded(amount: BigUint, pow: u32) -> BigUint {
let power = 10_u128.pow(pow);
let amount = &amount / power;
let rounding = &amount / (power / 10) % &amount;
if rounding > BigUint::from_u8(5).unwrap() {
amount + 1_u128
} else {
amount
}
let half_pow = power / 2;
(amount + half_pow) / power
}
trait ToHex {
fn to_hex(self) -> String;
Expand Down
15 changes: 6 additions & 9 deletions crates/starkwhale-alert/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use consts::{Token, TOKENS};
use dotenv::dotenv;
use log::info;
use num_bigint::{BigUint, ToBigInt};
use num_bigint::BigUint;
use reqwest::Url;
use starknet::{
core::types::EmittedEvent,
Expand Down Expand Up @@ -122,14 +122,11 @@ pub fn get_infura_client() -> JsonRpcClient<HttpTransport> {
}

fn to_u256(low: u128, high: u128) -> BigUint {
// TODO There is prob a better solution to do that...
let mut low_vec = low.to_bigint().unwrap().to_u32_digits().1;
let mut high_vec = high.to_bigint().unwrap().to_u32_digits().1;
for _ in low_vec.len()..4 {
low_vec.push(0_u32)
}
low_vec.append(&mut high_vec);
BigUint::new(low_vec)
let mut bytes: Vec<u8> = Vec::new();
bytes.extend(high.to_be_bytes());
bytes.extend(low.to_be_bytes());

BigUint::from_bytes_be(&bytes[..])
}
#[cfg(test)]
mod tests {
Expand Down

0 comments on commit fce99df

Please sign in to comment.