Переглянути джерело

Add transfer test for JS client

Loris Leiva 1 рік тому
батько
коміт
50bc090407

+ 35 - 0
clients/js/test/_setup.ts

@@ -28,6 +28,7 @@ import {
   getInitializeAccountInstruction,
   getInitializeMintInstruction,
   getMintSize,
+  getMintToInstruction,
   getTokenSize,
 } from '../src/index.js';
 
@@ -153,3 +154,37 @@ export const createToken = async (
 
   return token.address;
 };
+
+export const createTokenWithAmount = async (
+  client: Client,
+  payer: TransactionSigner,
+  mintAuthority: TransactionSigner,
+  mint: Address,
+  owner: Address,
+  amount: bigint
+): 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 }),
+    getMintToInstruction({ mint, token: token.address, mintAuthority, amount }),
+  ];
+  await pipe(
+    transactionMessage,
+    (tx) => appendTransactionMessageInstructions(instructions, tx),
+    (tx) => signAndSendTransaction(client, tx)
+  );
+
+  return token.address;
+};

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

@@ -30,7 +30,6 @@ test('it mints tokens to a token account', async (t) => {
   ]);
   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({

+ 70 - 0
clients/js/test/transfer.test.ts

@@ -0,0 +1,70 @@
+import {
+  appendTransactionMessageInstruction,
+  generateKeyPairSigner,
+  pipe,
+} from '@solana/web3.js';
+import test from 'ava';
+import {
+  Mint,
+  Token,
+  fetchMint,
+  fetchToken,
+  getTransferInstruction,
+} from '../src/index.js';
+import {
+  createDefaultSolanaClient,
+  createDefaultTransaction,
+  createMint,
+  createToken,
+  createTokenWithAmount,
+  generateKeyPairSignerWithSol,
+  signAndSendTransaction,
+} from './_setup.js';
+
+test('it transfers tokens from one account to another', async (t) => {
+  // Given a mint account and two token accounts.
+  // One with 100 tokens and the other with 0 tokens.
+  const client = createDefaultSolanaClient();
+  const [payer, mintAuthority, ownerA, ownerB] = await Promise.all([
+    generateKeyPairSignerWithSol(client),
+    generateKeyPairSigner(),
+    generateKeyPairSigner(),
+    generateKeyPairSigner(),
+  ]);
+  const mint = await createMint(client, payer, mintAuthority.address);
+  const [tokenA, tokenB] = await Promise.all([
+    createTokenWithAmount(
+      client,
+      payer,
+      mintAuthority,
+      mint,
+      ownerA.address,
+      100n
+    ),
+    createToken(client, payer, mint, ownerB.address),
+  ]);
+
+  // When owner A transfers 50 tokens to owner B.
+  const transfer = getTransferInstruction({
+    source: tokenA,
+    destination: tokenB,
+    authority: ownerA,
+    amount: 50n,
+  });
+  await pipe(
+    await createDefaultTransaction(client, payer),
+    (tx) => appendTransactionMessageInstruction(transfer, tx),
+    (tx) => signAndSendTransaction(client, tx)
+  );
+
+  // Then we expect the mint and token accounts to have the following updated data.
+  const [{ data: mintData }, { data: tokenDataA }, { data: tokenDataB }] =
+    await Promise.all([
+      fetchMint(client.rpc, mint),
+      fetchToken(client.rpc, tokenA),
+      fetchToken(client.rpc, tokenB),
+    ]);
+  t.like(mintData, <Mint>{ supply: 100n });
+  t.like(tokenDataA, <Token>{ amount: 50n });
+  t.like(tokenDataB, <Token>{ amount: 50n });
+});