test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. TOKEN_PROGRAM_ID,
  16. } from '@solana/spl-token';
  17. import * as borsh from "borsh";
  18. import { Buffer } from "buffer";
  19. function createKeypairFromFile(path: string): Keypair {
  20. return Keypair.fromSecretKey(
  21. Buffer.from(JSON.parse(require('fs').readFileSync(path, "utf-8")))
  22. )
  23. };
  24. class Assignable {
  25. constructor(properties) {
  26. Object.keys(properties).map((key) => {
  27. return (this[key] = properties[key]);
  28. });
  29. };
  30. };
  31. class CreateTokenArgs extends Assignable {
  32. toBuffer() {
  33. return Buffer.from(borsh.serialize(CreateTokenArgsSchema, this));
  34. }
  35. };
  36. const CreateTokenArgsSchema = new Map([
  37. [
  38. CreateTokenArgs, {
  39. kind: 'struct',
  40. fields: [
  41. ['token_title', 'string'],
  42. ['token_symbol', 'string'],
  43. ['token_uri', 'string'],
  44. ]
  45. }
  46. ]
  47. ]);
  48. describe("Create an SPL Token!", async () => {
  49. // const connection = new Connection(`http://localhost:8899`, 'confirmed');
  50. const connection = new Connection(`https://api.devnet.solana.com/`, 'confirmed');
  51. const payer = createKeypairFromFile(require('os').homedir() + '/.config/solana/id.json');
  52. const program = createKeypairFromFile('./program/target/deploy/program-keypair.json');
  53. const mintKeypair: Keypair = Keypair.generate();
  54. console.log(`New token: ${mintKeypair.publicKey}`);
  55. it("Create!", async () => {
  56. const metadataAddress = (PublicKey.findProgramAddressSync(
  57. [
  58. Buffer.from("metadata"),
  59. TOKEN_METADATA_PROGRAM_ID.toBuffer(),
  60. mintKeypair.publicKey.toBuffer(),
  61. ],
  62. TOKEN_METADATA_PROGRAM_ID
  63. ))[0];
  64. const metadataInstructionData = new CreateTokenArgs({
  65. token_title: "Solana Gold",
  66. token_symbol: "GOLDSOL",
  67. token_uri: "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json",
  68. });
  69. let ix = new TransactionInstruction({
  70. keys: [
  71. { pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true }, // Mint account
  72. { pubkey: payer.publicKey, isSigner: false, isWritable: true }, // Mint authority account
  73. { pubkey: metadataAddress, isSigner: false, isWritable: true }, // Metadata account
  74. { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // Payer
  75. { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, // Rent account
  76. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // System program
  77. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program
  78. { pubkey: TOKEN_METADATA_PROGRAM_ID, isSigner: false, isWritable: false }, // Token metadata program
  79. ],
  80. programId: program.publicKey,
  81. data: metadataInstructionData.toBuffer(),
  82. });
  83. const sx = await sendAndConfirmTransaction(
  84. connection,
  85. new Transaction().add(ix),
  86. [payer, mintKeypair]
  87. );
  88. console.log("Success!");
  89. console.log(` Mint Address: ${mintKeypair.publicKey}`);
  90. console.log(` Tx Signature: ${sx}`);
  91. });
  92. });