create-token.ts 4.2 KB

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