Skip to content

Commit

Permalink
updating thresholds + STRK new
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetbout committed Feb 20, 2024
1 parent bf5fb00 commit 24234ff
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion crates/starkwhale-alert/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ mod tests {
selector: "Transfer",
threshold: 50, // 50 eth
logo: "♦",
rate_api_id: "ethereum",
rate_api_id: Some("ethereum"),
};
fetch_events(&get_infura_client(), &eth, 200000, 200001)
.await
Expand Down
27 changes: 18 additions & 9 deletions crates/starkwhale-alert/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub struct Token {
pub selector: &'static str, // This should be the String of the selector (Transfer, ...), not the HEX value
pub threshold: u128,
pub logo: &'static str,
pub rate_api_id: &'static str,
pub rate_api_id: Option<&'static str>,
}

pub const TOKENS: &[Token] = &[
Expand All @@ -14,36 +14,45 @@ pub const TOKENS: &[Token] = &[
decimals: 18,
symbol: "ETH",
selector: "Transfer",
threshold: 70,
threshold: 100,
logo: "♦",
rate_api_id: "ethereum",
rate_api_id: Some("ethereum"),
},
Token {
address: "0x053c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8",
decimals: 6,
symbol: "USDC",
selector: "Transfer",
threshold: 100_000,
threshold: 200_000,
logo: "$",
rate_api_id: "usd-coin",
rate_api_id: Some("usd-coin"),
},
Token {
address: "0x068f5c6a61780768455de69077e07e89787839bf8166decfbf92b645209c0fb8",
decimals: 6,
symbol: "USDT",
selector: "Transfer",
threshold: 100_000,
threshold: 200_000,
logo: "$",
rate_api_id: "tether",
rate_api_id: Some("tether"),
},
Token {
address: "0x00da114221cb83fa859dbdb4c44beeaa0bb37c7537ad5ae66fe5e0efd20e6eb3",
decimals: 18,
symbol: "DAI",
selector: "Transfer",
threshold: 100_000,
threshold: 200_000,
logo: "D",
rate_api_id: "multi-collateral-dai",
rate_api_id: Some("multi-collateral-dai"),
},
Token {
address: "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d",
decimals: 18,
symbol: "STRK",
selector: "Transfer",
threshold: 150_000,
logo: "",
rate_api_id: None,
},
];

Expand Down
31 changes: 21 additions & 10 deletions crates/starkwhale-alert/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,19 @@ use crate::{api, consts::Token, consts::ADDRESS_LIST, get_infura_client, starkne

pub async fn get_formatted_text(emitted_event: EmittedEvent, token: &Token) -> String {
let from = emitted_event.data[0];
let to = emitted_event.data[1];
let to: FieldElement = emitted_event.data[1];
let mut amount = to_u256(
emitted_event.data[2].try_into().expect("Error: low"),
emitted_event.data[3].try_into().expect("Error: high"),
);
// 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();
let rate = BigUint::new(vec![(rate * 10000_f64).to_u32().unwrap()]);
let usd_value = (amount * rate).div(BigUint::new(vec![10000]));
let usd_value_string = usd_value
.to_u128()
.unwrap()
.to_formatted_string(&Locale::en);
let usd_value = get_usd_value(token, amount).await;

let first_line = format!(
"{:} #{} {} ({} USD)",
amount_string, token.symbol, token.logo, usd_value_string
amount_string, token.symbol, token.logo, usd_value
);
let second_line = if to == FieldElement::ZERO {
format!("{} bridged to Ethereum L1", format_address(from).await)
Expand All @@ -47,11 +41,28 @@ pub async fn get_formatted_text(emitted_event: EmittedEvent, token: &Token) -> S
format!("{}\n{}\n{}", first_line, second_line, third_line)
}

async fn get_usd_value(token: &Token, amount: BigUint) -> String {
match token.rate_api_id {
Some(coin_id) => {
let rate = api::fetch_coin(coin_id).await.unwrap();
let rate = BigUint::new(vec![(rate * 10000_f64).to_u32().unwrap()]);
let usd_value = (amount * rate).div(BigUint::new(vec![10000]));

usd_value
.to_u128()
.unwrap()
.to_formatted_string(&Locale::en)
}
None => "???".to_owned(),
}
}

fn to_rounded(amount: BigUint, pow: u32) -> BigUint {
let power = 10_u128.pow(pow);
let half_pow = power / 2;
(amount + half_pow) / power
}

trait ToHex {
fn to_hex(self) -> String;
}
Expand Down

0 comments on commit 24234ff

Please sign in to comment.