@@ -15,7 +15,11 @@ import {
15
15
toUniversal ,
16
16
universalAddress ,
17
17
} from "@wormhole-foundation/sdk-definitions" ;
18
- import type { AnyEvmAddress , EvmChains , EvmPlatformType } from "@wormhole-foundation/sdk-evm" ;
18
+ import type {
19
+ AnyEvmAddress ,
20
+ EvmChains ,
21
+ EvmPlatformType ,
22
+ } from "@wormhole-foundation/sdk-evm" ;
19
23
import {
20
24
EvmAddress ,
21
25
EvmPlatform ,
@@ -26,6 +30,7 @@ import {
26
30
import "@wormhole-foundation/sdk-evm-core" ;
27
31
28
32
import {
33
+ EvmNttTransceiver ,
29
34
Ntt ,
30
35
NttTransceiver ,
31
36
WormholeNttTransceiver ,
@@ -39,7 +44,10 @@ import {
39
44
} from "./bindings.js" ;
40
45
41
46
export class EvmNttWormholeTranceiver < N extends Network , C extends EvmChains >
42
- implements NttTransceiver < N , C , WormholeNttTransceiver . VAA > {
47
+ implements
48
+ WormholeNttTransceiver < N , C > ,
49
+ EvmNttTransceiver < N , C , WormholeNttTransceiver . VAA >
50
+ {
43
51
transceiver : NttTransceiverBindings . NttTransceiver ;
44
52
constructor (
45
53
readonly manager : EvmNtt < N , C > ,
@@ -52,15 +60,26 @@ export class EvmNttWormholeTranceiver<N extends Network, C extends EvmChains>
52
60
) ;
53
61
}
54
62
63
+ async getTransceiverType ( ) : Promise < string > {
64
+ // NOTE: We hardcode the type here as transceiver type is only available for versions >1.1.0
65
+ // For those versions, we can return `this.transceiver.getTransceiverType()` directly
66
+ return "wormhole" ;
67
+ }
68
+
55
69
getAddress ( ) : ChainAddress < C > {
56
- return { chain : this . manager . chain , address : toUniversal ( this . manager . chain , this . address ) } ;
70
+ return {
71
+ chain : this . manager . chain ,
72
+ address : toUniversal ( this . manager . chain , this . address ) ,
73
+ } ;
57
74
}
58
75
59
76
encodeFlags ( flags : { skipRelay : boolean } ) : Uint8Array {
60
77
return new Uint8Array ( [ flags . skipRelay ? 1 : 0 ] ) ;
61
78
}
62
79
63
- async * setPeer < P extends Chain > ( peer : ChainAddress < P > ) : AsyncGenerator < EvmUnsignedTransaction < N , C > > {
80
+ async * setPeer < P extends Chain > (
81
+ peer : ChainAddress < P >
82
+ ) : AsyncGenerator < EvmUnsignedTransaction < N , C > > {
64
83
const tx = await this . transceiver . setWormholePeer . populateTransaction (
65
84
toChainId ( peer . chain ) ,
66
85
universalAddress ( peer )
@@ -74,8 +93,14 @@ export class EvmNttWormholeTranceiver<N extends Network, C extends EvmChains>
74
93
}
75
94
76
95
async * setPauser ( pauser : AccountAddress < C > ) {
77
- const canonicalPauser = canonicalAddress ( { chain : this . manager . chain , address : pauser } ) ;
78
- const tx = await this . transceiver . transferPauserCapability . populateTransaction ( canonicalPauser ) ;
96
+ const canonicalPauser = canonicalAddress ( {
97
+ chain : this . manager . chain ,
98
+ address : pauser ,
99
+ } ) ;
100
+ const tx =
101
+ await this . transceiver . transferPauserCapability . populateTransaction (
102
+ canonicalPauser
103
+ ) ;
79
104
yield this . manager . createUnsignedTx ( tx , "WormholeTransceiver.setPauser" ) ;
80
105
}
81
106
@@ -102,7 +127,10 @@ export class EvmNttWormholeTranceiver<N extends Network, C extends EvmChains>
102
127
toChainId ( chain ) ,
103
128
isEvm
104
129
) ;
105
- yield this . manager . createUnsignedTx ( tx , "WormholeTransceiver.setIsEvmChain" ) ;
130
+ yield this . manager . createUnsignedTx (
131
+ tx ,
132
+ "WormholeTransceiver.setIsEvmChain"
133
+ ) ;
106
134
}
107
135
108
136
async * receive ( attestation : WormholeNttTransceiver . VAA ) {
@@ -122,10 +150,11 @@ export class EvmNttWormholeTranceiver<N extends Network, C extends EvmChains>
122
150
}
123
151
124
152
async * setIsWormholeRelayingEnabled ( destChain : Chain , enabled : boolean ) {
125
- const tx = await this . transceiver . setIsWormholeRelayingEnabled . populateTransaction (
126
- toChainId ( destChain ) ,
127
- enabled
128
- ) ;
153
+ const tx =
154
+ await this . transceiver . setIsWormholeRelayingEnabled . populateTransaction (
155
+ toChainId ( destChain ) ,
156
+ enabled
157
+ ) ;
129
158
yield this . manager . createUnsignedTx (
130
159
tx ,
131
160
"WormholeTransceiver.setWormholeRelayingEnabled"
@@ -139,10 +168,11 @@ export class EvmNttWormholeTranceiver<N extends Network, C extends EvmChains>
139
168
}
140
169
141
170
async * setIsSpecialRelayingEnabled ( destChain : Chain , enabled : boolean ) {
142
- const tx = await this . transceiver . setIsSpecialRelayingEnabled . populateTransaction (
143
- toChainId ( destChain ) ,
144
- enabled
145
- ) ;
171
+ const tx =
172
+ await this . transceiver . setIsSpecialRelayingEnabled . populateTransaction (
173
+ toChainId ( destChain ) ,
174
+ enabled
175
+ ) ;
146
176
yield this . manager . createUnsignedTx (
147
177
tx ,
148
178
"WormholeTransceiver.setSpecialRelayingEnabled"
@@ -151,7 +181,8 @@ export class EvmNttWormholeTranceiver<N extends Network, C extends EvmChains>
151
181
}
152
182
153
183
export class EvmNtt < N extends Network , C extends EvmChains >
154
- implements Ntt < N , C > {
184
+ implements Ntt < N , C >
185
+ {
155
186
tokenAddress : string ;
156
187
readonly chainId : bigint ;
157
188
manager : NttManagerBindings . NttManager ;
@@ -182,17 +213,32 @@ export class EvmNtt<N extends Network, C extends EvmChains>
182
213
this . provider
183
214
) ;
184
215
185
- if ( contracts . ntt . transceiver . wormhole != null ) {
186
- this . xcvrs = [
187
- // Enable more Transceivers here
188
- new EvmNttWormholeTranceiver (
189
- this ,
190
- contracts . ntt . transceiver . wormhole ,
191
- abiBindings !
192
- ) ,
216
+ this . xcvrs = [ ] ;
217
+ if (
218
+ "wormhole" in contracts . ntt . transceiver &&
219
+ contracts . ntt . transceiver [ "wormhole" ]
220
+ ) {
221
+ const transceiverTypes = [
222
+ "wormhole" , // wormhole xcvr should be ix 0
223
+ ...Object . keys ( contracts . ntt . transceiver ) . filter ( ( transceiverType ) => {
224
+ transceiverType !== "wormhole" ;
225
+ } ) ,
193
226
] ;
194
- } else {
195
- this . xcvrs = [ ] ;
227
+ transceiverTypes . map ( ( transceiverType ) => {
228
+ // we currently only support wormhole transceivers
229
+ if ( transceiverType !== "wormhole" ) {
230
+ throw new Error ( `Unsupported transceiver type: ${ transceiverType } ` ) ;
231
+ }
232
+
233
+ // Enable more Transceivers here
234
+ this . xcvrs . push (
235
+ new EvmNttWormholeTranceiver (
236
+ this ,
237
+ contracts . ntt ! . transceiver [ transceiverType ] ! ,
238
+ abiBindings !
239
+ )
240
+ ) ;
241
+ } ) ;
196
242
}
197
243
}
198
244
@@ -211,12 +257,12 @@ export class EvmNtt<N extends Network, C extends EvmChains>
211
257
}
212
258
213
259
async * pause ( ) {
214
- const tx = await this . manager . pause . populateTransaction ( )
260
+ const tx = await this . manager . pause . populateTransaction ( ) ;
215
261
yield this . createUnsignedTx ( tx , "Ntt.pause" ) ;
216
262
}
217
263
218
264
async * unpause ( ) {
219
- const tx = await this . manager . unpause . populateTransaction ( )
265
+ const tx = await this . manager . unpause . populateTransaction ( ) ;
220
266
yield this . createUnsignedTx ( tx , "Ntt.unpause" ) ;
221
267
}
222
268
@@ -230,13 +276,17 @@ export class EvmNtt<N extends Network, C extends EvmChains>
230
276
231
277
async * setOwner ( owner : AnyEvmAddress ) {
232
278
const canonicalOwner = new EvmAddress ( owner ) . toString ( ) ;
233
- const tx = await this . manager . transferOwnership . populateTransaction ( canonicalOwner ) ;
279
+ const tx = await this . manager . transferOwnership . populateTransaction (
280
+ canonicalOwner
281
+ ) ;
234
282
yield this . createUnsignedTx ( tx , "Ntt.setOwner" ) ;
235
283
}
236
284
237
285
async * setPauser ( pauser : AnyEvmAddress ) {
238
286
const canonicalPauser = new EvmAddress ( pauser ) . toString ( ) ;
239
- const tx = await this . manager . transferPauserCapability . populateTransaction ( canonicalPauser ) ;
287
+ const tx = await this . manager . transferPauserCapability . populateTransaction (
288
+ canonicalPauser
289
+ ) ;
240
290
yield this . createUnsignedTx ( tx , "Ntt.setPauser" ) ;
241
291
}
242
292
@@ -398,9 +448,14 @@ export class EvmNtt<N extends Network, C extends EvmChains>
398
448
}
399
449
400
450
async * setWormholeTransceiverPeer ( peer : ChainAddress < C > ) {
401
- // TODO: we only have one right now, so just set the peer on that one
402
- // in the future, these should(?) be keyed by attestation type
403
- yield * this . xcvrs [ 0 ] ! . setPeer ( peer ) ;
451
+ yield * this . setTransceiverPeer ( 0 , peer ) ;
452
+ }
453
+
454
+ async * setTransceiverPeer ( ix : number , peer : ChainAddress < C > ) {
455
+ if ( ix >= this . xcvrs . length ) {
456
+ throw new Error ( "Transceiver not found" ) ;
457
+ }
458
+ yield * this . xcvrs [ ix ] ! . setPeer ( peer ) ;
404
459
}
405
460
406
461
async * transfer (
@@ -475,7 +530,9 @@ export class EvmNtt<N extends Network, C extends EvmChains>
475
530
}
476
531
477
532
async getOutboundLimit ( ) : Promise < bigint > {
478
- const encoded : EncodedTrimmedAmount = ( await this . manager . getOutboundLimitParams ( ) ) . limit ;
533
+ const encoded : EncodedTrimmedAmount = (
534
+ await this . manager . getOutboundLimitParams ( )
535
+ ) . limit ;
479
536
const trimmedAmount : TrimmedAmount = decodeTrimmedAmount ( encoded ) ;
480
537
const tokenDecimals = await this . getTokenDecimals ( ) ;
481
538
@@ -492,7 +549,9 @@ export class EvmNtt<N extends Network, C extends EvmChains>
492
549
}
493
550
494
551
async getInboundLimit ( fromChain : Chain ) : Promise < bigint > {
495
- const encoded : EncodedTrimmedAmount = ( await this . manager . getInboundLimitParams ( toChainId ( fromChain ) ) ) . limit ;
552
+ const encoded : EncodedTrimmedAmount = (
553
+ await this . manager . getInboundLimitParams ( toChainId ( fromChain ) )
554
+ ) . limit ;
496
555
const trimmedAmount : TrimmedAmount = decodeTrimmedAmount ( encoded ) ;
497
556
const tokenDecimals = await this . getTokenDecimals ( ) ;
498
557
@@ -547,7 +606,7 @@ export class EvmNtt<N extends Network, C extends EvmChains>
547
606
manager : this . managerAddress ,
548
607
token : this . tokenAddress ,
549
608
transceiver : {
550
- wormhole : this . xcvrs [ 0 ] ? .address ,
609
+ ... ( this . xcvrs . length > 0 && { wormhole : this . xcvrs [ 0 ] ! . address } ) ,
551
610
} ,
552
611
// TODO: what about the quoter?
553
612
} ;
@@ -556,7 +615,7 @@ export class EvmNtt<N extends Network, C extends EvmChains>
556
615
manager : this . managerAddress ,
557
616
token : await this . manager . token ( ) ,
558
617
transceiver : {
559
- wormhole : ( await this . manager . getTransceivers ( ) ) [ 0 ] ! // TODO: make this more generic
618
+ wormhole : ( await this . manager . getTransceivers ( ) ) [ 0 ] ! , // TODO: make this more generic
560
619
} ,
561
620
} ;
562
621
@@ -569,7 +628,7 @@ export class EvmNtt<N extends Network, C extends EvmChains>
569
628
delete a [ k ] ;
570
629
}
571
630
}
572
- }
631
+ } ;
573
632
574
633
deleteMatching ( remote , local ) ;
575
634
@@ -612,14 +671,18 @@ function untrim(trimmed: TrimmedAmount, toDecimals: number): bigint {
612
671
return scale ( amount , fromDecimals , toDecimals ) ;
613
672
}
614
673
615
- function scale ( amount : bigint , fromDecimals : number , toDecimals : number ) : bigint {
674
+ function scale (
675
+ amount : bigint ,
676
+ fromDecimals : number ,
677
+ toDecimals : number
678
+ ) : bigint {
616
679
if ( fromDecimals == toDecimals ) {
617
680
return amount ;
618
681
}
619
682
620
683
if ( fromDecimals > toDecimals ) {
621
- return amount / ( 10n ** BigInt ( fromDecimals - toDecimals ) ) ;
684
+ return amount / 10n ** BigInt ( fromDecimals - toDecimals ) ;
622
685
} else {
623
- return amount * ( 10n ** BigInt ( toDecimals - fromDecimals ) ) ;
686
+ return amount * 10n ** BigInt ( toDecimals - fromDecimals ) ;
624
687
}
625
688
}
0 commit comments