Skip to content

Commit 9d49b04

Browse files
committed
solana/sdk: support transfer hook
1 parent 1123458 commit 9d49b04

File tree

2 files changed

+253
-33
lines changed

2 files changed

+253
-33
lines changed

solana/tests/example-native-token-transfer.ts

+89-19
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ import {
1616
serializePayload,
1717
deserializePayload,
1818
} from "@wormhole-foundation/sdk-definitions";
19-
import { NttMessage, postVaa, NTT, nttMessageLayout } from "../ts/sdk";
19+
import { postVaa, NTT, nttMessageLayout } from "../ts/sdk";
20+
import { WormholeTransceiverMessage } from "../ts/sdk/nttLayout";
21+
2022
import {
21-
NativeTokenTransfer,
22-
TransceiverMessage,
23-
WormholeTransceiverMessage,
24-
nativeTokenTransferLayout,
25-
nttManagerMessageLayout,
26-
} from "../ts/sdk/nttLayout";
23+
PublicKey,
24+
SystemProgram,
25+
Transaction,
26+
sendAndConfirmTransaction,
27+
} from "@solana/web3.js";
28+
29+
import { DummyTransferHook } from "../target/types/dummy_transfer_hook";
2730

2831
export const GUARDIAN_KEY =
2932
"cfb12303a19cde580bb4dd771639b0d26bc68353645571a8cff516ab2ee113a0";
@@ -48,25 +51,68 @@ describe("example-native-token-transfers", () => {
4851
const user = anchor.web3.Keypair.generate();
4952
let tokenAccount: anchor.web3.PublicKey;
5053

51-
let mint: anchor.web3.PublicKey;
54+
const mint = anchor.web3.Keypair.generate();
55+
56+
const dummyTransferHook = anchor.workspace
57+
.DummyTransferHook as anchor.Program<DummyTransferHook>;
58+
59+
const [extraAccountMetaListPDA] = PublicKey.findProgramAddressSync(
60+
[Buffer.from("extra-account-metas"), mint.publicKey.toBuffer()],
61+
dummyTransferHook.programId
62+
);
63+
64+
it("Initialize mint", async () => {
65+
const extensions = [spl.ExtensionType.TransferHook];
66+
const mintLen = spl.getMintLen(extensions);
67+
const lamports = await connection.getMinimumBalanceForRentExemption(
68+
mintLen
69+
);
70+
71+
const transaction = new Transaction().add(
72+
SystemProgram.createAccount({
73+
fromPubkey: payer.publicKey,
74+
newAccountPubkey: mint.publicKey,
75+
space: mintLen,
76+
lamports,
77+
programId: spl.TOKEN_2022_PROGRAM_ID,
78+
}),
79+
spl.createInitializeTransferHookInstruction(
80+
mint.publicKey,
81+
owner.publicKey,
82+
dummyTransferHook.programId,
83+
spl.TOKEN_2022_PROGRAM_ID
84+
),
85+
spl.createInitializeMintInstruction(
86+
mint.publicKey,
87+
9,
88+
owner.publicKey,
89+
null,
90+
spl.TOKEN_2022_PROGRAM_ID
91+
)
92+
);
5293

53-
before(async () => {
54-
// airdrop some tokens to payer
55-
mint = await spl.createMint(connection, payer, owner.publicKey, null, 9);
94+
await sendAndConfirmTransaction(connection, transaction, [payer, mint]);
5695

5796
tokenAccount = await spl.createAssociatedTokenAccount(
5897
connection,
5998
payer,
60-
mint,
61-
user.publicKey
99+
mint.publicKey,
100+
user.publicKey,
101+
undefined,
102+
spl.TOKEN_2022_PROGRAM_ID,
103+
spl.ASSOCIATED_TOKEN_PROGRAM_ID
62104
);
105+
63106
await spl.mintTo(
64107
connection,
65108
payer,
66-
mint,
109+
mint.publicKey,
67110
tokenAccount,
68111
owner,
69-
BigInt(10000000)
112+
BigInt(10000000),
113+
undefined,
114+
undefined,
115+
spl.TOKEN_2022_PROGRAM_ID
70116
);
71117
});
72118

@@ -75,22 +121,46 @@ describe("example-native-token-transfers", () => {
75121
expect(version).to.equal("1.0.0");
76122
});
77123

124+
it("Create ExtraAccountMetaList Account", async () => {
125+
const initializeExtraAccountMetaListInstruction =
126+
await dummyTransferHook.methods
127+
.initializeExtraAccountMetaList()
128+
.accountsStrict({
129+
payer: payer.publicKey,
130+
mint: mint.publicKey,
131+
extraAccountMetaList: extraAccountMetaListPDA,
132+
tokenProgram: spl.TOKEN_2022_PROGRAM_ID,
133+
associatedTokenProgram: spl.ASSOCIATED_TOKEN_PROGRAM_ID,
134+
systemProgram: SystemProgram.programId,
135+
})
136+
.instruction();
137+
138+
const transaction = new Transaction().add(
139+
initializeExtraAccountMetaListInstruction
140+
);
141+
142+
await sendAndConfirmTransaction(connection, transaction, [payer]);
143+
});
144+
78145
describe("Locking", () => {
79146
before(async () => {
80147
await spl.setAuthority(
81148
connection,
82149
payer,
83-
mint,
150+
mint.publicKey,
84151
owner,
85-
0, // mint
86-
ntt.tokenAuthorityAddress()
152+
spl.AuthorityType.MintTokens,
153+
ntt.tokenAuthorityAddress(),
154+
[],
155+
undefined,
156+
spl.TOKEN_2022_PROGRAM_ID
87157
);
88158

89159
await ntt.initialize({
90160
payer,
91161
owner: payer,
92162
chain: "solana",
93-
mint,
163+
mint: mint.publicKey,
94164
outboundLimit: new BN(1000000),
95165
mode: "locking",
96166
});

0 commit comments

Comments
 (0)