2020-07-13 21:46:25 +07:00
import { Component , OnInit , OnDestroy } from '@angular/core' ;
2022-09-21 17:23:45 +02:00
import { SeoService } from '../../services/seo.service' ;
2020-07-13 21:46:25 +07:00
import { switchMap , filter , catchError } from 'rxjs/operators' ;
import { ParamMap , ActivatedRoute } from '@angular/router' ;
import { Subscription , of } from 'rxjs' ;
import { BisqTransaction } from '../bisq.interfaces' ;
import { BisqApiService } from '../bisq-api.service' ;
2022-09-21 17:23:45 +02:00
import { WebsocketService } from '../../services/websocket.service' ;
2020-07-13 21:46:25 +07:00
@Component ( {
selector : 'app-bisq-address' ,
templateUrl : './bisq-address.component.html' ,
styleUrls : [ './bisq-address.component.scss' ]
} )
export class BisqAddressComponent implements OnInit , OnDestroy {
transactions : BisqTransaction [ ] ;
addressString : string ;
isLoadingAddress = true ;
error : any ;
mainSubscription : Subscription ;
totalReceived = 0 ;
totalSent = 0 ;
constructor (
2021-03-05 15:38:46 +07:00
private websocketService : WebsocketService ,
2020-07-13 21:46:25 +07:00
private route : ActivatedRoute ,
private seoService : SeoService ,
private bisqApiService : BisqApiService ,
) { }
ngOnInit() {
2021-03-05 15:38:46 +07:00
this . websocketService . want ( [ 'blocks' ] ) ;
2020-07-13 21:46:25 +07:00
this . mainSubscription = this . route . paramMap
. pipe (
switchMap ( ( params : ParamMap ) = > {
this . error = undefined ;
this . isLoadingAddress = true ;
this . transactions = null ;
document . body . scrollTo ( 0 , 0 ) ;
this . addressString = params . get ( 'id' ) || '' ;
2020-12-04 21:29:31 +07:00
this . seoService . setTitle ( $localize ` :@@bisq-address.component.browser-title:Address: ${ this . addressString } :INTERPOLATION: ` ) ;
2023-08-28 13:10:08 +09:00
this . seoService . setDescription ( $localize ` :@@meta.description.bisq.address:See current balance, pending transactions, and history of confirmed transactions for BSQ address ${ this . addressString } :INTERPOLATION:. ` ) ;
2020-07-13 21:46:25 +07:00
return this . bisqApiService . getAddress $ ( this . addressString )
. pipe (
catchError ( ( err ) = > {
this . isLoadingAddress = false ;
this . error = err ;
2023-03-09 02:34:21 -06:00
this . seoService . logSoft404 ( ) ;
2020-07-13 21:46:25 +07:00
console . log ( err ) ;
return of ( null ) ;
} )
) ;
} ) ,
filter ( ( transactions ) = > transactions !== null )
)
. subscribe ( ( transactions : BisqTransaction [ ] ) = > {
this . transactions = transactions ;
this . updateChainStats ( ) ;
this . isLoadingAddress = false ;
} ,
( error ) = > {
console . log ( error ) ;
this . error = error ;
2023-03-09 02:34:21 -06:00
this . seoService . logSoft404 ( ) ;
2020-07-13 21:46:25 +07:00
this . isLoadingAddress = false ;
} ) ;
}
updateChainStats() {
const shortenedAddress = this . addressString . substr ( 1 ) ;
this . totalSent = this . transactions . reduce ( ( acc , tx ) = >
acc + tx . inputs
. filter ( ( input ) = > input . address === shortenedAddress )
. reduce ( ( a , input ) = > a + input . bsqAmount , 0 ) , 0 ) ;
this . totalReceived = this . transactions . reduce ( ( acc , tx ) = >
acc + tx . outputs
. filter ( ( output ) = > output . address === shortenedAddress )
. reduce ( ( a , output ) = > a + output . bsqAmount , 0 ) , 0 ) ;
}
ngOnDestroy() {
this . mainSubscription . unsubscribe ( ) ;
}
}