@@ -15,12 +15,14 @@ import {
15
15
} from "@solana/web3.js" ;
16
16
import {
17
17
Chain ,
18
+ ChainAddress ,
18
19
ChainId ,
19
20
deserialize ,
20
21
deserializeLayout ,
21
22
encoding ,
22
23
keccak256 ,
23
24
rpc ,
25
+ toChain ,
24
26
toChainId ,
25
27
} from "@wormhole-foundation/sdk-connect" ;
26
28
@@ -38,14 +40,29 @@ import {
38
40
} from "./bindings.js" ;
39
41
import { chainToBytes , derivePda } from "./utils.js" ;
40
42
41
- export interface TransferArgs {
42
- amount : BN ;
43
- recipientChain : { id : ChainId } ;
44
- recipientAddress : number [ ] ;
45
- shouldQueue : boolean ;
46
- }
47
-
48
43
export namespace NTT {
44
+ export interface TransferArgs {
45
+ amount : BN ;
46
+ recipientChain : { id : ChainId } ;
47
+ recipientAddress : number [ ] ;
48
+ shouldQueue : boolean ;
49
+ }
50
+
51
+ export function transferArgs (
52
+ amount : bigint ,
53
+ recipient : ChainAddress ,
54
+ shouldQueue : boolean
55
+ ) : TransferArgs {
56
+ return {
57
+ amount : new BN ( amount . toString ( ) ) ,
58
+ recipientChain : { id : toChainId ( recipient . chain ) } ,
59
+ recipientAddress : Array . from (
60
+ recipient . address . toUniversalAddress ( ) . toUint8Array ( )
61
+ ) ,
62
+ shouldQueue : shouldQueue ,
63
+ } ;
64
+ }
65
+
49
66
export type Pdas = ReturnType < typeof pdas > ;
50
67
export const pdas = ( programId : PublicKeyInitData ) => {
51
68
const configAccount = ( ) : PublicKey => derivePda ( "config" , programId ) ;
@@ -174,28 +191,16 @@ export namespace NTT {
174
191
payer : PublicKey ;
175
192
from : PublicKey ;
176
193
fromAuthority : PublicKey ;
177
- amount : BN ;
178
- recipientChain : Chain ;
179
- recipientAddress : ArrayLike < number > ;
194
+ transferArgs : TransferArgs ;
180
195
outboxItem : PublicKey ;
181
- shouldQueue : boolean ;
182
196
} ,
183
197
pdas ?: Pdas
184
198
) : Promise < TransactionInstruction > {
185
- if ( config . paused ) throw new Error ( "Contract is paused" ) ;
186
-
187
- const chainId = toChainId ( args . recipientChain ) ;
188
- const transferArgs : TransferArgs = {
189
- amount : args . amount ,
190
- recipientChain : { id : chainId } ,
191
- recipientAddress : Array . from ( args . recipientAddress ) ,
192
- shouldQueue : args . shouldQueue ,
193
- } ;
194
-
195
199
pdas = pdas ?? NTT . pdas ( program . programId ) ;
196
200
201
+ const recipientChain = toChain ( args . transferArgs . recipientChain . id ) ;
197
202
const transferIx = await program . methods
198
- . transferBurn ( transferArgs )
203
+ . transferBurn ( args . transferArgs )
199
204
. accountsStrict ( {
200
205
common : {
201
206
payer : args . payer ,
@@ -208,11 +213,11 @@ export namespace NTT {
208
213
custody : await custodyAccountAddress ( pdas , config ) ,
209
214
systemProgram : SystemProgram . programId ,
210
215
} ,
211
- peer : pdas . peerAccount ( args . recipientChain ) ,
212
- inboxRateLimit : pdas . inboxRateLimitAccount ( args . recipientChain ) ,
216
+ peer : pdas . peerAccount ( recipientChain ) ,
217
+ inboxRateLimit : pdas . inboxRateLimitAccount ( recipientChain ) ,
213
218
sessionAuthority : pdas . sessionAuthority (
214
219
args . fromAuthority ,
215
- transferArgs
220
+ args . transferArgs
216
221
) ,
217
222
tokenAuthority : pdas . tokenAuthority ( ) ,
218
223
} )
@@ -230,7 +235,10 @@ export namespace NTT {
230
235
const source = args . from ;
231
236
const mint = config . mint ;
232
237
const destination = await custodyAccountAddress ( pdas , config ) ;
233
- const owner = pdas . sessionAuthority ( args . fromAuthority , transferArgs ) ;
238
+ const owner = pdas . sessionAuthority (
239
+ args . fromAuthority ,
240
+ args . transferArgs
241
+ ) ;
234
242
await addExtraAccountMetasForExecute (
235
243
program . provider . connection ,
236
244
transferIx ,
@@ -262,29 +270,19 @@ export namespace NTT {
262
270
payer : PublicKey ;
263
271
from : PublicKey ;
264
272
fromAuthority : PublicKey ;
265
- amount : BN ;
266
- recipientChain : Chain ;
267
- recipientAddress : ArrayLike < number > ;
268
- shouldQueue : boolean ;
273
+ transferArgs : NTT . TransferArgs ;
269
274
outboxItem : PublicKey ;
270
275
} ,
271
276
pdas ?: Pdas
272
277
) : Promise < TransactionInstruction > {
273
278
if ( config . paused ) throw new Error ( "Contract is paused" ) ;
274
279
275
- const chainId = toChainId ( args . recipientChain ) ;
276
-
277
- const transferArgs : TransferArgs = {
278
- amount : args . amount ,
279
- recipientChain : { id : chainId } ,
280
- recipientAddress : Array . from ( args . recipientAddress ) ,
281
- shouldQueue : args . shouldQueue ,
282
- } ;
283
-
284
280
pdas = pdas ?? NTT . pdas ( program . programId ) ;
285
281
282
+ const chain = toChain ( args . transferArgs . recipientChain . id ) ;
283
+
286
284
const transferIx = await program . methods
287
- . transferLock ( transferArgs )
285
+ . transferLock ( args . transferArgs )
288
286
. accounts ( {
289
287
common : {
290
288
payer : args . payer ,
@@ -296,11 +294,11 @@ export namespace NTT {
296
294
outboxRateLimit : pdas . outboxRateLimitAccount ( ) ,
297
295
custody : await custodyAccountAddress ( pdas , config ) ,
298
296
} ,
299
- peer : pdas . peerAccount ( args . recipientChain ) ,
300
- inboxRateLimit : pdas . inboxRateLimitAccount ( args . recipientChain ) ,
297
+ peer : pdas . peerAccount ( chain ) ,
298
+ inboxRateLimit : pdas . inboxRateLimitAccount ( chain ) ,
301
299
sessionAuthority : pdas . sessionAuthority (
302
300
args . fromAuthority ,
303
- transferArgs
301
+ args . transferArgs
304
302
) ,
305
303
} )
306
304
. instruction ( ) ;
@@ -316,7 +314,10 @@ export namespace NTT {
316
314
if ( transferHook ) {
317
315
const source = args . from ;
318
316
const destination = await custodyAccountAddress ( pdas , config ) ;
319
- const owner = pdas . sessionAuthority ( args . fromAuthority , transferArgs ) ;
317
+ const owner = pdas . sessionAuthority (
318
+ args . fromAuthority ,
319
+ args . transferArgs
320
+ ) ;
320
321
await addExtraAccountMetasForExecute (
321
322
program . provider . connection ,
322
323
transferIx ,
@@ -392,8 +393,6 @@ export namespace NTT {
392
393
} ,
393
394
pdas ?: Pdas
394
395
) : Promise < TransactionInstruction > {
395
- if ( config . paused ) throw new Error ( "Contract is paused" ) ;
396
-
397
396
pdas = pdas ?? NTT . pdas ( program . programId ) ;
398
397
399
398
const recipientAddress =
@@ -474,8 +473,6 @@ export namespace NTT {
474
473
} ,
475
474
pdas ?: Pdas
476
475
) {
477
- if ( config . paused ) throw new Error ( "Contract is paused" ) ;
478
-
479
476
const recipientAddress =
480
477
args . recipient ??
481
478
( await NTT . getInboxItem ( program , args . chain , args . nttMessage ) )
@@ -727,15 +724,12 @@ export namespace NTT {
727
724
728
725
export async function createReceiveWormholeMessageInstruction (
729
726
program : Program < NttBindings . NativeTokenTransfer < IdlVersion > > ,
730
- config : NttBindings . Config < IdlVersion > ,
731
727
args : {
732
728
wormholeId : PublicKey ;
733
729
payer : PublicKey ;
734
730
vaa : Uint8Array ;
735
731
}
736
732
) : Promise < TransactionInstruction > {
737
- if ( config . paused ) throw new Error ( "Contract is paused" ) ;
738
-
739
733
const pdas = NTT . pdas ( program . programId ) ;
740
734
741
735
const wormholeNTT = deserialize ( "Ntt:WormholeTransfer" , args . vaa ) ;
@@ -767,11 +761,10 @@ export namespace NTT {
767
761
args : {
768
762
payer : PublicKey ;
769
763
vaa : Uint8Array ;
770
- }
764
+ } ,
765
+ pdas ?: Pdas
771
766
) : Promise < TransactionInstruction > {
772
- if ( config . paused ) throw new Error ( "Contract is paused" ) ;
773
-
774
- const pdas = NTT . pdas ( program . programId ) ;
767
+ pdas = pdas ?? NTT . pdas ( program . programId ) ;
775
768
776
769
const wormholeNTT = deserialize ( "Ntt:WormholeTransfer" , args . vaa ) ;
777
770
const nttMessage = wormholeNTT . payload . nttManagerPayload ;
0 commit comments