Skip to content

Commit

Permalink
filtering on address list
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetbout committed Oct 19, 2023
1 parent 87a8279 commit 3718af1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const USDT: Token = Token {
rate_api_id: "tether",
};

const LAYER_SWAP: AddressToName = AddressToName {
pub const LAYER_SWAP: AddressToName = AddressToName {
address: "19252b1deef483477c4d30cfcc3e5ed9c82fafea44669c182a45a01b4fdb97a",
name: "Layerswap",
};
Expand Down
101 changes: 99 additions & 2 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use starknet::core::types::{EmittedEvent, FieldElement};

use crate::{
api::{address_to_domain, fetch_coin, Token},
consts::ADDRESS_LIST,
get_infura_client, to_u256,
};

Expand Down Expand Up @@ -68,12 +69,22 @@ fn to_hex(fe: FieldElement) -> String {
}

async fn format_address(address: FieldElement) -> String {
let address_as_hex = to_hex(address); // TODO ONGOING ADD FILTER ON CONTRACT ADDRESSES
let named_address = ADDRESS_LIST
.iter()
.find(|item| address_as_hex.ends_with(item.address));
if let Some(address_to_name) = named_address {
return address_to_name.name.to_string();
};
let starknet_id = address_to_domain(get_infura_client(), address).await;
match starknet_id {
Some(name) => name,
None => {
let address = to_hex(address);
format!("{}...{}", &address[0..5], &address[address.len() - 4..],)
format!(
"{}...{}",
&address_as_hex[0..5],
&address_as_hex[address_as_hex.len() - 4..],
)
}
}
}
Expand Down Expand Up @@ -329,4 +340,90 @@ mod tests {
"Should be correct"
);
}

#[tokio::test]
async fn test_get_formatted_text_to_layer_swap() {
let keys = vec![FieldElement::from_hex_be(
"0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9",
)
.unwrap()];
let data = vec![
FieldElement::from_hex_be(
"0x6e14b249c412a336e7a5a3473da083b9159e6845be4d02ee50f6095a5b3ce",
)
.unwrap(),
FieldElement::from_hex_be(
"0x019252b1deef483477c4d30cfcc3e5ed9c82fafea44669c182a45a01b4fdb97a",
)
.unwrap(),
FieldElement::from_hex_be("0xe8d4a51000").unwrap(),
FieldElement::from_hex_be("0x0").unwrap(),
];
let emitted_event = EmittedEvent {
from_address: FieldElement::from_hex_be(
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
)
.unwrap(),
keys,
data,
block_hash: FieldElement::from_hex_be(
"0x030905d20477c31ecc0951a8c7d2f8c91d16a2ce864aaad2730aa330e328dc6a",
)
.unwrap(),
block_number: 237165,
transaction_hash: FieldElement::from_hex_be(
"0x732b09d901fb0075d283ac23cbaae4f8c486123a88a621eeaa05d0b5ddfb8d8",
)
.unwrap(),
};
let response = get_formatted_text(emitted_event, &USDC).await;
assert!(
response
== "1.000.000 #USDC $ (1.000.000 USD)\nFrom 0x6e1...b3ce to Layerswap\nhttps://starkscan.co/tx/0x732b09d901fb0075d283ac23cbaae4f8c486123a88a621eeaa05d0b5ddfb8d8",
"Should be correct"
);
}
// TODO Refactor not to test get formatted text but just `format_address`
#[tokio::test]
async fn test_get_formatted_text_from_layer_swap() {
let keys = vec![FieldElement::from_hex_be(
"0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9",
)
.unwrap()];
let data = vec![
FieldElement::from_hex_be(
"0x019252b1deef483477c4d30cfcc3e5ed9c82fafea44669c182a45a01b4fdb97a",
)
.unwrap(),
FieldElement::from_hex_be(
"0x6e14b249c412a336e7a5a3473da083b9159e6845be4d02ee50f6095a5b3ce",
)
.unwrap(),
FieldElement::from_hex_be("0xe8d4a51000").unwrap(),
FieldElement::from_hex_be("0x0").unwrap(),
];
let emitted_event = EmittedEvent {
from_address: FieldElement::from_hex_be(
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
)
.unwrap(),
keys,
data,
block_hash: FieldElement::from_hex_be(
"0x030905d20477c31ecc0951a8c7d2f8c91d16a2ce864aaad2730aa330e328dc6a",
)
.unwrap(),
block_number: 237165,
transaction_hash: FieldElement::from_hex_be(
"0x732b09d901fb0075d283ac23cbaae4f8c486123a88a621eeaa05d0b5ddfb8d8",
)
.unwrap(),
};
let response = get_formatted_text(emitted_event, &USDC).await;
assert!(
response
== "1.000.000 #USDC $ (1.000.000 USD)\nFrom Layerswap to 0x6e1...b3ce\nhttps://starkscan.co/tx/0x732b09d901fb0075d283ac23cbaae4f8c486123a88a621eeaa05d0b5ddfb8d8",
"Should be correct"
);
}
}
21 changes: 1 addition & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use api::{fetch_events, Token};
use consts::{AddressToName, ADDRESS_LIST};
use dotenv::dotenv;
use log::info;
use num_bigint::{BigUint, ToBigInt};
Expand Down Expand Up @@ -99,16 +98,11 @@ fn to_u256(low: u128, high: u128) -> BigUint {
low_vec.append(&mut high_vec);
BigUint::new(low_vec)
}

fn ends_with(a: &str) -> Option<&AddressToName> {
ADDRESS_LIST.iter().find(|item| a.ends_with(item.address))
}

#[cfg(test)]
mod tests {
use num_bigint::BigUint;

use super::{ends_with, to_u256};
use super::to_u256;

#[test]
fn test_big_int() {
Expand All @@ -119,17 +113,4 @@ mod tests {
assert!(u256_1 < u256_2, "1");
assert!((u256_1 + BigUint::new(vec![1])).eq(&u256_2), "2");
}

#[test]
fn test_ends_with() {
let a = ends_with("0x7b393627bd514d2aa4c83e9f0c468939df15ea3c29980cd8e7be3ec847795f0");
assert!(a.is_some(), "Should be some");
assert!(
a.unwrap().name == "Orbiter Finance Bridge 1",
"Should be Orbiter Finance Bridge 1"
);

let b = ends_with("0x7b393627bd5132114c83e9f0c468939df15ea3c29980cd8e7be3ec847795f0");
assert!(b.is_none(), "Should be None");
}
}

0 comments on commit 3718af1

Please sign in to comment.