Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk: quote destination amount truncation fix #560

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
nik-suri marked this conversation as resolved.
Show resolved Hide resolved
);
return truncatedAmount;
}
}
Loading