Migrate IexecAccessors tests to ethers v6 - #189
Conversation
| expect(task.status).to.equal(TaskStatusEnum.ACTIVE); | ||
| expect(task.dealid).to.equal(dealId); | ||
| expect(task.idx).to.equal(taskIndex); | ||
| expect(task.timeref).to.equal(timeRef); |
There was a problem hiding this comment.
why remove that ?
There was a problem hiding this comment.
Good catch, some old testing, updated with Re-add timeref check, thanks 👍
| return ethers.utils._TypedDataEncoder.hashDomain({ | ||
| //TODO: Move to utils | ||
| export async function hashDomain(domain: IexecLibOrders_v5.EIP712DomainStructOutput) { | ||
| return ethers.TypedDataEncoder.hashDomain({ |
There was a problem hiding this comment.
| return ethers.TypedDataEncoder.hashDomain({ | |
| return TypedDataEncoder.hashDomain({ |
other option: directly import TypedDataEncoder module
There was a problem hiding this comment.
Sure see Import from ethers (same comment everywhere)
| function extractEvents( | ||
| logs: Log[], | ||
| address: string, | ||
| eventName: string, | ||
| eventAbi: string[], | ||
| ): LogDescription[] { | ||
| const eventInterface = new ethers.Interface(eventAbi); | ||
| const event = eventInterface.getEvent(eventName); | ||
| if (!event) { | ||
| throw new Error('Event name and abi mismatch'); | ||
| } | ||
| return ''; | ||
| const eventId = event.topicHash; | ||
| let extractedEvents = logs | ||
| // Get logs of the target contract. | ||
| .filter((log) => log.address === address && log.topics.includes(eventId)) | ||
| // Parse logs to events. | ||
| .map((log) => eventInterface.parseLog(log)) | ||
| // Get events with the target name. | ||
| .filter((event) => event && event.name === eventName); | ||
| // Get only non null elements. | ||
| // Note: using .filter(...) returns (LogDescription | null)[] | ||
| // which is not desired. | ||
| const events: LogDescription[] = []; | ||
| extractedEvents.forEach((element) => element && events.push(element)); | ||
| return events; |
There was a problem hiding this comment.
An other way to do that, that we use in SDK
export const getEventFromLogs = ({ contract, eventName, logs }) => {
const filter = contract.getEvent(eventName);
if (!filter) {
throw new Error(`Event filter not found for ${eventName}`);
}
const eventTopic = filter.fragment.topicHash;
// Find the event in the logs based on the topic
const eventFound = logs.find((log) => log.topics[0] === eventTopic);
if (!eventFound) {
throw new Error(`Event ${eventName} not found in logs`);
}
// Check if the event is already decoded
if (eventFound.args) {
return eventFound;
}
// If not decoded, decode the event
try {
return {
...eventFound,
args: contract.interface.decodeEventLog(
filter.fragment,
eventFound.data,
eventFound.topics
),
};
} catch (error) {
throw new Error(`Failed to decode event ${eventName}: ${error.message}`);
}
};
There was a problem hiding this comment.
In fact log.topics.includes(eventId) could be replace by log.topics[0] === eventTopic. Indeed this is the first element of the topic that correspond to the event signature.
Other improvements could be considered, such as removing the need to use the address.
There was a problem hiding this comment.
Co-authored-by: Zied Guesmi <[email protected]>
See job logs on
Run teststep ✔️ :