Skip to content

Commit

Permalink
fix: add token_address param to moralis erc20 API to query balance fo…
Browse files Browse the repository at this point in the history
…r specified wallet and token (#2774)

Co-authored-by: higherordertech <higherordertech>
  • Loading branch information
higherordertech authored May 30, 2024
1 parent cfbb47c commit c345e9b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
17 changes: 11 additions & 6 deletions tee-worker/litentry/core/data-providers/src/moralis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,10 @@ pub trait BalanceApiList {
) -> Result<Vec<GetSolanaTokenBalanceByWalletResponse>, Error>;

// https://docs.moralis.io/web3-data-api/evm/reference/wallet-api/get-token-balances-by-wallet
fn get_evm_tokens_balance_by_wallet(
fn get_evm_token_balance_by_wallet(
&mut self,
address: String,
token_address: String,
network: &Web3Network,
fast_fail: bool,
) -> Result<Vec<GetEvmTokenBalanceByWalletResponse>, Error>;
Expand Down Expand Up @@ -338,15 +339,19 @@ impl BalanceApiList for MoralisClient {
}
}

fn get_evm_tokens_balance_by_wallet(
fn get_evm_token_balance_by_wallet(
&mut self,
address: String,
token_address: String,
network: &Web3Network,
fast_fail: bool,
) -> Result<Vec<GetEvmTokenBalanceByWalletResponse>, Error> {
debug!("get_evm_tokens_balance_by_wallet, address: {}", address);
debug!("get_evm_token_balance_by_wallet, address: {}", address);

let query = Some(vec![("chain".to_string(), web3_network_to_chain(network))]);
let query = Some(vec![
("chain".to_string(), web3_network_to_chain(network)),
("token_addresses[0]".to_string(), token_address),
]);
let params = MoralisRequest { path: format!("{}/erc20", address), query };

match self.get::<Vec<GetEvmTokenBalanceByWalletResponse>>(
Expand All @@ -355,11 +360,11 @@ impl BalanceApiList for MoralisClient {
fast_fail,
) {
Ok(resp) => {
debug!("get_evm_tokens_balance_by_wallet, response: {:?}", resp);
debug!("get_evm_token_balance_by_wallet, response: {:?}", resp);
Ok(resp)
},
Err(e) => {
debug!("get_evm_tokens_balance_by_wallet, error: {:?}", e);
debug!("get_evm_token_balance_by_wallet, error: {:?}", e);
Err(e)
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,33 @@ pub fn get_balance(
let decimals = token_type.get_decimals(network);

let mut client = MoralisClient::new(data_provider_config);
let result =
client.get_evm_tokens_balance_by_wallet(address.1.clone(), &network, false);
let result = client.get_evm_token_balance_by_wallet(
address.1.clone(),
token_address.into(),
&network,
false,
);

match result {
Ok(items) =>
match items.iter().find(|&item| item.token_address == token_address) {
Some(item) => match item.balance.parse::<u128>() {
if !items.is_empty() {
match items[0].balance.parse::<u128>() {
Ok(balance) => {
total_balance +=
calculate_balance_with_decimals(balance, decimals);

Ok(LoopControls::Continue)
},
Err(err) => {
error!("Failed to parse {} to f64: {}", item.balance, err);
error!(
"Failed to parse {} to f64: {}",
items[0].balance, err
);
Err(Error::ParseError)
},
},
None => Ok(LoopControls::Continue),
}
} else {
Ok(LoopControls::Continue)
},
Err(err) => Err(err.into_error_detail()),
}
Expand Down

0 comments on commit c345e9b

Please sign in to comment.