import { Component , Input , OnChanges , OnInit } from '@angular/core' ; import { Account , Block } from '@signumjs/core' ; import { AccountService } from '../../../../setup/account/account.service' ; import { ChainTime } from '@signumjs/util' ; import { formatCurrency } from '@angular/common' ; import { Settings } from '../../../../settings' ; import { UnsubscribeOnDestroy } from '../../../../util/UnsubscribeOnDestroy' ; import { StoreService } from '../../../../store/store.service' ; import { takeUntil } from 'rxjs/operators' ; interface ForgedBlockInfo { count : number ; minedIncomeSigna : number ; lastBlock : Block ; } @ Component ( { selector : 'app-blockforged' , templateUrl : './blockforged.component.html' , styleUrls : [ './blockforged.component.scss' , '../widget.shared.scss' ] } ) export class BlockforgedComponent extends UnsubscribeOnDestroy implements OnInit , OnChanges { @ Input ( ) public account : Account ; @ Input ( ) public priceBtc : number ; @ Input ( ) public priceUsd : number ; @ Input ( ) public priceEur : number ; @ Input ( ) public priceRub : number ; locale = 'en' ; isLoading = true ; blockInfo : ForgedBlockInfo ; private interval : NodeJS . Timeout ; private unsubscribe = takeUntil ( this . unsubscribeAll ) ; constructor ( private accountService , private storeService ) { super ( ) ; this . blockInfo = { count : 0 , lastBlock : null , minedIncomeSigna : 0 } ; } async ngOnInit ( ) { await this . updateForgedBlocks ( ) ; this . isLoading = false ; this . interval = setInterval ( ( ) => this . updateForgedBlocks ( ) , 2 * 60 * 1000 ) ; this . storeService . settings . pipe ( this . unsubscribe ) . subscribe ( ( settings ) => { this . locale = settings . language ; } ) ; } ngOnChanges ( ) { this . updateForgedBlocks ( ) ; } ngOnDestroy ( ) { clearInterval ( this . interval ) ; } private async updateForgedBlocks ( ) { const { blocks } = await this . accountService . getForgedBlocks ( this . account ) ; this . blockInfo = blocks . reduce < ForgedBlockInfo > ( ( acc , b ) => { if ( ! ( acc . lastBlock && acc . lastBlock . timestamp >= b . timestamp ) ) { acc . lastBlock = b ; } acc . minedIncomeSigna += parseInt ( b . blockReward , 10 ) ; acc . count ++ ; return acc ; } , { minedIncomeSigna : 0 , lastBlock : null , count : 0 } ) ; } getLastBlockDate ( ) { return this . blockInfo . count > 0 ? ChainTime . fromChainTimestamp ( this . blockInfo . lastBlock . timestamp ) . getDate ( ) : null ; } private asCurrency ( value , currency , digitsInfo = '1.2-2' ) { return `${ formatCurrency ( value , this . locale , currency , '' , digitsInfo ) }` ; } public getPriceBtc = ( ) : string => { return `฿${ this . asCurrency ( this . priceBtc * this . blockInfo . minedIncomeSigna , '' , '1.0-4' ) }` ; } ; public getPriceUsd = ( ) : string => `$${ this . asCurrency ( this . priceUsd * this . blockInfo . minedIncomeSigna , '' , '1.0-2' ) }` ; public getPriceEur = ( ) : string => `€${ this . asCurrency ( this . priceEur * this . blockInfo . minedIncomeSigna , '' , '1.0-2' ) }` ; public getPriceRub = ( ) : string => `₽${ this . asCurrency ( this . priceRub * this . blockInfo . minedIncomeSigna , '' , '1.0-2' ) }` ; }