-
Notifications
You must be signed in to change notification settings - Fork 753
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
feat: add support for parsing emitted events #1227
base: develop
Are you sure you want to change the base?
Conversation
Hello, |
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.
lgtm!
It looks like we lost part of the information about events's block and tx after parsing the event. |
Hey, sorry, I can create issue if necessary. Issue this solves is situation where you get events via Only issue is how are these new keys ordered so backwards compatibility is not accidentally broken in code that uses |
The object key order used to be undefined, but when people kept relying on the JS engine behaviour that preserved the order it was eventually codified in the language specification. So non-numeric string keys, like the ones from this PR, should be ordered by their insertion order. |
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.
Just a question.
src/utils/events/index.ts
Outdated
@@ -194,57 +194,62 @@ function mergeAbiEvents(target: any, source: any): Object { | |||
* ``` | |||
*/ | |||
export function parseEvents( | |||
providerReceivedEvents: RPC.Event[], | |||
providerReceivedEvents: RPC.EmittedEvent[] | RPC.Event[], |
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.
Why RPC.EmittedEvent[] | RPC.Event[]
?
Isn't it only RPC.EmittedEvent[]
?
In which case are block_hash, block_number & transaction_hash not included?
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.
Block/tx info is not included in a case where you first fetch transaction receipt with starknet_getTransactionReceipt
. In those kinds of reponses RPC.Event
struct is returned (see: this). On the other hand when fetching data with starknet_fetchEvents
returned struct is RPC.EmittedEvents
.
So I thought it would be nice to leave that as is in case someone had code like this
events.parseEvents(receipts.map(t => t.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.
I think that the root cause of this wrong input type of events.parseEvents()
is due to Contract.parseEvent()
. This method is calling events.parseEvents()
with RPC.event
format ; here is the root cause of all this mess.
I think that you have to modify Contract.parseEvent()
:
- if the event is included in a block, this type has to be sent to
events.parseEvents()
:
type EMITTED_EVENT = EVENT & {
block_hash: BLOCK_HASH;
block_number: BLOCK_NUMBER;
transaction_hash: TXN_HASH;
};
- if the event is in a PENDING transaction, this type has to be sent to
events.parseEvents()
:
type EMITTED_EVENT = EVENT & {
transaction_hash: TXN_HASH;
};
events.parseEvents()
has to accept both types as input.
RPC.Event[]
type was here only to handle Contract.parseEvent()
; it's useless for users. They uses EVENTS_CHUNK
type (result of RpcProvider.getEvents()
), that includes EmittedEvent
type. Never RPC.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.
Not sure how pending transaction have anything to do with this. AFAIK events are created and emitted during transaction execution. So there is always tx hash, block hash and block number corresponding to the event. They are missing from events array of the recipiet because that is always available in parent (receipt) object.
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.
If PENDING is not appropriate, then we have still a problem in Contract.parseEvents() :
starknet.js/src/contract/default.ts
Line 333 in 47e52cf
(receipt as InvokeTransactionReceiptResponse).events?.filter( |
InvokeTransactionReceiptResponse
type is not the good one, because it's made of INVOKE_TXN_RECEIPT | PENDING_INVOKE_TXN_RECEIPT
And in this case, only
EMITTED_EVENT
should be transfered from Contract.parseEvent()
to events.parseEvents()
(not RPC.event
).Comment about uselessness of
RPC.event
as input of events.parseEvents()
remains valid.
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 if I understood correctly, you would like Contract.parseEvent()
to change and look like this?
public parseEvents(receipt: GetTransactionReceiptResponse): ParsedEvents {
return parseRawEvents(
(receipt as InvokeTransactionReceiptResponse).events?.
map(event => { return {
block_hash: (receipt as INVOKE_TXN_RECEIPT).block_hash,
block_number: (receipt as INVOKE_TXN_RECEIPT).block_number,
transaction_hash: (receipt as INVOKE_TXN_RECEIPT).transaction_hash,
...event
}})
.filter(
(event) => cleanHex(event.from_address) === cleanHex(this.address),
[]
) || [],
this.events,
this.structs,
CallData.getAbiEnum(this.abi)
);
}
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.
Yes, this way.
I tested successfully this :
contract/default.ts :
public parseEvents(receipt: GetTransactionReceiptResponse): ParsedEvents {
let parsed: ParsedEvents;
receipt.match({
success: (txR: SuccessfulTransactionReceiptResponse) => {
const emittedEvents =
(txR as InvokeTransactionReceiptResponse).events
?.map((event) => {
return {
block_hash: (txR as INVOKE_TXN_RECEIPT).block_hash,
block_number: (txR as INVOKE_TXN_RECEIPT).block_number,
transaction_hash: (txR as INVOKE_TXN_RECEIPT).transaction_hash,
...event,
};
})
.filter((event) => cleanHex(event.from_address) === cleanHex(this.address), []) || [];
parsed = parseRawEvents(
emittedEvents,
this.events,
this.structs,
CallData.getAbiEnum(this.abi)
);
},
_: () => {
throw Error('This transaction was not successful.');
},
});
return parsed!;
}
events/index.ts :
export function parseEvents(
providerReceivedEvents: RPC.EmittedEvent[],
abiEvents: AbiEvents,
abiStructs: AbiStructs,
abiEnums: AbiEnums
): ParsedEvents {
...
I have implemented my last message, to finalize this PR. |
Motivation and Resolution
provider.getEvents
method returns list ofEmittedEvents
which contains fields for block number, block hash and transaction hash. Parsing list of those events withparseEvents
would drop those fields. This PR adds them back to the returned array if they are available.RPC version (if applicable)
...
Usage related changes
Users now use additional data from parsed events structure.
Development related changes
N/A
Checklist: