create.test.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import {
  2. Account,
  3. appendTransactionMessageInstruction,
  4. pipe,
  5. } from '@solana/web3.js';
  6. import test from 'ava';
  7. import {
  8. Counter,
  9. fetchCounterFromSeeds,
  10. getCreateInstructionAsync,
  11. } from '../src/index.js';
  12. import {
  13. createDefaultSolanaClient,
  14. createDefaultTransaction,
  15. generateKeyPairSignerWithSol,
  16. signAndSendTransaction,
  17. } from './_setup.js';
  18. test('it creates a new counter account', async (t) => {
  19. // Given an authority key pair with some SOL.
  20. const client = createDefaultSolanaClient();
  21. const authority = await generateKeyPairSignerWithSol(client);
  22. // When we create a new counter account.
  23. const createIx = await getCreateInstructionAsync({ authority });
  24. await pipe(
  25. await createDefaultTransaction(client, authority),
  26. (tx) => appendTransactionMessageInstruction(createIx, tx),
  27. (tx) => signAndSendTransaction(client, tx)
  28. );
  29. // Then we expect the counter account to exist and have a value of 0.
  30. const counter = await fetchCounterFromSeeds(client.rpc, {
  31. authority: authority.address,
  32. });
  33. t.like(counter, <Account<Counter>>{
  34. data: {
  35. authority: authority.address,
  36. value: 0,
  37. },
  38. });
  39. });