test.ts 5.0 KB

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