|
| 1 | +import { PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID } from "@metaplex-foundation/mpl-token-metadata"; |
| 2 | +import { |
| 3 | + Connection, |
| 4 | + Keypair, |
| 5 | + PublicKey, |
| 6 | + SystemProgram, |
| 7 | + SYSVAR_RENT_PUBKEY, |
| 8 | + TransactionInstruction, |
| 9 | + Transaction, |
| 10 | + sendAndConfirmTransaction, |
| 11 | +} from "@solana/web3.js"; |
| 12 | +import { TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID } from "@solana/spl-token"; |
| 13 | +import * as borsh from "borsh"; |
| 14 | +import { Buffer } from "buffer"; |
| 15 | + |
| 16 | +function createKeypairFromFile(path: string): Keypair { |
| 17 | + return Keypair.fromSecretKey( |
| 18 | + Buffer.from(JSON.parse(require("fs").readFileSync(path, "utf-8"))) |
| 19 | + ); |
| 20 | +} |
| 21 | + |
| 22 | +class Assignable { |
| 23 | + constructor(properties) { |
| 24 | + Object.keys(properties).map((key) => { |
| 25 | + return (this[key] = properties[key]); |
| 26 | + }); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +class CreateTokenArgs extends Assignable { |
| 31 | + toBuffer() { |
| 32 | + return Buffer.from(borsh.serialize(CreateTokenArgsSchema, this)); |
| 33 | + } |
| 34 | +} |
| 35 | +const CreateTokenArgsSchema = new Map([ |
| 36 | + [ |
| 37 | + CreateTokenArgs, |
| 38 | + { |
| 39 | + kind: "struct", |
| 40 | + fields: [["token_decimals", "u8"]], |
| 41 | + }, |
| 42 | + ], |
| 43 | +]); |
| 44 | + |
| 45 | +describe("Create Token", async () => { |
| 46 | + const connection = new Connection( |
| 47 | + `https://api.devnet.solana.com/`, |
| 48 | + "confirmed" |
| 49 | + ); |
| 50 | + const payer = createKeypairFromFile( |
| 51 | + require("os").homedir() + "/.config/solana/id.json" |
| 52 | + ); |
| 53 | + const program = createKeypairFromFile( |
| 54 | + "./program/target/deploy/program-keypair.json" |
| 55 | + ); |
| 56 | + |
| 57 | + it("Create a Token-22 SPL-Token !", async () => { |
| 58 | + const mintKeypair: Keypair = Keypair.generate(); |
| 59 | + |
| 60 | + const instructionData = new CreateTokenArgs({ |
| 61 | + token_decimals: 9, |
| 62 | + }); |
| 63 | + |
| 64 | + const instruction = new TransactionInstruction({ |
| 65 | + keys: [ |
| 66 | + { pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true }, // Mint account |
| 67 | + { pubkey: payer.publicKey, isSigner: false, isWritable: true }, // Mint authority account |
| 68 | + { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // Transaction Payer |
| 69 | + { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, // Rent account |
| 70 | + { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // System program |
| 71 | + { pubkey: TOKEN_2022_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program |
| 72 | + ], |
| 73 | + programId: program.publicKey, |
| 74 | + data: instructionData.toBuffer(), |
| 75 | + }); |
| 76 | + |
| 77 | + const signature = await sendAndConfirmTransaction( |
| 78 | + connection, |
| 79 | + new Transaction().add(instruction), |
| 80 | + [payer, mintKeypair] |
| 81 | + ); |
| 82 | + |
| 83 | + console.log(`Token Mint Address: `, mintKeypair.publicKey.toBase58()); |
| 84 | + console.log(`Transaction Signature: `, signature); |
| 85 | + }); |
| 86 | +}); |
0 commit comments