counter.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {
  2. Keypair,
  3. PublicKey,
  4. SystemProgram,
  5. Transaction,
  6. TransactionInstruction
  7. } from '@solana/web3.js';
  8. import { assert } from 'chai';
  9. import { describe, test } from 'node:test';
  10. import { start } from 'solana-bankrun';
  11. import {
  12. COUNTER_ACCOUNT_SIZE,
  13. createIncrementInstruction,
  14. deserializeCounterAccount,
  15. PROGRAM_ID
  16. } from '../ts';
  17. describe('Counter Solana Native', async () => {
  18. // Randomly generate the program keypair and load the program to solana-bankrun
  19. const context = await start([{ name: 'counter_solana_native', programId: PROGRAM_ID }],[]);
  20. const client = context.banksClient;
  21. // Get the payer keypair from the context, this will be used to sign transactions with enough lamports
  22. const payer = context.payer;
  23. // Get the rent object to calculate rent for the accounts
  24. const rent = await client.getRent();
  25. test('Test allocate counter + increment tx', async () => {
  26. // Randomly generate the account key
  27. // to sign for setting up the Counter state
  28. const counterKeypair = Keypair.generate();
  29. const counter = counterKeypair.publicKey;
  30. // Create a TransactionInstruction to interact with our counter program
  31. const allocIx: TransactionInstruction = SystemProgram.createAccount({
  32. fromPubkey: payer.publicKey,
  33. newAccountPubkey: counter,
  34. lamports: Number(rent.minimumBalance(BigInt(COUNTER_ACCOUNT_SIZE))),
  35. space: COUNTER_ACCOUNT_SIZE,
  36. programId: PROGRAM_ID,
  37. });
  38. const incrementIx: TransactionInstruction = createIncrementInstruction({ counter },{});
  39. let tx = new Transaction().add(allocIx).add(incrementIx);
  40. // Explicitly set the feePayer to be our wallet (this is set to first signer by default)
  41. tx.feePayer = payer.publicKey;
  42. // Fetch a "timestamp" so validators know this is a recent transaction
  43. const blockhash = context.lastBlockhash;
  44. tx.recentBlockhash = blockhash;
  45. // Sign the transaction with the payer's keypair
  46. tx.sign(payer, counterKeypair);
  47. // Send transaction to bankrun
  48. await client.processTransaction(tx);
  49. // Get the counter account info from network
  50. const counterAccountInfo = await client.getAccount(counter);
  51. assert(counterAccountInfo, 'Expected counter account to have been created');
  52. // Deserialize the counter & check count has been incremented
  53. const counterAccount = deserializeCounterAccount(Buffer.from(counterAccountInfo.data));
  54. assert(counterAccount.count.toNumber() === 1,'Expected count to have been 1');
  55. console.log(`[alloc+increment] count is: ${counterAccount.count.toNumber()}`);
  56. });
  57. test('Test allocate tx and increment tx', async () => {
  58. const counterKeypair = Keypair.generate();
  59. const counter = counterKeypair.publicKey;
  60. // Check allocate tx
  61. const allocIx: TransactionInstruction = SystemProgram.createAccount({
  62. fromPubkey: payer.publicKey,
  63. newAccountPubkey: counter,
  64. lamports:Number(rent.minimumBalance(BigInt(COUNTER_ACCOUNT_SIZE))),
  65. space: COUNTER_ACCOUNT_SIZE,
  66. programId: PROGRAM_ID,
  67. });
  68. let tx = new Transaction().add(allocIx);
  69. const blockhash = context.lastBlockhash;
  70. tx.feePayer = payer.publicKey;
  71. tx.recentBlockhash = blockhash;
  72. tx.sign(payer, counterKeypair);
  73. await client.processTransaction(tx);
  74. let counterAccountInfo = await client.getAccount(counter);
  75. assert(counterAccountInfo, 'Expected counter account to have been created');
  76. let counterAccount = deserializeCounterAccount(Buffer.from(counterAccountInfo.data));
  77. assert(counterAccount.count.toNumber() === 0,'Expected count to have been 0');
  78. console.log(`[allocate] count is: ${counterAccount.count.toNumber()}`);
  79. // Check increment tx
  80. const incrementIx: TransactionInstruction = createIncrementInstruction({ counter },{});
  81. tx = new Transaction().add(incrementIx);
  82. tx.feePayer = payer.publicKey;
  83. tx.recentBlockhash = blockhash;
  84. tx.sign(payer);
  85. await client.processTransaction(tx);
  86. counterAccountInfo = await client.getAccount(counter);
  87. assert(counterAccountInfo, 'Expected counter account to have been created');
  88. counterAccount = deserializeCounterAccount(Buffer.from(counterAccountInfo.data));
  89. assert(counterAccount.count.toNumber() === 1,'Expected count to have been 1');
  90. console.log(`[increment] count is: ${counterAccount.count.toNumber()}`);
  91. });
  92. });