import { Injectable } from '@angular/core' ; import { HttpClient } from "@angular/common/http" ; import { NodeService } from "./node.service" ; import { AppSettingsService } from "./app-settings.service" ; export interface NinjaVerifiedRep { votingweight : number ; delegators : number ; uptime : number ; score : number ; account : string ; alias : string ; } @ Injectable ( ) export class ApiService { apiUrl = `https://nanovault.io/api` ; rpcUrl = `${ this . apiUrl }/node-api` ; constructor ( private http , private node , private appSettings ) { } private async request ( action , data ) { data . action = action ; let apiUrl = this . appSettings . settings . serverAPI || this . rpcUrl ; if ( this . appSettings . settings . serverNode ) { apiUrl += `?node=${ this . appSettings . settings . serverNode }` ; } if ( this . node . node . status === false ) { this . node . setLoading ( ) ; } return await this . http . post ( apiUrl , data ) . toPromise ( ) . then ( res => { this . node . setOnline ( ) ; return res ; } ) . catch ( err => { if ( err . status === 500 || err . status === 0 ) { this . node . setOffline ( ) ; } throw err ; } ) ; } async recommendedReps ( ) { return await this . http . get ( `${ this . apiUrl }/recommended-representatives` ) . toPromise ( ) as NinjaVerifiedRep [ ] ; } async accountsBalances ( accounts ) { return await this . request ( 'accounts_balances' , { accounts } ) ; } async accountsFrontiers ( accounts ) { return await this . request ( 'accounts_frontiers' , { accounts } ) ; } async accountsPending ( accounts , count = 50 ) { return await this . request ( 'accounts_pending' , { accounts , count , source : true } ) ; } async accountsPendingLimit ( accounts , threshold , count = 50 ) { return await this . request ( 'accounts_pending' , { accounts , count , threshold , source : true } ) ; } async delegatorsCount ( account ) { return await this . request ( 'delegators_count' , { account } ) ; } async representativesOnline ( ) { return await this . request ( 'representatives_online' , { } ) ; } async blocksInfo ( blocks ) { return await this . request ( 'blocks_info' , { hashes : blocks , pending : true , source : true } ) ; } async blockCount ( ) { return await this . request ( 'block_count' , { } ) ; } async workGenerate ( hash ) { return await this . request ( 'work_generate' , { hash } ) ; } async process ( block ) { return await this . request ( 'process' , { block : JSON . stringify ( block ) } ) ; } async accountHistory ( account , count = 25 , raw = false ) { return await this . request ( 'account_history' , { account , count , raw } ) ; } async accountInfo ( account ) { return await this . request ( 'account_info' , { account , pending : true , representative : true , weight : true } ) ; } async validateAccountNumber ( account ) { return await this . request ( 'validate_account_number' , { account } ) ; } async pending ( account , count ) { return await this . request ( 'pending' , { account , count , source : true } ) ; } async pendingLimit ( account , count , threshold ) { return await this . request ( 'pending' , { account , count , threshold , source : true } ) ; } }