test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import {
  2. PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID
  3. } from '@metaplex-foundation/mpl-token-metadata';
  4. import {
  5. Connection,
  6. Keypair,
  7. PublicKey,
  8. SystemProgram,
  9. SYSVAR_RENT_PUBKEY,
  10. TransactionInstruction,
  11. Transaction,
  12. sendAndConfirmTransaction,
  13. } from '@solana/web3.js';
  14. import {
  15. ASSOCIATED_TOKEN_PROGRAM_ID,
  16. getAssociatedTokenAddress,
  17. TOKEN_PROGRAM_ID,
  18. } from '@solana/spl-token';
  19. import { Buffer } from "buffer";
  20. import {
  21. CreateTokenArgs,
  22. MintToArgs,
  23. SplMinterInstruction,
  24. } from './instructions';
  25. import { BN } from 'bn.js';
  26. function createKeypairFromFile(path: string): Keypair {
  27. return Keypair.fromSecretKey(
  28. Buffer.from(JSON.parse(require('fs').readFileSync(path, "utf-8")))
  29. )
  30. };
  31. describe("SPL Token Minter", async () => {
  32. // const connection = new Connection(`http://localhost:8899`, 'confirmed');
  33. const connection = new Connection(`https://api.devnet.solana.com/`, 'confirmed');
  34. const payer = createKeypairFromFile(require('os').homedir() + '/.config/solana/id.json');
  35. const program = createKeypairFromFile('./program/target/deploy/program-keypair.json');
  36. const mintKeypair: Keypair = Keypair.generate();
  37. it("Create an SPL Token!", async () => {
  38. const metadataAddress = (PublicKey.findProgramAddressSync(
  39. [
  40. Buffer.from("metadata"),
  41. TOKEN_METADATA_PROGRAM_ID.toBuffer(),
  42. mintKeypair.publicKey.toBuffer(),
  43. ],
  44. TOKEN_METADATA_PROGRAM_ID
  45. ))[0];
  46. const instructionData = new CreateTokenArgs({
  47. instruction: SplMinterInstruction.Create,
  48. token_title: "Solana Gold",
  49. token_symbol: "GOLDSOL",
  50. token_uri: "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json",
  51. });
  52. let ix = new TransactionInstruction({
  53. keys: [
  54. { pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true }, // Mint account
  55. { pubkey: payer.publicKey, isSigner: false, isWritable: true }, // Mint authority account
  56. { pubkey: metadataAddress, isSigner: false, isWritable: true }, // Metadata account
  57. { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // Payer
  58. { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, // Rent account
  59. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // System program
  60. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program
  61. { pubkey: TOKEN_METADATA_PROGRAM_ID, isSigner: false, isWritable: false }, // Token metadata program
  62. ],
  63. programId: program.publicKey,
  64. data: instructionData.toBuffer(),
  65. });
  66. const sx = await sendAndConfirmTransaction(
  67. connection,
  68. new Transaction().add(ix),
  69. [payer, mintKeypair]
  70. );
  71. console.log("Success!");
  72. console.log(` Mint Address: ${mintKeypair.publicKey}`);
  73. console.log(` Tx Signature: ${sx}`);
  74. });
  75. it("Mint some tokens to your wallet!", async () => {
  76. const associatedTokenAccountAddress = await getAssociatedTokenAddress(
  77. mintKeypair.publicKey,
  78. payer.publicKey,
  79. );
  80. const instructionData = new MintToArgs({
  81. instruction: SplMinterInstruction.Mint,
  82. quantity: new BN(150),
  83. });
  84. let ix = new TransactionInstruction({
  85. keys: [
  86. { pubkey: mintKeypair.publicKey, isSigner: false, isWritable: true }, // Mint account
  87. { pubkey: payer.publicKey, isSigner: false, isWritable: true }, // Mint authority account
  88. { pubkey: associatedTokenAccountAddress, isSigner: false, isWritable: true }, // ATA
  89. { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // Payer
  90. { pubkey: SystemProgram.programId, isSigner: false, isWritable: true }, // System program
  91. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program
  92. { pubkey: ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // Token metadata program
  93. ],
  94. programId: program.publicKey,
  95. data: instructionData.toBuffer(),
  96. });
  97. const sx = await sendAndConfirmTransaction(
  98. connection,
  99. new Transaction().add(ix),
  100. [payer],
  101. );
  102. console.log("Success!");
  103. console.log(` ATA Address: ${associatedTokenAccountAddress}`);
  104. console.log(` Tx Signature: ${sx}`);
  105. });
  106. });