github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/js/src/test/event.test.ts (about)

     1  import * as assert from 'assert';
     2  import { compile } from '../contracts/compile';
     3  import { ContractEvent } from '../contracts/contract';
     4  import { client } from './test';
     5  
     6  describe('event', function () {
     7    it('listens to an event from a contract', async () => {
     8      const source = `
     9        pragma solidity >=0.0.0;
    10        contract Contract {
    11            event Event(
    12                address from
    13            );
    14  
    15            function announce() public {
    16                emit Event(msg.sender);
    17            }
    18        }
    19      `;
    20      const contract = compile(source, 'Contract');
    21      const instance: any = await contract.deploy(client);
    22      let count = 0;
    23  
    24      const event = instance.Event as ContractEvent;
    25      const stream = event((error, event) => {
    26        if (error) {
    27          throw error;
    28        } else {
    29          assert.strictEqual(event?.args?.from?.length, 40);
    30  
    31          count++;
    32  
    33          if (count === 2) {
    34            stream.cancel();
    35          }
    36        }
    37      });
    38  
    39      instance.announce();
    40      instance.announce();
    41    });
    42  });