Skip to content

Commit b884aa3

Browse files
shekhirinklkvr
authored andcommitted
feat: account for source/destination decimals in fund source
1 parent 032e461 commit b884aa3

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/rpc/relay.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,8 +1172,9 @@ impl Relay {
11721172
.and_then(|asset| asset.metadata.as_ref())
11731173
.and_then(|metadata| metadata.decimals);
11741174

1175-
// collect (chain, balance) for all other chains that have >0 balance
1176-
let mut sources: Vec<(ChainId, Address, U256)> = assets
1175+
// collect (chain, balance, adjusted balance) for all other chains that have >0 balance
1176+
// adjusted balance is the balance converted to the destination chain decimals
1177+
let mut sources: Vec<_> = assets
11771178
.0
11781179
.iter()
11791180
.filter_map(|(&chain, assets)| {
@@ -1198,9 +1199,9 @@ impl Relay {
11981199
if let Some(existing_decimals) = existing_decimals {
11991200
balance =
12001201
adjust_balance_for_decimals(balance, mapped.decimals, existing_decimals);
1201-
}
1202+
};
12021203

1203-
if balance.is_zero() { None } else { Some((chain, mapped.address, balance)) }
1204+
if balance.is_zero() { None } else { Some((chain, mapped, balance)) }
12041205
})
12051206
.collect();
12061207

@@ -1215,9 +1216,9 @@ impl Relay {
12151216
let funding_context = FundingIntentContext {
12161217
eoa,
12171218
chain_id: chain,
1218-
asset: asset.into(),
1219+
asset: asset.address.into(),
12191220
amount: U256::from(1),
1220-
fee_token: asset,
1221+
fee_token: asset.address,
12211222
// note(onbjerg): it doesn't matter what the output intent digest is for
12221223
// simulation, as long as it's not zero. otherwise, the gas
12231224
// costs will differ a lot.
@@ -1256,11 +1257,24 @@ impl Relay {
12561257
break;
12571258
}
12581259

1259-
let take = remaining.min(balance.saturating_sub(escrow_cost));
1260+
let escrow_cost_destination = if let Some(existing_decimals) = existing_decimals {
1261+
adjust_balance_for_decimals(escrow_cost, asset.decimals, existing_decimals)
1262+
} else {
1263+
escrow_cost
1264+
};
1265+
let take = remaining.min(balance.saturating_sub(escrow_cost_destination));
1266+
1267+
// Convert the amount back to the source chain asset decimals
1268+
let amount_source = if let Some(existing_decimals) = existing_decimals {
1269+
adjust_balance_for_decimals(take, existing_decimals, asset.decimals)
1270+
} else {
1271+
take
1272+
};
12601273
plan.push(FundSource {
12611274
chain_id: chain,
1262-
amount: take,
1263-
address: asset,
1275+
amount_source,
1276+
amount_destination: take,
1277+
address: asset.address,
12641278
cost: escrow_cost,
12651279
});
12661280
remaining = remaining.saturating_sub(take);
@@ -1419,7 +1433,7 @@ impl Relay {
14191433
)
14201434
.await?
14211435
{
1422-
(new_chains.iter().map(|source| source.amount).sum(), new_chains)
1436+
(new_chains.iter().map(|source| source.amount_destination).sum(), new_chains)
14231437
} else {
14241438
// We don't have enough funds across all chains, so we revert back to single chain
14251439
// to produce a quote with a `feeTokenDeficit`.
@@ -1573,7 +1587,7 @@ impl Relay {
15731587
eoa,
15741588
chain_id: source.chain_id,
15751589
asset: source.address.into(),
1576-
amount: source.amount,
1590+
amount: source.amount_source,
15771591
fee_token: source.address,
15781592
output_intent_digest,
15791593
output_chain_id,

src/types/intent.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,9 @@ pub struct FundSource {
650650
/// The chain ID the funds are on.
651651
pub chain_id: ChainId,
652652
/// The amount of funds on that chain.
653-
pub amount: U256,
653+
pub amount_source: U256,
654+
/// The amount of funds on that chain, denominated in the output chain's asset decimals.
655+
pub amount_destination: U256,
654656
/// The address of the funds.
655657
///
656658
/// # Note

0 commit comments

Comments
 (0)