Skip to content
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(contracts): handle contract events #140

Merged
merged 35 commits into from
Jul 23, 2024
Merged

feat(contracts): handle contract events #140

merged 35 commits into from
Jul 23, 2024

Conversation

1cedrus
Copy link
Collaborator

@1cedrus 1cedrus commented Jun 24, 2024

This PR addresses a task in #110 as the title said which adds:

  • Contract events executor which provides some useful methods (is, as, meta).
  • Contract events decoder (for supported contract metadata versions and not multiple anonymous events contract only).

A piece of code that tries to call a contract message, decode emitted events, and print out if it is Flipped event:

  const api = await LegacyClient.new(new WsProvider('ws://127.0.0.1:9944'));
  const contract = new Contract<FlipperContractApi>(api, METADATA, CONTRACT_ADDRESS);

  const dryRun = await contract.query.flip({ caller: alicePair.address });
  const tx = contract.tx.flip({ gasLimit: dryRun.raw.gasRequired });

  await tx.signAndSend(alicePair, ({ status, events }: any) => {
    if (status.type === 'Finalized') {
      events
        .filter((eventRecord: FrameSystemEventRecord) => api.events.contracts.ContractEmitted.is(eventRecord.event))
        .forEach((one: FrameSystemEventRecord) => {
          const decodedEvent = contract.decodeEvent(one);

          if (decodedEvent && contract.events.Flipped.is(decodedEvent)) {
            console.log(decodedEvent);
          }
        });
    }
  });
  • Also, an events interface generator that helps to interact with API more easily (type suggestion):

  • Unit tests and e2e tests for added features.

@1cedrus 1cedrus force-pushed the tweak/contract-events branch 2 times, most recently from b9581f7 to 6d99ccd Compare June 24, 2024 15:28
# Conflicts:
#	packages/cli/src/commands/typink.ts
#	packages/codegen/src/chaintypes/generator/TypesGen.ts
#	packages/codegen/src/chaintypes/index.ts
#	packages/codegen/src/shared/BaseTypesGen.ts
#	packages/codegen/src/shared/TypeImports.ts
#	packages/codegen/src/typink/generator/ConstructorQueryGen.ts
#	packages/codegen/src/typink/generator/ConstructorTxGen.ts
#	packages/codegen/src/typink/generator/IndexGen.ts
#	packages/codegen/src/typink/generator/QueryGen.ts
#	packages/codegen/src/typink/generator/TxGen.ts
#	packages/codegen/src/typink/generator/TypesGen.ts
#	packages/codegen/src/typink/generator/index.ts
#	packages/codegen/src/typink/index.ts
#	packages/codegen/src/typink/templates/constructor-query.hbs
#	packages/codegen/src/typink/templates/constructor-tx.hbs
#	packages/codegen/src/typink/templates/index.hbs
#	packages/codegen/src/typink/templates/query.hbs
#	packages/codegen/src/typink/templates/tx.hbs
#	packages/codegen/src/typink/templates/types.hbs
#	packages/codegen/src/utils.ts
#	packages/contracts/src/__tests__/Contract.spec.ts
#	packages/contracts/src/__tests__/ContractDeployer.spec.ts
#	packages/contracts/src/executor/ConstructorQueryExecutor.ts
#	packages/contracts/src/executor/QueryExecutor.ts
#	packages/contracts/src/executor/TxExecutor.ts
#	packages/contracts/src/types/index.ts
#	packages/contracts/src/types/shared.ts
#	packages/contracts/src/types/v4.ts
#	zombienet-tests/src/0001-check-contract-api.ts
@1cedrus 1cedrus marked this pull request as ready for review June 27, 2024 07:42
@1cedrus 1cedrus requested a review from sinzii June 27, 2024 09:48
@sinzii sinzii changed the title feat(contracts): Add Contract Event Handle feat(contracts): handle contract events Jun 28, 2024
Copy link
Member

@sinzii sinzii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work! Thanks for the additional specs 🚀

@sinzii sinzii added this pull request to the merge queue Jul 23, 2024
Merged via the queue into main with commit 7cb4bf7 Jul 23, 2024
2 checks passed
@sinzii sinzii deleted the tweak/contract-events branch July 23, 2024 06:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants