Skip to content

Commit

Permalink
feat: implement event parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
edisontim committed Aug 2, 2023
1 parent 384db9f commit 86cc430
Show file tree
Hide file tree
Showing 12 changed files with 3,567 additions and 1,738 deletions.
1,776 changes: 1,776 additions & 0 deletions __mocks__/event.casm

Large diffs are not rendered by default.

1,479 changes: 1,479 additions & 0 deletions __mocks__/event.json

Large diffs are not rendered by default.

119 changes: 119 additions & 0 deletions __tests__/contract.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BigNumberish, Contract, ContractFactory, RawArgs, json, stark } from '../src';
import { ParsedEvents } from '../src/types';
import { CallData } from '../src/utils/calldata';
import { felt, isCairo1Abi, tuple, uint256 } from '../src/utils/calldata/cairo';
import { getSelectorFromName } from '../src/utils/hash';
Expand All @@ -8,6 +9,8 @@ import { uint256ToBN } from '../src/utils/uint256';
import {
compiledErc20,
compiledErc20Echo,
compiledEvent,
compiledEventCasm,
compiledMulticall,
compiledTypeTransformation,
describeIfDevnet,
Expand Down Expand Up @@ -96,6 +99,122 @@ describe('contract module', () => {
});
});

describe('Event Parsing', () => {
let erc20Echo20Contract: Contract;
const factoryClassHash =
'0x011ab8626b891bcb29f7cc36907af7670d6fb8a0528c7944330729d8f01e9ea3'!;
let factory: ContractFactory;
let eventContract: Contract;

beforeAll(async () => {
factory = new ContractFactory({
compiledContract: compiledErc20Echo,
classHash: factoryClassHash,
account,
});

erc20Echo20Contract = await factory.deploy(
'Token',
'ERC20',
18,
uint256('1000000000'),
account.address,
['0x823d5a0c0eefdc9a6a1cb0e064079a6284f3b26566b677a32c71bbe7bf9f8c'],
22
);
const { deploy } = await account.declareAndDeploy({
contract: compiledEvent,
casm: compiledEventCasm,
});

eventContract = new Contract(compiledEvent.abi, deploy.contract_address!, account);
});

test('parse old event structure', async () => {
const to = stark.randomAddress();
const amount = uint256(1);
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: {
from_: BigInt(account.address),
to: BigInt(to),
value: {
low: BigInt(amount.low),
high: BigInt(amount.high),
},
},
};
return expect(events).toStrictEqual(shouldBe);
});
test('parse event returning a regular struct', async () => {
const simpleKeyVariable = 0n;
const simpleKeyStruct = {
first: 1n,
second: 2n,
};
const simpleKeyArray = [3n, 4n, 5n];
const simpleDataVariable = 6n;
const simpleDataStruct = {
first: 7n,
second: 8n,
};
const simpleDataArray = [9n, 10n, 11n];
const { transaction_hash } = await eventContract.emitEventRegular(
simpleKeyVariable,
simpleKeyStruct,
simpleKeyArray,
simpleDataVariable,
simpleDataStruct,
simpleDataArray
);
const shouldBe: 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);
});

test('parse event returning a nested struct', async () => {
const nestedKeyStruct = {
simpleStruct: {
first: 0n,
second: 1n,
},
simpleArray: [2n, 3n, 4n, 5n],
};
const nestedDataStruct = {
simpleStruct: {
first: 6n,
second: 7n,
},
simpleArray: [8n, 9n, 10n, 11n],
};
const { transaction_hash } = await eventContract.emitEventNested(
nestedKeyStruct,
nestedDataStruct
);
const shouldBe: ParsedEvents = {
EventNested: {
nestedKeyStruct,
nestedDataStruct,
},
};
const tx = await provider.waitForTransaction(transaction_hash);
const events = eventContract.parseEvents(tx);
return expect(events).toStrictEqual(shouldBe);
});
});

describe('Type Transformation', () => {
let typeTransformedContract: Contract;

Expand Down
2 changes: 2 additions & 0 deletions __tests__/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export const compiledOpenZeppelinAccount = readContract('Account');
export const compiledErc20 = readContract('ERC20');
export const compiledErc20Echo = readContract('ERC20-echo');
export const compiledL1L2 = readContract('l1l2_compiled');
export const compiledEvent = readContractSierra('event');
export const compiledEventCasm = readContractSierraCasm('event');
export const compiledTypeTransformation = readContract('contract');
export const compiledMulticall = readContract('multicall');
export const compiledTestDapp = readContract('TestDapp');
Expand Down
Loading

0 comments on commit 86cc430

Please sign in to comment.