test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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: false, isWritable: true }, // Mint close authority account
  62. { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // Transaction Payer
  63. { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, // Rent account
  64. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // System program
  65. { pubkey: TOKEN_2022_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program
  66. ],
  67. programId: program.publicKey,
  68. data: instructionData.toBuffer(),
  69. });
  70. const signature = await sendAndConfirmTransaction(
  71. connection,
  72. new Transaction().add(instruction),
  73. [payer, mintKeypair]
  74. );
  75. console.log(`Token Mint Address: `, mintKeypair.publicKey.toBase58());
  76. console.log(`Transaction Signature: `, signature);
  77. });
  78. });