Skip to content

Commit d9a0d9a

Browse files
committed
AutomaticTokenBridge gas dropoff percentage of max change and decimal scaling fix
Previously the AutomaticTokenBridge route calculated the gas dropoff amount as a percentage of the input amount. However this has been changed to be a percentage of the maxSwapAmount as it's an easier to use interface for users of the library (e.g. Connect). Also fixed an issue where the native gas wasn't scaled correctly when the source and destination chains have different decimals for the input token.
1 parent 5b66b46 commit d9a0d9a

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

connect/src/protocols/tokenBridge/tokenTransfer.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,18 @@ export namespace TokenTransfer {
658658
dstDecimals,
659659
);
660660

661-
const dstNativeGasAmountRequested = transfer.nativeGas ?? 0n;
661+
// nativeGas is in source chain decimals
662+
const srcNativeGasAmountRequested = transfer.nativeGas ?? 0n;
663+
// convert to destination chain decimals
664+
const dstNativeGasAmountRequested = amount.units(
665+
amount.scale(
666+
amount.truncate(
667+
amount.fromBaseUnits(srcNativeGasAmountRequested, srcDecimals),
668+
TokenTransfer.MAX_DECIMALS,
669+
),
670+
dstDecimals,
671+
),
672+
);
662673

663674
// The expected destination gas can be pulled from the destination token bridge
664675
let destinationNativeGas = 0n;

connect/src/routes/tokenBridge/automatic.ts

+22-7
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ export class AutomaticTokenBridgeRoute<N extends Network>
135135
if (options.nativeGas && (options.nativeGas > 1.0 || options.nativeGas < 0.0))
136136
throw new Error("Native gas must be between 0.0 and 1.0 (0% and 100%)");
137137

138-
// If destination is native, max out the nativeGas requested
138+
// native gas drop-off when the native token is the destination should be 0
139139
const { destination } = request;
140-
if (isNative(destination.id.address) && options.nativeGas === 0.0) options.nativeGas = 1.0;
140+
if (isNative(destination.id.address) && options.nativeGas === 0.0) options.nativeGas = 0;
141141

142142
const updatedParams = { ...params, options };
143143
const validatedParams: Vp = {
@@ -177,18 +177,33 @@ export class AutomaticTokenBridgeRoute<N extends Network>
177177

178178
const redeemableAmount = amount.units(amt) - fee;
179179

180-
// Determine nativeGas
181-
let nativeGasAmount = 0n;
180+
let srcNativeGasAmount = amount.fromBaseUnits(0n, request.source.decimals);
182181
if (params.options && params.options.nativeGas > 0) {
182+
const dtb = await request.toChain.getAutomaticTokenBridge();
183+
// the maxSwapAmount is in destination chain decimals
184+
const maxSwapAmount = await dtb.maxSwapAmount(request.destination.id.address);
183185
const scale = 10000;
184-
const scaledGas = BigInt(params.options.nativeGas * scale);
185-
nativeGasAmount = (redeemableAmount * scaledGas) / BigInt(scale);
186+
const scaledGasPercent = BigInt(params.options.nativeGas * scale);
187+
const dstNativeGasUnits = (maxSwapAmount * scaledGasPercent) / BigInt(scale);
188+
const dstNativeGasAmount = amount.fromBaseUnits(
189+
dstNativeGasUnits,
190+
request.destination.decimals,
191+
);
192+
// convert the native gas amount to source chain decimals
193+
srcNativeGasAmount = amount.scale(
194+
amount.truncate(dstNativeGasAmount, TokenTransfer.MAX_DECIMALS),
195+
request.source.decimals,
196+
);
197+
// can't request more gas than the redeemable amount
198+
if (amount.units(srcNativeGasAmount) > redeemableAmount) {
199+
srcNativeGasAmount = amount.fromBaseUnits(redeemableAmount, request.source.decimals);
200+
}
186201
}
187202

188203
return {
189204
fee: amount.fromBaseUnits(fee, request.source.decimals),
190205
amount: amt,
191-
nativeGasAmount: amount.fromBaseUnits(nativeGasAmount, request.source.decimals),
206+
nativeGasAmount: srcNativeGasAmount,
192207
};
193208
}
194209

0 commit comments

Comments
 (0)