import { Repository } from 'typeorm' ; import { Map } from 'immutable' ; import BN from 'bn.js' ; import { Injectable } from '@nestjs/common' ; import { InjectRepository } from '@nestjs/typeorm' ; import { AccountAssetDTO , AccountBalanceAssetService , AccountBalanceService , AssetService } from '@energyweb/exchange' ; import { ExportedAsset } from './exported.entity' ; @ Injectable ( ) export class ExportAccountingService extends AccountBalanceAssetService { constructor ( @ InjectRepository ( ExportedAsset ) private readonly repository , accountBalanceService , private readonly assetService ) { super ( ) ; accountBalanceService . registerAssetSource ( this ) ; } public async findByOwner ( ownerId ) { return this . repository . find ( { ownerId } ) ; } public async lockedAssets ( ownerId ) { const exportedAssets = await this . getExportedAssets ( ownerId ) ; return this . sumByAsset ( exportedAssets , ( exported ) => exported . asset , ( exported ) => new BN ( exported . amount ) ) ; } public async availableAssets ( ownerId ) { const exportedAssets = await this . getExportedAssets ( ownerId ) ; return this . sumByAsset ( exportedAssets , ( exported ) => exported . asset , ( exported ) => new BN ( exported . amount ) . neg ( ) ) ; } private async getExportedAssets ( ownerId ) { const exportedAssets = await this . findByOwner ( ownerId ) ; return await Promise . all ( exportedAssets . map ( async ( exported ) => ( { asset : await this . assetService . get ( exported . assetId ) , amount : exported . amount } ) ) ) ; } }