|
| 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