Skip to content

Commit dd8dbbd

Browse files
committed
solana/sdk: support transfer hook
1 parent fede1bc commit dd8dbbd

File tree

2 files changed

+254
-34
lines changed

2 files changed

+254
-34
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,44 +51,111 @@ 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

119+
it("Create ExtraAccountMetaList Account", async () => {
120+
const initializeExtraAccountMetaListInstruction =
121+
await dummyTransferHook.methods
122+
.initializeExtraAccountMetaList()
123+
.accountsStrict({
124+
payer: payer.publicKey,
125+
mint: mint.publicKey,
126+
extraAccountMetaList: extraAccountMetaListPDA,
127+
tokenProgram: spl.TOKEN_2022_PROGRAM_ID,
128+
associatedTokenProgram: spl.ASSOCIATED_TOKEN_PROGRAM_ID,
129+
systemProgram: SystemProgram.programId,
130+
})
131+
.instruction();
132+
133+
const transaction = new Transaction().add(
134+
initializeExtraAccountMetaListInstruction
135+
);
136+
137+
await sendAndConfirmTransaction(connection, transaction, [payer]);
138+
});
139+
73140
describe("Locking", () => {
74141
before(async () => {
75142
await spl.setAuthority(
76143
connection,
77144
payer,
78-
mint,
145+
mint.publicKey,
79146
owner,
80-
0, // mint
81-
ntt.tokenAuthorityAddress()
147+
spl.AuthorityType.MintTokens,
148+
ntt.tokenAuthorityAddress(),
149+
[],
150+
undefined,
151+
spl.TOKEN_2022_PROGRAM_ID
82152
);
83153

84154
await ntt.initialize({
85155
payer,
86156
owner: payer,
87157
chain: "solana",
88-
mint,
158+
mint: mint.publicKey,
89159
outboundLimit: new BN(1000000),
90160
mode: "locking",
91161
});

0 commit comments

Comments
 (0)