Skip to content

Commit

Permalink
test endpoints for getting balance
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed May 29, 2024
1 parent fd43593 commit 6217adb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 32 deletions.
87 changes: 56 additions & 31 deletions crates/erc20_payment_lib/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ pub async fn get_balance(
args.token_address.unwrap_or_default(),
);

if let (Some(token_address), Some(call_with_details)) =
let res = if let (Some(token_address), Some(call_with_details)) =
(args.token_address, args.call_with_details)
{
let abi_encoded_get_balance = encode_erc20_balance_of(args.address).map_err(err_from!())?;
Expand All @@ -499,7 +499,7 @@ pub async fn get_balance(
log::debug!("Checking balance (contract) for latest block");
None
};
let res = web3
match web3
.clone()
.eth_call(
CallRequest {
Expand All @@ -511,39 +511,64 @@ pub async fn get_balance(
block_id,
)
.await
.map_err(err_from!())?;

let (block_info, call_result) = decode_call_with_details(&res.0)?;
{
Ok(res) => {
let (block_info, call_result) = decode_call_with_details(&res.0)?;

/*let now = chrono::Utc::now();
let seconds_old = (now - block_info.block_datetime).num_seconds();
if seconds_old > 10 {
log::warn!("Balance is {seconds_old}s old");
}*/
//decode call_result
if let Some(chain_id) = args.chain_id {
if block_info.chain_id != chain_id {
return Err(err_custom_create!(
"Invalid chain id in response: {}, expected {}",
block_info.chain_id,
chain_id
));
}
}

/*let now = chrono::Utc::now();
let seconds_old = (now - block_info.block_datetime).num_seconds();
if seconds_old > 10 {
log::warn!("Balance is {seconds_old}s old");
}*/
//decode call_result
if let Some(chain_id) = args.chain_id {
if block_info.chain_id != chain_id {
return Err(err_custom_create!(
"Invalid chain id in response: {}, expected {}",
block_info.chain_id,
chain_id
));
let token_balance = U256::from_big_endian(&call_result);

log::debug!(
"Token balance response: {:?} - token balance: {}",
block_info,
token_balance
);
Some(GetBalanceResult {
gas_balance: Some(block_info.eth_balance),
token_balance: Some(token_balance),
block_number: block_info.block_number,
block_datetime: block_info.block_datetime,
})
}
Err(e) => {
if e.to_string().to_lowercase().contains("insufficient funds") {
log::warn!("Balance check via wrapper contract failed, falling back to standard method");
None
} else {
log::error!(
"Error getting balance for account: {:#x} - {}",
args.address,
e
);
return Err(err_custom_create!(
"Error getting balance for account: {:#x} - {}",
args.address,
e
));
}
}
}
} else {
None
};

let token_balance = U256::from_big_endian(&call_result);

log::debug!(
"Token balance response: {:?} - token balance: {}",
block_info,
token_balance
);
Ok(GetBalanceResult {
gas_balance: Some(block_info.eth_balance),
token_balance: Some(token_balance),
block_number: block_info.block_number,
block_datetime: block_info.block_datetime,
})
if let Some(res) = res {
Ok(res)
} else {
let block_id = if let Some(block_number) = args.block_number {
log::debug!("Checking balance for block number {}", block_number);
Expand Down
4 changes: 3 additions & 1 deletion scenarios/test_get_balance/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ def test_endpoint(network, test_endp, no_accounts, use_contract=False):

stdout, stderr = s.communicate()

print(stderr.decode("utf-8"))

# load json
try:
data = json.loads(stdout)
except json.JSONDecodeError:
print("Error: failed to parse JSON")
print(stdout)
print(stderr)
# print(stderr.decode("utf-8"))
raise
success_count = 0
for el in data:
Expand Down

0 comments on commit 6217adb

Please sign in to comment.