Skip to content

Commit

Permalink
sdk: quote destination amount truncation fix (#560)
Browse files Browse the repository at this point in the history
* sdk: quote destination amount truncation fix

Fixes an issue where calling quote with an amount that has
more fractional digits than the destination chain supports
throws an error. We need to truncate and scale the destination
amount to destination chain decimals.

* refactor

* rename, added comments

* rename function
  • Loading branch information
kev1n-peters authored Dec 2, 2024
1 parent bb1dc13 commit 2ce277c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
20 changes: 13 additions & 7 deletions sdk/route/src/automatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,17 @@ export class NttAutomaticRoute<N extends Network>
request.toChain.config.nativeTokenDecimals
);

const amt = amount.parse(params.amount, request.source.decimals);
const parsedAmount = amount.parse(params.amount, request.source.decimals);
// The trimmedAmount may differ from the parsedAmount if the parsedAmount includes dust
const trimmedAmount = NttRoute.trimAmount(
parsedAmount,
request.destination.decimals
);

const validatedParams: Vp = {
amount: params.amount,
normalizedParams: {
amount: amt,
amount: trimmedAmount,
sourceContracts: NttRoute.resolveNttContracts(
this.staticConfig,
request.source.id
Expand Down Expand Up @@ -160,6 +165,11 @@ export class NttAutomaticRoute<N extends Network>
params.normalizedParams.options
);

const dstAmount = amount.scale(
params.normalizedParams.amount,
request.destination.decimals
);

const result: QR = {
success: true,
params,
Expand All @@ -169,7 +179,7 @@ export class NttAutomaticRoute<N extends Network>
},
destinationToken: {
token: request.destination.id,
amount: amount.parse(params.amount, request.destination.decimals),
amount: dstAmount,
},
relayFee: {
token: Wormhole.tokenId(fromChain.chain, "native"),
Expand All @@ -190,10 +200,6 @@ export class NttAutomaticRoute<N extends Network>
const duration = await dstNtt.getRateLimitDuration();
if (duration > 0n) {
const capacity = await dstNtt.getCurrentInboundCapacity(fromChain.chain);
const dstAmount = amount.parse(
params.amount,
request.destination.decimals
);
if (
NttRoute.isCapacityThresholdExceeded(amount.units(dstAmount), capacity)
) {
Expand Down
21 changes: 14 additions & 7 deletions sdk/route/src/manual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ export class NttManualRoute<N extends Network>
): Promise<Vr> {
const options = params.options ?? this.getDefaultOptions();

const amt = amount.parse(params.amount, request.source.decimals);
const parsedAmount = amount.parse(params.amount, request.source.decimals);
// The trimmedAmount may differ from the parsedAmount if the parsedAmount includes dust
const trimmedAmount = NttRoute.trimAmount(
parsedAmount,
request.destination.decimals
);

const gasDropoff = amount.units(
amount.parse(
options.gasDropoff ?? "0.0",
Expand All @@ -113,7 +119,7 @@ export class NttManualRoute<N extends Network>
const validatedParams: Vp = {
amount: params.amount,
normalizedParams: {
amount: amt,
amount: trimmedAmount,
sourceContracts: NttRoute.resolveNttContracts(
this.staticConfig,
request.source.id
Expand All @@ -137,6 +143,11 @@ export class NttManualRoute<N extends Network>
request: routes.RouteTransferRequest<N>,
params: Vp
): Promise<QR> {
const dstAmount = amount.scale(
params.normalizedParams.amount,
request.destination.decimals
);

const result: QR = {
success: true,
params,
Expand All @@ -146,7 +157,7 @@ export class NttManualRoute<N extends Network>
},
destinationToken: {
token: request.destination.id,
amount: amount.parse(params.amount, request.destination.decimals),
amount: dstAmount,
},
eta: finality.estimateFinalityTime(request.fromChain.chain),
};
Expand All @@ -157,10 +168,6 @@ export class NttManualRoute<N extends Network>
const duration = await dstNtt.getRateLimitDuration();
if (duration > 0n) {
const capacity = await dstNtt.getCurrentInboundCapacity(fromChain.chain);
const dstAmount = amount.parse(
params.amount,
request.destination.decimals
);
if (
NttRoute.isCapacityThresholdExceeded(amount.units(dstAmount), capacity)
) {
Expand Down
14 changes: 14 additions & 0 deletions sdk/route/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export namespace NttRoute {
// Currently only wormhole attestations supported
export type TransceiverType = "wormhole";

export const TRIMMED_DECIMALS = 8;

export type TransceiverConfig = {
type: TransceiverType;
address: string;
Expand Down Expand Up @@ -212,4 +214,16 @@ export namespace NttRoute {
const threshold = (capacity * 95n) / 100n;
return amount > threshold;
}

export function trimAmount(
amt: amount.Amount,
dstTokenDecimals: number
): amount.Amount {
// remove dust to avoid `TransferAmountHasDust` revert reason
const truncatedAmount = amount.truncate(
amt,
Math.min(amt.decimals, dstTokenDecimals, NttRoute.TRIMMED_DECIMALS)
);
return truncatedAmount;
}
}

0 comments on commit 2ce277c

Please sign in to comment.