spl-token-minter.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { SplTokenMinter } from "../target/types/spl_token_minter";
  4. import { PublicKey, SystemProgram, SYSVAR_RENT_PUBKEY } from "@solana/web3.js";
  5. import { Metaplex } from "@metaplex-foundation/js";
  6. import {
  7. ASSOCIATED_TOKEN_PROGRAM_ID,
  8. getOrCreateAssociatedTokenAccount,
  9. TOKEN_PROGRAM_ID,
  10. } from "@solana/spl-token";
  11. describe("spl-token-minter", () => {
  12. // Configure the client to use the local cluster.
  13. const provider = anchor.AnchorProvider.env();
  14. anchor.setProvider(provider);
  15. // Generate a new keypair for the data account for the program
  16. const dataAccount = anchor.web3.Keypair.generate();
  17. // Generate a mint keypair
  18. const mintKeypair = anchor.web3.Keypair.generate();
  19. const wallet = provider.wallet as anchor.Wallet;
  20. const connection = provider.connection;
  21. const program = anchor.workspace.SplTokenMinter as Program<SplTokenMinter>;
  22. // Metadata for the Token
  23. const tokenTitle = "Solana Gold";
  24. const tokenSymbol = "GOLDSOL";
  25. const tokenUri =
  26. "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json";
  27. it("Is initialized!", async () => {
  28. // Initialize data account for the program, which is required by Solang
  29. const tx = await program.methods
  30. .new()
  31. .accounts({ dataAccount: dataAccount.publicKey })
  32. .signers([dataAccount])
  33. .rpc();
  34. console.log("Your transaction signature", tx);
  35. });
  36. it("Create an SPL Token!", async () => {
  37. // Get the metadata address for the mint
  38. const metaplex = Metaplex.make(connection);
  39. const metadataAddress = await metaplex
  40. .nfts()
  41. .pdas()
  42. .metadata({ mint: mintKeypair.publicKey });
  43. // Create the token mint
  44. const tx = await program.methods
  45. .createTokenMint(
  46. wallet.publicKey, // payer
  47. mintKeypair.publicKey, // mint
  48. wallet.publicKey, // mint authority
  49. wallet.publicKey, // freeze authority
  50. metadataAddress, // metadata address
  51. 9, // decimals
  52. tokenTitle, // token name
  53. tokenSymbol, // token symbol
  54. tokenUri // token uri
  55. )
  56. .accounts({ dataAccount: dataAccount.publicKey })
  57. .remainingAccounts([
  58. {
  59. pubkey: wallet.publicKey,
  60. isWritable: true,
  61. isSigner: true,
  62. },
  63. { pubkey: mintKeypair.publicKey, isWritable: true, isSigner: true },
  64. {
  65. pubkey: new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"), // Metadata program id
  66. isWritable: false,
  67. isSigner: false,
  68. },
  69. { pubkey: metadataAddress, isWritable: true, isSigner: false },
  70. { pubkey: SystemProgram.programId, isWritable: false, isSigner: false },
  71. { pubkey: SYSVAR_RENT_PUBKEY, isWritable: false, isSigner: false },
  72. ])
  73. .signers([mintKeypair])
  74. .rpc({ skipPreflight: true });
  75. console.log("Your transaction signature", tx);
  76. });
  77. it("Mint some tokens to your wallet!", async () => {
  78. // Wallet's associated token account address for mint
  79. const tokenAccount = await getOrCreateAssociatedTokenAccount(
  80. connection,
  81. wallet.payer, // payer
  82. mintKeypair.publicKey, // mint
  83. wallet.publicKey // owner
  84. );
  85. const tx = await program.methods
  86. .mintTo(
  87. wallet.publicKey, // payer
  88. tokenAccount.address, // associated token account address
  89. mintKeypair.publicKey, // mint
  90. new anchor.BN(150) // amount to mint
  91. )
  92. .accounts({ dataAccount: dataAccount.publicKey })
  93. .remainingAccounts([
  94. {
  95. pubkey: wallet.publicKey,
  96. isWritable: true,
  97. isSigner: true,
  98. },
  99. { pubkey: tokenAccount.address, isWritable: true, isSigner: false },
  100. { pubkey: mintKeypair.publicKey, isWritable: true, isSigner: false },
  101. {
  102. pubkey: SystemProgram.programId,
  103. isWritable: false,
  104. isSigner: false,
  105. },
  106. { pubkey: TOKEN_PROGRAM_ID, isWritable: false, isSigner: false },
  107. {
  108. pubkey: ASSOCIATED_TOKEN_PROGRAM_ID,
  109. isWritable: false,
  110. isSigner: false,
  111. },
  112. ])
  113. .rpc({ skipPreflight: true });
  114. console.log("Your transaction signature", tx);
  115. });
  116. });