Skip to content

Commit deedb86

Browse files
authored
Check that the token expected from bridging is acceptable (#613)
1 parent 7227ad4 commit deedb86

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

connect/src/routes/tokenBridge/automatic.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ import type {
66
TokenId,
77
TokenTransferDetails,
88
} from "@wormhole-foundation/sdk-definitions";
9-
import { isNative, isTokenId, nativeTokenId } from "@wormhole-foundation/sdk-definitions";
9+
import {
10+
ChainAddress,
11+
isNative,
12+
isTokenId,
13+
nativeTokenId,
14+
} from "@wormhole-foundation/sdk-definitions";
1015
import { TokenTransfer } from "../../protocols/tokenBridge/tokenTransfer.js";
1116
import type { AttestationReceipt, SourceInitiatedTransferReceipt } from "../../types.js";
1217
import { TransferState } from "../../types.js";
18+
import { Wormhole } from "../../wormhole.js";
1319
import type { StaticRouteMethods } from "../route.js";
1420
import { AutomaticRoute } from "../route.js";
1521
import type {
@@ -20,8 +26,6 @@ import type {
2026
ValidatedTransferParams,
2127
ValidationResult,
2228
} from "../types.js";
23-
import { ChainAddress } from "@wormhole-foundation/sdk-definitions";
24-
import { Wormhole } from "../../wormhole.js";
2529

2630
export namespace AutomaticTokenBridgeRoute {
2731
export type Options = {
@@ -90,7 +94,19 @@ export class AutomaticTokenBridgeRoute<N extends Network>
9094
toChain: ChainContext<N>,
9195
): Promise<TokenId[]> {
9296
try {
93-
return [await TokenTransfer.lookupDestinationToken(fromChain, toChain, sourceToken)];
97+
const expectedDestinationToken = await TokenTransfer.lookupDestinationToken(
98+
fromChain,
99+
toChain,
100+
sourceToken,
101+
);
102+
103+
const atb = await toChain.getAutomaticTokenBridge();
104+
const acceptable = await atb.isRegisteredToken(expectedDestinationToken.address);
105+
if (!acceptable) {
106+
throw new Error("Destination token is not accepted by the AutomaticTokenBridge");
107+
}
108+
109+
return [expectedDestinationToken];
94110
} catch (e) {
95111
console.error(`Failed to get destination token: ${e}`);
96112
return [];

core/definitions/src/protocols/tokenBridge/tokenBridge.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import type { Chain, Network } from "@wormhole-foundation/sdk-base";
22
import { lazyInstantiate } from "@wormhole-foundation/sdk-base";
3-
import type {
4-
AccountAddress,
5-
ChainAddress,
6-
NativeAddress,
7-
UniversalOrNative,
8-
} from "../../address.js";
3+
import type { AccountAddress, ChainAddress } from "../../address.js";
94
import type { TokenAddress, TokenId } from "../../types.js";
105
import type { UnsignedTransaction } from "../../unsignedTransaction.js";
116
import type { ProtocolPayload, ProtocolVAA } from "./../../vaa/index.js";
@@ -144,7 +139,7 @@ export interface TokenBridge<N extends Network = Network, C extends Chain = Chai
144139
* @returns The address of the native gas token that has been wrapped
145140
* for use where the gas token is not possible to use (e.g. bridging)
146141
*/
147-
getWrappedNative(): Promise<NativeAddress<C>>;
142+
getWrappedNative(): Promise<TokenAddress<C>>;
148143
/**
149144
* Check to see if a foreign token has a wrapped version
150145
*
@@ -158,7 +153,7 @@ export interface TokenBridge<N extends Network = Network, C extends Chain = Chai
158153
* @param foreignToken The token to check
159154
* @returns The address of the native version of this asset
160155
*/
161-
getWrappedAsset(foreignToken: TokenId<Chain>): Promise<NativeAddress<C>>;
156+
getWrappedAsset(foreignToken: TokenId<Chain>): Promise<TokenAddress<C>>;
162157
/**
163158
* Checks if a transfer VAA has been redeemed
164159
*
@@ -177,7 +172,7 @@ export interface TokenBridge<N extends Network = Network, C extends Chain = Chai
177172
*/
178173
createAttestation(
179174
token: TokenAddress<C>,
180-
payer?: UniversalOrNative<C>,
175+
payer?: AccountAddress<C>,
181176
): AsyncGenerator<UnsignedTransaction<N, C>>;
182177

183178
/**
@@ -189,7 +184,7 @@ export interface TokenBridge<N extends Network = Network, C extends Chain = Chai
189184
*/
190185
submitAttestation(
191186
vaa: TokenBridge.AttestVAA,
192-
payer?: UniversalOrNative<C>,
187+
payer?: AccountAddress<C>,
193188
): AsyncGenerator<UnsignedTransaction<N, C>>;
194189

195190
/**
@@ -251,7 +246,7 @@ export interface AutomaticTokenBridge<N extends Network = Network, C extends Cha
251246
/** Check if a given token is in the registered token list */
252247
isRegisteredToken(token: TokenAddress<C>): Promise<boolean>;
253248
/** Get the list of tokens that are registered and acceptable to send */
254-
getRegisteredTokens(): Promise<NativeAddress<C>[]>;
249+
getRegisteredTokens(): Promise<TokenAddress<C>[]>;
255250
/** Amount of native tokens a user would receive by swapping x amount of sending tokens */
256251
nativeTokenAmount(token: TokenAddress<C>, amount: bigint): Promise<bigint>;
257252
/** Maximum amount of sending tokens that can be swapped for native tokens */

0 commit comments

Comments
 (0)