create-token.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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, // freeze authority
  40. 9, // decimals for the mint
  41. tokenTitle, // token name
  42. tokenSymbol, // token symbol
  43. tokenUri // token uri (off-chain metadata)
  44. )
  45. .accounts({
  46. payer: wallet.publicKey, //payer
  47. mint: mintKeypair.publicKey, // mint
  48. metadata: metadataAddress, // metadata address
  49. mintAuthority: wallet.publicKey, // mint authority
  50. rentAddress: SYSVAR_RENT_PUBKEY,
  51. metadataProgramId: new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),
  52. })
  53. .signers([mintKeypair])
  54. .rpc({ skipPreflight: true });
  55. console.log("Your transaction signature", tx);
  56. });
  57. it("Create an NFT!", async () => {
  58. // Generate a new keypair for the mint
  59. const mintKeypair = anchor.web3.Keypair.generate();
  60. // Get the metadata address for the mint
  61. const metaplex = Metaplex.make(connection);
  62. const metadataAddress = await metaplex
  63. .nfts()
  64. .pdas()
  65. .metadata({ mint: mintKeypair.publicKey });
  66. const tx = await program.methods
  67. .createTokenMint(
  68. wallet.publicKey,
  69. 0, // decimals for the mint, 0 for NFTs "non-fungible"
  70. tokenTitle,
  71. tokenSymbol,
  72. tokenUri
  73. )
  74. .accounts({
  75. payer: wallet.publicKey, //payer
  76. mint: mintKeypair.publicKey, // mint
  77. metadata: metadataAddress, // metadata address
  78. mintAuthority: wallet.publicKey, // mint authority
  79. rentAddress: SYSVAR_RENT_PUBKEY,
  80. metadataProgramId: new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),
  81. })
  82. .signers([mintKeypair])
  83. .rpc({ skipPreflight: true });
  84. console.log("Your transaction signature", tx);
  85. });
  86. });