test.ts 3.9 KB

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