From 9067b1878f98d7f1460948d7e485c93583742931 Mon Sep 17 00:00:00 2001 From: scx1332 Date: Wed, 22 May 2024 14:59:15 +0200 Subject: [PATCH] added argument for get token balance with contract address --- crates/erc20_payment_lib/src/contracts.rs | 11 ++++++ crates/erc20_payment_lib/src/eth.rs | 37 +++++++++++++++++-- crates/erc20_payment_lib/src/runtime.rs | 34 ++++++++++++++--- crates/erc20_payment_lib/src/server/web.rs | 1 + crates/erc20_payment_lib/src/transaction.rs | 2 + .../src/account_balance.rs | 14 +++++-- src/main.rs | 1 + 7 files changed, 88 insertions(+), 12 deletions(-) diff --git a/crates/erc20_payment_lib/src/contracts.rs b/crates/erc20_payment_lib/src/contracts.rs index 5a28dde9..9b1653ac 100644 --- a/crates/erc20_payment_lib/src/contracts.rs +++ b/crates/erc20_payment_lib/src/contracts.rs @@ -89,6 +89,17 @@ pub fn encode_erc20_allowance( contract_encode(&ERC20_CONTRACT_TEMPLATE, "allowance", (owner, spender)) } +pub fn encode_call_with_details( + call_target_address: Address, + call_data: Vec, +) -> Result, web3::ethabi::Error> { + contract_encode( + &WRAPPER_CONTRACT_TEMPLATE, + "callWithDetails", + (call_target_address, call_data), + ) +} + pub fn encode_distribute( recipients: &[Address], amounts: &[U256], diff --git a/crates/erc20_payment_lib/src/eth.rs b/crates/erc20_payment_lib/src/eth.rs index fb55d7be..db4e7fe6 100644 --- a/crates/erc20_payment_lib/src/eth.rs +++ b/crates/erc20_payment_lib/src/eth.rs @@ -1,7 +1,7 @@ use crate::contracts::{ - encode_erc20_allowance, encode_erc20_balance_of, encode_get_attestation, - encode_get_deposit_details, encode_get_schema, encode_get_validate_deposit_signature, - encode_validate_contract, + encode_call_with_details, encode_erc20_allowance, encode_erc20_balance_of, + encode_get_attestation, encode_get_deposit_details, encode_get_schema, + encode_get_validate_deposit_signature, encode_validate_contract, }; use crate::error::*; use crate::runtime::ValidateDepositResult; @@ -459,6 +459,7 @@ pub async fn get_deposit_details( pub async fn get_balance( web3: Arc, token_address: Option
, + call_with_details: Option
, address: Address, check_gas: bool, block_number: Option, @@ -491,6 +492,36 @@ pub async fn get_balance( None }; + if let Some(token_address) = token_address { + if let Some(call_with_details) = call_with_details { + let abi_encoded_get_balance = encode_erc20_balance_of(address).map_err(err_from!())?; + + let call_data = encode_call_with_details(token_address, abi_encoded_get_balance) + .map_err(err_from!())?; + let res = web3 + .clone() + .eth_call( + CallRequest { + from: None, + to: Some(call_with_details), + gas: None, + gas_price: None, + value: None, + data: Some(Bytes::from(call_data)), + transaction_type: None, + access_list: None, + max_fee_per_gas: None, + max_priority_fee_per_gas: None, + }, + None, + ) + .await + .map_err(err_from!())?; + + log::debug!("Token balance response: {:?}", res); + } + }; + let token_balance = if let Some(token_address) = token_address { let call_data = encode_erc20_balance_of(address).map_err(err_from!())?; let res = web3 diff --git a/crates/erc20_payment_lib/src/runtime.rs b/crates/erc20_payment_lib/src/runtime.rs index 7647ee14..e822bdd3 100644 --- a/crates/erc20_payment_lib/src/runtime.rs +++ b/crates/erc20_payment_lib/src/runtime.rs @@ -733,7 +733,14 @@ impl PaymentRuntime { let web3 = self.setup.get_provider(chain_cfg.chain_id)?; - get_token_balance(web3, token_address, address, None).await + get_token_balance( + web3, + token_address, + chain_cfg.wrapper_contract.clone().map(|c| c.address), + address, + None, + ) + .await } /// Force sources and enpoints check depending on input. If wait is set to false, it is nonblocking. @@ -887,7 +894,15 @@ impl PaymentRuntime { let web3 = self.setup.get_provider(chain_cfg.chain_id)?; - let balance_result = crate::eth::get_balance(web3, None, address, true, None).await?; + let balance_result = crate::eth::get_balance( + web3, + None, + chain_cfg.wrapper_contract.clone().map(|c| c.address), + address, + true, + None, + ) + .await?; let gas_balance = balance_result .gas_balance @@ -1217,7 +1232,7 @@ pub async fn mint_golem_token( )); }; - let token_balance = get_token_balance(web3.clone(), glm_address, from, None) + let token_balance = get_token_balance(web3.clone(), glm_address, None, from, None) .await? .to_eth_saturate(); @@ -1518,6 +1533,7 @@ pub async fn make_deposit( let token_balance = get_token_balance( web3.clone(), glm_address, + None, from, Some(block_info.block_number), ) @@ -1579,11 +1595,19 @@ pub async fn make_deposit( pub async fn get_token_balance( web3: Arc, token_address: Address, + call_with_details: Option
, address: Address, block_number: Option, ) -> Result { - let balance_result = - crate::eth::get_balance(web3, Some(token_address), address, true, block_number).await?; + let balance_result = crate::eth::get_balance( + web3, + Some(token_address), + call_with_details, + address, + true, + block_number, + ) + .await?; let token_balance = balance_result .token_balance diff --git a/crates/erc20_payment_lib/src/server/web.rs b/crates/erc20_payment_lib/src/server/web.rs index be305229..28eb708e 100644 --- a/crates/erc20_payment_lib/src/server/web.rs +++ b/crates/erc20_payment_lib/src/server/web.rs @@ -934,6 +934,7 @@ async fn account_balance( let balance = get_balance( chain.provider.clone(), Some(chain.glm_address), + chain.wrapper_contract_address, account, true, Some(block_number.as_u64()), diff --git a/crates/erc20_payment_lib/src/transaction.rs b/crates/erc20_payment_lib/src/transaction.rs index 29335324..e8ed9834 100644 --- a/crates/erc20_payment_lib/src/transaction.rs +++ b/crates/erc20_payment_lib/src/transaction.rs @@ -459,6 +459,7 @@ pub async fn get_no_token_details( token_balance: get_token_balance( web3, glm_token, + None, Address::from_str(&web3_tx_dao.from_addr).map_err(err_from!())?, None, ) @@ -691,6 +692,7 @@ pub async fn send_transaction( token_balance: get_token_balance( web3, glm_token, + None, Address::from_str(&web3_tx_dao.from_addr) .map_err(err_from!())?, None, diff --git a/crates/erc20_payment_lib_extra/src/account_balance.rs b/crates/erc20_payment_lib_extra/src/account_balance.rs index 6dc1006b..73806105 100644 --- a/crates/erc20_payment_lib_extra/src/account_balance.rs +++ b/crates/erc20_payment_lib_extra/src/account_balance.rs @@ -110,10 +110,16 @@ pub async fn account_balance( let web3 = web3.clone(); async move { log::debug!("Getting balance for account: {:#x}", job); - let balance = - get_balance(web3, token, job, !account_balance_options.hide_gas, None) - .await - .unwrap(); + let balance = get_balance( + web3, + token, + None, + job, + !account_balance_options.hide_gas, + None, + ) + .await + .unwrap(); let gas_balance = balance.gas_balance.map(|b| b.to_string()); let token_balance = balance.token_balance.map(|b| b.to_string()); diff --git a/src/main.rs b/src/main.rs index 475a972b..c9d2c7f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -528,6 +528,7 @@ async fn main_internal() -> Result<(), PaymentError> { get_token_balance( payment_setup.get_provider(chain_cfg.chain_id)?, chain_cfg.token.address, + chain_cfg.wrapper_contract.clone().map(|c| c.address), public_addr, None, )