Skip to content

Commit 1bc603e

Browse files
committed
add bankrun tests
1 parent 6195ac8 commit 1bc603e

File tree

5 files changed

+270
-172
lines changed

5 files changed

+270
-172
lines changed

tokens/spl-token-minter/steel/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"scripts": {
3-
"test": "pnpm ts-mocha -p ./tsconfig.json -t 1000000 ./tests/test.ts"
3+
"test": "pnpm ts-mocha -p ./tsconfig.json -t 1000000 ./tests/bankrun.test.ts",
4+
"build-and-test": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./tests/fixtures && pnpm test",
5+
"build": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./program/target/so",
6+
"deploy": "solana program deploy ./program/target/so/spl_token_minter_program.so",
7+
"postinstall": "zx prepare.mjs"
48
},
59
"dependencies": {
610
"@metaplex-foundation/mpl-token-metadata": "^2.5.2",
@@ -17,6 +21,8 @@
1721
"chai": "^4.3.4",
1822
"mocha": "^9.0.3",
1923
"ts-mocha": "^10.0.0",
20-
"typescript": "^4.3.5"
24+
"typescript": "^4.3.5",
25+
"solana-bankrun": "^0.4.0",
26+
"zx": "^8.1.4"
2127
}
2228
}

tokens/spl-token-minter/steel/pnpm-lock.yaml

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env zx
2+
3+
import { mkdir, rm } from "node:fs/promises";
4+
import { join } from "node:path";
5+
import { $ } from "zx";
6+
7+
const programs = [
8+
{
9+
id: "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s",
10+
name: "token_metadata.so",
11+
},
12+
];
13+
14+
const outputDir = "tests/fixtures";
15+
const overwrite = true;
16+
17+
try {
18+
for (const program of programs) {
19+
const { id, name } = program;
20+
const outputFile = join(outputDir, name);
21+
await $`solana config set -um`;
22+
23+
try {
24+
await mkdir(outputDir, { recursive: true });
25+
if (overwrite) await rm(outputFile, { force: true });
26+
await $`solana program dump ${id} ${outputFile}`;
27+
console.log(`Program ${id} dumped to ${outputFile}`);
28+
} catch (error) {
29+
console.error(`Error dumping ${id}: ${error.message}`);
30+
}
31+
}
32+
} catch (error) {
33+
console.error(`Error preparing programs: ${error.message}`);
34+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import { Buffer } from "node:buffer";
2+
import { describe, test } from "node:test";
3+
import { PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID } from "@metaplex-foundation/mpl-token-metadata";
4+
import {
5+
ASSOCIATED_TOKEN_PROGRAM_ID,
6+
TOKEN_PROGRAM_ID,
7+
getAssociatedTokenAddressSync,
8+
} from "@solana/spl-token";
9+
import {
10+
Keypair,
11+
PublicKey,
12+
SYSVAR_RENT_PUBKEY,
13+
SystemProgram,
14+
Transaction,
15+
TransactionInstruction,
16+
} from "@solana/web3.js";
17+
import { BN } from "bn.js";
18+
import { start } from "solana-bankrun";
19+
import {
20+
CreateTokenArgs,
21+
MintToArgs,
22+
SplMinterInstruction,
23+
} from "./instructions";
24+
25+
describe("SPL Token Minter!", async () => {
26+
const PROGRAM_ID = new PublicKey(
27+
"8V26fyhrQobKbvkRCV3KvT6jZQLzviovdARfGrw8kUdG",
28+
);
29+
const context = await start(
30+
[
31+
{ name: "spl_token_minter_program", programId: PROGRAM_ID },
32+
{ name: "token_metadata", programId: TOKEN_METADATA_PROGRAM_ID },
33+
],
34+
[],
35+
);
36+
const client = context.banksClient;
37+
const payer = context.payer;
38+
39+
const mintKeypair: Keypair = Keypair.generate();
40+
41+
test("Create an SPL Token!", async () => {
42+
const metadataPDA = PublicKey.findProgramAddressSync(
43+
[
44+
Buffer.from("metadata"),
45+
TOKEN_METADATA_PROGRAM_ID.toBuffer(),
46+
mintKeypair.publicKey.toBuffer(),
47+
],
48+
TOKEN_METADATA_PROGRAM_ID,
49+
)[0];
50+
51+
// SPL Token default = 9 decimals
52+
//
53+
const createArgs = new CreateTokenArgs(
54+
"Solana Gold",
55+
"GOLDSOL",
56+
"https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json",
57+
);
58+
59+
const createTokenIx = new TransactionInstruction({
60+
keys: [
61+
{ pubkey: payer.publicKey, isSigner: true, isWritable: true },
62+
{ pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true },
63+
{ pubkey: metadataPDA, isSigner: false, isWritable: true },
64+
{
65+
pubkey: SystemProgram.programId,
66+
isSigner: false,
67+
isWritable: false,
68+
},
69+
{ pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
70+
{
71+
pubkey: TOKEN_METADATA_PROGRAM_ID,
72+
isSigner: false,
73+
isWritable: false,
74+
},
75+
{ pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
76+
],
77+
programId: PROGRAM_ID,
78+
data: createArgs.toBuffer(),
79+
});
80+
81+
const tx = new Transaction();
82+
const [blockhash, _] = await client.getLatestBlockhash();
83+
tx.recentBlockhash = blockhash;
84+
tx.add(createTokenIx).sign(payer, mintKeypair);
85+
86+
await client.processTransaction(tx);
87+
88+
console.log("Success!");
89+
console.log(` Mint Address: ${mintKeypair.publicKey}`);
90+
});
91+
92+
test("Mint some tokens to your wallet!", async () => {
93+
const recipientATA = getAssociatedTokenAddressSync(
94+
mintKeypair.publicKey,
95+
payer.publicKey,
96+
);
97+
98+
const mintArgs = new MintToArgs(100);
99+
100+
const mintToIx = new TransactionInstruction({
101+
keys: [
102+
{ pubkey: payer.publicKey, isSigner: true, isWritable: true }, // mint_authority
103+
{ pubkey: payer.publicKey, isSigner: false, isWritable: false }, // recipient
104+
{ pubkey: mintKeypair.publicKey, isSigner: false, isWritable: true }, // mint_pda must be writable
105+
{ pubkey: recipientATA, isSigner: false, isWritable: true }, // associated_token_account must be writable
106+
{ pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // spl_token::ID
107+
{
108+
pubkey: ASSOCIATED_TOKEN_PROGRAM_ID,
109+
isSigner: false,
110+
isWritable: false,
111+
}, // spl_associated_token_account::ID
112+
{
113+
pubkey: SystemProgram.programId,
114+
isSigner: false,
115+
isWritable: false,
116+
}, // system_program::ID
117+
],
118+
programId: PROGRAM_ID,
119+
data: mintArgs.toBuffer(),
120+
});
121+
122+
const tx = new Transaction();
123+
const [blockhash, _] = await client.getLatestBlockhash();
124+
tx.recentBlockhash = blockhash;
125+
tx.add(mintToIx).sign(payer);
126+
127+
await client.processTransaction(tx);
128+
129+
console.log("Success!");
130+
console.log(` ATA Address: ${recipientATA}`);
131+
});
132+
});

0 commit comments

Comments
 (0)