123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- import { Buffer } from 'node:buffer';
- import { describe, test } from 'node:test';
- import { PublicKey, Transaction, TransactionInstruction } from '@solana/web3.js';
- import * as borsh from 'borsh';
- import { start } from 'solana-bankrun';
- describe('Carnival', async () => {
- const PROGRAM_ID = PublicKey.unique();
- const context = await start([{ name: 'repository_layout_program', programId: PROGRAM_ID }], []);
- const client = context.banksClient;
- const payer = context.payer;
- class Assignable {
- constructor(properties) {
- Object.keys(properties).map((key) => {
- return (this[key] = properties[key]);
- });
- }
- }
- class CarnivalInstruction extends Assignable {
- toBuffer() {
- return Buffer.from(borsh.serialize(CarnivalInstructionSchema, this));
- }
- }
- const CarnivalInstructionSchema = new Map([
- [
- CarnivalInstruction,
- {
- kind: 'struct',
- fields: [
- ['name', 'string'],
- ['height', 'u32'],
- ['ticket_count', 'u32'],
- ['attraction', 'string'],
- ['attraction_name', 'string'],
- ],
- },
- ],
- ]);
- async function sendCarnivalInstructions(instructionsList: CarnivalInstruction[]) {
- const tx = new Transaction();
- for (const ix of instructionsList) {
- tx.recentBlockhash = context.lastBlockhash;
- tx.add(
- new TransactionInstruction({
- keys: [{ pubkey: payer.publicKey, isSigner: true, isWritable: true }],
- programId: PROGRAM_ID,
- data: ix.toBuffer(),
- }),
- ).sign(payer);
- }
- await client.processTransaction(tx);
- }
- test('Go on some rides!', async () => {
- await sendCarnivalInstructions([
- new CarnivalInstruction({
- name: 'Jimmy',
- height: 36,
- ticket_count: 15,
- attraction: 'ride',
- attraction_name: 'Scrambler',
- }),
- new CarnivalInstruction({
- name: 'Mary',
- height: 52,
- ticket_count: 1,
- attraction: 'ride',
- attraction_name: 'Ferris Wheel',
- }),
- new CarnivalInstruction({
- name: 'Alice',
- height: 56,
- ticket_count: 15,
- attraction: 'ride',
- attraction_name: 'Scrambler',
- }),
- new CarnivalInstruction({
- name: 'Bob',
- height: 49,
- ticket_count: 6,
- attraction: 'ride',
- attraction_name: 'Tilt-a-Whirl',
- }),
- ]);
- });
- test('Play some games!', async () => {
- await sendCarnivalInstructions([
- new CarnivalInstruction({
- name: 'Jimmy',
- height: 36,
- ticket_count: 15,
- attraction: 'game',
- attraction_name: 'I Got It!',
- }),
- new CarnivalInstruction({
- name: 'Mary',
- height: 52,
- ticket_count: 1,
- attraction: 'game',
- attraction_name: 'Ring Toss',
- }),
- new CarnivalInstruction({
- name: 'Alice',
- height: 56,
- ticket_count: 15,
- attraction: 'game',
- attraction_name: 'Ladder Climb',
- }),
- new CarnivalInstruction({
- name: 'Bob',
- height: 49,
- ticket_count: 6,
- attraction: 'game',
- attraction_name: 'Ring Toss',
- }),
- ]);
- });
- test('Eat some food!', async () => {
- await sendCarnivalInstructions([
- new CarnivalInstruction({
- name: 'Jimmy',
- height: 36,
- ticket_count: 15,
- attraction: 'food',
- attraction_name: 'Taco Shack',
- }),
- new CarnivalInstruction({
- name: 'Mary',
- height: 52,
- ticket_count: 1,
- attraction: 'food',
- attraction_name: "Larry's Pizza",
- }),
- new CarnivalInstruction({
- name: 'Alice',
- height: 56,
- ticket_count: 15,
- attraction: 'food',
- attraction_name: "Dough Boy's",
- }),
- new CarnivalInstruction({
- name: 'Bob',
- height: 49,
- ticket_count: 6,
- attraction: 'food',
- attraction_name: "Dough Boy's",
- }),
- ]);
- });
- });
|