Merge pull request #4793 from mempool/natsoni/fix-fiat-price-on-tesnet

Fix fiat price displayed on test networks
This commit is contained in:
softsimon 2024-03-21 12:08:18 +09:00 committed by GitHub
commit 7f2d77a73b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -101,6 +101,10 @@ export class PriceService {
lastQueriedCurrency: string; lastQueriedCurrency: string;
lastQueriedHistoricalCurrency: string; lastQueriedHistoricalCurrency: string;
network: string;
networkChangedSinceLastQuery = false;
networkChangedSinceLastSingleQuery = false;
historicalPrice: ConversionDict = { historicalPrice: ConversionDict = {
prices: null, prices: null,
exchangeRates: null, exchangeRates: null,
@ -110,6 +114,13 @@ export class PriceService {
private apiService: ApiService, private apiService: ApiService,
private stateService: StateService private stateService: StateService
) { ) {
this.stateService.networkChanged$.subscribe((network: string) => {
if (this.network !== network) {
this.network = network;
this.networkChangedSinceLastQuery = true;
this.networkChangedSinceLastSingleQuery = true;
}
});
} }
getEmptyPrice(): Price { getEmptyPrice(): Price {
@ -144,10 +155,11 @@ export class PriceService {
* query a different timestamp than the last one * query a different timestamp than the last one
*/ */
if (singlePrice) { if (singlePrice) {
if (!this.singlePriceObservable$ || (this.singlePriceObservable$ && (blockTimestamp !== this.lastQueriedTimestamp || currency !== this.lastQueriedCurrency))) { if (!this.singlePriceObservable$ || (this.singlePriceObservable$ && (blockTimestamp !== this.lastQueriedTimestamp || currency !== this.lastQueriedCurrency || this.networkChangedSinceLastSingleQuery))) {
this.singlePriceObservable$ = this.apiService.getHistoricalPrice$(blockTimestamp, currency).pipe(shareReplay()); this.singlePriceObservable$ = this.apiService.getHistoricalPrice$(blockTimestamp, currency).pipe(shareReplay());
this.lastQueriedTimestamp = blockTimestamp; this.lastQueriedTimestamp = blockTimestamp;
this.lastQueriedCurrency = currency; this.lastQueriedCurrency = currency;
this.networkChangedSinceLastSingleQuery = false;
} }
return this.singlePriceObservable$.pipe( return this.singlePriceObservable$.pipe(
@ -180,10 +192,11 @@ export class PriceService {
* Query all price history only once. The observable is invalidated after 1 hour * Query all price history only once. The observable is invalidated after 1 hour
*/ */
else { else {
if (!this.priceObservable$ || (this.priceObservable$ && (now - this.lastPriceHistoryUpdate > 3600 || currency !== this.lastQueriedHistoricalCurrency))) { if (!this.priceObservable$ || (this.priceObservable$ && (now - this.lastPriceHistoryUpdate > 3600 || currency !== this.lastQueriedHistoricalCurrency || this.networkChangedSinceLastQuery))) {
this.priceObservable$ = this.apiService.getHistoricalPrice$(undefined, currency).pipe(shareReplay()); this.priceObservable$ = this.apiService.getHistoricalPrice$(undefined, currency).pipe(shareReplay());
this.lastPriceHistoryUpdate = new Date().getTime() / 1000; this.lastPriceHistoryUpdate = new Date().getTime() / 1000;
this.lastQueriedHistoricalCurrency = currency; this.lastQueriedHistoricalCurrency = currency;
this.networkChangedSinceLastQuery = false;
} }
return this.priceObservable$.pipe( return this.priceObservable$.pipe(