@@ -10,6 +10,8 @@ import {
10
10
TransferReceipt as _TransferReceipt ,
11
11
amount ,
12
12
canonicalAddress ,
13
+ isNative ,
14
+ nativeTokenId ,
13
15
routes ,
14
16
} from "@wormhole-foundation/sdk-connect" ;
15
17
import { Ntt } from "@wormhole-foundation/sdk-definitions-ntt" ;
@@ -31,6 +33,7 @@ export namespace NttRoute {
31
33
manager : string ;
32
34
transceiver : TransceiverConfig [ ] ;
33
35
quoter ?: string ;
36
+ isWrappedGasToken ?: boolean ;
34
37
} ;
35
38
36
39
export type Config = {
@@ -107,15 +110,22 @@ export namespace NttRoute {
107
110
config : Config ,
108
111
fromChain : ChainContext < Network >
109
112
) : TokenId [ ] {
110
- const srcTokens = Object . entries ( config . tokens )
111
- . map ( ( [ , configs ] ) => {
113
+ const srcTokens = Object . entries ( config . tokens ) . reduce < TokenId [ ] > (
114
+ ( acc , [ , configs ] ) => {
112
115
const tokenConf = configs . find (
113
116
( config ) => config . chain === fromChain . chain
114
117
) ;
115
- if ( ! tokenConf ) return null ;
116
- return Wormhole . tokenId ( fromChain . chain , tokenConf ! . token ) ;
117
- } )
118
- . filter ( ( x ) => ! ! x ) as TokenId [ ] ;
118
+ if ( tokenConf ) {
119
+ acc . push ( Wormhole . tokenId ( fromChain . chain , tokenConf . token ) ) ;
120
+
121
+ if ( tokenConf . isWrappedGasToken ) {
122
+ acc . push ( nativeTokenId ( fromChain . chain ) ) ;
123
+ }
124
+ }
125
+ return acc ;
126
+ } ,
127
+ [ ]
128
+ ) ;
119
129
120
130
// TODO: dedupe? //return routes.uniqueTokens(srcTokens);
121
131
return srcTokens ;
@@ -132,8 +142,9 @@ export namespace NttRoute {
132
142
const match = configs . find (
133
143
( config ) =>
134
144
config . chain === fromChain . chain &&
135
- config . token . toLowerCase ( ) ===
136
- canonicalAddress ( sourceToken ) . toLowerCase ( )
145
+ ( config . token . toLowerCase ( ) ===
146
+ canonicalAddress ( sourceToken ) . toLowerCase ( ) ||
147
+ ( isNative ( sourceToken . address ) && config . isWrappedGasToken ) )
137
148
) ;
138
149
if ( ! match ) return ;
139
150
@@ -147,28 +158,56 @@ export namespace NttRoute {
147
158
148
159
export function resolveNttContracts (
149
160
config : Config ,
150
- token : TokenId
151
- ) : Ntt . Contracts {
161
+ srcToken : TokenId ,
162
+ dstToken : TokenId
163
+ ) : { srcContracts : Ntt . Contracts ; dstContracts : Ntt . Contracts } {
152
164
const cfg = Object . values ( config . tokens ) ;
153
- const address = canonicalAddress ( token ) ;
165
+ const srcAddress = canonicalAddress ( srcToken ) ;
166
+ const dstAddress = canonicalAddress ( dstToken ) ;
167
+
154
168
for ( const tokens of cfg ) {
155
- const found = tokens . find (
169
+ const srcFound = tokens . find (
156
170
( tc ) =>
157
- tc . token . toLowerCase ( ) === address . toLowerCase ( ) &&
158
- tc . chain === token . chain
171
+ tc . chain === srcToken . chain &&
172
+ ( tc . token . toLowerCase ( ) === srcAddress . toLowerCase ( ) ||
173
+ ( isNative ( srcToken . address ) && tc . isWrappedGasToken ) )
159
174
) ;
160
- if ( found )
161
- return {
162
- token : found . token ,
163
- manager : found . manager ,
164
- transceiver : {
165
- wormhole : found . transceiver . find ( ( v ) => v . type === "wormhole" ) !
166
- . address ,
167
- } ,
168
- quoter : found . quoter ,
169
- } ;
175
+
176
+ if ( srcFound ) {
177
+ const dstFound = tokens . find (
178
+ ( tc ) =>
179
+ tc . chain === dstToken . chain &&
180
+ ( tc . token . toLowerCase ( ) === dstAddress . toLowerCase ( ) ||
181
+ ( isNative ( dstToken . address ) && tc . isWrappedGasToken ) )
182
+ ) ;
183
+
184
+ if ( dstFound ) {
185
+ return {
186
+ srcContracts : {
187
+ token : srcFound . token ,
188
+ manager : srcFound . manager ,
189
+ transceiver : {
190
+ wormhole : srcFound . transceiver . find (
191
+ ( v ) => v . type === "wormhole"
192
+ ) ! . address ,
193
+ } ,
194
+ quoter : srcFound . quoter ,
195
+ } ,
196
+ dstContracts : {
197
+ token : dstFound . token ,
198
+ manager : dstFound . manager ,
199
+ transceiver : {
200
+ wormhole : dstFound . transceiver . find (
201
+ ( v ) => v . type === "wormhole"
202
+ ) ! . address ,
203
+ } ,
204
+ } ,
205
+ } ;
206
+ }
207
+ }
170
208
}
171
- throw new Error ( "Cannot find Ntt contracts in config for: " + address ) ;
209
+
210
+ throw new Error ( "Cannot find Ntt contracts in config for: " + srcAddress ) ;
172
211
}
173
212
174
213
export function resolveDestinationNttContracts < C extends Chain > (
0 commit comments