2021-03-05 15:38:46 +07:00
import { ChangeDetectionStrategy , Component , OnDestroy , OnInit } from '@angular/core' ;
2022-11-28 11:55:23 +09:00
import { UntypedFormBuilder , UntypedFormGroup } from '@angular/forms' ;
2021-04-21 22:12:35 +04:00
import { ActivatedRoute , Router } from '@angular/router' ;
2021-02-27 04:19:56 +07:00
import { combineLatest , merge , Observable , of } from 'rxjs' ;
2021-03-14 02:42:14 +07:00
import { map , switchMap } from 'rxjs/operators' ;
2022-09-21 17:23:45 +02:00
import { SeoService } from '../../services/seo.service' ;
import { WebsocketService } from '../../services/websocket.service' ;
2021-02-27 04:19:56 +07:00
import { BisqApiService } from '../bisq-api.service' ;
2021-04-23 03:49:55 +04:00
import { OffersMarket , Trade } from '../bisq.interfaces' ;
2021-02-27 04:19:56 +07:00
@Component ( {
selector : 'app-bisq-market' ,
templateUrl : './bisq-market.component.html' ,
2021-03-05 02:02:21 +07:00
styleUrls : [ './bisq-market.component.scss' ] ,
changeDetection : ChangeDetectionStrategy.OnPush
2021-02-27 04:19:56 +07:00
} )
2021-03-05 15:38:46 +07:00
export class BisqMarketComponent implements OnInit , OnDestroy {
2021-02-27 04:19:56 +07:00
hlocData$ : Observable < any > ;
currency$ : Observable < any > ;
2021-04-23 03:49:55 +04:00
offers$ : Observable < OffersMarket > ;
trades$ : Observable < Trade [ ] > ;
2022-11-28 11:55:23 +09:00
radioGroupForm : UntypedFormGroup ;
2021-03-13 18:24:50 +07:00
defaultInterval = 'day' ;
2021-02-27 04:19:56 +07:00
2021-04-21 20:22:34 +04:00
isLoadingGraph = false ;
2021-02-27 04:19:56 +07:00
constructor (
2021-03-05 15:38:46 +07:00
private websocketService : WebsocketService ,
2021-02-27 04:19:56 +07:00
private route : ActivatedRoute ,
private bisqApiService : BisqApiService ,
2022-11-28 11:55:23 +09:00
private formBuilder : UntypedFormBuilder ,
2021-03-14 23:24:06 +07:00
private seoService : SeoService ,
2021-04-21 22:12:35 +04:00
private router : Router ,
2021-02-27 04:19:56 +07:00
) { }
ngOnInit ( ) : void {
this . radioGroupForm = this . formBuilder . group ( {
interval : [ this . defaultInterval ] ,
} ) ;
2021-04-21 22:12:35 +04:00
if ( [ 'half_hour' , 'hour' , 'half_day' , 'day' , 'week' , 'month' , 'year' , 'auto' ] . indexOf ( this . route . snapshot . fragment ) > - 1 ) {
this . radioGroupForm . controls . interval . setValue ( this . route . snapshot . fragment , { emitEvent : false } ) ;
}
2021-02-27 04:19:56 +07:00
this . currency $ = this . bisqApiService . getMarkets $ ( )
. pipe (
switchMap ( ( markets ) = > combineLatest ( [ of ( markets ) , this . route . paramMap ] ) ) ,
map ( ( [ markets , routeParams ] ) = > {
const pair = routeParams . get ( 'pair' ) ;
2021-03-14 23:24:06 +07:00
const pairUpperCase = pair . replace ( '_' , '/' ) . toUpperCase ( ) ;
2023-08-28 13:10:08 +09:00
this . seoService . setTitle ( $localize ` :@@meta.title.bisq.market:Bisq market: ${ pairUpperCase } ` ) ;
this . seoService . setDescription ( $localize ` :@@meta.description.bisq.market:See price history, current buy/sell offers, and latest trades for the ${ pairUpperCase } market on Bisq. ` ) ;
2021-03-14 23:24:06 +07:00
2021-02-27 04:19:56 +07:00
return {
2021-03-14 23:24:06 +07:00
pair : pairUpperCase ,
2021-02-27 04:19:56 +07:00
market : markets [ pair ] ,
} ;
} )
) ;
2021-03-13 18:24:50 +07:00
this . trades $ = this . route . paramMap
. pipe (
map ( routeParams = > routeParams . get ( 'pair' ) ) ,
switchMap ( ( marketPair ) = > this . bisqApiService . getMarketTrades $ ( marketPair ) ) ,
) ;
2021-03-05 02:02:21 +07:00
this . offers $ = this . route . paramMap
. pipe (
map ( routeParams = > routeParams . get ( 'pair' ) ) ,
switchMap ( ( marketPair ) = > this . bisqApiService . getMarketOffers $ ( marketPair ) ) ,
2021-03-10 23:02:55 +07:00
map ( ( offers ) = > offers [ Object . keys ( offers ) [ 0 ] ] )
2021-03-05 02:02:21 +07:00
) ;
2021-02-27 04:19:56 +07:00
this . hlocData $ = combineLatest ( [
this . route . paramMap ,
2021-04-21 22:12:35 +04:00
merge ( this . radioGroupForm . get ( 'interval' ) . valueChanges , of ( this . radioGroupForm . get ( 'interval' ) . value ) ) ,
2021-02-27 04:19:56 +07:00
] )
. pipe (
switchMap ( ( [ routeParams , interval ] ) = > {
2021-04-21 20:22:34 +04:00
this . isLoadingGraph = true ;
2021-02-27 04:19:56 +07:00
const pair = routeParams . get ( 'pair' ) ;
return this . bisqApiService . getMarketsHloc $ ( pair , interval ) ;
} ) ,
2021-03-10 23:02:55 +07:00
map ( ( hlocData ) = > {
2021-04-21 20:22:34 +04:00
this . isLoadingGraph = false ;
2021-03-10 23:02:55 +07:00
hlocData = hlocData . map ( ( h ) = > {
2021-02-27 04:19:56 +07:00
h . time = h . period_start ;
return h ;
} ) ;
2021-04-25 02:07:29 +04:00
const hlocVolume = hlocData . map ( ( h ) = > {
return {
time : h.time ,
value : h.volume_right ,
color : h.close > h . avg ? 'rgba(0, 41, 74, 0.7)' : 'rgba(0, 41, 74, 1)' ,
} ;
} ) ;
// Add whitespace
if ( hlocData . length > 1 ) {
const newHloc = [ ] ;
newHloc . push ( hlocData [ 0 ] ) ;
const period = this . getUnixTimestampFromInterval ( this . radioGroupForm . get ( 'interval' ) . value ) ; // temp
let periods = 0 ;
const startingDate = hlocData [ 0 ] . period_start ;
let index = 1 ;
while ( true ) {
periods ++ ;
if ( hlocData [ index ] . period_start > startingDate + period * periods ) {
newHloc . push ( {
time : startingDate + period * periods ,
} ) ;
} else {
newHloc . push ( hlocData [ index ] ) ;
index ++ ;
if ( ! hlocData [ index ] ) {
break ;
}
}
}
hlocData = newHloc ;
}
2021-03-10 23:02:55 +07:00
return {
hloc : hlocData ,
2021-04-25 02:07:29 +04:00
volume : hlocVolume ,
2021-03-10 23:02:55 +07:00
} ;
2021-02-27 04:19:56 +07:00
} ) ,
) ;
}
2021-04-21 22:12:35 +04:00
setFragment ( fragment : string ) {
this . router . navigate ( [ ] , {
relativeTo : this.route ,
queryParamsHandling : 'merge' ,
fragment : fragment
} ) ;
}
2021-03-05 15:38:46 +07:00
ngOnDestroy ( ) : void {
this . websocketService . stopTrackingBisqMarket ( ) ;
}
2021-04-25 02:07:29 +04:00
getUnixTimestampFromInterval ( interval : string ) : number {
switch ( interval ) {
case 'minute' : return 60 ;
case 'half_hour' : return 1800 ;
case 'hour' : return 3600 ;
case 'half_day' : return 43200 ;
case 'day' : return 86400 ;
case 'week' : return 604800 ;
case 'month' : return 2592000 ;
case 'year' : return 31579200 ;
}
}
2021-02-27 04:19:56 +07:00
}