|
@@ -16,7 +16,7 @@ function createKeypairFromFile(path: string): Keypair {
|
|
|
};
|
|
|
|
|
|
|
|
|
-describe("custom-instruction-data", () => {
|
|
|
+describe("Carnival", () => {
|
|
|
|
|
|
const connection = new Connection(`http://localhost:8899`, 'confirmed');
|
|
|
const payer = createKeypairFromFile(require('os').homedir() + '/.config/solana/id.json');
|
|
@@ -29,55 +29,148 @@ describe("custom-instruction-data", () => {
|
|
|
});
|
|
|
};
|
|
|
};
|
|
|
-
|
|
|
- class InstructionData extends Assignable {};
|
|
|
-
|
|
|
- const InstructionDataSchema = new Map([
|
|
|
+
|
|
|
+ class CarnivalInstruction extends Assignable {
|
|
|
+ toBuffer() {
|
|
|
+ return Buffer.from(borsh.serialize(CarnivalInstructionSchema, this));
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ const CarnivalInstructionSchema = new Map([
|
|
|
[
|
|
|
- InstructionData, {
|
|
|
+ CarnivalInstruction, {
|
|
|
kind: 'struct',
|
|
|
fields: [
|
|
|
['name', 'string'],
|
|
|
['height', 'u32'],
|
|
|
+ ['ticket_count', 'u32'],
|
|
|
+ ['attraction', 'string'],
|
|
|
+ ['attraction_name', 'string'],
|
|
|
]
|
|
|
}
|
|
|
]
|
|
|
]);
|
|
|
+
|
|
|
+ async function sendCarnivalInstructions(instructionsList: CarnivalInstruction[]) {
|
|
|
+ let tx = new Transaction();
|
|
|
+ for (var ix of instructionsList) {
|
|
|
+ tx.add(new TransactionInstruction({
|
|
|
+ keys: [
|
|
|
+ {pubkey: payer.publicKey, isSigner: true, isWritable: true}
|
|
|
+ ],
|
|
|
+ programId: program.publicKey,
|
|
|
+ data: ix.toBuffer(),
|
|
|
+ }));
|
|
|
+ };
|
|
|
+ await sendAndConfirmTransaction(
|
|
|
+ connection,
|
|
|
+ tx,
|
|
|
+ [payer]
|
|
|
+ );
|
|
|
+ }
|
|
|
|
|
|
- it("Go to the park!", async () => {
|
|
|
+ it("Go on some rides!", async () => {
|
|
|
|
|
|
- const jimmy = new InstructionData({
|
|
|
- name: "Jimmy",
|
|
|
- height: 3
|
|
|
- });
|
|
|
+ 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",
|
|
|
+ }),
|
|
|
+ ]);
|
|
|
+ });
|
|
|
|
|
|
- const mary = new InstructionData({
|
|
|
- name: "Mary",
|
|
|
- height: 10
|
|
|
- });
|
|
|
|
|
|
- function toBuffer(obj: InstructionData): Buffer {
|
|
|
- return Buffer.from(borsh.serialize(InstructionDataSchema, obj));
|
|
|
- }
|
|
|
+ it("Play some games!", async () => {
|
|
|
|
|
|
- let ix1 = new TransactionInstruction({
|
|
|
- keys: [
|
|
|
- {pubkey: payer.publicKey, isSigner: true, isWritable: true}
|
|
|
- ],
|
|
|
- programId: program.publicKey,
|
|
|
- data: toBuffer(jimmy),
|
|
|
- });
|
|
|
+ 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",
|
|
|
+ }),
|
|
|
+ ]);
|
|
|
+ });
|
|
|
|
|
|
- let ix2 = new TransactionInstruction({
|
|
|
- ...ix1,
|
|
|
- data: toBuffer(mary),
|
|
|
- });
|
|
|
|
|
|
- await sendAndConfirmTransaction(
|
|
|
- connection,
|
|
|
- new Transaction().add(ix1).add(ix2),
|
|
|
- [payer]
|
|
|
- );
|
|
|
+ it("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",
|
|
|
+ }),
|
|
|
+ ]);
|
|
|
});
|
|
|
});
|
|
|
|