import { DatabaseService } from '@energyweb/origin-backend-utils' ; import { HttpStatus , INestApplication } from '@nestjs/common' ; import { expect } from 'chai' ; import request from 'supertest' ; import { AccountBalanceDTO } from '../src/pods/account-balance/account-balance.dto' ; import { AccountService } from '../src/pods/account/account.service' ; import { TransferService } from '../src/pods/transfer/transfer.service' ; import { authenticatedUser , bootstrapTestInstance } from './exchange' ; import { createDepositAddress } from './utils' ; describe ( 'account deposit confirmation' , ( ) => { let app ; let transferService ; let databaseService ; let accountService ; const user1Id = authenticatedUser . organization . id ; let user1Address = '' ; const dummyAsset = { address : '0x9876' , tokenId : '0' , deviceId : '0' , generationFrom : new Date ( '2020-01-01' ) , generationTo : new Date ( '2020-01-31' ) } ; const createTransactionHash = ( ) => `0x${ ( ( Math . random ( ) * 0xffffff ) << 0 ) . toString ( 16 ) }` ; const createDeposit = ( address , amount = '1000' , asset = dummyAsset , blockNumber = 123456 ) => { return transferService . createDeposit ( { address , transactionHash : createTransactionHash ( ) , amount , blockNumber , asset } ) ; } ; const confirmDeposit = ( transactionHash ) => { return transferService . setAsConfirmed ( transactionHash , 10000 ) ; } ; before ( async ( ) => { ( { transferService , accountService , databaseService , app } = await bootstrapTestInstance ( ) ) ; await app . init ( ) ; user1Address = await createDepositAddress ( accountService , user1Id ) ; } ) ; after ( async ( ) => { await databaseService . cleanUp ( ) ; await app . close ( ) ; } ) ; it ( 'should not list unconfirmed deposit' , async ( ) => { await createDeposit ( user1Address ) ; await request ( app . getHttpServer ( ) ) . get ( '/account-balance' ) . expect ( HttpStatus . OK ) . expect ( ( res ) => { const account = res . body as AccountBalanceDTO ; expect ( account . available . length ) . equals ( 0 ) ; } ) ; } ) ; it ( 'should list confirmed deposit' , async ( ) => { const amount = '1000' ; const { transactionHash } = await createDeposit ( user1Address , amount ) ; await confirmDeposit ( transactionHash ) ; await request ( app . getHttpServer ( ) ) . get ( '/account-balance' ) . expect ( HttpStatus . OK ) . expect ( ( res ) => { const account = res . body as AccountBalanceDTO ; expect ( account . available . length ) . equals ( 1 ) ; expect ( account . available [ 0 ] . amount ) . equals ( amount ) ; expect ( account . available [ 0 ] . asset ) . deep . equals ( JSON . parse ( JSON . stringify ( dummyAsset ) ) ) ; } ) ; } ) ; } ) ;