test.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. function createKeypairFromFile(path: string): Keypair {
  16. return Keypair.fromSecretKey(
  17. Buffer.from(JSON.parse(require("fs").readFileSync(path, "utf-8")))
  18. );
  19. }
  20. class Assignable {
  21. constructor(properties) {
  22. Object.keys(properties).map((key) => {
  23. return (this[key] = properties[key]);
  24. });
  25. }
  26. }
  27. class CreateTokenArgs extends Assignable {
  28. toBuffer() {
  29. return Buffer.from(borsh.serialize(CreateTokenArgsSchema, this));
  30. }
  31. }
  32. const CreateTokenArgsSchema = new Map([
  33. [
  34. CreateTokenArgs,
  35. {
  36. kind: "struct",
  37. fields: [["token_decimals", "u8"]],
  38. },
  39. ],
  40. ]);
  41. describe("Create Token", async () => {
  42. const connection = new Connection(
  43. `https://api.devnet.solana.com/`,
  44. "confirmed"
  45. );
  46. const payer = createKeypairFromFile(
  47. require("os").homedir() + "/.config/solana/id.json"
  48. );
  49. const program = createKeypairFromFile(
  50. "./program/target/deploy/program-keypair.json"
  51. );
  52. it("Create a Token-22 SPL-Token !", async () => {
  53. const mintKeypair: Keypair = Keypair.generate();
  54. const instructionData = new CreateTokenArgs({
  55. token_decimals: 9,
  56. });
  57. const instruction = new TransactionInstruction({
  58. keys: [
  59. { pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true }, // Mint account
  60. { pubkey: payer.publicKey, isSigner: false, isWritable: true }, // Mint authority account
  61. { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // Transaction Payer
  62. { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, // Rent account
  63. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // System program
  64. { pubkey: TOKEN_2022_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program
  65. ],
  66. programId: program.publicKey,
  67. data: instructionData.toBuffer(),
  68. });
  69. const signature = await sendAndConfirmTransaction(
  70. connection,
  71. new Transaction().add(instruction),
  72. [payer, mintKeypair]
  73. );
  74. console.log(`Token Mint Address: `, mintKeypair.publicKey.toBase58());
  75. console.log(`Transaction Signature: `, signature);
  76. });
  77. });