Skip to content

Commit

Permalink
formatter uses starknetid
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetbout committed Oct 19, 2023
1 parent 4a1dfbd commit 87a8279
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 46 deletions.
2 changes: 1 addition & 1 deletion db/block.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"last_processsed_block": 336406
"last_processsed_block": 336635
}
43 changes: 13 additions & 30 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::consts::STARKNET_ID_CONTRACT_ADDRESS;
use bigdecimal::num_traits;
use num_traits::cast::ToPrimitive;
use reqwest::{header, header::HeaderValue};
Expand Down Expand Up @@ -120,35 +121,37 @@ pub async fn fetch_events(
}
}

async fn address_to_domain(rpc_client: JsonRpcClient<HttpTransport>, address: FieldElement) {
let repsonse = rpc_client
pub async fn address_to_domain(
rpc_client: JsonRpcClient<HttpTransport>,
address: FieldElement,
) -> Option<String> {
let response = rpc_client
.call(
FunctionCall {
contract_address: FieldElement::from_hex_be(
"0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678",
)
.unwrap(),
contract_address: STARKNET_ID_CONTRACT_ADDRESS,
entry_point_selector: get_selector_from_name("address_to_domain").unwrap(),
calldata: vec![address],
},
BlockId::Tag(BlockTag::Latest),
)
.await
.unwrap();

if response.len() == 1 && response[0] == FieldElement::ZERO {
return None;
}
let mut domain = String::new();
repsonse.iter().skip(1).for_each(|value| {
response.iter().skip(1).for_each(|value| {
domain.push_str(decode(*value).as_str());
domain.push('.');
});
domain.push_str("stark");
println!("DOMAIN FOUND {}", domain);
Some(domain)
}

const BASIC_ALPHABET: &str = "abcdefghijklmnopqrstuvwxyz0123456789-";
const BIG_ALPHABET: &str = "这来";

pub fn decode(mut felt: FieldElement) -> String {
fn decode(mut felt: FieldElement) -> String {
let mut decoded: String = String::new();
let basic_plus = FieldElement::from(BASIC_ALPHABET.chars().count() + 1);
let basic_len = FieldElement::from(BASIC_ALPHABET.chars().count());
Expand Down Expand Up @@ -263,26 +266,6 @@ mod tests {
)
.await;

// eli
address_to_domain(
get_infura_client(),
FieldElement::from_hex_be(
"0x48f24d0d0618fa31813db91a45d8be6c50749e5e19ec699092ce29abe809294",
)
.unwrap(),
)
.await;

// scott
address_to_domain(
get_infura_client(),
FieldElement::from_hex_be(
"0x225bd17f4b4ede26c77673d8d40ec9805ec139a8167cae8d621bd295b260d13",
)
.unwrap(),
)
.await;

address_to_domain(
get_infura_client(),
FieldElement::from_hex_be("0x225bd17f4b4ede26c77673d8d3").unwrap(),
Expand Down
9 changes: 9 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
use starknet::core::types::FieldElement;

use crate::api::Token;

pub const STARKNET_ID_CONTRACT_ADDRESS: FieldElement = FieldElement::from_mont([
9876522541644636344,
16204762974907305178,
9525933456780166611,
327799339589885214,
]);

pub const TOKENS: &'static [Token] = &[ETH, USDC, USDT];
pub const ADDRESS_LIST: &'static [AddressToName] = &[
LAYER_SWAP,
Expand Down
240 changes: 227 additions & 13 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use num_bigint::BigUint;
use starknet::core::types::{EmittedEvent, FieldElement};

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

pub async fn get_formatted_text(emitted_event: EmittedEvent, token: &Token) -> String {
Expand Down Expand Up @@ -44,11 +44,15 @@ pub async fn get_formatted_text(emitted_event: EmittedEvent, token: &Token) -> S
amount_string, token.symbol, token.logo, usd_value_string
);
let second_line = if to == FieldElement::ZERO {
format!("{} bridged to Ethereum L1", format_address(from))
format!("{} bridged to Ethereum L1", format_address(from).await)
} else if from == FieldElement::ZERO {
format!("{} bridged to Starknet L2", format_address(to))
format!("{} bridged to Starknet L2", format_address(to).await)
} else {
format!("From {} to {}", format_address(from), format_address(to),)
format!(
"From {} to {}",
format_address(from).await,
format_address(to).await
)
};

let third_line = format!(
Expand All @@ -63,21 +67,25 @@ fn to_hex(fe: FieldElement) -> String {
format!("{:#x}", fe)
}

fn format_address(address: FieldElement) -> String {
let address = to_hex(address);
format!("{}...{}", &address[0..5], &address[address.len() - 4..],)
async fn format_address(address: FieldElement) -> 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..],)
}
}
}

#[cfg(test)]
mod tests {
use starknet::core::types::{EmittedEvent, FieldElement};

use crate::consts::USDC;

use super::get_formatted_text;
use crate::consts::USDC;
use starknet::core::types::{EmittedEvent, FieldElement};

#[tokio::test]
async fn test_get_formatted_text() {
async fn test_get_formatted_text_bridge_to_starknet() {
let keys = vec![FieldElement::from_hex_be(
"0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9",
)
Expand Down Expand Up @@ -115,4 +123,210 @@ mod tests {
"Should be https://twitter.com/StarkWhaleAlert/status/1703701997629722850"
);
}

#[tokio::test]
async fn test_get_formatted_text_bridge_to_starknet_with_starknet_id() {
let keys = vec![FieldElement::from_hex_be(
"0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9",
)
.unwrap()];
let data = vec![
FieldElement::from_hex_be("0x0").unwrap(),
FieldElement::from_hex_be(
"0x1f4055a52c859593e79988bfe998b536066805fe757522ece47945f46f6b6e7",
)
.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)\nstark.stark bridged to Starknet L2\nhttps://starkscan.co/tx/0x732b09d901fb0075d283ac23cbaae4f8c486123a88a621eeaa05d0b5ddfb8d8",
"Should be https://twitter.com/StarkWhaleAlert/status/1703701997629722850"
);
}

#[tokio::test]
async fn test_get_formatted_text_bridge_to_l1() {
let keys = vec![FieldElement::from_hex_be(
"0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9",
)
.unwrap()];
let data = vec![
FieldElement::from_hex_be(
"0x6e14b28449c412a336e7a5a3473da083b9159e6845be4d02ee50f6095a5b3ce",
)
.unwrap(),
FieldElement::from_hex_be("0x0").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)\n0x6e1...b3ce bridged to Ethereum L1\nhttps://starkscan.co/tx/0x732b09d901fb0075d283ac23cbaae4f8c486123a88a621eeaa05d0b5ddfb8d8",
"Should be correct"
);
}

#[tokio::test]
async fn test_get_formatted_text_bridge_to_l1_with_starknet_id() {
let keys = vec![FieldElement::from_hex_be(
"0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9",
)
.unwrap()];
let data = vec![
FieldElement::from_hex_be(
"0x1f4055a52c859593e79988bfe998b536066805fe757522ece47945f46f6b6e7",
)
.unwrap(),
FieldElement::from_hex_be("0x0").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)\nstark.stark bridged to Ethereum L1\nhttps://starkscan.co/tx/0x732b09d901fb0075d283ac23cbaae4f8c486123a88a621eeaa05d0b5ddfb8d8",
"Should be correct"
);
}

#[tokio::test]
async fn test_get_formatted_text_from_starknet_id() {
let keys = vec![FieldElement::from_hex_be(
"0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9",
)
.unwrap()];
let data = vec![
FieldElement::from_hex_be(
"0x1f4055a52c859593e79988bfe998b536066805fe757522ece47945f46f6b6e7",
)
.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 stark.stark to 0x6e1...b3ce\nhttps://starkscan.co/tx/0x732b09d901fb0075d283ac23cbaae4f8c486123a88a621eeaa05d0b5ddfb8d8",
"Should be correct"
);
}

#[tokio::test]
async fn test_get_formatted_text_to_starknet_id() {
let keys = vec![FieldElement::from_hex_be(
"0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9",
)
.unwrap()];
let data = vec![
FieldElement::from_hex_be(
"0x6e14b249c412a336e7a5a3473da083b9159e6845be4d02ee50f6095a5b3ce",
)
.unwrap(),
FieldElement::from_hex_be(
"0x1f4055a52c859593e79988bfe998b536066805fe757522ece47945f46f6b6e7",
)
.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 stark.stark\nhttps://starkscan.co/tx/0x732b09d901fb0075d283ac23cbaae4f8c486123a88a621eeaa05d0b5ddfb8d8",
"Should be correct"
);
}
}
Loading

0 comments on commit 87a8279

Please sign in to comment.