Skip to content

Commit a8607b0

Browse files
authored
fix(CA): proper bridging fee calculation (#921)
1 parent 59ef775 commit a8607b0

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

integration/chain_orchestrator.test.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ describe('Chain abstraction orchestrator', () => {
3333

3434
// Amount to send to Optimism
3535
const amount_to_send = 3_000_000
36-
// Amount bridging slippage
37-
const amount_slippage = 2; // +2% topup
3836

3937
let usdc_contracts = {};
4038
usdc_contracts[chain_id_optimism] = "0x0b2c639c533813f4aa9d7837caf62653d097ff85";
@@ -133,7 +131,6 @@ describe('Chain abstraction orchestrator', () => {
133131

134132
// How much needs to be topped up
135133
const amount_to_topup = Math.round(amount_to_send - usdc_funds[chain_id_optimism]);
136-
const amount_to_topup_with_fees = Math.round(((amount_to_topup * amount_slippage) / 100) + amount_to_topup);
137134

138135
const data_encoded = erc20Interface.encodeFunctionData('transfer', [
139136
receiver_address,
@@ -167,7 +164,7 @@ describe('Chain abstraction orchestrator', () => {
167164
expect(approvalTransaction.nonce).not.toBe("0x00")
168165
expect(() => BigInt(approvalTransaction.gasLimit)).not.toThrow();
169166
const decodedData = erc20Interface.decodeFunctionData('approve', approvalTransaction.input);
170-
if (decodedData.amount < BigInt(amount_to_topup_with_fees)) {
167+
if (decodedData.amount <= BigInt(amount_to_topup)) {
171168
throw new Error(`Expected amount is lower then the minimal required`);
172169
}
173170

@@ -188,10 +185,10 @@ describe('Chain abstraction orchestrator', () => {
188185
expect(fundingFrom.chainId).toBe(chain_id_base)
189186
expect(fundingFrom.symbol).toBe(usdc_token_symbol)
190187
expect(fundingFrom.tokenContract).toBe(usdc_contracts[chain_id_base].toLowerCase())
191-
if (BigInt(fundingFrom.amount) <= BigInt(amount_to_topup_with_fees)) {
188+
if (BigInt(fundingFrom.amount) <= BigInt(amount_to_topup)) {
192189
throw new Error(`Expected amount is lower then the minimal required`);
193190
}
194-
if (BigInt(fundingFrom.bridgingFee) != BigInt(fundingFrom.amount - amount_to_topup)){
191+
if (BigInt(fundingFrom.bridgingFee) < BigInt(fundingFrom.amount - amount_to_topup)){
195192
throw new Error(`Expected bridging fee is incorrect. `);
196193
}
197194
// Check the initialTransaction metadata
@@ -212,7 +209,6 @@ describe('Chain abstraction orchestrator', () => {
212209

213210
// How much needs to be topped up
214211
const amount_to_topup = Math.round(amount_to_send - usdt_funds[chain_id_optimism]);
215-
const amount_to_topup_with_fees = Math.round(((amount_to_topup * amount_slippage) / 100) + amount_to_topup);
216212

217213
const data_encoded = erc20Interface.encodeFunctionData('transfer', [
218214
receiver_address,
@@ -246,7 +242,7 @@ describe('Chain abstraction orchestrator', () => {
246242
expect(approvalTransaction.nonce).not.toBe("0x00")
247243
expect(() => BigInt(approvalTransaction.gasLimit)).not.toThrow();
248244
const decodedData = erc20Interface.decodeFunctionData('approve', approvalTransaction.input);
249-
if (decodedData.amount < BigInt(amount_to_topup_with_fees)) {
245+
if (decodedData.amount <= BigInt(amount_to_topup)) {
250246
throw new Error(`Expected amount is lower then the minimal required`);
251247
}
252248

@@ -267,10 +263,10 @@ describe('Chain abstraction orchestrator', () => {
267263
expect(fundingFrom.chainId).toBe(chain_id_arbitrum)
268264
expect(fundingFrom.symbol).toBe(usdt_token_symbol)
269265
expect(fundingFrom.tokenContract).toBe(usdt_contracts[chain_id_arbitrum].toLowerCase())
270-
if (BigInt(fundingFrom.amount) <= BigInt(amount_to_topup_with_fees)) {
266+
if (BigInt(fundingFrom.amount) <= BigInt(amount_to_topup)) {
271267
throw new Error(`Expected amount is lower then the minimal required`);
272268
}
273-
if (BigInt(fundingFrom.bridgingFee) != BigInt(fundingFrom.amount - amount_to_topup)){
269+
if (BigInt(fundingFrom.bridgingFee) < BigInt(fundingFrom.amount - amount_to_topup)){
274270
throw new Error(`Expected bridging fee is incorrect. `);
275271
}
276272
// Check the initialTransaction metadata

src/handlers/chain_agnostic/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ pub mod assets;
2222
pub mod route;
2323
pub mod status;
2424

25-
/// How much to multiply the amount by when bridging to cover bridging differences
26-
pub const BRIDGING_AMOUNT_SLIPPAGE: i8 = 50; // 50%
25+
/// How much to multiply the bridging fee amount to cover bridging fee volatility
26+
pub const BRIDGING_FEE_SLIPPAGE: i8 = 50; // 50%
2727

2828
/// Bridging timeout in seconds
2929
pub const BRIDGING_TIMEOUT: u64 = 1800; // 30 minutes

src/handlers/chain_agnostic/route.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use {
22
super::{
33
super::HANDLER_TASK_METRICS, check_bridging_for_erc20_transfer,
44
find_supported_bridging_asset, get_assets_changes_from_simulation, BridgingStatus,
5-
StorageBridgingItem, BRIDGING_AMOUNT_SLIPPAGE, STATUS_POLLING_INTERVAL,
5+
StorageBridgingItem, BRIDGING_FEE_SLIPPAGE, STATUS_POLLING_INTERVAL,
66
},
77
crate::{
88
analytics::{
@@ -314,9 +314,10 @@ async fn handler_internal(
314314
U256::from_str(&bridging_amount).map_err(|_| RpcError::InvalidValue(bridging_amount))?;
315315
let bridging_fee = erc20_topup_value - bridging_amount;
316316

317-
// Calculate the required bridging topup amount with the bridging fee and slippage
317+
// Calculate the required bridging topup amount with the bridging fee
318+
// and bridging fee * slippage to cover volatility
318319
let required_topup_amount = erc20_topup_value + bridging_fee;
319-
let required_topup_amount = ((required_topup_amount * U256::from(BRIDGING_AMOUNT_SLIPPAGE))
320+
let required_topup_amount = ((bridging_fee * U256::from(BRIDGING_FEE_SLIPPAGE))
320321
/ U256::from(100))
321322
+ required_topup_amount;
322323
if current_bridging_asset_balance < required_topup_amount {
@@ -362,7 +363,7 @@ async fn handler_internal(
362363
);
363364
return Err(RpcError::BridgingFinalAmountLess);
364365
}
365-
let final_bridging_fee = bridging_amount - erc20_topup_value;
366+
let final_bridging_fee = bridging_amount - required_topup_amount;
366367

367368
// Build bridging transaction
368369
let bridge_tx = state

0 commit comments

Comments
 (0)