Skip to content

Commit

Permalink
Merge pull request #825 from o1-labs/fix/events-transaction-info
Browse files Browse the repository at this point in the history
Change fetchEvents to return transactionInfo inside events type
  • Loading branch information
MartinMinkov committed Apr 4, 2023
2 parents f460877 + 1665606 commit f585b64
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Fixed

- Improved Event Handling in SnarkyJS https://github.com/o1-labs/snarkyjs/pull/825
- Updated the internal event type to better handle events emitted in different zkApp transactions and when multiple zkApp transactions are present within a block.
- The internal event type now includes event data and transaction information as separate objects, allowing for more accurate information about each event and its associated transaction.
- Removed multiple best tip blocks when fetching action data https://github.com/o1-labs/snarkyjs/pull/817
- Implemented a temporary fix that filters out multiple best tip blocks, if they exist, while fetching actions. This fix will be removed once the related issue in the Archive-Node-API repository (https://github.com/o1-labs/Archive-Node-API/issues/7) is resolved.

Expand Down
30 changes: 16 additions & 14 deletions src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,12 +545,12 @@ type FetchedEvents = {
parentHash: string;
chainStatus: string;
};
transactionInfo: {
hash: string;
memo: string;
status: string;
};
eventData: {
transactionInfo: {
hash: string;
memo: string;
status: string;
};
data: string[];
}[];
};
Expand Down Expand Up @@ -593,12 +593,12 @@ const getEventsQuery = (
parentHash
chainStatus
}
transactionInfo {
hash
memo
status
}
eventData {
transactionInfo {
hash
memo
status
}
data
}
}
Expand Down Expand Up @@ -692,7 +692,12 @@ async function fetchEvents(
}

return fetchedEvents.map((event) => {
let events = event.eventData.map((eventData) => eventData.data);
let events = event.eventData.map(({ data, transactionInfo }) => {
return {
data,
transactionInfo,
};
});

return {
events,
Expand All @@ -701,9 +706,6 @@ async function fetchEvents(
parentBlockHash: event.blockInfo.parentHash,
globalSlot: UInt32.from(event.blockInfo.globalSlotSinceGenesis),
chainStatus: event.blockInfo.chainStatus,
transactionHash: event.transactionInfo.hash,
transactionStatus: event.transactionInfo.status,
transactionMemo: event.transactionInfo.memo,
};
});
}
Expand Down
15 changes: 11 additions & 4 deletions src/lib/mina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,18 +475,25 @@ function LocalBlockchain({
if (events[addr][tokenId] === undefined) {
events[addr][tokenId] = [];
}
let updatedEvents = p.body.events.map((data) => {
return {
data,
transactionInfo: {
transactionHash: '',
transactionStatus: '',
transactionMemo: '',
},
};
});
events[addr][tokenId].push({
events: p.body.events,
events: updatedEvents,
blockHeight: networkState.blockchainLength,
globalSlot: networkState.globalSlotSinceGenesis,
// The following fields are fetched from the Mina network. For now, we mock these values out
// since networkState does not contain these fields.
blockHash: '',
parentBlockHash: '',
chainStatus: '',
transactionHash: '',
transactionStatus: '',
transactionMemo: '',
});
}

Expand Down
36 changes: 27 additions & 9 deletions src/lib/zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1069,15 +1069,19 @@ super.init();
): Promise<
{
type: string;
event: ProvablePure<any>;
event: {
data: ProvablePure<any>;
transactionInfo: {
transactionHash: string;
transactionStatus: string;
transactionMemo: string;
};
};
blockHeight: UInt32;
blockHash: string;
parentBlockHash: string;
globalSlot: UInt32;
chainStatus: string;
transactionHash: string;
transactionStatus: string;
transactionMemo: string;
}[]
> {
// filters all elements so that they are within the given range
Expand Down Expand Up @@ -1114,26 +1118,40 @@ super.init();
if (sortedEventTypes.length === 1) {
let type = sortedEventTypes[0];
let event = this.events[type].fromFields(
eventData.event.map((f: string) => Field(f))
eventData.event.data.map((f: string) => Field(f))
);
return {
...eventData,
type,
event,
event: {
data: event,
transactionInfo: {
transactionHash: eventData.event.transactionInfo.hash,
transactionStatus: eventData.event.transactionInfo.status,
transactionMemo: eventData.event.transactionInfo.memo,
},
},
};
} else {
// if there are multiple events we have to use the index event[0] to find the exact event type
let eventObjectIndex = Number(eventData.event[0]);
let eventObjectIndex = Number(eventData.event.data[0]);
let type = sortedEventTypes[eventObjectIndex];
// all other elements of the array are values used to construct the original object, we can drop the first value since its just an index
let eventProps = eventData.event.slice(1);
let eventProps = eventData.event.data.slice(1);
let event = this.events[type].fromFields(
eventProps.map((f: string) => Field(f))
);
return {
...eventData,
type,
event,
event: {
data: event,
transactionInfo: {
transactionHash: eventData.event.transactionInfo.hash,
transactionStatus: eventData.event.transactionInfo.status,
transactionMemo: eventData.event.transactionInfo.memo,
},
},
};
}
});
Expand Down

0 comments on commit f585b64

Please sign in to comment.