Browse Source

Update test.ts

Valentin Madrid 2 years ago
parent
commit
2519af1e52
1 changed files with 141 additions and 145 deletions
  1. 141 145
      tokens/create-token/native/tests/test.ts

+ 141 - 145
tokens/create-token/native/tests/test.ts

@@ -1,163 +1,159 @@
-import { PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID } from "@metaplex-foundation/mpl-token-metadata";
+import { 
+    PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID
+} from '@metaplex-foundation/mpl-token-metadata';
 import {
-  Connection,
-  Keypair,
-  PublicKey,
-  SystemProgram,
-  SYSVAR_RENT_PUBKEY,
-  TransactionInstruction,
-  Transaction,
-  sendAndConfirmTransaction,
-} from "@solana/web3.js";
-import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
+    Connection,
+    Keypair,
+    PublicKey,
+    SystemProgram,
+    SYSVAR_RENT_PUBKEY,
+    TransactionInstruction,
+    Transaction,
+    sendAndConfirmTransaction,
+} from '@solana/web3.js';
+import {
+    TOKEN_PROGRAM_ID,
+} from '@solana/spl-token';
 import * as borsh from "borsh";
 import { Buffer } from "buffer";
 
+
 function createKeypairFromFile(path: string): Keypair {
-  return Keypair.fromSecretKey(
-    Buffer.from(JSON.parse(require("fs").readFileSync(path, "utf-8")))
-  );
-}
+    return Keypair.fromSecretKey(
+        Buffer.from(JSON.parse(require('fs').readFileSync(path, "utf-8")))
+    )
+};
+
 
 class Assignable {
-  constructor(properties) {
-    Object.keys(properties).map((key) => {
-      return (this[key] = properties[key]);
-    });
-  }
-}
+    constructor(properties) {
+        Object.keys(properties).map((key) => {
+            return (this[key] = properties[key]);
+        });
+    };
+};
 
 class CreateTokenArgs extends Assignable {
-  toBuffer() {
-    return Buffer.from(borsh.serialize(CreateTokenArgsSchema, this));
-  }
-}
+    toBuffer() {
+        return Buffer.from(borsh.serialize(CreateTokenArgsSchema, this));
+    }
+};
 const CreateTokenArgsSchema = new Map([
-  [
-    CreateTokenArgs,
-    {
-      kind: "struct",
-      fields: [
-        ["token_title", "string"],
-        ["token_symbol", "string"],
-        ["token_uri", "string"],
-        ["token_decimals", "u8"],
-      ],
-    },
-  ],
+    [
+        CreateTokenArgs, {
+            kind: 'struct',
+            fields: [
+                ['token_title', 'string'],
+                ['token_symbol', 'string'],
+                ['token_uri', 'string'],
+                ['token_decimals', 'u8'],
+            ]
+        }
+    ]
 ]);
 
-describe("Create Tokens!", async () => {
-  const connection = new Connection(`http://localhost:8899`, "confirmed");
-  // const connection = new Connection(`https://api.devnet.solana.com/`, 'confirmed');
-  const payer = createKeypairFromFile(
-    require("os").homedir() + "/.config/solana/id.json"
-  );
-  const program = createKeypairFromFile(
-    "./program/target/deploy/program-keypair.json"
-  );
-
-  it("Create an SPL Token!", async () => {
-    const mintKeypair: Keypair = Keypair.generate();
-
-    const metadataAddress = PublicKey.findProgramAddressSync(
-      [
-        Buffer.from("metadata"),
-        TOKEN_METADATA_PROGRAM_ID.toBuffer(),
-        mintKeypair.publicKey.toBuffer(),
-      ],
-      TOKEN_METADATA_PROGRAM_ID
-    )[0];
-
-    // SPL Token default = 9 decimals
-    //
-    const instructionData = new CreateTokenArgs({
-      token_title: "Solana Gold",
-      token_symbol: "GOLDSOL",
-      token_uri:
-        "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json",
-      token_decimals: 9,
-    });
 
-    let ix = new TransactionInstruction({
-      keys: [
-        { pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true }, // Mint account
-        { pubkey: payer.publicKey, isSigner: false, isWritable: true }, // Mint authority account
-        { pubkey: metadataAddress, isSigner: false, isWritable: true }, // Metadata account
-        { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // Payer
-        { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, // Rent account
-        { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // System program
-        { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program
-        {
-          pubkey: TOKEN_METADATA_PROGRAM_ID,
-          isSigner: false,
-          isWritable: false,
-        }, // Token metadata program
-      ],
-      programId: program.publicKey,
-      data: instructionData.toBuffer(),
-    });
-
-    const sx = await sendAndConfirmTransaction(
-      connection,
-      new Transaction().add(ix),
-      [payer, mintKeypair]
-    );
-
-    console.log("Success!");
-    console.log(`   Mint Address: ${mintKeypair.publicKey}`);
-    console.log(`   Tx Signature: ${sx}`);
-  });
+describe("Create Tokens!", async () => {
 
-  it("Create an NFT!", async () => {
-    const mintKeypair: Keypair = Keypair.generate();
-
-    const metadataAddress = PublicKey.findProgramAddressSync(
-      [
-        Buffer.from("metadata"),
-        TOKEN_METADATA_PROGRAM_ID.toBuffer(),
-        mintKeypair.publicKey.toBuffer(),
-      ],
-      TOKEN_METADATA_PROGRAM_ID
-    )[0];
-
-    // NFT default = 0 decimals
-    //
-    const instructionData = new CreateTokenArgs({
-      token_title: "Homer NFT",
-      token_symbol: "HOMR",
-      token_uri:
-        "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.json",
-      token_decimals: 9,
+    // const connection = new Connection(`http://localhost:8899`, 'confirmed');
+    const connection = new Connection(`https://api.devnet.solana.com/`, 'confirmed');
+    const payer = createKeypairFromFile(require('os').homedir() + '/.config/solana/id.json');
+    const program = createKeypairFromFile('./program/target/deploy/program-keypair.json');
+
+    it("Create an SPL Token!", async () => {
+
+        const mintKeypair: Keypair = Keypair.generate();
+
+        const metadataAddress = (PublicKey.findProgramAddressSync(
+            [
+              Buffer.from("metadata"),
+              TOKEN_METADATA_PROGRAM_ID.toBuffer(),
+              mintKeypair.publicKey.toBuffer(),
+            ],
+            TOKEN_METADATA_PROGRAM_ID
+        ))[0];
+        
+        // SPL Token default = 9 decimals
+        //
+        const instructionData = new CreateTokenArgs({
+            token_title: "Solana Gold",
+            token_symbol: "GOLDSOL",
+            token_uri: "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json",
+            token_decimals: 9,
+        });
+
+        let ix = new TransactionInstruction({
+            keys: [
+                { pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true },            // Mint account
+                { pubkey: payer.publicKey, isSigner: false, isWritable: true },                 // Mint authority account
+                { pubkey: metadataAddress, isSigner: false, isWritable: true },                 // Metadata account
+                { pubkey: payer.publicKey, isSigner: true, isWritable: true },                  // Payer
+                { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },             // Rent account
+                { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },        // System program
+                { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },               // Token program
+                { pubkey: TOKEN_METADATA_PROGRAM_ID, isSigner: false, isWritable: false },      // Token metadata program
+            ],
+            programId: program.publicKey,
+            data: instructionData.toBuffer(),
+        });
+
+        const sx = await sendAndConfirmTransaction(
+            connection, 
+            new Transaction().add(ix),
+            [payer, mintKeypair]
+        );
+
+        console.log("Success!");
+        console.log(`   Mint Address: ${mintKeypair.publicKey}`);
+        console.log(`   Tx Signature: ${sx}`);
     });
 
-    let ix = new TransactionInstruction({
-      keys: [
-        { pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true }, // Mint account
-        { pubkey: payer.publicKey, isSigner: false, isWritable: true }, // Mint authority account
-        { pubkey: metadataAddress, isSigner: false, isWritable: true }, // Metadata account
-        { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // Payer
-        { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, // Rent account
-        { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // System program
-        { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program
-        {
-          pubkey: TOKEN_METADATA_PROGRAM_ID,
-          isSigner: false,
-          isWritable: false,
-        }, // Token metadata program
-      ],
-      programId: program.publicKey,
-      data: instructionData.toBuffer(),
+    it("Create an NFT!", async () => {
+
+        const mintKeypair: Keypair = Keypair.generate();
+
+        const metadataAddress = (PublicKey.findProgramAddressSync(
+            [
+              Buffer.from("metadata"),
+              TOKEN_METADATA_PROGRAM_ID.toBuffer(),
+              mintKeypair.publicKey.toBuffer(),
+            ],
+            TOKEN_METADATA_PROGRAM_ID
+        ))[0];
+        
+        // NFT default = 0 decimals
+        //
+        const instructionData = new CreateTokenArgs({
+            token_title: "Homer NFT",
+            token_symbol: "HOMR",
+            token_uri: "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.json",
+            token_decimals: 9,
+        });
+
+        let ix = new TransactionInstruction({
+            keys: [
+                { pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true },            // Mint account
+                { pubkey: payer.publicKey, isSigner: false, isWritable: true },                 // Mint authority account
+                { pubkey: metadataAddress, isSigner: false, isWritable: true },                 // Metadata account
+                { pubkey: payer.publicKey, isSigner: true, isWritable: true },                  // Payer
+                { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },             // Rent account
+                { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },        // System program
+                { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },               // Token program
+                { pubkey: TOKEN_METADATA_PROGRAM_ID, isSigner: false, isWritable: false },      // Token metadata program
+            ],
+            programId: program.publicKey,
+            data: instructionData.toBuffer(),
+        });
+
+        const sx = await sendAndConfirmTransaction(
+            connection, 
+            new Transaction().add(ix),
+            [payer, mintKeypair]
+        );
+
+        console.log("Success!");
+        console.log(`   Mint Address: ${mintKeypair.publicKey}`);
+        console.log(`   Tx Signature: ${sx}`);
     });
-
-    const sx = await sendAndConfirmTransaction(
-      connection,
-      new Transaction().add(ix),
-      [payer, mintKeypair]
-    );
-
-    console.log("Success!");
-    console.log(`   Mint Address: ${mintKeypair.publicKey}`);
-    console.log(`   Tx Signature: ${sx}`);
   });
-});
+