瀏覽代碼

Add mintTo test for JS client

Loris Leiva 1 年之前
父節點
當前提交
0eda740a95
共有 3 個文件被更改,包括 101 次插入10 次删除
  1. 41 6
      clients/js/test/_setup.ts
  2. 5 4
      clients/js/test/initializeAccount.test.ts
  3. 55 0
      clients/js/test/mintTo.test.ts

+ 41 - 6
clients/js/test/_setup.ts

@@ -1,3 +1,4 @@
+import { getCreateAccountInstruction } from '@solana-program/system';
 import {
   Address,
   Commitment,
@@ -24,10 +25,11 @@ import {
 } from '@solana/web3.js';
 import {
   TOKEN_PROGRAM_ADDRESS,
+  getInitializeAccountInstruction,
   getInitializeMintInstruction,
   getMintSize,
+  getTokenSize,
 } from '../src/index.js';
-import { getCreateAccountInstruction } from '@solana-program/system';
 
 type Client = {
   rpc: Rpc<SolanaRpcApi>;
@@ -88,17 +90,19 @@ export const getBalance = async (client: Client, address: Address) =>
 
 export const createMint = async (
   client: Client,
-  authority: TransactionSigner
+  payer: TransactionSigner,
+  mintAuthority: Address,
+  decimals: number = 0
 ): Promise<Address> => {
   const space = BigInt(getMintSize());
   const [transactionMessage, rent, mint] = await Promise.all([
-    createDefaultTransaction(client, authority),
+    createDefaultTransaction(client, payer),
     client.rpc.getMinimumBalanceForRentExemption(space).send(),
     generateKeyPairSigner(),
   ]);
   const instructions = [
     getCreateAccountInstruction({
-      payer: authority,
+      payer,
       newAccount: mint,
       lamports: rent,
       space,
@@ -106,8 +110,8 @@ export const createMint = async (
     }),
     getInitializeMintInstruction({
       mint: mint.address,
-      decimals: 2,
-      mintAuthority: authority.address,
+      decimals,
+      mintAuthority,
     }),
   ];
   await pipe(
@@ -118,3 +122,34 @@ export const createMint = async (
 
   return mint.address;
 };
+
+export const createToken = async (
+  client: Client,
+  payer: TransactionSigner,
+  mint: Address,
+  owner: Address
+): Promise<Address> => {
+  const space = BigInt(getTokenSize());
+  const [transactionMessage, rent, token] = await Promise.all([
+    createDefaultTransaction(client, payer),
+    client.rpc.getMinimumBalanceForRentExemption(space).send(),
+    generateKeyPairSigner(),
+  ]);
+  const instructions = [
+    getCreateAccountInstruction({
+      payer,
+      newAccount: token,
+      lamports: rent,
+      space,
+      programAddress: TOKEN_PROGRAM_ADDRESS,
+    }),
+    getInitializeAccountInstruction({ account: token.address, mint, owner }),
+  ];
+  await pipe(
+    transactionMessage,
+    (tx) => appendTransactionMessageInstructions(instructions, tx),
+    (tx) => signAndSendTransaction(client, tx)
+  );
+
+  return token.address;
+};

+ 5 - 4
clients/js/test/initializeAccount.test.ts

@@ -27,19 +27,20 @@ test('it creates and initializes a new token account', async (t) => {
   // Given a mint account, its mint authority and two generated keypairs
   // for the token to be created and its owner.
   const client = createDefaultSolanaClient();
-  const [mintAuthority, token, owner] = await Promise.all([
+  const [payer, mintAuthority, token, owner] = await Promise.all([
     generateKeyPairSignerWithSol(client),
     generateKeyPairSigner(),
     generateKeyPairSigner(),
+    generateKeyPairSigner(),
   ]);
-  const mint = await createMint(client, mintAuthority);
+  const mint = await createMint(client, payer, mintAuthority.address);
 
   // When we create and initialize a token account at this address.
   const space = BigInt(getTokenSize());
   const rent = await client.rpc.getMinimumBalanceForRentExemption(space).send();
   const instructions = [
     getCreateAccountInstruction({
-      payer: mintAuthority,
+      payer,
       newAccount: token,
       lamports: rent,
       space,
@@ -52,7 +53,7 @@ test('it creates and initializes a new token account', async (t) => {
     }),
   ];
   await pipe(
-    await createDefaultTransaction(client, mintAuthority),
+    await createDefaultTransaction(client, payer),
     (tx) => appendTransactionMessageInstructions(instructions, tx),
     (tx) => signAndSendTransaction(client, tx)
   );

+ 55 - 0
clients/js/test/mintTo.test.ts

@@ -0,0 +1,55 @@
+import {
+  appendTransactionMessageInstruction,
+  generateKeyPairSigner,
+  pipe,
+} from '@solana/web3.js';
+import test from 'ava';
+import {
+  Mint,
+  Token,
+  fetchMint,
+  fetchToken,
+  getMintToInstruction,
+} from '../src/index.js';
+import {
+  createDefaultSolanaClient,
+  createDefaultTransaction,
+  createMint,
+  createToken,
+  generateKeyPairSignerWithSol,
+  signAndSendTransaction,
+} from './_setup.js';
+
+test('it mints tokens to a token account', async (t) => {
+  // Given a mint account and a token account.
+  const client = createDefaultSolanaClient();
+  const [payer, mintAuthority, owner] = await Promise.all([
+    generateKeyPairSignerWithSol(client),
+    generateKeyPairSigner(),
+    generateKeyPairSigner(),
+  ]);
+  const mint = await createMint(client, payer, mintAuthority.address);
+  const token = await createToken(client, payer, mint, owner.address);
+  await new Promise((resolve) => setTimeout(resolve, 2000));
+
+  // When the mint authority mints tokens to the token account.
+  const mintTo = getMintToInstruction({
+    mint,
+    token,
+    mintAuthority,
+    amount: 100n,
+  });
+  await pipe(
+    await createDefaultTransaction(client, payer),
+    (tx) => appendTransactionMessageInstruction(mintTo, tx),
+    (tx) => signAndSendTransaction(client, tx)
+  );
+
+  // Then we expect the mint and token accounts to have the following updated data.
+  const [{ data: mintData }, { data: tokenData }] = await Promise.all([
+    fetchMint(client.rpc, mint),
+    fetchToken(client.rpc, token),
+  ]);
+  t.like(mintData, <Mint>{ supply: 100n });
+  t.like(tokenData, <Token>{ amount: 100n });
+});