@subwallet/extension-base 1.1.64-0 → 1.1.66-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 (60) hide show
  1. package/cjs/koni/api/dotsama/transfer.js +16 -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/handlers/State.js +2 -0
  5. package/cjs/packageInfo.js +1 -1
  6. package/cjs/services/balance-service/helpers/subscribe/index.js +1 -1
  7. package/cjs/services/balance-service/helpers/subscribe/substrate/index.js +88 -8
  8. package/cjs/services/chain-service/constants.js +8 -1
  9. package/cjs/services/chain-service/handler/EvmChainHandler.js +1 -1
  10. package/cjs/services/chain-service/handler/SubstrateApi.js +15 -9
  11. package/cjs/services/chain-service/handler/SubstrateChainHandler.js +76 -57
  12. package/cjs/services/chain-service/index.js +45 -30
  13. package/cjs/services/chain-service/types.js +1 -1
  14. package/cjs/services/chain-service/utils/index.js +38 -15
  15. package/cjs/services/chain-service/utils/patch.js +1 -1
  16. package/cjs/services/earning-service/constants/chains.js +2 -2
  17. package/cjs/services/earning-service/handlers/native-staking/relay-chain.js +3 -0
  18. package/cjs/services/earning-service/handlers/nomination-pool/index.js +1 -0
  19. package/cjs/services/price-service/index.js +31 -17
  20. package/cjs/stores/CurrentCurrencyStore.js +18 -0
  21. package/cjs/stores/index.js +8 -1
  22. package/cjs/utils/gear/grc20.js +141 -0
  23. package/cjs/utils/gear/index.js +16 -0
  24. package/cjs/utils/index.js +40 -28
  25. package/koni/api/dotsama/transfer.js +18 -2
  26. package/koni/api/staking/bonding/relayChain.d.ts +1 -2
  27. package/koni/api/staking/bonding/relayChain.js +6 -0
  28. package/koni/api/tokens/wasm/utils.js +5 -3
  29. package/koni/background/handlers/State.js +2 -0
  30. package/package.json +32 -15
  31. package/packageInfo.js +1 -1
  32. package/services/balance-service/helpers/subscribe/index.js +1 -1
  33. package/services/balance-service/helpers/subscribe/substrate/index.js +81 -4
  34. package/services/chain-service/constants.d.ts +1 -0
  35. package/services/chain-service/constants.js +8 -1
  36. package/services/chain-service/handler/EvmChainHandler.d.ts +1 -1
  37. package/services/chain-service/handler/EvmChainHandler.js +1 -1
  38. package/services/chain-service/handler/SubstrateApi.js +6 -0
  39. package/services/chain-service/handler/SubstrateChainHandler.d.ts +4 -1
  40. package/services/chain-service/handler/SubstrateChainHandler.js +76 -57
  41. package/services/chain-service/index.js +17 -8
  42. package/services/chain-service/types.js +1 -1
  43. package/services/chain-service/utils/index.d.ts +4 -0
  44. package/services/chain-service/utils/index.js +30 -15
  45. package/services/chain-service/utils/patch.js +1 -1
  46. package/services/earning-service/constants/chains.js +2 -2
  47. package/services/earning-service/handlers/native-staking/relay-chain.js +3 -0
  48. package/services/earning-service/handlers/nomination-pool/index.js +1 -0
  49. package/services/price-service/index.d.ts +4 -1
  50. package/services/price-service/index.js +31 -17
  51. package/stores/CurrentCurrencyStore.d.ts +5 -0
  52. package/stores/CurrentCurrencyStore.js +10 -0
  53. package/stores/index.d.ts +1 -0
  54. package/stores/index.js +2 -1
  55. package/utils/gear/grc20.d.ts +29 -0
  56. package/utils/gear/grc20.js +131 -0
  57. package/utils/gear/index.d.ts +1 -0
  58. package/utils/gear/index.js +4 -0
  59. package/utils/index.d.ts +6 -5
  60. package/utils/index.js +7 -6
@@ -502,6 +502,7 @@ export default class RelayNativeStakingPoolHandler extends BaseNativeStakingPool
502
502
  if (paramsCount === 2) {
503
503
  bondTx = chainApi.api.tx.staking.bond(binaryAmount, bondDest);
504
504
  } else {
505
+ // @ts-ignore
505
506
  bondTx = chainApi.api.tx.staking.bond(address, binaryAmount, bondDest);
506
507
  }
507
508
  nominateTx = chainApi.api.tx.staking.nominate(validatorParamList);
@@ -512,6 +513,7 @@ export default class RelayNativeStakingPoolHandler extends BaseNativeStakingPool
512
513
  if (paramsCount === 2) {
513
514
  bondTx = chainApi.api.tx.staking.bond(binaryAmount, bondDest);
514
515
  } else {
516
+ // @ts-ignore
515
517
  bondTx = chainApi.api.tx.staking.bond(address, binaryAmount, bondDest);
516
518
  }
517
519
  nominateTx = chainApi.api.tx.staking.nominate(validatorParamList);
@@ -619,6 +621,7 @@ export default class RelayNativeStakingPoolHandler extends BaseNativeStakingPool
619
621
  const slashingSpanCount = _slashingSpans !== null ? _slashingSpans.spanIndex : '0';
620
622
  return chainApi.api.tx.staking.withdrawUnbonded(slashingSpanCount);
621
623
  } else {
624
+ // @ts-ignore
622
625
  return chainApi.api.tx.staking.withdrawUnbonded();
623
626
  }
624
627
  }
@@ -565,6 +565,7 @@ export default class NominationPoolHandler extends BasePoolHandler {
565
565
  Id: address
566
566
  }, slashingSpanCount);
567
567
  } else {
568
+ // @ts-ignore
568
569
  return chainApi.api.tx.nominationPools.withdrawUnbonded({
569
570
  Id: address
570
571
  });
@@ -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";