2021-02-27 04:19:56 +07:00
import { ChangeDetectionStrategy , Component , OnInit } from '@angular/core' ;
2021-04-23 15:09:51 +04:00
import { Observable , combineLatest , BehaviorSubject , of } from 'rxjs' ;
import { map , share , switchMap } from 'rxjs/operators' ;
2022-09-21 17:23:45 +02:00
import { SeoService } from '../../services/seo.service' ;
import { StateService } from '../../services/state.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 { Trade } from '../bisq.interfaces' ;
2021-02-27 04:19:56 +07:00
@Component ( {
selector : 'app-bisq-dashboard' ,
templateUrl : './bisq-dashboard.component.html' ,
styleUrls : [ './bisq-dashboard.component.scss' ] ,
changeDetection : ChangeDetectionStrategy.OnPush
} )
export class BisqDashboardComponent implements OnInit {
tickers$ : Observable < any > ;
2021-03-16 01:17:40 +07:00
volumes$ : Observable < any > ;
2021-04-23 03:49:55 +04:00
trades$ : Observable < Trade [ ] > ;
2021-04-23 15:09:51 +04:00
sort $ = new BehaviorSubject < string > ( 'trades' ) ;
2021-03-16 01:17:40 +07:00
2021-03-14 02:42:14 +07:00
allowCryptoCoins = [ 'usdc' , 'l-btc' , 'bsq' ] ;
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 bisqApiService : BisqApiService ,
2021-04-25 19:40:43 +04:00
public stateService : StateService ,
2021-03-14 23:24:06 +07:00
private seoService : SeoService ,
2021-02-27 04:19:56 +07:00
) { }
ngOnInit ( ) : void {
2023-08-28 13:10:08 +09:00
this . seoService . setTitle ( $localize ` :@@meta.title.bisq.markets:Markets ` ) ;
this . seoService . setDescription ( $localize ` :@@meta.description.bisq.markets:Explore the full Bitcoin ecosystem with The Mempool Open Project™. See Bisq market prices, trading activity, and more. ` ) ;
2021-03-05 15:38:46 +07:00
this . websocketService . want ( [ 'blocks' ] ) ;
2021-03-16 01:17:40 +07:00
this . volumes $ = this . bisqApiService . getAllVolumesDay $ ( )
. pipe (
map ( ( volumes ) = > {
const data = volumes . map ( ( volume ) = > {
return {
time : volume.period_start ,
value : volume.volume ,
} ;
} ) ;
const linesData = volumes . map ( ( volume ) = > {
return {
time : volume.period_start ,
value : volume.num_trades ,
} ;
} ) ;
return {
data : data ,
linesData : linesData ,
} ;
} )
) ;
2021-04-23 03:49:55 +04:00
const getMarkets = this . bisqApiService . getMarkets $ ( ) . pipe ( share ( ) ) ;
2021-02-27 04:19:56 +07:00
this . tickers $ = combineLatest ( [
this . bisqApiService . getMarketsTicker $ ( ) ,
2021-04-23 03:49:55 +04:00
getMarkets ,
2021-03-10 23:02:55 +07:00
this . bisqApiService . getMarketVolumesByTime $ ( '7d' ) ,
2021-02-27 04:19:56 +07:00
] )
. pipe (
2021-02-28 17:18:29 +07:00
map ( ( [ tickers , markets , volumes ] ) = > {
2021-03-14 02:42:14 +07:00
2021-02-27 04:19:56 +07:00
const newTickers = [ ] ;
for ( const t in tickers ) {
2021-03-14 02:42:14 +07:00
2021-07-27 17:10:38 +03:00
if ( this . stateService . env . BASE_MODULE !== 'bisq' ) {
2021-03-14 02:42:14 +07:00
const pair = t . split ( '_' ) ;
if ( pair [ 1 ] === 'btc' && this . allowCryptoCoins . indexOf ( pair [ 0 ] ) === - 1 ) {
continue ;
}
}
2021-04-23 03:49:55 +04:00
const mappedTicker : any = tickers [ t ] ;
mappedTicker . pair_url = t ;
mappedTicker . pair = t . replace ( '_' , '/' ) . toUpperCase ( ) ;
mappedTicker . market = markets [ t ] ;
mappedTicker . volume = volumes [ t ] ;
2021-04-23 15:09:51 +04:00
mappedTicker . name = ` ${ mappedTicker . market . rtype === 'crypto' ? mappedTicker.market.lname : mappedTicker.market.rname } ( ${ mappedTicker . market . rtype === 'crypto' ? mappedTicker.market.lsymbol : mappedTicker.market.rsymbol } ` ;
2021-04-23 03:49:55 +04:00
newTickers . push ( mappedTicker ) ;
2021-02-27 04:19:56 +07:00
}
return newTickers ;
2021-04-23 15:09:51 +04:00
} ) ,
switchMap ( ( tickers ) = > combineLatest ( [ this . sort $ , of ( tickers ) ] ) ) ,
map ( ( [ sort , tickers ] ) = > {
if ( sort === 'trades' ) {
tickers . sort ( ( a , b ) = > ( b . volume && b . volume . num_trades || 0 ) - ( a . volume && a . volume . num_trades || 0 ) ) ;
} else if ( sort === 'volumes' ) {
tickers . sort ( ( a , b ) = > ( b . volume && b . volume . volume || 0 ) - ( a . volume && a . volume . volume || 0 ) ) ;
} else if ( sort === 'name' ) {
tickers . sort ( ( a , b ) = > a . name . localeCompare ( b . name ) ) ;
}
return tickers ;
2021-02-27 04:19:56 +07:00
} )
) ;
2021-04-23 03:49:55 +04:00
this . trades $ = combineLatest ( [
this . bisqApiService . getMarketTrades $ ( 'all' ) ,
getMarkets ,
] )
. pipe (
map ( ( [ trades , markets ] ) = > {
2021-07-27 17:10:38 +03:00
if ( this . stateService . env . BASE_MODULE !== 'bisq' ) {
2021-04-24 20:33:56 +04:00
trades = trades . filter ( ( trade ) = > {
const pair = trade . market . split ( '_' ) ;
return ! ( pair [ 1 ] === 'btc' && this . allowCryptoCoins . indexOf ( pair [ 0 ] ) === - 1 ) ;
} ) ;
}
2021-04-23 15:09:51 +04:00
return trades . map ( ( trade = > {
trade . _market = markets [ trade . market ] ;
return trade ;
} ) ) ;
} )
) ;
2021-02-27 04:19:56 +07:00
}
trackByFn ( index : number ) {
return index ;
}
2021-04-23 15:09:51 +04:00
sort ( by : string ) {
this . sort $ . next ( by ) ;
}
2021-02-27 04:19:56 +07:00
}