Browse Source

Add more JS client tests

Loris Leiva 1 year ago
parent
commit
57dba55a7e

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

@@ -9,6 +9,7 @@ import {
   SolanaRpcSubscriptionsApi,
   TransactionSigner,
   airdropFactory,
+  appendTransactionInstruction,
   createSolanaRpc,
   createSolanaRpcSubscriptions,
   createTransaction,
@@ -21,6 +22,12 @@ import {
   setTransactionLifetimeUsingBlockhash,
   signTransactionWithSigners,
 } from '@solana/web3.js';
+import {
+  SYSTEM_PROGRAM_ADDRESS,
+  getCreateAccountInstruction,
+  getInitializeNonceAccountInstruction,
+  getNonceSize,
+} from '../src';
 
 type Client = {
   rpc: Rpc<SolanaRpcApi>;
@@ -76,3 +83,30 @@ export const signAndSendTransaction = async (
 export const getBalance = async (client: Client, address: Address) =>
   (await client.rpc.getBalance(address, { commitment: 'confirmed' }).send())
     .value;
+
+export const createNonceAccount = async (
+  client: Client,
+  payer: TransactionSigner,
+  nonce: TransactionSigner,
+  nonceAuthority: TransactionSigner
+) => {
+  const space = BigInt(getNonceSize());
+  const rent = await client.rpc.getMinimumBalanceForRentExemption(space).send();
+  const createAccount = getCreateAccountInstruction({
+    payer,
+    newAccount: nonce,
+    lamports: rent,
+    space,
+    programAddress: SYSTEM_PROGRAM_ADDRESS,
+  });
+  const initializeNonceAccount = getInitializeNonceAccountInstruction({
+    nonceAccount: nonce.address,
+    nonceAuthority: nonceAuthority.address,
+  });
+  await pipe(
+    await createDefaultTransaction(client, payer),
+    (tx) => appendTransactionInstruction(createAccount, tx),
+    (tx) => appendTransactionInstruction(initializeNonceAccount, tx),
+    (tx) => signAndSendTransaction(client, tx)
+  );
+};

+ 44 - 0
clients/js/test/advanceNonceAccount.test.ts

@@ -0,0 +1,44 @@
+import {
+  appendTransactionInstruction,
+  generateKeyPairSigner,
+  pipe,
+} from '@solana/web3.js';
+import test from 'ava';
+import { fetchNonce, getAdvanceNonceAccountInstruction } from '../src';
+import {
+  createDefaultSolanaClient,
+  createDefaultTransaction,
+  createNonceAccount,
+  generateKeyPairSignerWithSol,
+  signAndSendTransaction,
+} from './_setup';
+
+test('it advances the nonce account', async (t) => {
+  // Given an existing nonce account.
+  const client = createDefaultSolanaClient();
+  const [payer, nonce, authority] = await Promise.all([
+    generateKeyPairSignerWithSol(client),
+    generateKeyPairSigner(),
+    generateKeyPairSigner(),
+  ]);
+  await createNonceAccount(client, payer, nonce, authority);
+  const originalNonceAccount = await fetchNonce(client.rpc, nonce.address);
+
+  // When the authority advances the nonce account.
+  const createAccount = getAdvanceNonceAccountInstruction({
+    nonceAccount: nonce.address,
+    nonceAuthority: authority,
+  });
+  await pipe(
+    await createDefaultTransaction(client, payer),
+    (tx) => appendTransactionInstruction(createAccount, tx),
+    (tx) => signAndSendTransaction(client, tx)
+  );
+
+  // Then we expect the blockhash to have been updated.
+  const updatedNonceAccount = await fetchNonce(client.rpc, nonce.address);
+  t.not(
+    originalNonceAccount.data.blockhash,
+    updatedNonceAccount.data.blockhash
+  );
+});

+ 53 - 0
clients/js/test/createAccount.test.ts

@@ -0,0 +1,53 @@
+import {
+  appendTransactionInstruction,
+  fetchEncodedAccount,
+  generateKeyPairSigner,
+  pipe,
+} from '@solana/web3.js';
+import test from 'ava';
+import { SYSTEM_PROGRAM_ADDRESS, getCreateAccountInstruction } from '../src';
+import {
+  createDefaultSolanaClient,
+  createDefaultTransaction,
+  generateKeyPairSignerWithSol,
+  signAndSendTransaction,
+} from './_setup';
+
+test('it creates a new empty account', async (t) => {
+  // Given we have a newly generated account keypair to create with 42 bytes of space.
+  const client = createDefaultSolanaClient();
+  const space = 42n;
+  const [payer, newAccount, lamports] = await Promise.all([
+    generateKeyPairSignerWithSol(client),
+    generateKeyPairSigner(),
+    client.rpc.getMinimumBalanceForRentExemption(space).send(),
+  ]);
+
+  // When we call createAccount in a transaction.
+  const createAccount = getCreateAccountInstruction({
+    payer,
+    newAccount,
+    space,
+    lamports,
+    programAddress: SYSTEM_PROGRAM_ADDRESS,
+  });
+  await pipe(
+    await createDefaultTransaction(client, payer),
+    (tx) => appendTransactionInstruction(createAccount, tx),
+    (tx) => signAndSendTransaction(client, tx)
+  );
+
+  // Then we expect the following account data.
+  const fetchedAccount = await fetchEncodedAccount(
+    client.rpc,
+    newAccount.address
+  );
+  t.deepEqual(fetchedAccount, {
+    executable: false,
+    lamports,
+    programAddress: SYSTEM_PROGRAM_ADDRESS,
+    address: newAccount.address,
+    data: new Uint8Array(Array.from({ length: 42 }, () => 0)),
+    exists: true,
+  });
+});

+ 5 - 3
clients/js/test/initializeNonceAccount.test.ts

@@ -12,6 +12,7 @@ import {
   fetchNonce,
   getCreateAccountInstruction,
   getInitializeNonceAccountInstruction,
+  getNonceSize,
 } from '../src';
 import {
   createDefaultSolanaClient,
@@ -20,7 +21,7 @@ import {
   signAndSendTransaction,
 } from './_setup';
 
-test('it can create and initialize a durable nonce account', async (t) => {
+test('it creates and initialize a durable nonce account', async (t) => {
   // Given some brand now payer, authority, and nonce KeyPairSigners.
   const client = createDefaultSolanaClient();
   const payer = await generateKeyPairSignerWithSol(client);
@@ -28,12 +29,13 @@ test('it can create and initialize a durable nonce account', async (t) => {
   const nonceAuthority = await generateKeyPairSigner();
 
   // When we use them to create and initialize a nonce account.
-  const rent = await client.rpc.getMinimumBalanceForRentExemption(80n).send();
+  const space = BigInt(getNonceSize());
+  const rent = await client.rpc.getMinimumBalanceForRentExemption(space).send();
   const createAccount = getCreateAccountInstruction({
     payer,
     newAccount: nonce,
     lamports: rent,
-    space: 80,
+    space,
     programAddress: SYSTEM_PROGRAM_ADDRESS,
   });
   const initializeNonceAccount = getInitializeNonceAccountInstruction({

+ 2 - 2
clients/js/test/transferSol.test.ts

@@ -16,7 +16,7 @@ import {
   signAndSendTransaction,
 } from './_setup';
 
-test('it can transfer SOL from one account to another', async (t) => {
+test('it transfers SOL from one account to another', async (t) => {
   // Given a source account with 3 SOL and a destination account with no SOL.
   const client = createDefaultSolanaClient();
   const source = await generateKeyPairSignerWithSol(client, 3_000_000_000n);
@@ -43,7 +43,7 @@ test('it can transfer SOL from one account to another', async (t) => {
   t.is(await getBalance(client, destination), lamports(1_000_000_000n));
 });
 
-test('it can parse the accounts and data of an existing transfer SOL instruction', async (t) => {
+test('it parses the accounts and the data of an existing transfer SOL instruction', async (t) => {
   // Given a transfer SOL instruction with the following accounts and data.
   const source = await generateKeyPairSigner();
   const destination = (await generateKeyPairSigner()).address;