Skip to content

Instantly share code, notes, and snippets.

@Enigo
Created April 27, 2023 03:26
Show Gist options
  • Save Enigo/e548700c2cb1bb2dc06307d6c8139ea1 to your computer and use it in GitHub Desktop.
Save Enigo/e548700c2cb1bb2dc06307d6c8139ea1 to your computer and use it in GitHub Desktop.
async fn process_wallet(wallet: String, api_key: &String) {
let mut page = 1;
let mut transaction_to_token_id = HashMap::new();
loop {
let transactions = fetch_transactions(wallet.clone(), api_key, page).await;
if transactions.is_empty() {
break;
}
for res in transactions {
if res.is_error == "1" || res.to != LAND_CONTRACT {
continue;
}
let input_to_decode = res.input.replace(res.method_id.as_str(), "");
match decode_input_and_get_token_id(input_to_decode.as_str()) {
Ok(token_id) => {
if res.value == "0" {
transaction_to_token_id.insert(res.hash.clone(), token_id);
} else {
...
}
}
Err(e) => { error!("Error decoding input {e}") }
};
}
page += 1;
}
if !transaction_to_token_id.is_empty() {
process_tokens(&wallet, transaction_to_token_id, api_key).await;
}
}
async fn process_tokens(wallet: &String, transaction_to_token_id: HashMap<String, i32>, api_key: &String) {
fn convert_into_value(value_str: &str, token_decimal_str: &str) -> f32 {
let value = Decimal::from_str(value_str).unwrap();
let token_decimal = Decimal::from_str(&format!("1{}", "0".repeat(token_decimal_str.parse::<u32>().unwrap().try_into().unwrap()))).unwrap();
let f32_value = value / token_decimal;
return f32_value.to_f32().unwrap();
}
let mut page = 1;
loop {
let tokens = fetch_tokens(wallet.clone(), api_key, page).await;
if tokens.is_empty() {
break;
}
for res in tokens {
if let Some(token_id) = transaction_to_token_id.get(&res.hash) {
let price = convert_into_value(res.value.as_str(), res.token_decimal.as_str());
// match token_ids and persist price
}
}
page += 1;
}
}
async fn fetch_tokens(
wallet: String,
api_key: &String,
page: i8,
) -> Vec<token::TheResult> {
let endpoint = format!("https://api.etherscan.io/api?module=account&action=tokentx&address={}&page={}&offset=10000&startblock={}&endblock=99999999&sort=asc&apikey={}",
wallet, page, LAND_CONTRACT_CREATION_BLOCK, api_key);
return match api_utils::fetch_single_api_response::<token::Token>(endpoint.as_str()).await {
Ok(token) => {
if token.status == "1" {
return token.result.unwrap();
}
return vec![];
}
_ => vec![]
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment