import { ExchangeStore , StrategyStore , WalletStore } from '@store' ; import { ExchangeCollection , StrategyCollection , ExchangeConfig } from '@lib' ; import { logger } from '@util' ; export class CoreHelpers { public async getExchanges ( ) { const exchanges = await ExchangeStore . instance . loadAll ( ) ; return this . mapStrategiesToExchanges ( exchanges ) ; } public checkAddExchangeParams ( exchangeConfig ) { if ( ! exchangeConfig . exchange ) return this . error ( 'exchange name is required' ) ; if ( ! exchangeConfig . tradePollInterval ) return this . error ( 'tradePollInterval is required' ) ; if ( exchangeConfig . exchange === 'chaos' ) return false ; if ( ! exchangeConfig [ 'auth.apiKey' ] ) return this . error ( 'auth.apiKey is required' ) ; if ( ! exchangeConfig [ 'auth.secret' ] ) return this . error ( 'auth.secret is required' ) ; return false ; } public error ( error ) { logger . error ( error ) ; return { error : `Core Error: ${ error }` } ; } private async mapStrategiesToExchanges ( exchanges ) { return Promise . all ( exchanges . map ( ( exchange ) => this . mapStrategyToExchange ( exchange ) ) ) ; } private async mapStrategyToExchange ( exchange ) { const strategies = await this . getStrategies ( exchange . exchange ) ; return { ... exchange , strategies } ; } private async getStrategies ( exchange ) { const strategies = await StrategyStore . instance . loadAllForExchange ( exchange ) ; return this . mapWalletsToStrategies ( strategies ) ; } private mapWalletsToStrategies ( strategies ) { return Promise . all ( strategies . map ( ( strategy ) => this . mapWalletToStrategy ( strategy ) ) ) ; } private async mapWalletToStrategy ( strategy ) { const wallet = await this . getWallet ( strategy . exchange , strategy . symbol , strategy . strategy ) ; return { ... strategy , wallet } ; } private getWallet ( exchange , symbol , strategy ) { return WalletStore . instance . loadWallet ( { exchange , symbol , strategy } ) ; } }