test.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import * as anchor from "@project-serum/anchor";
  2. import { MintTokenTo } from "../target/types/mint_token_to";
  3. const TOKEN_METADATA_PROGRAM_ID = new anchor.web3.PublicKey(
  4. "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"
  5. );
  6. describe("mint-token", () => {
  7. const provider = anchor.AnchorProvider.env();
  8. anchor.setProvider(provider);
  9. const payer = provider.wallet as anchor.Wallet;
  10. const program = anchor.workspace.MintTokenTo as anchor.Program<MintTokenTo>;
  11. const mintKeypair: anchor.web3.Keypair = anchor.web3.Keypair.generate();
  12. console.log(`New token: ${mintKeypair.publicKey}`);
  13. const testTokenTitle = "Solana Gold";
  14. const testTokenSymbol = "GOLDSOL";
  15. const testTokenUri = "https://raw.githubusercontent.com/solana-developers/program-examples/main/tokens/token_metadata.json";
  16. it("Mint!", async () => {
  17. const metadataAddress = (await anchor.web3.PublicKey.findProgramAddress(
  18. [
  19. Buffer.from("metadata"),
  20. TOKEN_METADATA_PROGRAM_ID.toBuffer(),
  21. mintKeypair.publicKey.toBuffer(),
  22. ],
  23. TOKEN_METADATA_PROGRAM_ID
  24. ))[0];
  25. // Transact with the "create_token_mint" function in our on-chain program
  26. //
  27. await program.methods.createTokenMint(
  28. testTokenTitle, testTokenSymbol, testTokenUri
  29. )
  30. .accounts({
  31. metadataAccount: metadataAddress,
  32. mintAccount: mintKeypair.publicKey,
  33. mintAuthority: payer.publicKey,
  34. systemProgram: anchor.web3.SystemProgram.programId,
  35. tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
  36. tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
  37. })
  38. .signers([payer.payer, mintKeypair])
  39. .rpc();
  40. });
  41. it("Mint to a wallet!", async () => {
  42. const amountToMint = 1;
  43. const tokenAddress = await anchor.utils.token.associatedAddress({
  44. mint: mintKeypair.publicKey,
  45. owner: payer.publicKey
  46. });
  47. // Transact with the "mint_to_wallet" function in our on-chain program
  48. //
  49. await program.methods.mintToWallet(new anchor.BN(amountToMint))
  50. .accounts({
  51. mintAccount: mintKeypair.publicKey,
  52. tokenAccount: tokenAddress,
  53. mintAuthority: payer.publicKey,
  54. systemProgram: anchor.web3.SystemProgram.programId,
  55. tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
  56. associatedTokenProgram: anchor.utils.token.ASSOCIATED_PROGRAM_ID,
  57. })
  58. .signers([payer.payer, mintKeypair])
  59. .rpc();
  60. });
  61. });