Skip to content

Commit 80bbcd9

Browse files
authored
remove supportedSourceTokens function (#777)
1 parent cb10494 commit 80bbcd9

File tree

13 files changed

+28
-109
lines changed

13 files changed

+28
-109
lines changed

README.md

-9
Original file line numberDiff line numberDiff line change
@@ -639,15 +639,6 @@ Once created, the resolver can be used to provide a list of input and possible o
639639

640640
<!--EXAMPLE_RESOLVER_LIST_TOKENS-->
641641
```ts
642-
// what tokens are available on the source chain?
643-
const srcTokens = await resolver.supportedSourceTokens(sendChain);
644-
console.log(
645-
"Allowed source tokens: ",
646-
srcTokens.map((t) => canonicalAddress(t)),
647-
);
648-
649-
// Grab the first one for the example
650-
// const sendToken = srcTokens[0]!;
651642
const sendToken = Wormhole.tokenId(sendChain.chain, "native");
652643

653644
// given the send token, what can we possibly get on the destination chain?

connect/__tests__/mocks/routes/automatic.ts

-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ export class AutomaticMockRoute<N extends Network>
4747
static supportedChains(network: Network): Chain[] {
4848
return ["Solana", "Ethereum"];
4949
}
50-
static async supportedSourceTokens(fromChain: ChainContext<Network>): Promise<TokenId[]> {
51-
await delay(250);
52-
return [nativeTokenId(fromChain.chain)];
53-
}
5450
static async supportedDestinationTokens<N extends Network>(
5551
sourceToken: TokenId,
5652
fromChain: ChainContext<N>,

connect/__tests__/mocks/routes/manual.ts

-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ export class ManualMockRoute<N extends Network>
4545
static supportedChains(network: Network): Chain[] {
4646
return ["Solana", "Ethereum"];
4747
}
48-
static async supportedSourceTokens(fromChain: ChainContext<Network>): Promise<TokenId[]> {
49-
await delay(250);
50-
return [nativeTokenId(fromChain.chain)];
51-
}
5248
static async supportedDestinationTokens<N extends Network>(
5349
sourceToken: TokenId,
5450
fromChain: ChainContext<N>,

connect/src/routes/cctp/automatic.ts

-7
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,6 @@ export class AutomaticCCTPRoute<N extends Network>
7676
return [];
7777
}
7878

79-
// get the list of source tokens that are possible to send
80-
static async supportedSourceTokens(fromChain: ChainContext<Network>): Promise<TokenId[]> {
81-
const { network, chain } = fromChain;
82-
if (!circle.usdcContract.has(network, chain)) return [];
83-
return [Wormhole.chainAddress(chain, circle.usdcContract.get(network, chain)!)];
84-
}
85-
8679
// get the list of destination tokens that may be received on the destination chain
8780
static async supportedDestinationTokens<N extends Network>(
8881
sourceToken: TokenId,

connect/src/routes/cctp/manual.ts

-7
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,6 @@ export class CCTPRoute<N extends Network>
6969
return [];
7070
}
7171

72-
// get the list of source tokens that are possible to send
73-
static async supportedSourceTokens(fromChain: ChainContext<Network>): Promise<TokenId[]> {
74-
const { network, chain } = fromChain;
75-
if (!circle.usdcContract.has(network, chain)) return [];
76-
return [Wormhole.chainAddress(chain, circle.usdcContract.get(network, chain)!)];
77-
}
78-
7972
// get the list of destination tokens that may be received on the destination chain
8073
static async supportedDestinationTokens<N extends Network>(
8174
sourceToken: TokenId,

connect/src/routes/portico/automatic.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,6 @@ export class AutomaticPorticoRoute<N extends Network>
9191
return [];
9292
}
9393

94-
static async supportedSourceTokens(fromChain: ChainContext<Network>): Promise<TokenId[]> {
95-
const pb = await fromChain.getPorticoBridge();
96-
const { tokenMap } = fromChain.config;
97-
return pb
98-
.supportedTokens()
99-
.filter((t) => !tokenMap || filters.byAddress(tokenMap, canonicalAddress(t.token)))
100-
.map((t) => t.token);
101-
}
102-
10394
static async supportedDestinationTokens<N extends Network>(
10495
sourceToken: TokenId,
10596
fromChain: ChainContext<N>,
@@ -113,6 +104,10 @@ export class AutomaticPorticoRoute<N extends Network>
113104
const tokenAddress = canonicalAddress(srcTokenAddress);
114105

115106
const pb = await fromChain.getPorticoBridge();
107+
// Make sure the source token is supported
108+
if (!pb.supportedTokens().some((t) => canonicalAddress(t.token) === tokenAddress)) {
109+
return [];
110+
}
116111

117112
try {
118113
// The highway token that will be used to bridge
@@ -147,8 +142,12 @@ export class AutomaticPorticoRoute<N extends Network>
147142
async validate(request: RouteTransferRequest<N>, params: TP): Promise<VR> {
148143
try {
149144
if (
150-
!AutomaticPorticoRoute.supportedChains(request.fromChain.network).includes(request.fromChain.chain) ||
151-
!AutomaticPorticoRoute.supportedChains(request.toChain.network).includes(request.toChain.chain)
145+
!AutomaticPorticoRoute.supportedChains(request.fromChain.network).includes(
146+
request.fromChain.chain,
147+
) ||
148+
!AutomaticPorticoRoute.supportedChains(request.toChain.network).includes(
149+
request.toChain.chain,
150+
)
152151
) {
153152
throw new Error("Protocol not supported");
154153
}

connect/src/routes/resolver.ts

+6-28
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,6 @@ export class RouteResolver<N extends Network> {
2020
this.routeConstructors = routeConstructors;
2121
}
2222

23-
async supportedSourceTokens(chain: ChainContext<N>): Promise<TokenId[]> {
24-
if (this.inputTokenList) return this.inputTokenList;
25-
const itl = await Promise.all(
26-
this.routeConstructors.map(async (rc) => {
27-
try {
28-
return await rc.supportedSourceTokens(chain);
29-
} catch (e) {
30-
console.error(`Failed to get supported source tokens for ${rc.meta.name}: `, e);
31-
return [];
32-
}
33-
}),
34-
);
35-
this.inputTokenList = uniqueTokens(itl.flat());
36-
return this.inputTokenList!;
37-
}
38-
3923
async supportedDestinationTokens(
4024
inputToken: TokenId,
4125
fromChain: ChainContext<N>,
@@ -50,7 +34,10 @@ export class RouteResolver<N extends Network> {
5034
}
5135

5236
const supportedChains = rc.supportedChains(fromChain.network);
53-
if (!supportedChains.includes(fromChain.chain) || !supportedChains.includes(toChain.chain)) {
37+
if (
38+
!supportedChains.includes(fromChain.chain) ||
39+
!supportedChains.includes(toChain.chain)
40+
) {
5441
return [];
5542
}
5643

@@ -72,16 +59,7 @@ export class RouteResolver<N extends Network> {
7259
const protocolSupported =
7360
rc.supportedNetworks().includes(this.wh.network) &&
7461
rc.supportedChains(this.wh.network).includes(request.toChain.chain) &&
75-
rc.supportedChains(this.wh.network).includes(request.fromChain.chain)
76-
77-
const sourceTokenAddress = canonicalAddress(
78-
isNative(request.source.id.address) ? request.source.wrapped! : request.source.id,
79-
);
80-
81-
const sourceTokenSupported =
82-
(await rc.supportedSourceTokens(request.fromChain)).filter((tokenId: TokenId) => {
83-
return canonicalAddress(tokenId) === sourceTokenAddress;
84-
}).length > 0;
62+
rc.supportedChains(this.wh.network).includes(request.fromChain.chain);
8563

8664
const dstTokenAddress = canonicalAddress(
8765
isNative(request.destination.id.address)
@@ -99,7 +77,7 @@ export class RouteResolver<N extends Network> {
9977
return canonicalAddress(tokenId) === dstTokenAddress;
10078
}).length > 0;
10179

102-
return protocolSupported && sourceTokenSupported && destinationTokenSupported;
80+
return protocolSupported && destinationTokenSupported;
10381
} catch (e) {
10482
return false;
10583
}

connect/src/routes/route.ts

-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ export interface RouteConstructor<OP extends Options = Options> {
8989
supportedNetworks(): Network[];
9090
/** get the list of chains this route supports */
9191
supportedChains(network: Network): Chain[];
92-
/** get the list of source tokens that are possible to send */
93-
supportedSourceTokens(fromChain: ChainContext<Network>): Promise<TokenId[]>;
9492
/** get the list of destination tokens that may be received on the destination chain */
9593
supportedDestinationTokens<N extends Network>(
9694
token: TokenId,

connect/src/routes/tokenBridge/automatic.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
TokenId,
88
TokenTransferDetails,
99
} from "@wormhole-foundation/sdk-definitions";
10-
import { isNative, isTokenId, nativeTokenId } from "@wormhole-foundation/sdk-definitions";
10+
import { isNative, isTokenId } from "@wormhole-foundation/sdk-definitions";
1111
import { TokenTransfer } from "../../protocols/tokenBridge/tokenTransfer.js";
1212
import type { AttestationReceipt, SourceInitiatedTransferReceipt } from "../../types.js";
1313
import { TransferState } from "../../types.js";
@@ -73,25 +73,21 @@ export class AutomaticTokenBridgeRoute<N extends Network>
7373
return [];
7474
}
7575

76-
// get the list of source tokens that are possible to send
77-
static async supportedSourceTokens(fromChain: ChainContext<Network>): Promise<TokenId[]> {
78-
const atb = await fromChain.getAutomaticTokenBridge();
79-
const registered = await atb.getRegisteredTokens();
80-
return [
81-
nativeTokenId(fromChain.chain),
82-
...registered.map((v) => {
83-
return { chain: fromChain.chain, address: v };
84-
}),
85-
];
86-
}
87-
8876
// get the list of destination tokens that may be received on the destination chain
8977
static async supportedDestinationTokens<N extends Network>(
9078
sourceToken: TokenId,
9179
fromChain: ChainContext<N>,
9280
toChain: ChainContext<N>,
9381
): Promise<TokenId[]> {
9482
try {
83+
if (!isNative(sourceToken)) {
84+
const srcAtb = await fromChain.getAutomaticTokenBridge();
85+
const srcIsAccepted = await srcAtb.isRegisteredToken(sourceToken.address);
86+
if (!srcIsAccepted) {
87+
return [];
88+
}
89+
}
90+
9591
const expectedDestinationToken = await TokenTransfer.lookupDestinationToken(
9692
fromChain,
9793
toChain,
@@ -101,7 +97,7 @@ export class AutomaticTokenBridgeRoute<N extends Network>
10197
const atb = await toChain.getAutomaticTokenBridge();
10298
const acceptable = await atb.isRegisteredToken(expectedDestinationToken.address);
10399
if (!acceptable) {
104-
throw new Error("Destination token is not accepted by the AutomaticTokenBridge");
100+
return [];
105101
}
106102

107103
return [expectedDestinationToken];

connect/src/routes/tokenBridge/manual.ts

-8
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,6 @@ export class TokenBridgeRoute<N extends Network>
6767
return contracts.tokenBridgeChains(network);
6868
}
6969

70-
// get the list of source tokens that are possible to send
71-
static async supportedSourceTokens(fromChain: ChainContext<Network>): Promise<TokenId[]> {
72-
// Default list for the chain
73-
return Object.values(fromChain.config.tokenMap!).map((td) =>
74-
Wormhole.tokenId(td.chain, td.address),
75-
);
76-
}
77-
7870
// get the list of destination tokens that may be received on the destination chain
7971
static async supportedDestinationTokens<N extends Network>(
8072
sourceToken: TokenId,

examples/src/router.ts

-9
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ import { getSigner } from "./helpers/index.js";
2828
// EXAMPLE_RESOLVER_CREATE
2929

3030
// EXAMPLE_RESOLVER_LIST_TOKENS
31-
// what tokens are available on the source chain?
32-
const srcTokens = await resolver.supportedSourceTokens(sendChain);
33-
console.log(
34-
"Allowed source tokens: ",
35-
srcTokens.map((t) => canonicalAddress(t)),
36-
);
37-
38-
// Grab the first one for the example
39-
// const sendToken = srcTokens[0]!;
4031
const sendToken = Wormhole.tokenId(sendChain.chain, "native");
4132

4233
// given the send token, what can we possibly get on the destination chain?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.json

sdk/README.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -532,12 +532,7 @@ Once created, the resolver can be used to provide a list of input and possible o
532532

533533
<!--EXAMPLE_RESOLVER_LIST_TOKENS-->
534534
```ts
535-
// what tokens are available on the source chain?
536-
const srcTokens = await resolver.supportedSourceTokens(sendChain);
537-
console.log(
538-
"Allowed source tokens: ",
539-
srcTokens.map((t) => canonicalAddress(t)),
540-
);
535+
const sendToken = Wormhole.tokenId(sendChain, "native");
541536

542537
// given the send token, what can we possibly get on the destination chain?
543538
const destTokens = await resolver.supportedDestinationTokens(sendToken, sendChain, destChain);

0 commit comments

Comments
 (0)