import * as assert from 'assert' ; import { ethers } from 'ethers' ; import { EthereumListener } from '../../../src/types/ethereum' ; import { getLogger , sleep } from '@pigi/core-utils/build/src/app' ; const log = getLogger ( 'ethereum-test-utils' , true ) ; export class TestListener < T > implements EthereumListener < T > { private received : T [ ] ; private syncCompleted : boolean ; public constructor ( private readonly sleepMillis = 50 ) { this . syncCompleted = false ; this . received = [ ] ; } public async onSyncCompleted ( ) { this . syncCompleted = true ; } public async handle ( t ) { log . debug ( `Received ${ JSON . stringify ( t ) }` ) ; if ( this . received . length === 0 || t !== this . received [ this . received . length - 1 ] ) { this . received . push ( t ) ; } } public getReceived ( ) { return this . received . splice ( 0 ) ; } public async waitForReceive ( num = 1 ) { while ( this . received . length < num ) { await sleep ( this . sleepMillis ) ; } return this . getReceived ( ) ; } public async waitForSyncToComplete ( ) { while ( ! this . syncCompleted ) { await sleep ( this . sleepMillis ) ; } return this . getReceived ( ) ; } public async assertNotReceivedAfter ( millis ) { await sleep ( millis ) ; assert ( this . getReceived ( ) . length === 0 , 'Should not have received but did!' ) ; } } const TestToken = require ( './contracts/TestToken.json' ) ; export const deployTokenContract = async ( ownerWallet , initialSupply ) : Promise < ethers . Contract > => { const factory = new ethers . ContractFactory ( TestToken . abi , TestToken . bytecode , ownerWallet ) ; const tokenContract = await factory . deploy ( initialSupply ) ; return tokenContract . deployed ( ) ; } ;