counter.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 {
  11. bignum
  12. } from '@metaplex-foundation/beet';
  13. import { assert } from 'chai';
  14. import { BN } from 'bn.js';
  15. import {
  16. createIncrementInstruction,
  17. Counter,
  18. PROGRAM_ID,
  19. } from '../ts';
  20. function convertBignumToNumber(bignum: bignum): number {
  21. return new BN(bignum).toNumber();
  22. }
  23. describe("Counter Solana Native", () => {
  24. const connection = new Connection("http://localhost:8899");
  25. it("Test allocate counter + increment tx", async () => {
  26. // Randomly generate our wallet
  27. const payerKeypair = Keypair.generate();
  28. const payer = payerKeypair.publicKey;
  29. // Randomly generate the account key
  30. // to sign for setting up the Counter state
  31. const counterKeypair = Keypair.generate();
  32. const counter = counterKeypair.publicKey;
  33. // Airdrop our wallet 1 Sol
  34. await connection.requestAirdrop(payer, LAMPORTS_PER_SOL);
  35. // Create a TransactionInstruction to interact with our counter program
  36. const allocIx: TransactionInstruction = SystemProgram.createAccount({
  37. fromPubkey: payer,
  38. newAccountPubkey: counter,
  39. lamports: await connection.getMinimumBalanceForRentExemption(Counter.byteSize),
  40. space: Counter.byteSize,
  41. programId: PROGRAM_ID
  42. })
  43. const incrementIx: TransactionInstruction = createIncrementInstruction({ counter });
  44. let tx = new Transaction().add(allocIx).add(incrementIx);
  45. // Explicitly set the feePayer to be our wallet (this is set to first signer by default)
  46. tx.feePayer = payer;
  47. // Fetch a "timestamp" so validators know this is a recent transaction
  48. tx.recentBlockhash = (await connection.getLatestBlockhash('confirmed')).blockhash;
  49. // Send transaction to network (local network)
  50. await sendAndConfirmTransaction(
  51. connection,
  52. tx,
  53. [payerKeypair, counterKeypair],
  54. { skipPreflight: true, commitment: 'confirmed' }
  55. );
  56. // Get the counter account info from network
  57. let count = (await Counter.fromAccountAddress(connection, counter)).count;
  58. assert((new BN(count)).toNumber() === 1, "Expected count to have been 1");
  59. console.log(`[alloc+increment] count is: ${count}`);
  60. });
  61. it("Test allocate tx and increment tx", async () => {
  62. const payerKeypair = Keypair.generate();
  63. const payer = payerKeypair.publicKey;
  64. const counterKeypair = Keypair.generate();
  65. const counter = counterKeypair.publicKey;
  66. await connection.requestAirdrop(payer, LAMPORTS_PER_SOL);
  67. // Check allocate tx
  68. const allocIx: TransactionInstruction = SystemProgram.createAccount({
  69. fromPubkey: payer,
  70. newAccountPubkey: counter,
  71. lamports: await connection.getMinimumBalanceForRentExemption(Counter.byteSize),
  72. space: Counter.byteSize,
  73. programId: PROGRAM_ID
  74. })
  75. let tx = new Transaction().add(allocIx);
  76. tx.feePayer = payer;
  77. tx.recentBlockhash = (await connection.getLatestBlockhash('confirmed')).blockhash;
  78. await sendAndConfirmTransaction(
  79. connection,
  80. tx,
  81. [payerKeypair, counterKeypair],
  82. { skipPreflight: true, commitment: 'confirmed' }
  83. );
  84. let count = (await Counter.fromAccountAddress(connection, counter)).count;
  85. assert(convertBignumToNumber(count) === 0, "Expected count to have been 0");
  86. console.log(`[allocate] count is: ${count}`);
  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. count = (await Counter.fromAccountAddress(connection, counter)).count;
  99. assert(convertBignumToNumber(count) === 1, "Expected count to have been 1");
  100. console.log(`[increment] count is: ${count}`);
  101. })
  102. })