initializeNonceAccount.test.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import {
  2. appendTransactionInstruction,
  3. generateKeyPairSigner,
  4. pipe,
  5. } from '@solana/web3.js';
  6. import test from 'ava';
  7. import {
  8. Nonce,
  9. NonceState,
  10. NonceVersion,
  11. SYSTEM_PROGRAM_ADDRESS,
  12. fetchNonce,
  13. getCreateAccountInstruction,
  14. getInitializeNonceAccountInstruction,
  15. getNonceSize,
  16. } from '../src/index.js';
  17. import {
  18. createDefaultSolanaClient,
  19. createDefaultTransaction,
  20. generateKeyPairSignerWithSol,
  21. signAndSendTransaction,
  22. } from './_setup.js';
  23. test('it creates and initialize a durable nonce account', async (t) => {
  24. // Given some brand now payer, authority, and nonce KeyPairSigners.
  25. const client = createDefaultSolanaClient();
  26. const payer = await generateKeyPairSignerWithSol(client);
  27. const nonce = await generateKeyPairSigner();
  28. const nonceAuthority = await generateKeyPairSigner();
  29. // When we use them to create and initialize a nonce account.
  30. const space = BigInt(getNonceSize());
  31. const rent = await client.rpc.getMinimumBalanceForRentExemption(space).send();
  32. const createAccount = getCreateAccountInstruction({
  33. payer,
  34. newAccount: nonce,
  35. lamports: rent,
  36. space,
  37. programAddress: SYSTEM_PROGRAM_ADDRESS,
  38. });
  39. const initializeNonceAccount = getInitializeNonceAccountInstruction({
  40. nonceAccount: nonce.address,
  41. nonceAuthority: nonceAuthority.address,
  42. });
  43. await pipe(
  44. await createDefaultTransaction(client, payer),
  45. (tx) => appendTransactionInstruction(createAccount, tx),
  46. (tx) => appendTransactionInstruction(initializeNonceAccount, tx),
  47. (tx) => signAndSendTransaction(client, tx)
  48. );
  49. // Then we expect the nonce account to exist with the following data.
  50. t.like(await fetchNonce(client.rpc, nonce.address), <Nonce>{
  51. address: nonce.address,
  52. data: {
  53. version: NonceVersion.Current,
  54. state: NonceState.Initialized,
  55. authority: nonceAuthority.address,
  56. },
  57. });
  58. });