initializeNonceAccount.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. } from '../src';
  16. import {
  17. createDefaultSolanaClient,
  18. createDefaultTransaction,
  19. generateKeyPairSignerWithSol,
  20. signAndSendTransaction,
  21. } from './_setup';
  22. test('it can create and initialize a durable nonce account', async (t) => {
  23. // Given some brand now payer, authority, and nonce KeyPairSigners.
  24. const client = createDefaultSolanaClient();
  25. const payer = await generateKeyPairSignerWithSol(client);
  26. const nonce = await generateKeyPairSigner();
  27. const nonceAuthority = await generateKeyPairSigner();
  28. // When we use them to create and initialize a nonce account.
  29. const rent = await client.rpc.getMinimumBalanceForRentExemption(80n).send();
  30. const createAccount = getCreateAccountInstruction({
  31. payer,
  32. newAccount: nonce,
  33. lamports: rent,
  34. space: 80,
  35. programAddress: SYSTEM_PROGRAM_ADDRESS,
  36. });
  37. const initializeNonceAccount = getInitializeNonceAccountInstruction({
  38. nonceAccount: nonce.address,
  39. nonceAuthority: nonceAuthority.address,
  40. });
  41. await pipe(
  42. await createDefaultTransaction(client, payer),
  43. (tx) => appendTransactionInstruction(createAccount, tx),
  44. (tx) => appendTransactionInstruction(initializeNonceAccount, tx),
  45. (tx) => signAndSendTransaction(client, tx)
  46. );
  47. // Then we expect the nonce account to exist with the following data.
  48. t.like(await fetchNonce(client.rpc, nonce.address), <Nonce>{
  49. address: nonce.address,
  50. data: {
  51. version: NonceVersion.Current,
  52. state: NonceState.Initialized,
  53. authority: nonceAuthority.address,
  54. },
  55. });
  56. });