Skip to content

Commit b62754f

Browse files
authored
Bug Fixes: misc (#302)
1 parent 6af5908 commit b62754f

File tree

37 files changed

+289
-258
lines changed

37 files changed

+289
-258
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ node_modules/
66
tsconfig-build.tsbuildinfo
77
tsconfig.tsbuildinfo
88
__tests__/staging
9+
wormhole-foundation-*.tgz

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.4.0-beta.5
1+
0.4.0-beta.8

connect/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@wormhole-foundation/connect-sdk",
3-
"version": "0.4.0-beta.5",
3+
"version": "0.4.0-beta.8",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com/wormhole-foundation/connect-sdk.git"
@@ -45,7 +45,7 @@
4545
},
4646
"dependencies": {
4747
"axios": "^1.4.0",
48-
"@wormhole-foundation/sdk-base": "^0.4.0-beta.5",
49-
"@wormhole-foundation/sdk-definitions": "^0.4.0-beta.5"
48+
"@wormhole-foundation/sdk-base": "^0.4.0-beta.8",
49+
"@wormhole-foundation/sdk-definitions": "^0.4.0-beta.8"
5050
}
5151
}

connect/src/protocols/tokenTransfer.ts

+23-19
Original file line numberDiff line numberDiff line change
@@ -461,53 +461,57 @@ export class TokenTransfer<N extends Network = Network>
461461
const srcAmount = amountFromBaseUnits(transfer.amount, srcDecimals);
462462

463463
const srcAmountTruncated = truncateAmount(srcAmount, TOKEN_BRIDGE_MAX_DECIMALS);
464-
const dstAmountTruncated = scaleAmount(srcAmountTruncated, dstDecimals);
464+
const dstAmountReceivable = scaleAmount(srcAmountTruncated, dstDecimals);
465465

466466
if (!transfer.automatic) {
467467
return {
468468
sourceToken: { token: srcToken, amount: baseUnits(srcAmountTruncated) },
469-
destinationToken: { token: dstToken, amount: baseUnits(dstAmountTruncated) },
469+
destinationToken: { token: dstToken, amount: baseUnits(dstAmountReceivable) },
470470
};
471471
}
472472

473473
// Otherwise automatic
474474

475-
// If a native gas dropoff is requested, remove that from the amount they'll get
476-
const dstNativeGasAmount = scaleAmount(
477-
truncateAmount(
478-
amountFromBaseUnits(transfer.nativeGas ?? 0n, srcDecimals),
479-
TOKEN_BRIDGE_MAX_DECIMALS,
480-
),
481-
dstDecimals,
482-
);
483-
const dstNativeGasBaseUnits = baseUnits(dstNativeGasAmount);
484-
485-
let dstAmountBaseUnits = baseUnits(dstAmountTruncated) - dstNativeGasBaseUnits;
486-
487-
// The fee is also removed from the amount transferred
475+
// The fee is removed from the amount transferred
488476
// quoted on the source chain
489477
const stb = await srcChain.getAutomaticTokenBridge();
490478
const fee = await stb.getRelayerFee(transfer.from.address, transfer.to, srcToken.address);
491-
492479
const feeAmountDest = scaleAmount(
493480
truncateAmount(amountFromBaseUnits(fee, srcDecimals), TOKEN_BRIDGE_MAX_DECIMALS),
494481
dstDecimals,
495482
);
496-
dstAmountBaseUnits -= baseUnits(feeAmountDest);
483+
484+
let dstNativeGasAmountRequested = transfer.nativeGas ?? 0n;
497485

498486
// The expected destination gas can be pulled from the destination token bridge
499487
let destinationNativeGas = 0n;
500488
if (transfer.nativeGas) {
501489
const dtb = await dstChain.getAutomaticTokenBridge();
502-
destinationNativeGas = await dtb.nativeTokenAmount(dstToken.address, dstNativeGasBaseUnits);
490+
491+
// There is a limit applied to the amount of the source
492+
// token that may be swapped for native gas on the destination
493+
const maxNativeAmountIn = await dtb.maxSwapAmount(dstToken.address);
494+
dstNativeGasAmountRequested =
495+
dstNativeGasAmountRequested > maxNativeAmountIn
496+
? maxNativeAmountIn
497+
: dstNativeGasAmountRequested;
498+
499+
// Get the actual amount we should receive
500+
destinationNativeGas = await dtb.nativeTokenAmount(
501+
dstToken.address,
502+
dstNativeGasAmountRequested,
503+
);
503504
}
504505

506+
const destAmountLessFee =
507+
baseUnits(dstAmountReceivable) - dstNativeGasAmountRequested - baseUnits(feeAmountDest);
508+
505509
return {
506510
sourceToken: {
507511
token: srcToken,
508512
amount: baseUnits(srcAmountTruncated),
509513
},
510-
destinationToken: { token: dstToken, amount: dstAmountBaseUnits },
514+
destinationToken: { token: dstToken, amount: destAmountLessFee },
511515
relayFee: { token: srcToken, amount: fee },
512516
destinationNativeGas,
513517
};

connect/src/routes/mock/automatic.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
WormholeMessageId,
1111
encoding,
1212
nativeTokenId,
13+
parseAmount,
1314
} from "../..";
1415
import { AutomaticRoute, StaticRouteMethods } from "../route";
1516
import {
@@ -78,15 +79,15 @@ export class AutomaticMockRoute<N extends Network>
7879
const fakeQuote: Q = {
7980
sourceToken: {
8081
token: this.request.source.id,
81-
amount: params.amount,
82+
amount: parseAmount(params.amount, this.request.source.decimals),
8283
},
8384
destinationToken: {
8485
token: this.request.destination!.id,
85-
amount: params.amount,
86+
amount: parseAmount(params.amount, this.request.destination.decimals),
8687
},
8788
relayFee: {
8889
token: this.request.source.id,
89-
amount: "0.01",
90+
amount: parseAmount("0.01", this.request.source.decimals),
9091
},
9192
};
9293
return fakeQuote;

connect/src/routes/mock/manual.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
WormholeMessageId,
1212
encoding,
1313
nativeTokenId,
14+
parseAmount,
1415
} from "../..";
1516
import { ManualRoute, StaticRouteMethods } from "../route";
1617
import {
@@ -73,15 +74,15 @@ export class ManualMockRoute<N extends Network>
7374
const fakeQuote: Q = {
7475
sourceToken: {
7576
token: this.request.source.id,
76-
amount: params.amount,
77+
amount: parseAmount(params.amount, this.request.destination.decimals),
7778
},
7879
destinationToken: {
7980
token: this.request.destination!.id,
80-
amount: params.amount,
81+
amount: parseAmount(params.amount, this.request.destination.decimals),
8182
},
8283
relayFee: {
8384
token: this.request.source.id,
84-
amount: "0.01",
85+
amount: parseAmount("0.01", this.request.source.decimals),
8586
},
8687
};
8788
return fakeQuote;

connect/src/routes/portico/automatic.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export class AutomaticPorticoRoute<N extends Network>
234234

235235
return {
236236
quote,
237-
...this.request.displayQuote({
237+
...(await this.request.displayQuote({
238238
sourceToken: {
239239
token: params.normalizedParams.sourceToken,
240240
amount: baseUnits(params.normalizedParams.amount),
@@ -247,7 +247,7 @@ export class AutomaticPorticoRoute<N extends Network>
247247
token: params.normalizedParams.destinationToken,
248248
amount: fee,
249249
},
250-
}),
250+
})),
251251
};
252252
}
253253

connect/src/routes/request.ts

+15-22
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
import { Network, amountFromBaseUnits, displayAmount } from "@wormhole-foundation/sdk-base";
2-
import {
3-
ChainAddress,
4-
ChainContext,
5-
TokenId,
6-
isSameToken,
7-
} from "@wormhole-foundation/sdk-definitions";
8-
import { Amount, parseAmount } from "@wormhole-foundation/sdk-base";
1+
import { Amount, Network, amountFromBaseUnits, parseAmount } from "@wormhole-foundation/sdk-base";
2+
import { ChainAddress, ChainContext, TokenId } from "@wormhole-foundation/sdk-definitions";
93
import { TransferQuote } from "../types";
104
import { Wormhole } from "../wormhole";
115
import { TokenDetails, getTokenDetails } from "./token";
@@ -44,36 +38,35 @@ export class RouteTransferRequest<N extends Network> {
4438
return amountFromBaseUnits(amt, this.source.decimals);
4539
}
4640

47-
displayQuote(quote: TransferQuote): Quote {
41+
async displayQuote(quote: TransferQuote): Promise<Quote> {
42+
// If we have a destination native gas
43+
// since the dest token is `native` on the dest chain
44+
const dstDecimals = quote.destinationNativeGas
45+
? await this.toChain.getDecimals(quote.destinationToken.token.address)
46+
: this.destination.decimals;
47+
4848
let dq: Quote = {
4949
sourceToken: {
5050
token: quote.sourceToken.token,
51-
amount: displayAmount(amountFromBaseUnits(quote.sourceToken.amount, this.source.decimals)),
51+
amount: amountFromBaseUnits(quote.sourceToken.amount, this.source.decimals),
5252
},
5353
destinationToken: {
5454
token: quote.destinationToken.token,
55-
amount: displayAmount(
56-
amountFromBaseUnits(quote.destinationToken.amount, this.destination.decimals),
57-
),
55+
amount: amountFromBaseUnits(quote.destinationToken.amount, dstDecimals),
5856
},
5957
};
6058

6159
if (quote.relayFee) {
62-
let amount = isSameToken(quote.relayFee.token, quote.sourceToken.token)
63-
? displayAmount(amountFromBaseUnits(quote.sourceToken.amount, this.source.decimals))
64-
: displayAmount(
65-
amountFromBaseUnits(quote.destinationToken.amount, this.destination.decimals),
66-
);
67-
6860
dq.relayFee = {
6961
token: quote.relayFee.token,
70-
amount,
62+
amount: amountFromBaseUnits(quote.relayFee.amount, this.source.decimals),
7163
};
7264
}
7365

7466
if (quote.destinationNativeGas) {
75-
dq.destinationNativeGas = displayAmount(
76-
amountFromBaseUnits(quote.destinationNativeGas, this.source.decimals),
67+
dq.destinationNativeGas = amountFromBaseUnits(
68+
quote.destinationNativeGas,
69+
this.destination.decimals,
7770
);
7871
}
7972

connect/src/routes/tokenBridge/automatic.ts

+23-31
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {
2+
Amount,
23
Chain,
34
Network,
4-
contracts,
5-
Amount,
6-
baseUnits,
75
amountFromBaseUnits,
6+
baseUnits,
7+
contracts,
88
displayAmount,
99
} from "@wormhole-foundation/sdk-base";
1010
import {
@@ -86,7 +86,7 @@ export class AutomaticTokenBridgeRoute<N extends Network>
8686
];
8787
}
8888

89-
// get the liist of destination tokens that may be recieved on the destination chain
89+
// get the list of destination tokens that may be recieved on the destination chain
9090
static async supportedDestinationTokens<N extends Network>(
9191
sourceToken: TokenId,
9292
fromChain: ChainContext<N>,
@@ -120,20 +120,17 @@ export class AutomaticTokenBridgeRoute<N extends Network>
120120
try {
121121
const options = params.options ?? this.getDefaultOptions();
122122

123-
const { destination } = this.request;
124-
let nativeGasPerc = options.nativeGas ?? 0.0;
125-
126-
if (nativeGasPerc > 1.0 || nativeGasPerc < 0.0)
123+
if (options.nativeGas && (options.nativeGas > 1.0 || options.nativeGas < 0.0))
127124
throw new Error("Native gas must be between 0.0 and 1.0 (0% and 100%)");
128125

129126
// If destination is native, max out the nativeGas requested
130-
if (destination && isNative(destination.id.address) && nativeGasPerc === 0.0)
131-
nativeGasPerc = 1.0;
127+
const { destination } = this.request;
128+
if (isNative(destination.id.address) && options.nativeGas === 0.0) options.nativeGas = 1.0;
132129

130+
const updatedParams = { ...params, options };
133131
const validatedParams: Vp = {
134-
amount: params.amount,
135-
options: { ...params.options, nativeGas: nativeGasPerc },
136-
normalizedParams: await this.normalizeTransferParams(params),
132+
...updatedParams,
133+
normalizedParams: await this.normalizeTransferParams(updatedParams),
137134
};
138135

139136
return { valid: true, params: validatedParams };
@@ -142,7 +139,9 @@ export class AutomaticTokenBridgeRoute<N extends Network>
142139
}
143140
}
144141

145-
async normalizeTransferParams(params: Tp): Promise<AutomaticTokenBridgeRoute.NormalizedParams> {
142+
private async normalizeTransferParams(
143+
params: Tp,
144+
): Promise<AutomaticTokenBridgeRoute.NormalizedParams> {
146145
const amount = this.request.parseAmount(params.amount);
147146

148147
const inputToken = isNative(this.request.source.id.address)
@@ -159,29 +158,22 @@ export class AutomaticTokenBridgeRoute<N extends Network>
159158
// Min amount is fee + 5%
160159
const minAmount = (fee * 105n) / 100n;
161160
if (baseUnits(amount) < minAmount) {
162-
throw new Error(`Minimum amount is ${displayAmount(amount)}`);
161+
throw new Error(
162+
`Minimum amount is ${displayAmount({
163+
amount: minAmount.toString(),
164+
decimals: amount.decimals,
165+
})}`,
166+
);
163167
}
164168

165-
const transferableAmount = baseUnits(amount) - fee;
166-
167-
const { destination } = this.request;
168-
const options = params.options ?? this.getDefaultOptions();
169-
170-
let nativeGasPerc = options.nativeGas ?? 0.0;
171-
// If destination is native, max out the nativeGas requested
172-
if (destination && isNative(destination.id.address) && nativeGasPerc === 0.0)
173-
nativeGasPerc = 1.0;
174-
if (nativeGasPerc > 1.0 || nativeGasPerc < 0.0) {
175-
throw new Error("Native gas must be between 0.0 and 1.0 (0% and 100%)");
176-
}
169+
const redeemableAmount = baseUnits(amount) - fee;
177170

178171
// Determine nativeGas
179172
let nativeGasAmount = 0n;
180-
if (nativeGasPerc > 0) {
181-
// TODO: currently supporting 2 decimals of the percentage requested
173+
if (params.options && params.options.nativeGas > 0) {
182174
const scale = 10000;
183-
const scaledGas = BigInt(options.nativeGas * scale);
184-
nativeGasAmount = (transferableAmount * scaledGas) / BigInt(scale);
175+
const scaledGas = BigInt(params.options.nativeGas * scale);
176+
nativeGasAmount = (redeemableAmount * scaledGas) / BigInt(scale);
185177
}
186178

187179
return {

connect/src/routes/types.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TokenId } from "@wormhole-foundation/sdk-definitions";
22
import { AttestationReceipt, TransferReceipt } from "../types";
3+
import { Amount } from "@wormhole-foundation/sdk-base";
34

45
// Extend Options to provide custom options
56
// to use for the transfer
@@ -19,23 +20,22 @@ export type Receipt<AT extends AttestationReceipt = AttestationReceipt> = Transf
1920
export interface Quote {
2021
sourceToken: {
2122
token: TokenId;
22-
amount: string;
23+
amount: Amount;
2324
};
24-
2525
destinationToken: {
2626
token: TokenId;
27-
amount: string;
27+
amount: Amount;
2828
};
2929
// If the transfer being quoted is automatic
3030
// a relayer fee may apply
3131
relayFee?: {
3232
token: TokenId;
33-
amount: string;
33+
amount: Amount;
3434
};
3535
// If the transfer being quoted asked for native gas dropoff
3636
// this will contain the amount of native gas that is to be minted
3737
// on the destination chain given the current swap rates
38-
destinationNativeGas?: string;
38+
destinationNativeGas?: Amount;
3939
}
4040

4141
// Transfer params after being validated.

core/base/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@wormhole-foundation/sdk-base",
3-
"version": "0.4.0-beta.5",
3+
"version": "0.4.0-beta.8",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com/wormhole-foundation/connect-sdk.git"

0 commit comments

Comments
 (0)