-
Notifications
You must be signed in to change notification settings - Fork 103
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
fetchEvents
for LocalBlockchain
#323
Conversation
// used to match field values back to their original type | ||
let sortedEventTypes = Object.keys(this.events).sort(); | ||
|
||
return events.map((event: any) => { |
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.
It would probably be smarter to set new events at the beginning of the array instead of the end (reverse). How is it enforced on the protocol level/archive api?
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.
hmm, not sure what's better to use 🤔 yeah we should probably mirror whatever the graphql API ends up doing
fetchEvents
to LocalBlockchainfetchEvents
for LocalBlockchain
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.
nice work on your first PR! left a couple of suggestions
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.
Nice work Florian! I left some comments.
@prop pub: PublicKey; | ||
@prop value: Field; | ||
|
||
constructor(pub: PublicKey, value: Field) { |
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.
We had a discussion awhile back about adding a static from
method that invokes the original constructor instead of overriding the constructor in examples to encourage this pattern. Here is the discussion for background and here is an example.
What you are doing works (passing constructor arguments to super) but we want to encourage the .from
pattern to the community. @mitschabaude do you still think we should encourage this .from
pattern in our examples?
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.
Uhm, to be honest, I'm not sure. I think it's fine either way. My original idea was that you'd have to do nothing and use the generic constructor. .from
would've only been for the few cases where you'd want to transform the input arguments. However, by now I think a constructor like this, which is a no-op, is still nice to add, because it becomes properly typed
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.
That works for me. I'm fine leaving it as is if we are in agreement.
src/lib/mina.ts
Outdated
): Promise<any[]> { | ||
let eventsForAccount = | ||
events?.[publicKey.toBase58()]?.[Ledger.fieldToBase58(tokenId)]; | ||
return eventsForAccount === undefined ? [] : eventsForAccount; |
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.
return events?.[publicKey.toBase58()]?.[Ledger.fieldToBase58(tokenId)] ?? []
😉
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.
ahh, perfect
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.
Nothing to add that others haven't said for feedback, approving preemptively!
Great job! :D
Description
This is the first part of extending
LocalBlockchain
to supportfetchEvents
and later in a second PRfetchSequenceEvents
.Under the hood
A smart contract can emit events
but the
LocalBlockchain
did not store emitted events until now. After the transaction has successfully been applied to theLedger
viaapplyJsonTransaction
, all events are stored in a variableevents
. Events are identified bypublicKey
andtokenId
.LocalBlockchain
now exposes a closurefetchEvents()
, which returns all events that have been emitted.The
SmartContract
class has been extended by adding a new methodfetchEvents(start: UInt32, end?: UInt32): { type: string; event: AsFieldElements<any> }[]
, which returns a list of all events that have been emitted by aSmartContract
instance, using thetokenId
andpublicKey
as identifier andstart
andend
as range options.Note: There has been a discussion regarding what type to use in order to filter events. I went with using
slots
as an indicator for now.Example
Developers can now call the
fetchEvents(start: UInt32, end?: UInt32)
method on an instance of a smart contract that uses the LocalBlockchain as active instance.