import './setup' ; import { newInMemoryDB } from '@pigi/core-db' ; import { getLogger , DefaultSignatureProvider , SimpleClient , } from '@pigi/core-utils' ; import { Wallet } from 'ethers' ; import { AGGREGATOR_ADDRESS , AGGREGATOR_MNEMONIC , DummyBlockSubmitter , DummyRollupStateSolver , getGenesisState , } from './helpers' ; import { AggregatorServer , DefaultRollupStateMachine , UnipigTransitioner , RollupAggregator , RollupStateMachine , UNI_TOKEN_TYPE , PIGI_TOKEN_TYPE , RollupClient , Balances , } from '../src' ; const log = getLogger ( 'client-aggregator-integration' , true ) ; /*********
 * TESTS *
 *********/ const timeout = 20000 ; const testRecipientAddress = '0x7777b66b3C70137264BE7303812090EC42D85B4d' ; describe ( 'Mock Client/Aggregator Integration' , ( ) => { let accountAddress ; let aggregatorServer ; let aggregator ; let ovm ; let rollupClient ; let unipigTransitioner ; beforeEach ( async function ( ) { this . timeout ( timeout ) ; ovm = new DummyRollupStateSolver ( ) ; rollupClient = new RollupClient ( newInMemoryDB ( ) , AGGREGATOR_ADDRESS ) ; unipigTransitioner = new UnipigTransitioner ( newInMemoryDB ( ) , ovm , rollupClient , new DefaultSignatureProvider ( ) , AGGREGATOR_ADDRESS ) ; accountAddress = await unipigTransitioner . getAddress ( ) ; const rollupStateMachine = await DefaultRollupStateMachine . create ( getGenesisState ( accountAddress ) , newInMemoryDB ( ) , AGGREGATOR_ADDRESS ) ; aggregator = await RollupAggregator . create ( newInMemoryDB ( ) , rollupStateMachine , new DummyBlockSubmitter ( ) , new DefaultSignatureProvider ( Wallet . fromMnemonic ( AGGREGATOR_MNEMONIC ) ) ) ; await aggregator . onSyncCompleted ( ) ; aggregatorServer = new AggregatorServer ( aggregator , 'localhost' , 3000 ) ; await aggregatorServer . listen ( ) ; rollupClient . connect ( new SimpleClient ( 'http://127.0.0.1:3000' ) ) ; } ) ; afterEach ( async ( ) => { if ( ! ! aggregator ) { await aggregatorServer . close ( ) ; } } ) ; describe ( 'UnipigTransitioner' , ( ) => { it ( 'should be able to query the aggregators balances' , async ( ) => { const response = await unipigTransitioner . getBalances ( accountAddress ) ; response . should . deep . equal ( { [ UNI_TOKEN_TYPE ] : 50 , [ PIGI_TOKEN_TYPE ] : 50 , } ) ; } ) . timeout ( timeout ) ; it ( 'should return an error if the wallet tries to transfer money it doesnt have' , async ( ) => { try { await unipigTransitioner . send ( UNI_TOKEN_TYPE , testRecipientAddress , 10 ) ; } catch ( err ) { } } ) . timeout ( timeout ) ; it ( 'should successfully transfer if alice sends money' , async ( ) => { await unipigTransitioner . send ( UNI_TOKEN_TYPE , testRecipientAddress , 10 ) ; const recipientBalances = await unipigTransitioner . getBalances ( testRecipientAddress ) ; recipientBalances [ UNI_TOKEN_TYPE ] . should . equal ( 10 ) ; } ) . timeout ( timeout ) ; it ( 'should successfully transfer if first faucet is requested' , async ( ) => { const secondTransitioner = new UnipigTransitioner ( newInMemoryDB ( ) , ovm , rollupClient , new DefaultSignatureProvider ( ) , AGGREGATOR_ADDRESS ) ; const newAddress = await secondTransitioner . getAddress ( ) ; await secondTransitioner . requestFaucetFunds ( 10 ) ; const balances = await secondTransitioner . getBalances ( newAddress ) ; balances . should . deep . equal ( { [ UNI_TOKEN_TYPE ] : 10 , [ PIGI_TOKEN_TYPE ] : 10 , } ) ; await secondTransitioner . send ( UNI_TOKEN_TYPE , testRecipientAddress , 10 ) ; const recipientBalances = await secondTransitioner . getBalances ( testRecipientAddress ) ; recipientBalances [ UNI_TOKEN_TYPE ] . should . equal ( 10 ) ; } ) . timeout ( timeout ) ; } ) ; } ) ;