-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update default query for zkapp fetch actions to not add params #1794
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There may be something I don't understand here. I ran an example script with an initialized smart contract and got the events payload from the graphQL query, then the script failed here because How does a smart contract get its There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
class Contract extends SmartContract {
events = { ... } // HERE (note: this is executed when a new instance is created)
} I think you could also just throw an error in this function if the Anyway, if you keep this case @45930, it would be nice to have it share most of the code with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The point of having SmartContract-specific methods instead of generic Mina methods is usually to make use of the custom type layout that a SC defines, for example for 8-field state and here for events There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mitschabaude so in my example, I am using the mina NFT contract and this logs out
But the smart contract does emit events, e.g.
Though the developer did not define the events property, the contract may still emit events, so I would expect an instance of it to be able to fetch its own events. If the user must set this value, or we recommend to use The original code checks for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should probably make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah, we already do this, so that makes the decision easier. I only got myself into this state by transcribing a smart contract incorrectly, and not including the events definition. I will update this case to throw an error instead so that it matches, but still gives a better error message than the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New error message would be:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mitschabaude do the changes in 82dcbe1 look good to you? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. beautiful! |
||
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)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing this filter because it is a duplicate of the filter applied by the archive node. Was it intentional to leave this as insurance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, this implementation was written when the archive node API was still in an early WIP phase and simply always returned the full list of events
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so, good to remove