Skip to content

fix: enable integration tests ci #1018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/event_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ jobs:
RPC_PROXY_PROVIDER_ALLNODES_API_KEY: ""
RPC_PROXY_PROVIDER_MELD_API_KEY: ""
RPC_PROXY_PROVIDER_MELD_API_URL: ""
- run: docker compose up -d --build proxy
- uses: ./.github/workflows/sub-validate.yml
with:
stage: local
stage-url: http://localhost:3001
- run: docker logs mock-bundler-anvil-1
if: failure()
- run: docker logs mock-bundler-alto-1
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ services:
context: .
dockerfile: Dockerfile
ports:
- "3000:3000"
- "3001:3000"
environment:
- RPC_PROXY_LOG_LEVEL=DEBUG
- RPC_PROXY_HOST=0.0.0.0
Expand Down
11 changes: 8 additions & 3 deletions src/handlers/chain_agnostic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use {
tenderly::{AssetChangeType, TokenStandard},
SimulationProvider,
},
utils::crypto::get_erc20_balance,
utils::{crypto::get_erc20_balance, token_amount::TokenAmount},
Metrics,
},
alloy::primitives::{Address, B256, U256},
Expand Down Expand Up @@ -262,7 +262,9 @@ pub async fn check_bridging_for_erc20_transfer(
.await?;
for (account, contract_address, current_balance) in erc20_balances {
// Check if the balance compared to the transfer value is enough, applied to the transfer token decimals
if convert_amount(current_balance, decimals, amount_token_decimals) >= value {
if TokenAmount::new(current_balance, decimals)
>= TokenAmount::new(value, amount_token_decimals)
{
// Use the priority asset if found
if token_symbol == token_symbol_priority {
return Ok(Some(BridgingAsset {
Expand All @@ -278,10 +280,13 @@ pub async fn check_bridging_for_erc20_transfer(
// or use the asset with the highest found balance
if let Some(BridgingAsset {
current_balance: existing_balance,
decimals: existing_decimals,
..
}) = &bridging_asset_found
{
if current_balance <= *existing_balance {
if TokenAmount::new(current_balance, decimals)
<= TokenAmount::new(*existing_balance, *existing_decimals)
{
continue;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod permissions;
pub mod rate_limit;
pub mod sessions;
pub mod simple_request_json;
pub mod token_amount;

pub fn generate_random_string(len: usize) -> String {
let rng = rand::thread_rng();
Expand Down
204 changes: 204 additions & 0 deletions src/utils/token_amount.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
use alloy::primitives::U256;
use std::cmp::Ordering;

/// Basically a BigInt utility, but restricted to U256 and u8 types
#[derive(Debug, Clone)]
pub struct TokenAmount {
amount: U256,
decimals: u8,
}

impl TokenAmount {
pub fn new(amount: U256, decimals: u8) -> Self {
Self { amount, decimals }
}
}

impl PartialOrd for TokenAmount {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(compare_amounts(
self.amount,
other.amount,
self.decimals,
other.decimals,
))
}
}

impl PartialEq for TokenAmount {
fn eq(&self, other: &Self) -> bool {
self.partial_cmp(other) == Some(Ordering::Equal)
}
}

/// Compare two amounts with different decimals
fn compare_amounts(a_amount: U256, b_amount: U256, a_decimals: u8, b_decimals: u8) -> Ordering {
// Same assertion as alloy's Unit type
assert!(a_decimals <= 77);
assert!(b_decimals <= 77);

let base_10 = U256::from(10);
match a_decimals.cmp(&b_decimals) {
Ordering::Equal => a_amount.cmp(&b_amount),
Ordering::Greater => {
// A has more decimals than B
let diff = a_decimals - b_decimals;
let exp = U256::from(diff as u64);
let factor = base_10.checked_pow(exp);
if let Some(factor) = factor {
let b_amount_adjusted = b_amount.checked_mul(factor);
if let Some(b_amount_adjusted) = b_amount_adjusted {
a_amount.cmp(&b_amount_adjusted)
} else {
Ordering::Less
}
} else {
// Branch should never be reached because 2^256 > 10^77
Ordering::Less
}
}
Ordering::Less => {
// A has less decimals than B
let diff = b_decimals - a_decimals;
let exp = U256::from(diff as u64);
let factor = base_10.checked_pow(exp);
if let Some(factor) = factor {
let a_amount_adjusted = a_amount.checked_mul(factor);
if let Some(a_amount_adjusted) = a_amount_adjusted {
a_amount_adjusted.cmp(&b_amount)
} else {
Ordering::Greater
}
} else {
// Branch should never be reached because 2^256 > 10^77
Ordering::Greater
}
}
}
}

#[cfg(test)]
#[allow(clippy::neg_cmp_op_on_partial_ord)]
mod tests {
use super::*;

#[test]
fn test_equal_amounts_0() {
let a_amount = TokenAmount {
amount: U256::from(0),
decimals: 0,
};
let b_amount = TokenAmount {
amount: U256::from(0),
decimals: 0,
};
assert_eq!(a_amount, b_amount);
assert_eq!(a_amount.partial_cmp(&b_amount), Some(Ordering::Equal));
assert!(a_amount <= b_amount);
assert!(a_amount >= b_amount);
assert!(b_amount <= a_amount);
assert!(b_amount >= a_amount);
assert!(!(a_amount < b_amount));
assert!(!(a_amount > b_amount));
}

#[test]
fn test_equal_amounts_different_decimals() {
let a_amount = TokenAmount {
amount: U256::from(0),
decimals: 10,
};
let b_amount = TokenAmount {
amount: U256::from(0),
decimals: 0,
};
assert_eq!(a_amount, b_amount);
assert_eq!(a_amount.partial_cmp(&b_amount), Some(Ordering::Equal));
assert!(a_amount <= b_amount);
assert!(a_amount >= b_amount);
assert!(b_amount <= a_amount);
assert!(b_amount >= a_amount);
assert!(!(a_amount < b_amount));
assert!(!(a_amount > b_amount));
}

#[test]
fn test_different_amounts() {
let a_amount = TokenAmount {
amount: U256::from(1),
decimals: 0,
};
let b_amount = TokenAmount {
amount: U256::from(0),
decimals: 0,
};
assert!(a_amount > b_amount);
assert!(a_amount >= b_amount);
assert!(!(a_amount < b_amount));
assert!(!(a_amount <= b_amount));
assert!(!(a_amount == b_amount));
assert!(b_amount < a_amount);
assert!(b_amount <= a_amount);
assert!(!(b_amount > a_amount));
assert!(!(b_amount >= a_amount));
assert!(!(b_amount == a_amount));
}

#[test]
fn test_different_decimals() {
let a_amount = TokenAmount {
amount: U256::from(1),
decimals: 0,
};
let b_amount = TokenAmount {
amount: U256::from(1),
decimals: 1,
};
assert!(a_amount > b_amount);
assert!(a_amount >= b_amount);
assert!(!(a_amount < b_amount));
assert!(!(a_amount <= b_amount));
assert!(!(a_amount == b_amount));
assert!(b_amount < a_amount);
assert!(b_amount <= a_amount);
assert!(!(b_amount > a_amount));
assert!(!(b_amount >= a_amount));
assert!(!(b_amount == a_amount));
}

#[test]
fn test_overflow_amount() {
let a_amount_amount = U256::from(1000000);
let a_amount_decimals = 0;
let b_amount_amount = U256::from(1);
let b_amount_decimals = 77;
assert!(U256::from(10)
.checked_pow(U256::from(b_amount_decimals - a_amount_decimals))
.is_some());
assert!(a_amount_amount
.checked_mul(U256::from(
U256::from(10)
.checked_pow(U256::from(b_amount_decimals - a_amount_decimals))
.unwrap()
))
.is_none());
let a_amount = TokenAmount {
amount: a_amount_amount,
decimals: a_amount_decimals,
};
let b_amount = TokenAmount {
amount: b_amount_amount,
decimals: b_amount_decimals,
};
assert!(a_amount > b_amount);
assert!(a_amount >= b_amount);
assert!(!(a_amount < b_amount));
assert!(!(a_amount <= b_amount));
assert!(!(a_amount == b_amount));
assert!(b_amount < a_amount);
assert!(b_amount <= a_amount);
assert!(!(b_amount > a_amount));
assert!(!(b_amount >= a_amount));
assert!(!(b_amount == a_amount));
}
}
Loading