Skip to content

Commit

Permalink
feat(history): fulfilling Solana transactions history with the tokens…
Browse files Browse the repository at this point in the history
… metadata
  • Loading branch information
geekbrother committed Sep 6, 2024
1 parent c13f05b commit 70c69e5
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions src/providers/solscan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use {
fungible_price::FungiblePriceItem,
history::{
HistoryQueryParams, HistoryResponseBody, HistoryTransaction,
HistoryTransactionMetadata, HistoryTransactionTransfer,
HistoryTransactionTransferQuantity,
HistoryTransactionFungibleInfo, HistoryTransactionMetadata,
HistoryTransactionTransfer, HistoryTransactionTransferQuantity,
HistoryTransactionURLItem,
},
},
utils::crypto::SOLANA_NATIVE_TOKEN_ADDRESS,
Expand Down Expand Up @@ -314,9 +315,11 @@ impl HistoryProvider for SolScanProvider {
params: HistoryQueryParams,
http_client: reqwest::Client,
) -> RpcResult<HistoryResponseBody> {
let page_size = 100;
let mut url =
Url::parse(ACCOUNT_HISTORY_URL).map_err(|_| RpcError::BalanceParseURLError)?;
url.query_pairs_mut().append_pair("page_size", "100");
url.query_pairs_mut()
.append_pair("page_size", &page_size.to_string());
url.query_pairs_mut().append_pair("remove_spam", "true");
url.query_pairs_mut()
.append_pair("exclude_amount_zero", "true");
Expand All @@ -339,15 +342,15 @@ impl HistoryProvider for SolScanProvider {
}
let body = response.json::<HistoryResponse>().await?;

let transactions: Vec<HistoryTransaction> = body
.data
.into_iter()
.map(|f| HistoryTransaction {
id: f.block_id.to_string(),
let mut transactions: Vec<HistoryTransaction> = Vec::new();
for item in &body.data {
let token_info = self.get_token_info(&item.token_address).await?;
let transaction = HistoryTransaction {
id: item.block_id.to_string(),
metadata: HistoryTransactionMetadata {
operation_type: match f.activity_type {
operation_type: match item.activity_type {
HistoryActivityType::Transfer => {
if f.flow == HistoryDirectionType::In {
if item.flow == HistoryDirectionType::In {
"receive".to_string()
} else {
"send".to_string()
Expand All @@ -357,29 +360,36 @@ impl HistoryProvider for SolScanProvider {
HistoryActivityType::Mint => "mint".to_string(),
HistoryActivityType::CreateAccount => "execute".to_string(),
},
hash: f.trans_id,
mined_at: f.time,
hash: item.trans_id.clone(),
mined_at: item.time.clone(),
nonce: 0,
sent_from: f.from_address,
sent_to: f.to_address,
sent_from: item.from_address.clone(),
sent_to: item.to_address.clone(),
status: "confirmed".to_string(), // Balance changes are always confirmed
application: None,
chain: Some(SOLANA_MAINNET_CHAIN_ID.to_string()),
},
transfers: Some(vec![HistoryTransactionTransfer {
fungible_info: None, // Todo: Add fungible info from saved tokens info list
fungible_info: Some(HistoryTransactionFungibleInfo {
name: Some(token_info.name),
symbol: Some(token_info.symbol),
icon: Some(HistoryTransactionURLItem {
url: token_info.icon.unwrap_or_default(),
}),
}),
nft_info: None,
direction: f.flow.to_string(),
direction: item.flow.to_string(),
quantity: HistoryTransactionTransferQuantity {
numeric: f.amount.to_string(),
numeric: item.amount.to_string(),
},
value: None,
price: None,
value: Some(item.amount as f64 / 10f64.powf(9.0) * token_info.price),
price: Some(token_info.price),
}]),
})
.collect();
};
transactions.push(transaction);
}

let next = if !transactions.is_empty() {
let next = if !transactions.is_empty() && body.data.len() == page_size {
Some((page.parse::<u64>().unwrap_or(1) + 1).to_string())
} else {
None
Expand Down

0 comments on commit 70c69e5

Please sign in to comment.