@subwallet/extension-base 1.1.65-0 → 1.1.67-0

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.
Files changed (65) hide show
  1. package/cjs/koni/api/dotsama/transfer.js +11 -0
  2. package/cjs/koni/api/staking/bonding/relayChain.js +6 -0
  3. package/cjs/koni/api/tokens/wasm/utils.js +5 -3
  4. package/cjs/koni/background/cron.js +13 -0
  5. package/cjs/koni/background/handlers/State.js +2 -0
  6. package/cjs/packageInfo.js +1 -1
  7. package/cjs/services/balance-service/helpers/subscribe/index.js +1 -1
  8. package/cjs/services/balance-service/helpers/subscribe/substrate/index.js +67 -0
  9. package/cjs/services/chain-service/constants.js +8 -1
  10. package/cjs/services/chain-service/handler/EvmChainHandler.js +1 -1
  11. package/cjs/services/chain-service/handler/SubstrateApi.js +15 -9
  12. package/cjs/services/chain-service/handler/SubstrateChainHandler.js +76 -57
  13. package/cjs/services/chain-service/index.js +45 -30
  14. package/cjs/services/chain-service/types.js +1 -1
  15. package/cjs/services/chain-service/utils/index.js +38 -15
  16. package/cjs/services/chain-service/utils/patch.js +1 -1
  17. package/cjs/services/earning-service/constants/chains.js +2 -2
  18. package/cjs/services/earning-service/handlers/native-staking/relay-chain.js +3 -0
  19. package/cjs/services/earning-service/handlers/nomination-pool/index.js +1 -0
  20. package/cjs/services/earning-service/service.js +2 -2
  21. package/cjs/services/price-service/index.js +31 -17
  22. package/cjs/stores/CurrentCurrencyStore.js +18 -0
  23. package/cjs/stores/index.js +8 -1
  24. package/cjs/utils/gear/grc20.js +141 -0
  25. package/cjs/utils/gear/index.js +16 -0
  26. package/cjs/utils/index.js +40 -28
  27. package/koni/api/dotsama/transfer.js +13 -2
  28. package/koni/api/staking/bonding/relayChain.d.ts +1 -2
  29. package/koni/api/staking/bonding/relayChain.js +6 -0
  30. package/koni/api/tokens/wasm/utils.js +5 -3
  31. package/koni/background/cron.d.ts +2 -0
  32. package/koni/background/cron.js +14 -1
  33. package/koni/background/handlers/State.js +2 -0
  34. package/package.json +32 -15
  35. package/packageInfo.js +1 -1
  36. package/services/balance-service/helpers/subscribe/index.js +1 -1
  37. package/services/balance-service/helpers/subscribe/substrate/index.js +67 -2
  38. package/services/chain-service/constants.d.ts +1 -0
  39. package/services/chain-service/constants.js +8 -1
  40. package/services/chain-service/handler/EvmChainHandler.d.ts +1 -1
  41. package/services/chain-service/handler/EvmChainHandler.js +1 -1
  42. package/services/chain-service/handler/SubstrateApi.js +6 -0
  43. package/services/chain-service/handler/SubstrateChainHandler.d.ts +4 -1
  44. package/services/chain-service/handler/SubstrateChainHandler.js +76 -57
  45. package/services/chain-service/index.js +17 -8
  46. package/services/chain-service/types.js +1 -1
  47. package/services/chain-service/utils/index.d.ts +4 -0
  48. package/services/chain-service/utils/index.js +30 -15
  49. package/services/chain-service/utils/patch.js +1 -1
  50. package/services/earning-service/constants/chains.js +2 -2
  51. package/services/earning-service/handlers/native-staking/relay-chain.js +3 -0
  52. package/services/earning-service/handlers/nomination-pool/index.js +1 -0
  53. package/services/earning-service/service.js +2 -2
  54. package/services/price-service/index.d.ts +4 -1
  55. package/services/price-service/index.js +31 -17
  56. package/stores/CurrentCurrencyStore.d.ts +5 -0
  57. package/stores/CurrentCurrencyStore.js +10 -0
  58. package/stores/index.d.ts +1 -0
  59. package/stores/index.js +2 -1
  60. package/utils/gear/grc20.d.ts +29 -0
  61. package/utils/gear/grc20.js +131 -0
  62. package/utils/gear/index.d.ts +1 -0
  63. package/utils/gear/index.js +4 -0
  64. package/utils/index.d.ts +6 -5
  65. package/utils/index.js +7 -6
@@ -190,7 +190,7 @@ export default class EarningService {
190
190
  this.status = ServiceStatus.STARTING;
191
191
 
192
192
  // Start subscribe pools' info
193
- await this.runSubscribePoolsInfo();
193
+ // await this.runSubscribePoolsInfo();
194
194
 
195
195
  // Start subscribe pools' position
196
196
  await this.runSubscribePoolsPosition();
@@ -217,7 +217,7 @@ export default class EarningService {
217
217
  await this.persistData();
218
218
 
219
219
  // Stop subscribe pools' info
220
- this.runUnsubscribePoolsInfo();
220
+ // this.runUnsubscribePoolsInfo();
221
221
 
222
222
  // Stop subscribe pools' position
223
223
  this.runUnsubscribePoolsPosition();
@@ -14,9 +14,12 @@ export declare class PriceService implements StoppableServiceInterface, PersistD
14
14
  private rawExchangeRateMap;
15
15
  private refreshTimeout;
16
16
  private priceIds;
17
- private currency;
17
+ private readonly currency;
18
18
  constructor(dbService: DatabaseService, eventService: EventService, chainService: ChainService);
19
19
  private getTokenPrice;
20
+ private getCurrentCurrencySubject;
21
+ private setCurrentCurrency;
22
+ private getCurrentCurrency;
20
23
  private refreshPromise;
21
24
  private refreshPriceMapByAction;
22
25
  private calculatePriceMap;
@@ -5,6 +5,7 @@ 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 { CurrentCurrencyStore } from '@subwallet/extension-base/stores';
8
9
  import { createPromiseHandler } from '@subwallet/extension-base/utils/promise';
9
10
  import { staticData, StaticKey } from '@subwallet/extension-base/utils/staticData';
10
11
  import { BehaviorSubject } from 'rxjs';
@@ -23,12 +24,10 @@ const DEFAULT_PRICE_SUBJECT = {
23
24
  };
24
25
  export class PriceService {
25
26
  priceIds = new Set();
27
+ currency = new CurrentCurrencyStore();
26
28
  constructor(dbService, eventService, chainService) {
27
- const currency = SWStorage.instance.getItem(CURRENCY);
28
- this.currency = new BehaviorSubject(currency || DEFAULT_CURRENCY);
29
29
  this.priceSubject = new BehaviorSubject({
30
- ...DEFAULT_PRICE_SUBJECT,
31
- currency: this.currency.value
30
+ ...DEFAULT_PRICE_SUBJECT
32
31
  });
33
32
  this.rawPriceSubject = new BehaviorSubject({});
34
33
  this.rawExchangeRateMap = new BehaviorSubject({});
@@ -36,7 +35,11 @@ export class PriceService {
36
35
  this.dbService = dbService;
37
36
  this.eventService = eventService;
38
37
  this.chainService = chainService;
39
- this.init().catch(console.error);
38
+ const updateCurrency = currentCurrency => {
39
+ const currency = SWStorage.instance.getItem(CURRENCY);
40
+ this.setCurrentCurrency(currency || currentCurrency || DEFAULT_CURRENCY);
41
+ };
42
+ this.init().then(() => this.getCurrentCurrency(updateCurrency)).catch(console.error);
40
43
  }
41
44
  async getTokenPrice(priceIds, currency, resolve, reject) {
42
45
  await Promise.all([getExchangeRateMap(), getPriceMap(priceIds, currency)]).then(([exchangeRateMap, priceMap]) => {
@@ -44,12 +47,29 @@ export class PriceService {
44
47
  this.rawPriceSubject.next(priceMap);
45
48
  });
46
49
  }
50
+ getCurrentCurrencySubject() {
51
+ return this.currency.getSubject();
52
+ }
53
+ setCurrentCurrency(currency) {
54
+ this.currency.set('Currency', currency);
55
+ }
56
+ getCurrentCurrency(update) {
57
+ this.currency.get('Currency', value => {
58
+ update(value || DEFAULT_CURRENCY);
59
+ });
60
+ }
47
61
  refreshPromise = null;
48
62
  refreshPriceMapByAction() {
49
63
  this.refreshPromise = (async () => {
50
64
  try {
51
65
  await this.refreshPromise;
52
- const newPriceMap = await this.calculatePriceMap();
66
+ const {
67
+ promise,
68
+ resolve
69
+ } = createPromiseHandler();
70
+ this.getCurrentCurrency(resolve);
71
+ const currencyKey = await promise;
72
+ const newPriceMap = await this.calculatePriceMap(currencyKey || DEFAULT_CURRENCY);
53
73
  if (newPriceMap) {
54
74
  this.priceSubject.next(newPriceMap);
55
75
  }
@@ -60,16 +80,13 @@ export class PriceService {
60
80
  }
61
81
  })();
62
82
  }
63
- async calculatePriceMap() {
83
+ async calculatePriceMap(currency) {
64
84
  const {
65
85
  price24hMap,
66
86
  priceMap
67
87
  } = this.rawPriceSubject.value;
68
88
  const exchangeRateData = this.rawExchangeRateMap.value;
69
- const currencyKey = this.currency.value;
70
- if (Object.keys(this.rawPriceSubject.value).length === 0) {
71
- return;
72
- }
89
+ const currencyKey = currency || DEFAULT_CURRENCY;
73
90
  if (Object.keys(exchangeRateData).length === 0) {
74
91
  return;
75
92
  }
@@ -105,10 +122,7 @@ export class PriceService {
105
122
  return new Set(priceIdList);
106
123
  }
107
124
  async setPriceCurrency(newCurrencyCode) {
108
- if (newCurrencyCode === this.currency.value) {
109
- return false;
110
- }
111
- this.currency.next(newCurrencyCode);
125
+ this.setCurrentCurrency(newCurrencyCode);
112
126
 
113
127
  // Await 1s to get the latest exchange rate
114
128
  await new Promise(resolve => setTimeout(resolve, 300));
@@ -140,9 +154,9 @@ export class PriceService {
140
154
  this.refreshPriceMapByAction();
141
155
  }
142
156
  };
143
- this.currency.subscribe(currency => {
157
+ this.getCurrentCurrencySubject().subscribe(currency => {
144
158
  console.log('Currency changed', currency);
145
- this.calculatePriceMap().then(data => {
159
+ this.calculatePriceMap(currency).then(data => {
146
160
  if (data) {
147
161
  this.priceSubject.next(data);
148
162
  }
@@ -0,0 +1,5 @@
1
+ import { CurrencyType } from '@subwallet/extension-base/background/KoniTypes';
2
+ import SubscribableStore from '@subwallet/extension-base/stores/SubscribableStore';
3
+ export default class CurrentCurrencyStore extends SubscribableStore<CurrencyType> {
4
+ constructor();
5
+ }
@@ -0,0 +1,10 @@
1
+ // Copyright 2019-2022 @subwallet/extension-koni-ui authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { EXTENSION_PREFIX } from '@subwallet/extension-base/defaults';
5
+ import SubscribableStore from '@subwallet/extension-base/stores/SubscribableStore';
6
+ export default class CurrentCurrencyStore extends SubscribableStore {
7
+ constructor() {
8
+ super(EXTENSION_PREFIX ? `${EXTENSION_PREFIX}current_currency` : null);
9
+ }
10
+ }
package/stores/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { default as AccountsStore } from './Accounts';
2
2
  export { default as MetadataStore } from './Metadata';
3
3
  export { default as CurrentAccountStore } from './CurrentAccountStore';
4
+ export { default as CurrentCurrencyStore } from './CurrentCurrencyStore';
package/stores/index.js CHANGED
@@ -3,4 +3,5 @@
3
3
 
4
4
  export { default as AccountsStore } from "./Accounts.js";
5
5
  export { default as MetadataStore } from "./Metadata.js";
6
- export { default as CurrentAccountStore } from "./CurrentAccountStore.js";
6
+ export { default as CurrentAccountStore } from "./CurrentAccountStore.js";
7
+ export { default as CurrentCurrencyStore } from "./CurrentCurrencyStore.js";
@@ -0,0 +1,29 @@
1
+ /// <reference types="node" />
2
+ import { GearApi } from '@gear-js/api';
3
+ import { TransactionBuilder } from 'sails-js';
4
+ import { ApiPromise } from '@polkadot/api';
5
+ export declare type ActorId = `0x${string}`;
6
+ export declare type U256 = bigint;
7
+ export declare const DEFAULT_GEAR_ADDRESS: {
8
+ ALICE: string;
9
+ BOB: string;
10
+ };
11
+ export declare class GRC20 {
12
+ api: GearApi;
13
+ programId: `0x${string}`;
14
+ private registry;
15
+ constructor(api: GearApi, programId?: `0x${string}`);
16
+ newCtorFromCode(code: Uint8Array | Buffer, name: string, symbol: string, decimals: number | string): TransactionBuilder<null>;
17
+ newCtorFromCodeId(codeId: `0x${string}`, name: string, symbol: string, decimals: number | string): TransactionBuilder<null>;
18
+ approve(spender: ActorId, value: U256): TransactionBuilder<boolean>;
19
+ fromTransfer(from: ActorId, to: ActorId, value: U256): TransactionBuilder<boolean>;
20
+ setBalance(newBalance: U256): TransactionBuilder<boolean>;
21
+ transfer(to: ActorId, value: U256): TransactionBuilder<boolean>;
22
+ allowance(owner: ActorId, spender: ActorId, originAddress: string, value?: number | string | bigint, atBlock?: `0x${string}`): Promise<U256>;
23
+ balanceOf(owner: ActorId, originAddress: string, value?: number | string | bigint, atBlock?: `0x${string}`): Promise<U256>;
24
+ decimals(originAddress: string, value?: number | string | bigint, atBlock?: `0x${string}`): Promise<number | string>;
25
+ name(originAddress: string, value?: number | string | bigint, atBlock?: `0x${string}`): Promise<string>;
26
+ symbol(originAddress: string, value?: number | string | bigint, atBlock?: `0x${string}`): Promise<string>;
27
+ totalSupply(originAddress: string, value?: number | string | bigint, atBlock?: `0x${string}`): Promise<U256>;
28
+ }
29
+ export declare function getGRC20ContractPromise(apiPromise: ApiPromise, contractAddress: string): GRC20;
@@ -0,0 +1,131 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // https://github.com/breathx/gear-erc20/blob/master/js/src/lib.ts
5
+
6
+ import { decodeAddress } from '@gear-js/api';
7
+ import { TransactionBuilder } from 'sails-js';
8
+ import { TypeRegistry } from '@polkadot/types';
9
+ export const DEFAULT_GEAR_ADDRESS = {
10
+ ALICE: '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
11
+ BOB: '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty'
12
+ };
13
+ export class GRC20 {
14
+ constructor(api, programId = '0x') {
15
+ this.api = api;
16
+ this.programId = programId;
17
+ const types = {
18
+ ActorId: '([u8; 32])',
19
+ U256: '([u64; 4])'
20
+ };
21
+ this.registry = new TypeRegistry();
22
+ this.registry.setKnownTypes({
23
+ types
24
+ });
25
+ this.registry.register(types);
26
+ }
27
+ newCtorFromCode(code, name, symbol, decimals) {
28
+ const builder = new TransactionBuilder(this.api, this.registry, 'upload_program', ['New', name, symbol, decimals], '(String, String, String, u8)', 'String', code);
29
+ this.programId = builder.programId;
30
+ return builder;
31
+ }
32
+ newCtorFromCodeId(codeId, name, symbol, decimals) {
33
+ const builder = new TransactionBuilder(this.api, this.registry, 'create_program', ['New', name, symbol, decimals], '(String, String, String, u8)', 'String', codeId);
34
+ this.programId = builder.programId;
35
+ return builder;
36
+ }
37
+ approve(spender, value) {
38
+ return new TransactionBuilder(this.api, this.registry, 'send_message', ['Approve', spender, value], '(String, ActorId, U256)', 'bool', this.programId);
39
+ }
40
+ fromTransfer(from, to, value) {
41
+ return new TransactionBuilder(this.api, this.registry, 'send_message', ['FromTransfer', from, to, value], '(String, ActorId, ActorId, U256)', 'bool', this.programId);
42
+ }
43
+ setBalance(newBalance) {
44
+ return new TransactionBuilder(this.api, this.registry, 'send_message', ['SetBalance', newBalance], '(String, U256)', 'bool', this.programId);
45
+ }
46
+ transfer(to, value) {
47
+ return new TransactionBuilder(this.api, this.registry, 'send_message', ['Transfer', to, value], '(String, ActorId, U256)', 'bool', this.programId);
48
+ }
49
+ async allowance(owner, spender, originAddress, value, atBlock) {
50
+ const payload = this.registry.createType('(String, ActorId, ActorId)', ['Allowance', owner, spender]).toU8a();
51
+ const reply = await this.api.message.calculateReply({
52
+ destination: this.programId,
53
+ origin: decodeAddress(originAddress),
54
+ payload,
55
+ value: value || 0,
56
+ gasLimit: this.api.blockGasLimit.toBigInt(),
57
+ at: atBlock
58
+ });
59
+ const result = this.registry.createType('(String, U256)', reply.payload);
60
+ return result[1].toBigInt();
61
+ }
62
+ async balanceOf(owner, originAddress, value, atBlock) {
63
+ const payload = this.registry.createType('(String, ActorId)', ['BalanceOf', owner]).toU8a();
64
+ const reply = await this.api.message.calculateReply({
65
+ destination: this.programId,
66
+ origin: decodeAddress(originAddress),
67
+ payload,
68
+ value: value || 0,
69
+ gasLimit: this.api.blockGasLimit.toBigInt(),
70
+ at: atBlock
71
+ });
72
+ const result = this.registry.createType('(String, U256)', reply.payload);
73
+ return result[1].toBigInt();
74
+ }
75
+ async decimals(originAddress, value, atBlock) {
76
+ const payload = this.registry.createType('String', 'Decimals').toU8a();
77
+ const reply = await this.api.message.calculateReply({
78
+ destination: this.programId,
79
+ origin: decodeAddress(originAddress),
80
+ payload,
81
+ value: value || 0,
82
+ gasLimit: this.api.blockGasLimit.toBigInt(),
83
+ at: atBlock
84
+ });
85
+ const result = this.registry.createType('(String, u8)', reply.payload);
86
+ return result[1].toNumber();
87
+ }
88
+ async name(originAddress, value, atBlock) {
89
+ const payload = this.registry.createType('String', 'Name').toU8a();
90
+ const reply = await this.api.message.calculateReply({
91
+ destination: this.programId,
92
+ origin: decodeAddress(originAddress),
93
+ payload,
94
+ value: value || 0,
95
+ gasLimit: this.api.blockGasLimit.toBigInt(),
96
+ at: atBlock
97
+ });
98
+ const result = this.registry.createType('(String, String)', reply.payload);
99
+ return result[1].toString();
100
+ }
101
+ async symbol(originAddress, value, atBlock) {
102
+ const payload = this.registry.createType('String', 'Symbol').toU8a();
103
+ const reply = await this.api.message.calculateReply({
104
+ destination: this.programId,
105
+ origin: decodeAddress(originAddress),
106
+ payload,
107
+ value: value || 0,
108
+ gasLimit: this.api.blockGasLimit.toBigInt(),
109
+ at: atBlock
110
+ });
111
+ const result = this.registry.createType('(String, String)', reply.payload);
112
+ return result[1].toString();
113
+ }
114
+ async totalSupply(originAddress, value, atBlock) {
115
+ const payload = this.registry.createType('String', 'TotalSupply').toU8a();
116
+ const reply = await this.api.message.calculateReply({
117
+ destination: this.programId,
118
+ origin: decodeAddress(originAddress),
119
+ payload,
120
+ value: value || 0,
121
+ gasLimit: this.api.blockGasLimit.toBigInt(),
122
+ at: atBlock
123
+ });
124
+ const result = this.registry.createType('(String, U256)', reply.payload);
125
+ return result[1].toBigInt();
126
+ }
127
+ }
128
+ export function getGRC20ContractPromise(apiPromise, contractAddress) {
129
+ const gearApi = apiPromise;
130
+ return new GRC20(gearApi, contractAddress);
131
+ }
@@ -0,0 +1 @@
1
+ export * from './grc20';
@@ -0,0 +1,4 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export * from "./grc20.js";
package/utils/index.d.ts CHANGED
@@ -45,12 +45,13 @@ export * from './array';
45
45
  export * from './asset';
46
46
  export * from './environment';
47
47
  export * from './eth';
48
- export * from './number';
48
+ export * from './fetchEvmChainInfo';
49
+ export * from './fetchStaticData';
50
+ export * from './gear';
49
51
  export * from './lazy';
52
+ export * from './number';
53
+ export * from './object';
50
54
  export * from './promise';
51
55
  export * from './registry';
52
- export * from './translate';
53
- export * from './object';
54
- export * from './fetchStaticData';
55
- export * from './fetchEvmChainInfo';
56
56
  export * from './swap';
57
+ export * from './translate';
package/utils/index.js CHANGED
@@ -347,12 +347,13 @@ export * from "./array.js";
347
347
  export * from "./asset.js";
348
348
  export * from "./environment.js";
349
349
  export * from "./eth.js";
350
- export * from "./number.js";
350
+ export * from "./fetchEvmChainInfo.js";
351
+ export * from "./fetchStaticData.js";
352
+ export * from "./gear/index.js";
351
353
  export * from "./lazy.js";
354
+ export * from "./number.js";
355
+ export * from "./object.js";
352
356
  export * from "./promise.js";
353
357
  export * from "./registry.js";
354
- export * from "./translate.js";
355
- export * from "./object.js";
356
- export * from "./fetchStaticData.js";
357
- export * from "./fetchEvmChainInfo.js";
358
- export * from "./swap.js";
358
+ export * from "./swap.js";
359
+ export * from "./translate.js";