counter.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import {
  2. Connection,
  3. Keypair,
  4. LAMPORTS_PER_SOL,
  5. Transaction,
  6. TransactionInstruction,
  7. sendAndConfirmTransaction,
  8. SystemProgram
  9. } from '@solana/web3.js';
  10. import { assert } from 'chai';
  11. import {
  12. createIncrementInstruction,
  13. deserializeCounterAccount,
  14. Counter,
  15. COUNTER_ACCOUNT_SIZE,
  16. PROGRAM_ID,
  17. } from '../ts';
  18. describe("Counter Solana Native", () => {
  19. const connection = new Connection("http://localhost:8899");
  20. it("Test allocate counter + increment tx", async () => {
  21. // Randomly generate our wallet
  22. const payerKeypair = Keypair.generate();
  23. const payer = payerKeypair.publicKey;
  24. // Randomly generate the account key
  25. // to sign for setting up the Counter state
  26. const counterKeypair = Keypair.generate();
  27. const counter = counterKeypair.publicKey;
  28. // Airdrop our wallet 1 Sol
  29. await connection.requestAirdrop(payer, LAMPORTS_PER_SOL);
  30. // Create a TransactionInstruction to interact with our counter program
  31. const allocIx: TransactionInstruction = SystemProgram.createAccount({
  32. fromPubkey: payer,
  33. newAccountPubkey: counter,
  34. lamports: await connection.getMinimumBalanceForRentExemption(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;
  42. // Fetch a "timestamp" so validators know this is a recent transaction
  43. tx.recentBlockhash = (await connection.getLatestBlockhash('confirmed')).blockhash;
  44. // Send transaction to network (local network)
  45. await sendAndConfirmTransaction(
  46. connection,
  47. tx,
  48. [payerKeypair, counterKeypair],
  49. { skipPreflight: true, commitment: 'confirmed' }
  50. );
  51. // Get the counter account info from network
  52. const counterAccountInfo = await connection.getAccountInfo(counter, { commitment: "confirmed" });
  53. assert(counterAccountInfo, "Expected counter account to have been created");
  54. // Deserialize the counter & check count has been incremented
  55. const counterAccount = deserializeCounterAccount(counterAccountInfo.data);
  56. assert(counterAccount.count.toNumber() === 1, "Expected count to have been 1");
  57. console.log(`[alloc+increment] count is: ${counterAccount.count.toNumber()}`);
  58. });
  59. it("Test allocate tx and increment tx", async () => {
  60. const payerKeypair = Keypair.generate();
  61. const payer = payerKeypair.publicKey;
  62. const counterKeypair = Keypair.generate();
  63. const counter = counterKeypair.publicKey;
  64. await connection.requestAirdrop(payer, LAMPORTS_PER_SOL);
  65. // Check allocate tx
  66. const allocIx: TransactionInstruction = SystemProgram.createAccount({
  67. fromPubkey: payer,
  68. newAccountPubkey: counter,
  69. lamports: await connection.getMinimumBalanceForRentExemption(COUNTER_ACCOUNT_SIZE),
  70. space: COUNTER_ACCOUNT_SIZE,
  71. programId: PROGRAM_ID
  72. })
  73. let tx = new Transaction().add(allocIx);
  74. tx.feePayer = payer;
  75. tx.recentBlockhash = (await connection.getLatestBlockhash('confirmed')).blockhash;
  76. await sendAndConfirmTransaction(
  77. connection,
  78. tx,
  79. [payerKeypair, counterKeypair],
  80. { skipPreflight: true, commitment: 'confirmed' }
  81. );
  82. let counterAccountInfo = await connection.getAccountInfo(counter, { commitment: "confirmed" });
  83. assert(counterAccountInfo, "Expected counter account to have been created");
  84. let counterAccount = deserializeCounterAccount(counterAccountInfo.data);
  85. assert(counterAccount.count.toNumber() === 0, "Expected count to have been 0");
  86. console.log(`[allocate] count is: ${counterAccount.count.toNumber()}`);
  87. // Check increment tx
  88. const incrementIx: TransactionInstruction = createIncrementInstruction({ counter }, {});
  89. tx = new Transaction().add(incrementIx);
  90. tx.feePayer = payer;
  91. tx.recentBlockhash = (await connection.getLatestBlockhash('confirmed')).blockhash;
  92. await sendAndConfirmTransaction(
  93. connection,
  94. tx,
  95. [payerKeypair],
  96. { skipPreflight: true, commitment: 'confirmed' }
  97. );
  98. counterAccountInfo = await connection.getAccountInfo(counter, { commitment: "confirmed" });
  99. assert(counterAccountInfo, "Expected counter account to have been created");
  100. counterAccount = deserializeCounterAccount(counterAccountInfo.data);
  101. assert(counterAccount.count.toNumber() === 1, "Expected count to have been 1");
  102. console.log(`[increment] count is: ${counterAccount.count.toNumber()}`);
  103. })
  104. })