import { Injectable } from '@angular/core' ; import 'rxjs/add/operator/toPromise' ; import 'rxjs/add/operator/timeout' ; import { Api , Block , BlockchainStatus , SuggestedFees , Peer , PeerAddressList , BlockList } from '@signumjs/core' ; import { ApiService } from '../api.service' ; import { StoreService } from 'app/store/store.service' ; import { BehaviorSubject } from 'rxjs' ; import { constants } from '../constants' ; export interface MiningInfo { height : string ; generationSignature : string ; baseTarget : string ; averageCommitmentNQT : string ; lastBlockReward : string ; timestamp : string ; } @ Injectable ( ) export class NetworkService { private api : Api ; private _isMainNet = true ; public blocks : BehaviorSubject < any > = new BehaviorSubject ( [ ] ) ; public isMainNet$ : BehaviorSubject < boolean > = new BehaviorSubject ( true ) ; constructor ( apiService , private storeService ) { this . storeService . settings . subscribe ( async ( ready ) => { if ( ! ready ) { return ; } this . api = apiService . api ; const isMainNet = await this . fetchIsMainNet ( ) ; this . isMainNet$ . next ( isMainNet ) ; this . _isMainNet = isMainNet ; } ) ; } public suggestFee ( ) { return this . api . network . getSuggestedFees ( ) ; } public getBlockchainStatus ( ) { return this . api . network . getBlockchainStatus ( ) ; } public getBlockById ( id ? ) { return this . api . block . getBlockById ( id , false ) ; } public getBlocks ( firstIndex ? , lastIndex ? , includeTransactions ? ) { return this . api . block . getBlocks ( firstIndex , lastIndex , includeTransactions ) ; } public getMiningInfo ( ) { return this . api . service . query ( 'getMiningInfo' ) ; } public getPeer ( address ) { return this . api . network . getPeer ( address ) ; } public getPeers ( ) { return this . api . network . getPeers ( ) ; } public setBlocks ( blocks ) { this . blocks . next ( blocks ) ; } public addBlock ( block ) { this . setBlocks ( [ block ] . concat ( this . blocks . value ) ) ; } private async fetchIsMainNet ( ) { try { const { previousBlockHash } = await this . api . block . getBlockByHeight ( constants . mainnetIndicator . block , false ) ; return previousBlockHash === constants . mainnetIndicator . previousHash ; } catch ( e ) { return false ; } } public isMainNet ( ) { return this . _isMainNet ; } }