Skip to content

Commit

Permalink
solana/sdk: support transfer hook
Browse files Browse the repository at this point in the history
  • Loading branch information
kcsongor committed Apr 17, 2024
1 parent 1123458 commit 9d49b04
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 33 deletions.
108 changes: 89 additions & 19 deletions solana/tests/example-native-token-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ import {
serializePayload,
deserializePayload,
} from "@wormhole-foundation/sdk-definitions";
import { NttMessage, postVaa, NTT, nttMessageLayout } from "../ts/sdk";
import { postVaa, NTT, nttMessageLayout } from "../ts/sdk";
import { WormholeTransceiverMessage } from "../ts/sdk/nttLayout";

import {
NativeTokenTransfer,
TransceiverMessage,
WormholeTransceiverMessage,
nativeTokenTransferLayout,
nttManagerMessageLayout,
} from "../ts/sdk/nttLayout";
PublicKey,
SystemProgram,
Transaction,
sendAndConfirmTransaction,
} from "@solana/web3.js";

import { DummyTransferHook } from "../target/types/dummy_transfer_hook";

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

let mint: anchor.web3.PublicKey;
const mint = anchor.web3.Keypair.generate();

const dummyTransferHook = anchor.workspace
.DummyTransferHook as anchor.Program<DummyTransferHook>;

const [extraAccountMetaListPDA] = PublicKey.findProgramAddressSync(
[Buffer.from("extra-account-metas"), mint.publicKey.toBuffer()],
dummyTransferHook.programId
);

it("Initialize mint", async () => {
const extensions = [spl.ExtensionType.TransferHook];
const mintLen = spl.getMintLen(extensions);
const lamports = await connection.getMinimumBalanceForRentExemption(
mintLen
);

const transaction = new Transaction().add(
SystemProgram.createAccount({
fromPubkey: payer.publicKey,
newAccountPubkey: mint.publicKey,
space: mintLen,
lamports,
programId: spl.TOKEN_2022_PROGRAM_ID,
}),
spl.createInitializeTransferHookInstruction(
mint.publicKey,
owner.publicKey,
dummyTransferHook.programId,
spl.TOKEN_2022_PROGRAM_ID
),
spl.createInitializeMintInstruction(
mint.publicKey,
9,
owner.publicKey,
null,
spl.TOKEN_2022_PROGRAM_ID
)
);

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

tokenAccount = await spl.createAssociatedTokenAccount(
connection,
payer,
mint,
user.publicKey
mint.publicKey,
user.publicKey,
undefined,
spl.TOKEN_2022_PROGRAM_ID,
spl.ASSOCIATED_TOKEN_PROGRAM_ID
);

await spl.mintTo(
connection,
payer,
mint,
mint.publicKey,
tokenAccount,
owner,
BigInt(10000000)
BigInt(10000000),
undefined,
undefined,
spl.TOKEN_2022_PROGRAM_ID
);
});

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

it("Create ExtraAccountMetaList Account", async () => {
const initializeExtraAccountMetaListInstruction =
await dummyTransferHook.methods
.initializeExtraAccountMetaList()
.accountsStrict({
payer: payer.publicKey,
mint: mint.publicKey,
extraAccountMetaList: extraAccountMetaListPDA,
tokenProgram: spl.TOKEN_2022_PROGRAM_ID,
associatedTokenProgram: spl.ASSOCIATED_TOKEN_PROGRAM_ID,
systemProgram: SystemProgram.programId,
})
.instruction();

const transaction = new Transaction().add(
initializeExtraAccountMetaListInstruction
);

await sendAndConfirmTransaction(connection, transaction, [payer]);
});

describe("Locking", () => {
before(async () => {
await spl.setAuthority(
connection,
payer,
mint,
mint.publicKey,
owner,
0, // mint
ntt.tokenAuthorityAddress()
spl.AuthorityType.MintTokens,
ntt.tokenAuthorityAddress(),
[],
undefined,
spl.TOKEN_2022_PROGRAM_ID
);

await ntt.initialize({
payer,
owner: payer,
chain: "solana",
mint,
mint: mint.publicKey,
outboundLimit: new BN(1000000),
mode: "locking",
});
Expand Down
Loading

0 comments on commit 9d49b04

Please sign in to comment.