From ef13fa1082d60d525d2248ee498e697fb7551d4f Mon Sep 17 00:00:00 2001 From: Coby Date: Tue, 20 Aug 2024 16:24:51 -0400 Subject: [PATCH 1/2] update default query for zkapp fetch actions to not add params --- src/lib/mina/zkapp.ts | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/lib/mina/zkapp.ts b/src/lib/mina/zkapp.ts index 7b58bfc361..48efce327c 100644 --- a/src/lib/mina/zkapp.ts +++ b/src/lib/mina/zkapp.ts @@ -16,6 +16,7 @@ import { AccountUpdateLayout, AccountUpdateTree, } from './account-update.js'; +import type { EventActionFilterOptions } from './graphql.js'; import { cloneCircuitValue, FlexibleProvablePure, @@ -1058,21 +1059,18 @@ super.init(); chainStatus: string; }[] > { + const queryFilterOptions: EventActionFilterOptions = {}; + if(start.greaterThan(UInt32.from(0)).toBoolean()) { + queryFilterOptions.from = start; + } + if(end) { + queryFilterOptions.to = end; + } // filters all elements so that they are within the given range // only returns { type: "", event: [] } in a flat format let events = ( - await Mina.fetchEvents(this.address, this.self.body.tokenId, { - from: start, - to: end, - }) + await Mina.fetchEvents(this.address, this.self.body.tokenId, queryFilterOptions) ) - .filter((eventData) => { - let height = UInt32.from(eventData.blockHeight); - return end === undefined - ? start.lessThanOrEqual(height).toBoolean() - : start.lessThanOrEqual(height).toBoolean() && - height.lessThanOrEqual(end).toBoolean(); - }) .map((event) => { return event.events.map((eventData) => { let { events, ...rest } = event; @@ -1088,8 +1086,23 @@ super.init(); let sortedEventTypes = Object.keys(this.events).sort(); return events.map((eventData) => { + // If we don't know what types of events this zkapp may emit, then return the raw data + if (sortedEventTypes.length === 0) { + return { + ...eventData, + type: "Object", + event: { + data: eventData.event.data, + transactionInfo: { + transactionHash: eventData.event.transactionInfo.hash, + transactionStatus: eventData.event.transactionInfo.status, + transactionMemo: eventData.event.transactionInfo.memo, + }, + }, + }; + } // if there is only one event type, the event structure has no index and can directly be matched to the event type - if (sortedEventTypes.length === 1) { + else if (sortedEventTypes.length === 1) { let type = sortedEventTypes[0]; let event = this.events[type].fromFields( eventData.event.data.map((f: string) => Field(f)) From 82dcbe177c3c53c9da7b996983242d5b83459f34 Mon Sep 17 00:00:00 2001 From: Coby Date: Wed, 28 Aug 2024 17:25:17 -0400 Subject: [PATCH 2/2] updating error message on no events defined case --- src/lib/mina/zkapp.ts | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/lib/mina/zkapp.ts b/src/lib/mina/zkapp.ts index 3a89d3a63e..09409367a1 100644 --- a/src/lib/mina/zkapp.ts +++ b/src/lib/mina/zkapp.ts @@ -1092,6 +1092,20 @@ super.init(); chainStatus: string; }[] > { + // used to match field values back to their original type + const sortedEventTypes = Object.keys(this.events).sort(); + if (sortedEventTypes.length === 0) { + throw Error( + 'fetchEvents: You are trying to fetch events without having declared the types of your events.\n' + + `Make sure to add a property \`events\` on ${this.constructor.name}, for example: \n` + + `class ${this.constructor.name} extends SmartContract {\n` + + ` events = { 'my-event': Field }\n` + + `}\n` + + `Or, if you want to access the events from the zkapp account ${this.address.toBase58()} without casting their types\n` + + `then try Mina.fetchEvents('${this.address.toBase58()}') instead.` + ); + } + const queryFilterOptions: EventActionFilterOptions = {}; if(start.greaterThan(UInt32.from(0)).toBoolean()) { queryFilterOptions.from = start; @@ -1115,27 +1129,9 @@ super.init(); }) .flat(); - // used to match field values back to their original type - let sortedEventTypes = Object.keys(this.events).sort(); - return events.map((eventData) => { - // If we don't know what types of events this zkapp may emit, then return the raw data - if (sortedEventTypes.length === 0) { - return { - ...eventData, - type: "Object", - event: { - data: eventData.event.data, - transactionInfo: { - transactionHash: eventData.event.transactionInfo.hash, - transactionStatus: eventData.event.transactionInfo.status, - transactionMemo: eventData.event.transactionInfo.memo, - }, - }, - }; - } // if there is only one event type, the event structure has no index and can directly be matched to the event type - else if (sortedEventTypes.length === 1) { + if (sortedEventTypes.length === 1) { let type = sortedEventTypes[0]; let event = this.events[type].fromFields( eventData.event.data.map((f: string) => Field(f))