initializeNonceAccount.test.ts 1.9 KB

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