Skip to content

Commit 0260c34

Browse files
committed
replace transfer instructions
1 parent f0f5549 commit 0260c34

File tree

2 files changed

+83
-350
lines changed

2 files changed

+83
-350
lines changed

solana/ts/lib/ntt.ts

+48-55
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import {
1515
} from "@solana/web3.js";
1616
import {
1717
Chain,
18+
ChainAddress,
1819
ChainId,
1920
deserialize,
2021
deserializeLayout,
2122
encoding,
2223
keccak256,
2324
rpc,
25+
toChain,
2426
toChainId,
2527
} from "@wormhole-foundation/sdk-connect";
2628

@@ -38,14 +40,29 @@ import {
3840
} from "./bindings.js";
3941
import { chainToBytes, derivePda } from "./utils.js";
4042

41-
export interface TransferArgs {
42-
amount: BN;
43-
recipientChain: { id: ChainId };
44-
recipientAddress: number[];
45-
shouldQueue: boolean;
46-
}
47-
4843
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+
4966
export type Pdas = ReturnType<typeof pdas>;
5067
export const pdas = (programId: PublicKeyInitData) => {
5168
const configAccount = (): PublicKey => derivePda("config", programId);
@@ -174,28 +191,16 @@ export namespace NTT {
174191
payer: PublicKey;
175192
from: PublicKey;
176193
fromAuthority: PublicKey;
177-
amount: BN;
178-
recipientChain: Chain;
179-
recipientAddress: ArrayLike<number>;
194+
transferArgs: TransferArgs;
180195
outboxItem: PublicKey;
181-
shouldQueue: boolean;
182196
},
183197
pdas?: Pdas
184198
): 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-
195199
pdas = pdas ?? NTT.pdas(program.programId);
196200

201+
const recipientChain = toChain(args.transferArgs.recipientChain.id);
197202
const transferIx = await program.methods
198-
.transferBurn(transferArgs)
203+
.transferBurn(args.transferArgs)
199204
.accountsStrict({
200205
common: {
201206
payer: args.payer,
@@ -208,11 +213,11 @@ export namespace NTT {
208213
custody: await custodyAccountAddress(pdas, config),
209214
systemProgram: SystemProgram.programId,
210215
},
211-
peer: pdas.peerAccount(args.recipientChain),
212-
inboxRateLimit: pdas.inboxRateLimitAccount(args.recipientChain),
216+
peer: pdas.peerAccount(recipientChain),
217+
inboxRateLimit: pdas.inboxRateLimitAccount(recipientChain),
213218
sessionAuthority: pdas.sessionAuthority(
214219
args.fromAuthority,
215-
transferArgs
220+
args.transferArgs
216221
),
217222
tokenAuthority: pdas.tokenAuthority(),
218223
})
@@ -230,7 +235,10 @@ export namespace NTT {
230235
const source = args.from;
231236
const mint = config.mint;
232237
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+
);
234242
await addExtraAccountMetasForExecute(
235243
program.provider.connection,
236244
transferIx,
@@ -262,29 +270,19 @@ export namespace NTT {
262270
payer: PublicKey;
263271
from: PublicKey;
264272
fromAuthority: PublicKey;
265-
amount: BN;
266-
recipientChain: Chain;
267-
recipientAddress: ArrayLike<number>;
268-
shouldQueue: boolean;
273+
transferArgs: NTT.TransferArgs;
269274
outboxItem: PublicKey;
270275
},
271276
pdas?: Pdas
272277
): Promise<TransactionInstruction> {
273278
if (config.paused) throw new Error("Contract is paused");
274279

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-
284280
pdas = pdas ?? NTT.pdas(program.programId);
285281

282+
const chain = toChain(args.transferArgs.recipientChain.id);
283+
286284
const transferIx = await program.methods
287-
.transferLock(transferArgs)
285+
.transferLock(args.transferArgs)
288286
.accounts({
289287
common: {
290288
payer: args.payer,
@@ -296,11 +294,11 @@ export namespace NTT {
296294
outboxRateLimit: pdas.outboxRateLimitAccount(),
297295
custody: await custodyAccountAddress(pdas, config),
298296
},
299-
peer: pdas.peerAccount(args.recipientChain),
300-
inboxRateLimit: pdas.inboxRateLimitAccount(args.recipientChain),
297+
peer: pdas.peerAccount(chain),
298+
inboxRateLimit: pdas.inboxRateLimitAccount(chain),
301299
sessionAuthority: pdas.sessionAuthority(
302300
args.fromAuthority,
303-
transferArgs
301+
args.transferArgs
304302
),
305303
})
306304
.instruction();
@@ -316,7 +314,10 @@ export namespace NTT {
316314
if (transferHook) {
317315
const source = args.from;
318316
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+
);
320321
await addExtraAccountMetasForExecute(
321322
program.provider.connection,
322323
transferIx,
@@ -392,8 +393,6 @@ export namespace NTT {
392393
},
393394
pdas?: Pdas
394395
): Promise<TransactionInstruction> {
395-
if (config.paused) throw new Error("Contract is paused");
396-
397396
pdas = pdas ?? NTT.pdas(program.programId);
398397

399398
const recipientAddress =
@@ -474,8 +473,6 @@ export namespace NTT {
474473
},
475474
pdas?: Pdas
476475
) {
477-
if (config.paused) throw new Error("Contract is paused");
478-
479476
const recipientAddress =
480477
args.recipient ??
481478
(await NTT.getInboxItem(program, args.chain, args.nttMessage))
@@ -727,15 +724,12 @@ export namespace NTT {
727724

728725
export async function createReceiveWormholeMessageInstruction(
729726
program: Program<NttBindings.NativeTokenTransfer<IdlVersion>>,
730-
config: NttBindings.Config<IdlVersion>,
731727
args: {
732728
wormholeId: PublicKey;
733729
payer: PublicKey;
734730
vaa: Uint8Array;
735731
}
736732
): Promise<TransactionInstruction> {
737-
if (config.paused) throw new Error("Contract is paused");
738-
739733
const pdas = NTT.pdas(program.programId);
740734

741735
const wormholeNTT = deserialize("Ntt:WormholeTransfer", args.vaa);
@@ -767,11 +761,10 @@ export namespace NTT {
767761
args: {
768762
payer: PublicKey;
769763
vaa: Uint8Array;
770-
}
764+
},
765+
pdas?: Pdas
771766
): 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);
775768

776769
const wormholeNTT = deserialize("Ntt:WormholeTransfer", args.vaa);
777770
const nttMessage = wormholeNTT.payload.nttManagerPayload;

0 commit comments

Comments
 (0)