test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { Buffer } from 'node:buffer';
  2. import { describe, test } from 'node:test';
  3. import { PublicKey, Transaction, TransactionInstruction } from '@solana/web3.js';
  4. import * as borsh from 'borsh';
  5. import { start } from 'solana-bankrun';
  6. describe('Carnival', async () => {
  7. const PROGRAM_ID = PublicKey.unique();
  8. const context = await start([{ name: 'repository_layout_program', programId: PROGRAM_ID }], []);
  9. const client = context.banksClient;
  10. const payer = context.payer;
  11. class Assignable {
  12. constructor(properties) {
  13. Object.keys(properties).map((key) => {
  14. return (this[key] = properties[key]);
  15. });
  16. }
  17. }
  18. class CarnivalInstruction extends Assignable {
  19. toBuffer() {
  20. return Buffer.from(borsh.serialize(CarnivalInstructionSchema, this));
  21. }
  22. }
  23. const CarnivalInstructionSchema = new Map([
  24. [
  25. CarnivalInstruction,
  26. {
  27. kind: 'struct',
  28. fields: [
  29. ['name', 'string'],
  30. ['height', 'u32'],
  31. ['ticket_count', 'u32'],
  32. ['attraction', 'string'],
  33. ['attraction_name', 'string'],
  34. ],
  35. },
  36. ],
  37. ]);
  38. async function sendCarnivalInstructions(instructionsList: CarnivalInstruction[]) {
  39. const tx = new Transaction();
  40. for (const ix of instructionsList) {
  41. tx.recentBlockhash = context.lastBlockhash;
  42. tx.add(
  43. new TransactionInstruction({
  44. keys: [{ pubkey: payer.publicKey, isSigner: true, isWritable: true }],
  45. programId: PROGRAM_ID,
  46. data: ix.toBuffer(),
  47. }),
  48. ).sign(payer);
  49. }
  50. await client.processTransaction(tx);
  51. }
  52. test('Go on some rides!', async () => {
  53. await sendCarnivalInstructions([
  54. new CarnivalInstruction({
  55. name: 'Jimmy',
  56. height: 36,
  57. ticket_count: 15,
  58. attraction: 'ride',
  59. attraction_name: 'Scrambler',
  60. }),
  61. new CarnivalInstruction({
  62. name: 'Mary',
  63. height: 52,
  64. ticket_count: 1,
  65. attraction: 'ride',
  66. attraction_name: 'Ferris Wheel',
  67. }),
  68. new CarnivalInstruction({
  69. name: 'Alice',
  70. height: 56,
  71. ticket_count: 15,
  72. attraction: 'ride',
  73. attraction_name: 'Scrambler',
  74. }),
  75. new CarnivalInstruction({
  76. name: 'Bob',
  77. height: 49,
  78. ticket_count: 6,
  79. attraction: 'ride',
  80. attraction_name: 'Tilt-a-Whirl',
  81. }),
  82. ]);
  83. });
  84. test('Play some games!', async () => {
  85. await sendCarnivalInstructions([
  86. new CarnivalInstruction({
  87. name: 'Jimmy',
  88. height: 36,
  89. ticket_count: 15,
  90. attraction: 'game',
  91. attraction_name: 'I Got It!',
  92. }),
  93. new CarnivalInstruction({
  94. name: 'Mary',
  95. height: 52,
  96. ticket_count: 1,
  97. attraction: 'game',
  98. attraction_name: 'Ring Toss',
  99. }),
  100. new CarnivalInstruction({
  101. name: 'Alice',
  102. height: 56,
  103. ticket_count: 15,
  104. attraction: 'game',
  105. attraction_name: 'Ladder Climb',
  106. }),
  107. new CarnivalInstruction({
  108. name: 'Bob',
  109. height: 49,
  110. ticket_count: 6,
  111. attraction: 'game',
  112. attraction_name: 'Ring Toss',
  113. }),
  114. ]);
  115. });
  116. test('Eat some food!', async () => {
  117. await sendCarnivalInstructions([
  118. new CarnivalInstruction({
  119. name: 'Jimmy',
  120. height: 36,
  121. ticket_count: 15,
  122. attraction: 'food',
  123. attraction_name: 'Taco Shack',
  124. }),
  125. new CarnivalInstruction({
  126. name: 'Mary',
  127. height: 52,
  128. ticket_count: 1,
  129. attraction: 'food',
  130. attraction_name: "Larry's Pizza",
  131. }),
  132. new CarnivalInstruction({
  133. name: 'Alice',
  134. height: 56,
  135. ticket_count: 15,
  136. attraction: 'food',
  137. attraction_name: "Dough Boy's",
  138. }),
  139. new CarnivalInstruction({
  140. name: 'Bob',
  141. height: 49,
  142. ticket_count: 6,
  143. attraction: 'food',
  144. attraction_name: "Dough Boy's",
  145. }),
  146. ]);
  147. });
  148. });