@subwallet/extension-base 1.1.60-0 → 1.1.60-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,6 +13,6 @@ const packageInfo = {
13
13
  name: '@subwallet/extension-base',
14
14
  path: typeof __dirname === 'string' ? __dirname : 'auto',
15
15
  type: 'cjs',
16
- version: '1.1.60-0'
16
+ version: '1.1.60-1'
17
17
  };
18
18
  exports.packageInfo = packageInfo;
@@ -8,7 +8,6 @@ var _constants = require("@subwallet/extension-base/constants");
8
8
  var _types = require("@subwallet/extension-base/services/base/types");
9
9
  var _coingecko = require("@subwallet/extension-base/services/price-service/coingecko");
10
10
  var _storage = require("@subwallet/extension-base/storage");
11
- var _utils = require("@subwallet/extension-base/utils");
12
11
  var _promise = require("@subwallet/extension-base/utils/promise");
13
12
  var _staticData = require("@subwallet/extension-base/utils/staticData");
14
13
  var _rxjs = require("rxjs");
@@ -43,43 +42,6 @@ class PriceService {
43
42
  this.dbService = dbService;
44
43
  this.eventService = eventService;
45
44
  this.chainService = chainService;
46
- const mergeDataSubject = (0, _rxjs.merge)(this.rawPriceSubject, this.rawExchangeRateMap, this.currency);
47
- const onUpdate = () => {
48
- (0, _utils.addLazy)('updatePriceData', () => {
49
- const priceSubjectValue = JSON.parse(JSON.stringify(this.rawPriceSubject.value));
50
- const exchangeRateMapValue = JSON.parse(JSON.stringify(this.rawExchangeRateMap.value));
51
- const currencyKey = this.currency.value;
52
- if (Object.keys(priceSubjectValue).length === 0) {
53
- return;
54
- }
55
- if (Object.keys(exchangeRateMapValue).length === 0) {
56
- return;
57
- }
58
- if (currencyKey === DEFAULT_CURRENCY) {
59
- this.priceSubject.next({
60
- ...priceSubjectValue,
61
- currency: currencyKey,
62
- exchangeRateMap: exchangeRateMapValue,
63
- currencyData: _staticData.staticData[_staticData.StaticKey.CURRENCY_SYMBOL][currencyKey]
64
- });
65
- }
66
- Object.keys(priceSubjectValue.price24hMap).forEach(key => {
67
- priceSubjectValue.price24hMap[key] *= exchangeRateMapValue[currencyKey].exchange;
68
- priceSubjectValue.priceMap[key] *= exchangeRateMapValue[currencyKey].exchange;
69
- });
70
- this.priceSubject.next({
71
- ...priceSubjectValue,
72
- currency: currencyKey,
73
- exchangeRateMap: exchangeRateMapValue,
74
- currencyData: _staticData.staticData[_staticData.StaticKey.CURRENCY_SYMBOL][currencyKey || DEFAULT_CURRENCY]
75
- });
76
- this.dbService.updatePriceStore({
77
- ...priceSubjectValue,
78
- exchangeRateMap: exchangeRateMapValue
79
- }).catch(console.error);
80
- });
81
- };
82
- mergeDataSubject.subscribe(onUpdate);
83
45
  this.init().catch(console.error);
84
46
  }
85
47
  async getTokenPrice(priceIds, currency, resolve, reject) {
@@ -89,6 +51,48 @@ class PriceService {
89
51
  this.rawPriceSubject.next(priceMap);
90
52
  });
91
53
  }
54
+ refreshPromise = null;
55
+ refreshPriceMapByAction() {
56
+ this.refreshPromise = (async () => {
57
+ try {
58
+ await this.refreshPromise;
59
+ const newPriceMap = await this.calculatePriceMap();
60
+ if (newPriceMap) {
61
+ this.priceSubject.next(newPriceMap);
62
+ }
63
+ } catch (e) {
64
+ console.error(e);
65
+ } finally {
66
+ this.refreshPromise = null;
67
+ }
68
+ })();
69
+ }
70
+ async calculatePriceMap() {
71
+ const rawPrice = this.rawPriceSubject.value;
72
+ const exchangeRateData = this.rawExchangeRateMap.value;
73
+ const currencyKey = this.currency.value;
74
+ if (Object.keys(rawPrice).length === 0) {
75
+ return;
76
+ }
77
+ if (Object.keys(exchangeRateData).length === 0) {
78
+ return;
79
+ }
80
+ const finalPriceMap = {
81
+ ...JSON.parse(JSON.stringify(rawPrice)),
82
+ currency: currencyKey,
83
+ exchangeRateMap: exchangeRateData,
84
+ currencyData: _staticData.staticData[_staticData.StaticKey.CURRENCY_SYMBOL][currencyKey || DEFAULT_CURRENCY]
85
+ };
86
+ if (currencyKey === DEFAULT_CURRENCY) {
87
+ return finalPriceMap;
88
+ }
89
+ Object.keys(finalPriceMap.price24hMap).forEach(key => {
90
+ finalPriceMap.price24hMap[key] = rawPrice.price24hMap[key] * exchangeRateData[currencyKey].exchange;
91
+ finalPriceMap.priceMap[key] = rawPrice.priceMap[key] * exchangeRateData[currencyKey].exchange;
92
+ });
93
+ await this.dbService.updatePriceStore(finalPriceMap);
94
+ return finalPriceMap;
95
+ }
92
96
  async getPrice() {
93
97
  return Promise.resolve(this.priceSubject.value);
94
98
  }
@@ -110,16 +114,15 @@ class PriceService {
110
114
  _storage.SWStorage.instance.setItem(_constants.CURRENCY, newCurrencyCode);
111
115
  return true;
112
116
  }
113
- refreshPriceData(priceIds, resolve, reject) {
117
+ refreshPriceData(priceIds) {
114
118
  clearTimeout(this.refreshTimeout);
115
119
  this.priceIds = priceIds || this.getPriceIds();
116
120
 
117
121
  // Update for tokens price
118
122
  this.getTokenPrice(this.priceIds, this.priceSubject.value.currency).then(() => {
119
- resolve && resolve(true);
123
+ this.refreshPriceMapByAction();
120
124
  }).catch(e => {
121
125
  console.error(e);
122
- reject && reject(false);
123
126
  });
124
127
  this.refreshTimeout = setTimeout(this.refreshPriceData.bind(this), _constants.CRON_REFRESH_PRICE_INTERVAL);
125
128
  }
@@ -133,9 +136,17 @@ class PriceService {
133
136
  // Compare two set newPriceIds and this.priceIds
134
137
  if (newPriceIds.size !== this.priceIds.size || !Array.from(newPriceIds).every(v => this.priceIds.has(v))) {
135
138
  this.priceIds = newPriceIds;
136
- this.refreshPriceData(this.priceIds);
139
+ this.refreshPriceMapByAction();
137
140
  }
138
141
  };
142
+ this.currency.subscribe(currency => {
143
+ console.log('Currency changed', currency);
144
+ this.calculatePriceMap().then(data => {
145
+ if (data) {
146
+ this.priceSubject.next(data);
147
+ }
148
+ }).catch(console.error);
149
+ });
139
150
  this.status = _types.ServiceStatus.INITIALIZED;
140
151
  this.eventService.on('asset.updateState', eventHandler);
141
152
  }
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "./cjs/detectPackage.js"
18
18
  ],
19
19
  "type": "module",
20
- "version": "1.1.60-0",
20
+ "version": "1.1.60-1",
21
21
  "main": "./cjs/index.js",
22
22
  "module": "./index.js",
23
23
  "types": "./index.d.ts",
@@ -1875,10 +1875,10 @@
1875
1875
  "@sora-substrate/type-definitions": "^1.17.7",
1876
1876
  "@substrate/connect": "^0.8.9",
1877
1877
  "@subwallet/chain-list": "0.2.58",
1878
- "@subwallet/extension-base": "^1.1.60-0",
1879
- "@subwallet/extension-chains": "^1.1.60-0",
1880
- "@subwallet/extension-dapp": "^1.1.60-0",
1881
- "@subwallet/extension-inject": "^1.1.60-0",
1878
+ "@subwallet/extension-base": "^1.1.60-1",
1879
+ "@subwallet/extension-chains": "^1.1.60-1",
1880
+ "@subwallet/extension-dapp": "^1.1.60-1",
1881
+ "@subwallet/extension-inject": "^1.1.60-1",
1882
1882
  "@subwallet/keyring": "^0.1.5",
1883
1883
  "@subwallet/ui-keyring": "^0.1.5",
1884
1884
  "@walletconnect/sign-client": "^2.8.4",
package/packageInfo.js CHANGED
@@ -7,5 +7,5 @@ export const packageInfo = {
7
7
  name: '@subwallet/extension-base',
8
8
  path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto',
9
9
  type: 'esm',
10
- version: '1.1.60-0'
10
+ version: '1.1.60-1'
11
11
  };
@@ -17,11 +17,14 @@ export declare class PriceService implements StoppableServiceInterface, PersistD
17
17
  private currency;
18
18
  constructor(dbService: DatabaseService, eventService: EventService, chainService: ChainService);
19
19
  private getTokenPrice;
20
+ private refreshPromise;
21
+ private refreshPriceMapByAction;
22
+ private calculatePriceMap;
20
23
  getPrice(): Promise<PriceJson>;
21
24
  getPriceSubject(): BehaviorSubject<PriceJson>;
22
25
  getPriceIds(): Set<string>;
23
26
  setPriceCurrency(newCurrencyCode: CurrencyType): Promise<boolean>;
24
- refreshPriceData(priceIds?: Set<string>, resolve?: (rs: boolean) => void, reject?: (e: boolean) => void): void;
27
+ refreshPriceData(priceIds?: Set<string>): void;
25
28
  init(): Promise<void>;
26
29
  loadData(): Promise<void>;
27
30
  persistData(): Promise<void>;
@@ -5,10 +5,9 @@ import { CRON_REFRESH_PRICE_INTERVAL, CURRENCY } from '@subwallet/extension-base
5
5
  import { ServiceStatus } from '@subwallet/extension-base/services/base/types';
6
6
  import { getExchangeRateMap, getPriceMap } from '@subwallet/extension-base/services/price-service/coingecko';
7
7
  import { SWStorage } from '@subwallet/extension-base/storage';
8
- import { addLazy } from '@subwallet/extension-base/utils';
9
8
  import { createPromiseHandler } from '@subwallet/extension-base/utils/promise';
10
9
  import { staticData, StaticKey } from '@subwallet/extension-base/utils/staticData';
11
- import { BehaviorSubject, merge } from 'rxjs';
10
+ import { BehaviorSubject } from 'rxjs';
12
11
  const DEFAULT_CURRENCY = 'USD';
13
12
  const DEFAULT_PRICE_SUBJECT = {
14
13
  currency: DEFAULT_CURRENCY,
@@ -37,43 +36,6 @@ export class PriceService {
37
36
  this.dbService = dbService;
38
37
  this.eventService = eventService;
39
38
  this.chainService = chainService;
40
- const mergeDataSubject = merge(this.rawPriceSubject, this.rawExchangeRateMap, this.currency);
41
- const onUpdate = () => {
42
- addLazy('updatePriceData', () => {
43
- const priceSubjectValue = JSON.parse(JSON.stringify(this.rawPriceSubject.value));
44
- const exchangeRateMapValue = JSON.parse(JSON.stringify(this.rawExchangeRateMap.value));
45
- const currencyKey = this.currency.value;
46
- if (Object.keys(priceSubjectValue).length === 0) {
47
- return;
48
- }
49
- if (Object.keys(exchangeRateMapValue).length === 0) {
50
- return;
51
- }
52
- if (currencyKey === DEFAULT_CURRENCY) {
53
- this.priceSubject.next({
54
- ...priceSubjectValue,
55
- currency: currencyKey,
56
- exchangeRateMap: exchangeRateMapValue,
57
- currencyData: staticData[StaticKey.CURRENCY_SYMBOL][currencyKey]
58
- });
59
- }
60
- Object.keys(priceSubjectValue.price24hMap).forEach(key => {
61
- priceSubjectValue.price24hMap[key] *= exchangeRateMapValue[currencyKey].exchange;
62
- priceSubjectValue.priceMap[key] *= exchangeRateMapValue[currencyKey].exchange;
63
- });
64
- this.priceSubject.next({
65
- ...priceSubjectValue,
66
- currency: currencyKey,
67
- exchangeRateMap: exchangeRateMapValue,
68
- currencyData: staticData[StaticKey.CURRENCY_SYMBOL][currencyKey || DEFAULT_CURRENCY]
69
- });
70
- this.dbService.updatePriceStore({
71
- ...priceSubjectValue,
72
- exchangeRateMap: exchangeRateMapValue
73
- }).catch(console.error);
74
- });
75
- };
76
- mergeDataSubject.subscribe(onUpdate);
77
39
  this.init().catch(console.error);
78
40
  }
79
41
  async getTokenPrice(priceIds, currency, resolve, reject) {
@@ -82,6 +44,48 @@ export class PriceService {
82
44
  this.rawPriceSubject.next(priceMap);
83
45
  });
84
46
  }
47
+ refreshPromise = null;
48
+ refreshPriceMapByAction() {
49
+ this.refreshPromise = (async () => {
50
+ try {
51
+ await this.refreshPromise;
52
+ const newPriceMap = await this.calculatePriceMap();
53
+ if (newPriceMap) {
54
+ this.priceSubject.next(newPriceMap);
55
+ }
56
+ } catch (e) {
57
+ console.error(e);
58
+ } finally {
59
+ this.refreshPromise = null;
60
+ }
61
+ })();
62
+ }
63
+ async calculatePriceMap() {
64
+ const rawPrice = this.rawPriceSubject.value;
65
+ const exchangeRateData = this.rawExchangeRateMap.value;
66
+ const currencyKey = this.currency.value;
67
+ if (Object.keys(rawPrice).length === 0) {
68
+ return;
69
+ }
70
+ if (Object.keys(exchangeRateData).length === 0) {
71
+ return;
72
+ }
73
+ const finalPriceMap = {
74
+ ...JSON.parse(JSON.stringify(rawPrice)),
75
+ currency: currencyKey,
76
+ exchangeRateMap: exchangeRateData,
77
+ currencyData: staticData[StaticKey.CURRENCY_SYMBOL][currencyKey || DEFAULT_CURRENCY]
78
+ };
79
+ if (currencyKey === DEFAULT_CURRENCY) {
80
+ return finalPriceMap;
81
+ }
82
+ Object.keys(finalPriceMap.price24hMap).forEach(key => {
83
+ finalPriceMap.price24hMap[key] = rawPrice.price24hMap[key] * exchangeRateData[currencyKey].exchange;
84
+ finalPriceMap.priceMap[key] = rawPrice.priceMap[key] * exchangeRateData[currencyKey].exchange;
85
+ });
86
+ await this.dbService.updatePriceStore(finalPriceMap);
87
+ return finalPriceMap;
88
+ }
85
89
  async getPrice() {
86
90
  return Promise.resolve(this.priceSubject.value);
87
91
  }
@@ -103,16 +107,15 @@ export class PriceService {
103
107
  SWStorage.instance.setItem(CURRENCY, newCurrencyCode);
104
108
  return true;
105
109
  }
106
- refreshPriceData(priceIds, resolve, reject) {
110
+ refreshPriceData(priceIds) {
107
111
  clearTimeout(this.refreshTimeout);
108
112
  this.priceIds = priceIds || this.getPriceIds();
109
113
 
110
114
  // Update for tokens price
111
115
  this.getTokenPrice(this.priceIds, this.priceSubject.value.currency).then(() => {
112
- resolve && resolve(true);
116
+ this.refreshPriceMapByAction();
113
117
  }).catch(e => {
114
118
  console.error(e);
115
- reject && reject(false);
116
119
  });
117
120
  this.refreshTimeout = setTimeout(this.refreshPriceData.bind(this), CRON_REFRESH_PRICE_INTERVAL);
118
121
  }
@@ -126,9 +129,17 @@ export class PriceService {
126
129
  // Compare two set newPriceIds and this.priceIds
127
130
  if (newPriceIds.size !== this.priceIds.size || !Array.from(newPriceIds).every(v => this.priceIds.has(v))) {
128
131
  this.priceIds = newPriceIds;
129
- this.refreshPriceData(this.priceIds);
132
+ this.refreshPriceMapByAction();
130
133
  }
131
134
  };
135
+ this.currency.subscribe(currency => {
136
+ console.log('Currency changed', currency);
137
+ this.calculatePriceMap().then(data => {
138
+ if (data) {
139
+ this.priceSubject.next(data);
140
+ }
141
+ }).catch(console.error);
142
+ });
132
143
  this.status = ServiceStatus.INITIALIZED;
133
144
  this.eventService.on('asset.updateState', eventHandler);
134
145
  }