events.spec.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // SPDX-License-Identifier: Apache-2.0
  2. import expect from 'expect';
  3. import { weight, createConnection, deploy, transaction, aliceKeypair, } from './index';
  4. import { ContractPromise } from '@polkadot/api-contract';
  5. import { ApiPromise } from '@polkadot/api';
  6. import { DecodedEvent } from '@polkadot/api-contract/types';
  7. describe('Deploy events contract and test event data, docs and topics', () => {
  8. let conn: ApiPromise;
  9. before(async function () {
  10. conn = await createConnection();
  11. });
  12. after(async function () {
  13. await conn.disconnect();
  14. });
  15. it('events', async function () {
  16. this.timeout(50000);
  17. const alice = aliceKeypair();
  18. let deploy_contract = await deploy(conn, alice, 'Events.contract', BigInt(0));
  19. let contract = new ContractPromise(conn, deploy_contract.abi, deploy_contract.address);
  20. let gasLimit = await weight(conn, contract, "emitEvent");
  21. let tx = contract.tx.emitEvent({ gasLimit });
  22. let res0: any = await transaction(tx, alice);
  23. let events: DecodedEvent[] = res0.contractEvents;
  24. expect(events.length).toEqual(5);
  25. expect(events[0].event.identifier).toBe("Events::foo1");
  26. expect(events[0].event.docs).toEqual(["Ladida tada"]);
  27. expect(events[0].args.map(a => a.toJSON())).toEqual([254, "hello there"]);
  28. expect(events[1].event.identifier).toBe("Events::foo2");
  29. expect(events[1].event.docs).toEqual(["Event Foo2\n\nJust a test\n\nAuthor: them is me"]);
  30. expect(events[1].args.map(a => a.toJSON())).toEqual(["0x7fffffffffffffff", "minor", deploy_contract.address.toString()]);
  31. expect(events[2].event.identifier).toBe("Events::ThisEventTopicShouldGetHashed");
  32. expect(events[2].args.map(a => a.toJSON())).toEqual([alice.address]);
  33. // Expect the 3rd event to yield the following event topics:
  34. // - blake2x256 sum of its signature: 'ThisEventTopicShouldGetHashed(address)'
  35. // - Address of the caller
  36. let field_topic = await conn.query.system.eventTopics(alice.addressRaw);
  37. expect(field_topic.length).toBe(1);
  38. let event_topic = await conn.query.system.eventTopics("0x95c29b3e1b835071ab157a22d89cfc81d176add91127a1ee8766abf406a2cbc3");
  39. expect(event_topic.length).toBe(1);
  40. expect(events[3].event.identifier).toBe("Events::Event");
  41. expect(events[3].args.map(a => a.toJSON())).toEqual([true]);
  42. // The 4th event yields the following event topics:
  43. // - blake2x256 sum of its signature: 'Event(bool)'
  44. // - unhashed data (because encoded length is <= 32 bytes) of 'true'
  45. field_topic = await conn.query.system.eventTopics("0x0100000000000000000000000000000000000000000000000000000000000000");
  46. expect(field_topic.length).toBe(1);
  47. event_topic = await conn.query.system.eventTopics("0xc2bc7a077121efada8bc6a0af16c1e886406e8c2d1716979cb1b92098d8b49bc");
  48. expect(event_topic.length).toBe(1);
  49. // The 5th event yields no data and the following event topic:
  50. // - blake2x256 sum of its signature: 'Empty()'
  51. event_topic = await conn.query.system.eventTopics("0x2c54a2a9a9aa474af5d6e2bb2e5a35b84051acaf95b233f98f96860d36f2b81b");
  52. expect(event_topic.length).toBe(1);
  53. expect(events[4].args.map(a => a.toJSON())).toEqual([]);
  54. });
  55. });