Skip to content

Commit

Permalink
fix: modify parseEvents structure + add testcase for multiple differe…
Browse files Browse the repository at this point in the history
…nt event in same tx
  • Loading branch information
edisontim committed Aug 9, 2023
1 parent 3d86446 commit b63b0d6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 35 deletions.
72 changes: 55 additions & 17 deletions __tests__/cairo1v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,18 +602,18 @@ describe('Cairo 1 Devnet', () => {
simpleDataStruct,
simpleDataArray
);
const shouldBe: types.ParsedEvents = {
EventRegular: [
{
const shouldBe: types.ParsedEvents = [
{
EventRegular: {
simpleKeyVariable,
simpleKeyStruct,
simpleKeyArray,
simpleDataVariable,
simpleDataStruct,
simpleDataArray,
},
],
};
},
];
const tx = await provider.waitForTransaction(transaction_hash);
const events = eventContract.parseEvents(tx);
return expect(events).toStrictEqual(shouldBe);
Expand All @@ -624,41 +624,43 @@ describe('Cairo 1 Devnet', () => {
nestedKeyStruct,
nestedDataStruct
);
const shouldBe: types.ParsedEvents = {
EventNested: [
{
const shouldBe: types.ParsedEvents = [
{
EventNested: {
nestedKeyStruct,
nestedDataStruct,
},
],
};
},
];
const tx = await provider.waitForTransaction(transaction_hash);
const events = eventContract.parseEvents(tx);
return expect(events).toStrictEqual(shouldBe);
});

test('parse tx returning multiple events', async () => {
test('parse tx returning multiple similar events', async () => {
const anotherKeyVariable = 100n;
const shouldBe: types.ParsedEvents = {
EventRegular: [
{
const shouldBe: types.ParsedEvents = [
{
EventRegular: {
simpleKeyVariable,
simpleKeyStruct,
simpleKeyArray,
simpleDataVariable,
simpleDataStruct,
simpleDataArray,
},
{
},
{
EventRegular: {
simpleKeyVariable: anotherKeyVariable,
simpleKeyStruct,
simpleKeyArray,
simpleDataVariable,
simpleDataStruct,
simpleDataArray,
},
],
};
},
];
const callData1 = eventContract.populate('emitEventRegular', [
simpleKeyVariable,
simpleKeyStruct,
Expand All @@ -680,5 +682,41 @@ describe('Cairo 1 Devnet', () => {
const events = eventContract.parseEvents(tx);
return expect(events).toStrictEqual(shouldBe);
});
test('parse tx returning multiple different events', async () => {
const shouldBe: types.ParsedEvents = [
{
EventRegular: {
simpleKeyVariable,
simpleKeyStruct,
simpleKeyArray,
simpleDataVariable,
simpleDataStruct,
simpleDataArray,
},
},
{
EventNested: {
nestedKeyStruct,
nestedDataStruct,
},
},
];
const callData1 = eventContract.populate('emitEventRegular', [
simpleKeyVariable,
simpleKeyStruct,
simpleKeyArray,
simpleDataVariable,
simpleDataStruct,
simpleDataArray,
]);
const callData2 = eventContract.populate('emitEventNested', [
nestedKeyStruct,
nestedDataStruct,
]);
const { transaction_hash } = await account.execute([callData1, callData2]);
const tx = await provider.waitForTransaction(transaction_hash);
const events = eventContract.parseEvents(tx);
return expect(events).toStrictEqual(shouldBe);
});
});
});
10 changes: 5 additions & 5 deletions __tests__/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,18 @@ describe('contract module', () => {
const { transaction_hash } = await erc20Echo20Contract.transfer(to, amount);
const tx = await provider.waitForTransaction(transaction_hash);
const events: ParsedEvents = erc20Echo20Contract.parseEvents(tx);
const shouldBe: ParsedEvents = {
Transfer: [
{
const shouldBe: ParsedEvents = [
{
Transfer: {
from_: BigInt(account.address),
to: BigInt(to),
value: {
low: BigInt(amount.low),
high: BigInt(amount.high),
},
},
],
};
},
];
return expect(events).toStrictEqual(shouldBe);
});
});
Expand Down
6 changes: 2 additions & 4 deletions src/types/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ export type InvokeOptions = Pick<
'maxFee' | 'nonce' | 'signature' | 'parseRequest'
>;

export type ParsedEvent = ParsedStruct;
export type ParsedEvent = { [name: string]: ParsedStruct };

export type ParsedEvents = {
[name: string]: Array<ParsedEvent> | ParsedEvent;
};
export type ParsedEvents = Array<ParsedEvent>;
24 changes: 15 additions & 9 deletions src/utils/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function parseEvents(

// Create our final event object
const parsedEvent: ParsedEvent = {};

parsedEvent[abiEvent.name] = {};
// Remove the event's name hashed from the keys array
recEvent.keys.shift();

Expand All @@ -62,18 +62,24 @@ export function parseEvents(
(abiEvent as LegacyEvent).data;

abiEventKeys.forEach((key) => {
parsedEvent[key.name] = responseParser(keysIter, key, abiStructs, parsedEvent);
parsedEvent[abiEvent.name][key.name] = responseParser(
keysIter,
key,
abiStructs,
parsedEvent[abiEvent.name]
);
});

abiEventData.forEach((data) => {
parsedEvent[data.name] = responseParser(dataIter, data, abiStructs, parsedEvent);
parsedEvent[abiEvent.name][data.name] = responseParser(
dataIter,
data,
abiStructs,
parsedEvent[abiEvent.name]
);
});

(acc[abiEvent.name] as Array<ParsedEvent>) = (
(acc[abiEvent.name] as Array<ParsedEvent>) ?? []
).concat(parsedEvent) || [parsedEvent];

acc.push(parsedEvent);
return acc;
}, {} as ParsedEvents);
}, [] as ParsedEvents);
return ret;
}

0 comments on commit b63b0d6

Please sign in to comment.