Skip to content

Commit b4a1574

Browse files
authored
feat(CA): prioritizing the same asset for bridging routes (#900)
1 parent e828ca8 commit b4a1574

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

integration/chain_orchestrator.test.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ describe('Chain abstraction orchestrator', () => {
127127

128128
})
129129

130-
it('bridging routes (routes available, USDC Optimism ⇄ USDT Arbitrum)', async () => {
131-
// Sending USDC to Optimism, but having the maximum balance of USDT on Arbitrum chain
130+
it('bridging routes (routes available, USDC Optimism ⇄ USDC Base)', async () => {
131+
// Sending USDC to Optimism, but having the balance of USDC on Base chain
132132
// which expected to be used for bridging
133133

134134
// How much needs to be topped up
@@ -163,17 +163,17 @@ describe('Chain abstraction orchestrator', () => {
163163

164164
// First transaction expected to be the approval transaction
165165
const approvalTransaction = data.transactions[0]
166-
expect(approvalTransaction.chainId).toBe(chain_id_arbitrum)
166+
expect(approvalTransaction.chainId).toBe(chain_id_base)
167167
expect(approvalTransaction.nonce).not.toBe("0x00")
168168
expect(() => BigInt(approvalTransaction.gasLimit)).not.toThrow();
169169
const decodedData = erc20Interface.decodeFunctionData('approve', approvalTransaction.input);
170170
if (decodedData.amount < BigInt(amount_to_topup_with_fees)) {
171171
throw new Error(`Expected amount is lower then the minimal required`);
172172
}
173173

174-
// Second transaction expected to be the bridging to the Arbitrum
174+
// Second transaction expected to be the bridging to the Base
175175
const bridgingTransaction = data.transactions[1]
176-
expect(bridgingTransaction.chainId).toBe(chain_id_arbitrum)
176+
expect(bridgingTransaction.chainId).toBe(chain_id_base)
177177
expect(bridgingTransaction.nonce).not.toBe("0x00")
178178
expect(() => BigInt(approvalTransaction.gasLimit)).not.toThrow();
179179

@@ -185,9 +185,9 @@ describe('Chain abstraction orchestrator', () => {
185185

186186
// Check the metadata fundingFrom
187187
const fundingFrom = data.metadata.fundingFrom[0]
188-
expect(fundingFrom.chainId).toBe(chain_id_arbitrum)
189-
expect(fundingFrom.symbol).toBe(usdt_token_symbol)
190-
expect(fundingFrom.tokenContract).toBe(usdt_contracts[chain_id_arbitrum].toLowerCase())
188+
expect(fundingFrom.chainId).toBe(chain_id_base)
189+
expect(fundingFrom.symbol).toBe(usdc_token_symbol)
190+
expect(fundingFrom.tokenContract).toBe(usdc_contracts[chain_id_base].toLowerCase())
191191
if (BigInt(fundingFrom.amount) <= BigInt(amount_to_topup_with_fees)) {
192192
throw new Error(`Expected amount is lower then the minimal required`);
193193
}

src/handlers/chain_agnostic/mod.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,18 @@ pub struct BridgingAsset {
120120
pub current_balance: U256,
121121
}
122122

123-
/// Check available assets for bridging and return
124-
/// the chain_id, token symbol and contract_address
123+
/// Checking available assets amount for bridging excluding the initial transaction
124+
/// asset, prioritizing the asset with the highest balance or the asset with the
125+
/// same symbol to avoid unnecessary swapping
125126
pub async fn check_bridging_for_erc20_transfer(
126127
rpc_project_id: String,
127128
value: U256,
128129
sender: Address,
130+
// Exclude the initial transaction asset from the check
129131
exclude_chain_id: String,
130132
exclude_contract_address: Address,
133+
// Using the same asset as a priority for bridging
134+
token_symbol_priority: String,
131135
) -> Result<Option<BridgingAsset>, RpcError> {
132136
// Check ERC20 tokens balance for each of supported assets
133137
let mut contracts_per_chain: HashMap<(String, String, u8), Vec<String>> = HashMap::new();
@@ -161,7 +165,18 @@ pub async fn check_bridging_for_erc20_transfer(
161165
.await?;
162166
for (contract_address, current_balance) in erc20_balances {
163167
if current_balance >= value {
164-
// Use the asset with the highest found balance
168+
// Use the priority asset if found
169+
if token_symbol == token_symbol_priority {
170+
return Ok(Some(BridgingAsset {
171+
chain_id: chain_id.clone(),
172+
token_symbol: token_symbol.clone(),
173+
contract_address,
174+
current_balance,
175+
decimals,
176+
}));
177+
}
178+
179+
// or use the asset with the highest found balance
165180
if let Some(BridgingAsset {
166181
current_balance: existing_balance,
167182
..

src/handlers/chain_agnostic/route.rs

+1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ async fn handler_internal(
246246
from_address,
247247
initial_transaction.chain_id.clone(),
248248
asset_transfer_contract,
249+
initial_tx_token_symbol.clone(),
249250
)
250251
.await?
251252
else {

0 commit comments

Comments
 (0)