Skip to content

Commit

Permalink
added argument for get token balance with contract address
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed May 22, 2024
1 parent fa516bb commit 9067b18
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 12 deletions.
11 changes: 11 additions & 0 deletions crates/erc20_payment_lib/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
) -> Result<Vec<u8>, web3::ethabi::Error> {
contract_encode(
&WRAPPER_CONTRACT_TEMPLATE,
"callWithDetails",
(call_target_address, call_data),
)
}

pub fn encode_distribute(
recipients: &[Address],
amounts: &[U256],
Expand Down
37 changes: 34 additions & 3 deletions crates/erc20_payment_lib/src/eth.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -459,6 +459,7 @@ pub async fn get_deposit_details(
pub async fn get_balance(
web3: Arc<Web3RpcPool>,
token_address: Option<Address>,
call_with_details: Option<Address>,
address: Address,
check_gas: bool,
block_number: Option<u64>,
Expand Down Expand Up @@ -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
Expand Down
34 changes: 29 additions & 5 deletions crates/erc20_payment_lib/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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),
)
Expand Down Expand Up @@ -1579,11 +1595,19 @@ pub async fn make_deposit(
pub async fn get_token_balance(
web3: Arc<Web3RpcPool>,
token_address: Address,
call_with_details: Option<Address>,
address: Address,
block_number: Option<u64>,
) -> Result<U256, PaymentError> {
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
Expand Down
1 change: 1 addition & 0 deletions crates/erc20_payment_lib/src/server/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
2 changes: 2 additions & 0 deletions crates/erc20_payment_lib/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 10 additions & 4 deletions crates/erc20_payment_lib_extra/src/account_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down

0 comments on commit 9067b18

Please sign in to comment.