spl-token-minter.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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(wallet.publicKey)
  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. wallet.publicKey, // owner of token account
  91. new anchor.BN(150) // amount to mint
  92. )
  93. .accounts({ dataAccount: dataAccount.publicKey })
  94. .remainingAccounts([
  95. {
  96. pubkey: wallet.publicKey,
  97. isWritable: true,
  98. isSigner: true,
  99. },
  100. { pubkey: tokenAccount.address, isWritable: true, isSigner: false },
  101. { pubkey: mintKeypair.publicKey, isWritable: true, isSigner: false },
  102. {
  103. pubkey: SystemProgram.programId,
  104. isWritable: false,
  105. isSigner: false,
  106. },
  107. { pubkey: TOKEN_PROGRAM_ID, isWritable: false, isSigner: false },
  108. {
  109. pubkey: ASSOCIATED_TOKEN_PROGRAM_ID,
  110. isWritable: false,
  111. isSigner: false,
  112. },
  113. ])
  114. .rpc({ skipPreflight: true })
  115. console.log("Your transaction signature", tx)
  116. })
  117. })