2020-05-02 12:36:35 +07:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { HttpClient } from '@angular/common/http';
|
2020-05-09 20:37:50 +07:00
|
|
|
import { Observable } from 'rxjs';
|
2022-01-04 05:26:46 +04:00
|
|
|
import { map, shareReplay, switchMap } from 'rxjs/operators';
|
2020-11-23 18:22:21 +07:00
|
|
|
import { StateService } from './state.service';
|
2022-02-06 03:31:50 +04:00
|
|
|
import { environment } from 'src/environments/environment';
|
|
|
|
import { AssetExtended } from '../interfaces/electrs.interface';
|
2020-05-02 12:36:35 +07:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class AssetsService {
|
2022-02-06 03:31:50 +04:00
|
|
|
nativeAssetId = this.stateService.network === 'liquidtestnet' ? environment.nativeTestAssetId : environment.nativeAssetId;
|
|
|
|
|
2022-02-06 04:18:56 +04:00
|
|
|
getAssetsJson$: Observable<{ array: AssetExtended[]; objects: any}>;
|
2020-05-05 15:26:23 +07:00
|
|
|
getAssetsMinimalJson$: Observable<any>;
|
2020-05-02 12:36:35 +07:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private httpClient: HttpClient,
|
2020-11-23 18:22:21 +07:00
|
|
|
private stateService: StateService,
|
2020-05-02 12:36:35 +07:00
|
|
|
) {
|
2020-11-27 23:01:47 +09:00
|
|
|
let apiBaseUrl = '';
|
2020-11-23 18:22:21 +07:00
|
|
|
if (!this.stateService.isBrowser) {
|
2020-11-27 23:01:47 +09:00
|
|
|
apiBaseUrl = this.stateService.env.NGINX_PROTOCOL + '://' + this.stateService.env.NGINX_HOSTNAME + ':' + this.stateService.env.NGINX_PORT;
|
2020-11-23 18:22:21 +07:00
|
|
|
}
|
|
|
|
|
2021-12-29 00:42:34 +04:00
|
|
|
this.getAssetsJson$ = this.stateService.networkChanged$
|
|
|
|
.pipe(
|
|
|
|
switchMap(() => this.httpClient.get(`${apiBaseUrl}/resources/assets${this.stateService.network === 'liquidtestnet' ? '-testnet' : ''}.json`)),
|
2022-02-06 03:31:50 +04:00
|
|
|
map((rawAssets) => {
|
|
|
|
const assets: AssetExtended[] = Object.values(rawAssets);
|
|
|
|
|
|
|
|
if (this.stateService.network === 'liquid') {
|
|
|
|
// @ts-ignore
|
|
|
|
assets.push({
|
|
|
|
name: 'Liquid Bitcoin',
|
|
|
|
ticker: 'L-BTC',
|
|
|
|
asset_id: this.nativeAssetId,
|
|
|
|
});
|
|
|
|
} else if (this.stateService.network === 'liquidtestnet') {
|
|
|
|
// @ts-ignore
|
|
|
|
assets.push({
|
|
|
|
name: 'Test Liquid Bitcoin',
|
|
|
|
ticker: 'tL-BTC',
|
|
|
|
asset_id: this.nativeAssetId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-06 04:18:56 +04:00
|
|
|
return {
|
|
|
|
objects: rawAssets,
|
|
|
|
array: assets.sort((a: any, b: any) => a.name.localeCompare(b.name)),
|
|
|
|
};
|
2022-02-06 03:31:50 +04:00
|
|
|
}),
|
2021-12-29 00:42:34 +04:00
|
|
|
shareReplay(1),
|
|
|
|
);
|
|
|
|
this.getAssetsMinimalJson$ = this.stateService.networkChanged$
|
|
|
|
.pipe(
|
|
|
|
switchMap(() => this.httpClient.get(`${apiBaseUrl}/resources/assets${this.stateService.network === 'liquidtestnet' ? '-testnet' : ''}.minimal.json`)),
|
2022-01-04 05:26:46 +04:00
|
|
|
map((assetsMinimal) => {
|
|
|
|
if (this.stateService.network === 'liquidtestnet') {
|
|
|
|
// Hard coding the Liquid Testnet native asset
|
|
|
|
assetsMinimal['144c654344aa716d6f3abcc1ca90e5641e4e2a7f633bc09fe3baf64585819a49'] = [null, "tL-BTC", "Test Liquid Bitcoin", 8];
|
|
|
|
}
|
|
|
|
return assetsMinimal;
|
|
|
|
}),
|
2021-12-29 00:42:34 +04:00
|
|
|
shareReplay(1),
|
|
|
|
);
|
2020-05-02 16:29:34 +07:00
|
|
|
}
|
2020-05-02 12:36:35 +07:00
|
|
|
}
|