@subwallet/extension-base 1.0.7-1 → 1.0.8-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 (55) hide show
  1. package/background/KoniTypes.d.ts +6 -4
  2. package/background/KoniTypes.js +1 -0
  3. package/background/errors/TransactionError.js +5 -1
  4. package/cjs/background/KoniTypes.js +1 -0
  5. package/cjs/background/errors/TransactionError.js +4 -0
  6. package/cjs/constants/index.js +6 -3
  7. package/cjs/koni/api/dotsama/balance.js +2 -1
  8. package/cjs/koni/api/dotsama/crowdloan.js +1 -1
  9. package/cjs/koni/api/dotsama/transfer.js +2 -2
  10. package/cjs/koni/api/staking/bonding/astar.js +5 -4
  11. package/cjs/koni/api/staking/bonding/relayChain.js +12 -3
  12. package/cjs/koni/api/staking/bonding/utils.js +7 -0
  13. package/cjs/koni/api/xcm/polkadotXcm.js +18 -37
  14. package/cjs/koni/api/xcm/utils.js +78 -11
  15. package/cjs/koni/api/xcm/xTokens.js +4 -33
  16. package/cjs/koni/api/xcm/xcmPallet.js +4 -36
  17. package/cjs/koni/background/handlers/Extension.js +179 -97
  18. package/cjs/koni/background/handlers/State.js +1 -1
  19. package/cjs/packageInfo.js +1 -1
  20. package/cjs/services/chain-service/constants.js +8 -6
  21. package/cjs/services/chain-service/index.js +19 -15
  22. package/cjs/services/chain-service/utils.js +1 -5
  23. package/cjs/services/transaction-service/helpers/index.js +45 -2
  24. package/cjs/services/transaction-service/index.js +58 -24
  25. package/cjs/utils/number.js +112 -0
  26. package/constants/index.d.ts +1 -0
  27. package/constants/index.js +1 -0
  28. package/koni/api/dotsama/balance.js +2 -1
  29. package/koni/api/dotsama/crowdloan.js +2 -2
  30. package/koni/api/dotsama/transfer.js +2 -2
  31. package/koni/api/staking/bonding/astar.js +5 -4
  32. package/koni/api/staking/bonding/relayChain.js +13 -4
  33. package/koni/api/staking/bonding/utils.d.ts +5 -0
  34. package/koni/api/staking/bonding/utils.js +6 -0
  35. package/koni/api/xcm/polkadotXcm.js +20 -39
  36. package/koni/api/xcm/utils.d.ts +36 -3
  37. package/koni/api/xcm/utils.js +72 -11
  38. package/koni/api/xcm/xTokens.js +6 -35
  39. package/koni/api/xcm/xcmPallet.js +5 -35
  40. package/koni/background/handlers/Extension.js +109 -29
  41. package/koni/background/handlers/State.js +2 -2
  42. package/package.json +13 -8
  43. package/packageInfo.js +1 -1
  44. package/services/chain-service/constants.d.ts +1 -0
  45. package/services/chain-service/constants.js +8 -7
  46. package/services/chain-service/index.js +13 -8
  47. package/services/chain-service/types.d.ts +8 -0
  48. package/services/chain-service/utils.d.ts +0 -1
  49. package/services/chain-service/utils.js +1 -4
  50. package/services/transaction-service/helpers/index.d.ts +2 -0
  51. package/services/transaction-service/helpers/index.js +42 -0
  52. package/services/transaction-service/index.js +54 -20
  53. package/services/transaction-service/types.d.ts +2 -2
  54. package/utils/number.d.ts +9 -0
  55. package/utils/number.js +100 -0
@@ -10,7 +10,7 @@ import { ALL_ACCOUNT_KEY, ALL_GENESIS_HASH } from '@subwallet/extension-base/con
10
10
  import { BalanceService } from '@subwallet/extension-base/services/balance-service';
11
11
  import { ChainService } from '@subwallet/extension-base/services/chain-service';
12
12
  import { _PREDEFINED_SINGLE_MODES } from '@subwallet/extension-base/services/chain-service/constants';
13
- import { _getEvmChainId, _getSubstrateGenesisHash, _isAssetFungibleToken, _isChainEnabled, _isChainTestNet, _isSubstrateParachain, _parseMetadataForSmartContractAsset } from '@subwallet/extension-base/services/chain-service/utils';
13
+ import { _getEvmChainId, _getSubstrateGenesisHash, _isAssetFungibleToken, _isChainEnabled, _isChainTestNet, _isSubstrateParaChain, _parseMetadataForSmartContractAsset } from '@subwallet/extension-base/services/chain-service/utils';
14
14
  import { EventService } from '@subwallet/extension-base/services/event-service';
15
15
  import { HistoryService } from '@subwallet/extension-base/services/history-service';
16
16
  import { KeyringService } from '@subwallet/extension-base/services/keyring-service';
@@ -44,7 +44,7 @@ const getSuri = (seed, type) => {
44
44
  const generateDefaultCrowdloanMap = () => {
45
45
  const crowdloanMap = {};
46
46
  Object.entries(ChainInfoMap).forEach(([networkKey, chainInfo]) => {
47
- if (_isSubstrateParachain(chainInfo)) {
47
+ if (_isSubstrateParaChain(chainInfo)) {
48
48
  crowdloanMap[networkKey] = {
49
49
  state: APIItemState.PENDING,
50
50
  contribute: '0'
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "./cjs/detectPackage.js"
18
18
  ],
19
19
  "type": "module",
20
- "version": "1.0.7-1",
20
+ "version": "1.0.8-0",
21
21
  "main": "./cjs/index.js",
22
22
  "module": "./index.js",
23
23
  "types": "./index.d.ts",
@@ -1635,6 +1635,11 @@
1635
1635
  "require": "./cjs/utils/keyring.js",
1636
1636
  "default": "./utils/keyring.js"
1637
1637
  },
1638
+ "./utils/number": {
1639
+ "types": "./utils/number.d.ts",
1640
+ "require": "./cjs/utils/number.js",
1641
+ "default": "./utils/number.js"
1642
+ },
1638
1643
  "./utils/promise": {
1639
1644
  "types": "./utils/promise.d.ts",
1640
1645
  "require": "./cjs/utils/promise.js",
@@ -1697,13 +1702,13 @@
1697
1702
  "@sora-substrate/type-definitions": "^1.17.7",
1698
1703
  "@subsocial/types": "^0.6.8",
1699
1704
  "@substrate/connect": "^0.7.26",
1700
- "@subwallet/chain-list": "^0.1.9",
1701
- "@subwallet/extension-base": "^1.0.7-1",
1702
- "@subwallet/extension-chains": "^1.0.7-1",
1703
- "@subwallet/extension-dapp": "^1.0.7-1",
1704
- "@subwallet/extension-inject": "^1.0.7-1",
1705
- "@subwallet/keyring": "^0.0.9",
1706
- "@subwallet/ui-keyring": "^0.0.9",
1705
+ "@subwallet/chain-list": "0.1.10",
1706
+ "@subwallet/extension-base": "^1.0.8-0",
1707
+ "@subwallet/extension-chains": "^1.0.8-0",
1708
+ "@subwallet/extension-dapp": "^1.0.8-0",
1709
+ "@subwallet/extension-inject": "^1.0.8-0",
1710
+ "@subwallet/keyring": "^0.0.10",
1711
+ "@subwallet/ui-keyring": "^0.0.10",
1707
1712
  "@unique-nft/types": "^0.6.0-4",
1708
1713
  "@zeitgeistpm/type-defs": "^1.0.0",
1709
1714
  "@zeroio/type-definitions": "^0.0.14",
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.0.7-1'
10
+ version: '1.0.8-0'
11
11
  };
@@ -36,6 +36,7 @@ export declare const _STAKING_CHAIN_GROUP: {
36
36
  nominationPool: string[];
37
37
  bifrost: string[];
38
38
  aleph: string[];
39
+ ternoa: string[];
39
40
  };
40
41
  export declare const _STAKING_ERA_LENGTH_MAP: Record<string, number>;
41
42
  export declare const _PARACHAIN_INFLATION_DISTRIBUTION: Record<string, Record<string, number>>;
@@ -25,8 +25,8 @@ export const _BALANCE_CHAIN_GROUP = {
25
25
  kintsugi: ['kintsugi', 'interlay', 'kintsugi_test'],
26
26
  genshiro: ['genshiro_testnet', 'genshiro'],
27
27
  equilibrium_parachain: ['equilibrium_parachain'],
28
- bifrost: ['bifrost', 'acala', 'karura', 'acala_testnet', 'pioneer', 'bitcountry'],
29
- statemine: ['statemine', 'astar', 'shiden', 'statemint', 'moonbeam', 'moonbase', 'moonriver', 'crabParachain', 'darwinia2'],
28
+ bifrost: ['bifrost', 'acala', 'karura', 'acala_testnet', 'pioneer', 'bitcountry', 'bifrost_dot', 'hydradx_main'],
29
+ statemine: ['statemine', 'astar', 'shiden', 'statemint', 'moonbeam', 'moonbase', 'moonriver', 'crabParachain', 'darwinia2', 'parallel'],
30
30
  kusama: ['kusama', 'kintsugi', 'kintsugi_test', 'interlay', 'acala', 'statemint', 'karura', 'bifrost'] // perhaps there are some runtime updates
31
31
  };
32
32
 
@@ -55,9 +55,10 @@ export const _STAKING_CHAIN_GROUP = {
55
55
  kilt: ['kilt', 'kilt_peregrine'],
56
56
  nominationPool: ['polkadot', 'kusama', 'westend', 'alephTest', 'aleph'],
57
57
  bifrost: ['bifrost', 'bifrost_testnet'],
58
- aleph: ['aleph', 'alephTest'] // A0 has distinct tokenomics
58
+ aleph: ['aleph', 'alephTest'],
59
+ // A0 has distinct tokenomics
60
+ ternoa: ['ternoa', 'ternoa_alphanet']
59
61
  };
60
-
61
62
  export const _STAKING_ERA_LENGTH_MAP = {
62
63
  // in hours
63
64
  alephTest: 24,
@@ -175,11 +176,11 @@ export const _KNOWN_CHAIN_INFLATION_PARAMS = {
175
176
  export const _TRANSFER_NOT_SUPPORTED_CHAINS = ['subspace_gemini_3a', 'kulupu', 'joystream', 'equilibrium_parachain', 'genshiro_testnet', 'genshiro'];
176
177
  export const _TRANSFER_CHAIN_GROUP = {
177
178
  acala: ['karura', 'acala', 'acala_testnet'],
178
- kintsugi: ['kintsugi', 'kintsugi_test', 'interlay'],
179
+ kintsugi: ['kintsugi', 'kintsugi_test', 'interlay', 'bifrost_dot', 'hydradx_main'],
179
180
  genshiro: ['genshiro_testnet', 'genshiro', 'equilibrium_parachain'],
180
181
  crab: ['crab', 'pangolin'],
181
182
  bitcountry: ['pioneer', 'bitcountry'],
182
- statemine: ['statemint', 'statemine', 'darwinia2']
183
+ statemine: ['statemint', 'statemine', 'darwinia2', 'astar', 'parallel']
183
184
  };
184
185
  export const _BALANCE_PARSING_CHAIN_GROUP = {
185
186
  bobabeam: ['bobabeam', 'bobabase']
@@ -188,7 +189,7 @@ export const _BALANCE_PARSING_CHAIN_GROUP = {
188
189
  // XCM------------------------------------------------------------------------------------------------------------------
189
190
 
190
191
  export const _XCM_CHAIN_GROUP = {
191
- polkadotXcm: ['astar', 'shiden', 'statemine', 'statemint'],
192
+ polkadotXcm: ['astar', 'shiden', 'statemine', 'statemint', 'equilibrium_parachain'],
192
193
  xcmPallet: ['polkadot', 'kusama']
193
194
  // default is xTokens pallet
194
195
  };
@@ -40,21 +40,25 @@ export class ChainService {
40
40
  this.chainInfoMapSubject.next(this.dataMap.chainInfoMap);
41
41
  this.chainStateMapSubject.next(this.dataMap.chainStateMap);
42
42
  this.assetRegistrySubject.next(this.dataMap.assetRegistry);
43
- this.xcmRefMapSubject.next(this.getXcmRefMap());
43
+ this.xcmRefMapSubject.next(this.dataMap.assetRefMap);
44
44
  this.logger = createLogger('chain-service');
45
45
  this.refreshChainStateInterval(3000, 6);
46
46
  }
47
47
 
48
48
  // Getter
49
49
  getXcmRefMap() {
50
- const result = {};
51
- Object.entries(AssetRefMap).forEach(([key, assetRef]) => {
52
- if (assetRef.path === _AssetRefPath.XCM) {
53
- result[key] = assetRef;
54
- }
55
- });
56
- return result;
50
+ return this.dataMap.assetRefMap;
51
+ // const result: Record<string, _AssetRef> = {};
52
+ //
53
+ // Object.entries(AssetRefMap).forEach(([key, assetRef]) => {
54
+ // if (assetRef.path === _AssetRefPath.XCM) {
55
+ // result[key] = assetRef;
56
+ // }
57
+ // });
58
+ //
59
+ // return result;
57
60
  }
61
+
58
62
  getEvmApi(slug) {
59
63
  return this.evmChainHandler.getEvmApiByChain(slug);
60
64
  }
@@ -356,6 +360,7 @@ export class ChainService {
356
360
  this.chainInfoMapSubject.next(this.getChainInfoMap());
357
361
  this.chainStateMapSubject.next(this.getChainStateMap());
358
362
  this.assetRegistrySubject.next(this.getAssetRegistry());
363
+ this.xcmRefMapSubject.next(this.dataMap.assetRefMap);
359
364
  this.initApis();
360
365
  await this.initAssetSettings();
361
366
  }
@@ -93,6 +93,14 @@ export declare type _NetworkUpsertParams = {
93
93
  providerError?: _CHAIN_VALIDATION_ERROR;
94
94
  };
95
95
  export declare const _CUSTOM_PREFIX = "custom-";
96
+ export interface EnableChainParams {
97
+ chainSlug: string;
98
+ enableTokens?: boolean;
99
+ }
100
+ export interface EnableMultiChainParams {
101
+ chainSlugs: string[];
102
+ enableTokens?: boolean;
103
+ }
96
104
  export interface _ValidateCustomAssetRequest {
97
105
  contractAddress: string;
98
106
  originChain: string;
@@ -26,7 +26,6 @@ export declare function _getSubstrateRelayParent(chainInfo: _ChainInfo): string;
26
26
  export declare function _getSubstrateGenesisHash(chainInfo: _ChainInfo): string;
27
27
  export declare function _isChainSupportSubstrateStaking(chainInfo: _ChainInfo): boolean;
28
28
  export declare function _isChainEnabled(chainState: _ChainState): boolean;
29
- export declare function _isSubstrateParachain(chainInfo: _ChainInfo): boolean;
30
29
  export declare function _getChainSubstrateAddressPrefix(chainInfo: _ChainInfo): number;
31
30
  export declare function _isChainSupportNativeNft(chainInfo: _ChainInfo): boolean;
32
31
  export declare function _isChainSupportEvmNft(chainInfo: _ChainInfo): boolean;
@@ -130,9 +130,6 @@ export function _isChainSupportSubstrateStaking(chainInfo) {
130
130
  export function _isChainEnabled(chainState) {
131
131
  return chainState.active;
132
132
  }
133
- export function _isSubstrateParachain(chainInfo) {
134
- return chainInfo.substrateInfo !== null && chainInfo.substrateInfo.paraId !== null;
135
- }
136
133
  export function _getChainSubstrateAddressPrefix(chainInfo) {
137
134
  var _chainInfo$substrateI5, _chainInfo$substrateI6;
138
135
  return (_chainInfo$substrateI5 = chainInfo === null || chainInfo === void 0 ? void 0 : (_chainInfo$substrateI6 = chainInfo.substrateInfo) === null || _chainInfo$substrateI6 === void 0 ? void 0 : _chainInfo$substrateI6.addressPrefix) !== null && _chainInfo$substrateI5 !== void 0 ? _chainInfo$substrateI5 : -1;
@@ -259,7 +256,7 @@ export function _isSubstrateRelayChain(chainInfo) {
259
256
  }
260
257
  export function _isSubstrateParaChain(chainInfo) {
261
258
  var _chainInfo$substrateI11;
262
- return ((_chainInfo$substrateI11 = chainInfo.substrateInfo) === null || _chainInfo$substrateI11 === void 0 ? void 0 : _chainInfo$substrateI11.chainType) === _SubstrateChainType.PARACHAIN;
259
+ return chainInfo.substrateInfo !== null && chainInfo.substrateInfo.paraId !== null && ((_chainInfo$substrateI11 = chainInfo.substrateInfo) === null || _chainInfo$substrateI11 === void 0 ? void 0 : _chainInfo$substrateI11.chainType) === _SubstrateChainType.PARACHAIN;
263
260
  }
264
261
  export function _getEvmAbiExplorer(chainInfo) {
265
262
  var _chainInfo$evmInfo3;
@@ -1,5 +1,7 @@
1
+ import { _ChainInfo } from '@subwallet/chain-list/types';
1
2
  import { SWTransaction } from '@subwallet/extension-base/services/transaction-service/types';
2
3
  import { SubmittableExtrinsic } from '@polkadot/api/promise/types';
3
4
  export declare const getTransactionId: (chainType: string, chain: string, isInternal: boolean) => string;
4
5
  export declare const getValidationId: (chainType: string, chain: string) => string;
5
6
  export declare const isSubstrateTransaction: (tx: SWTransaction['transaction']) => tx is SubmittableExtrinsic;
7
+ export declare const getBaseTransactionInfo: (transaction: SWTransaction, chainInfoMap: Record<string, _ChainInfo>) => string;
@@ -1,6 +1,7 @@
1
1
  // Copyright 2019-2022 @subwallet/extension-base authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ import { ExtrinsicType } from '@subwallet/extension-base/background/KoniTypes';
4
5
  let transactionCount = 0;
5
6
  let validationCount = 0;
6
7
  export const getTransactionId = (chainType, chain, isInternal) => {
@@ -11,4 +12,45 @@ export const getValidationId = (chainType, chain) => {
11
12
  };
12
13
  export const isSubstrateTransaction = tx => {
13
14
  return !!tx.send;
15
+ };
16
+ const typeName = type => {
17
+ switch (type) {
18
+ case ExtrinsicType.TRANSFER_BALANCE:
19
+ case ExtrinsicType.TRANSFER_TOKEN:
20
+ case ExtrinsicType.TRANSFER_XCM:
21
+ return 'Transfer';
22
+ case ExtrinsicType.SEND_NFT:
23
+ return 'Send NFT';
24
+ case ExtrinsicType.CROWDLOAN:
25
+ return 'Crowdloan contribution';
26
+ case ExtrinsicType.STAKING_JOIN_POOL:
27
+ return 'Join pool';
28
+ case ExtrinsicType.STAKING_LEAVE_POOL:
29
+ return 'Leave pool';
30
+ case ExtrinsicType.STAKING_BOND:
31
+ return 'Bond';
32
+ case ExtrinsicType.STAKING_UNBOND:
33
+ return 'Unbond';
34
+ case ExtrinsicType.STAKING_CLAIM_REWARD:
35
+ return 'Claim reward';
36
+ case ExtrinsicType.STAKING_WITHDRAW:
37
+ return 'Withdraw';
38
+ case ExtrinsicType.STAKING_CANCEL_UNSTAKE:
39
+ return 'Cancel unstake';
40
+ case ExtrinsicType.STAKING_COMPOUNDING:
41
+ return 'Stake compound';
42
+ case ExtrinsicType.EVM_EXECUTE:
43
+ return 'EVM execute';
44
+ case ExtrinsicType.STAKING_CANCEL_COMPOUNDING:
45
+ return 'Cancel compounding';
46
+ case ExtrinsicType.STAKING_POOL_WITHDRAW:
47
+ return 'Withdraw pool';
48
+ case ExtrinsicType.UNKNOWN:
49
+ default:
50
+ return 'unknown';
51
+ }
52
+ };
53
+ export const getBaseTransactionInfo = (transaction, chainInfoMap) => {
54
+ var _chainInfoMap$transac;
55
+ return `${typeName(transaction.extrinsicType)} on ${((_chainInfoMap$transac = chainInfoMap[transaction.chain]) === null || _chainInfoMap$transac === void 0 ? void 0 : _chainInfoMap$transac.name) || 'unknown network'}`;
14
56
  };
@@ -6,11 +6,12 @@ import { TransactionError } from '@subwallet/extension-base/background/errors/Tr
6
6
  import { BasicTxErrorType, BasicTxWarningCode, ChainType, EvmProviderErrorType, ExtrinsicStatus, ExtrinsicType, NotificationType, TransactionDirection } from '@subwallet/extension-base/background/KoniTypes';
7
7
  import { TransactionWarning } from '@subwallet/extension-base/background/warnings/TransactionWarning';
8
8
  import { ALL_ACCOUNT_KEY } from '@subwallet/extension-base/constants';
9
+ import { _TRANSFER_CHAIN_GROUP } from '@subwallet/extension-base/services/chain-service/constants';
9
10
  import { _getChainNativeTokenBasicInfo, _getEvmChainId } from '@subwallet/extension-base/services/chain-service/utils';
10
11
  import { EXTENSION_REQUEST_URL } from '@subwallet/extension-base/services/request-service/constants';
11
12
  import { TRANSACTION_TIMEOUT } from '@subwallet/extension-base/services/transaction-service/constants';
12
13
  import { parseTransferEventLogs, parseXcmEventLogs } from '@subwallet/extension-base/services/transaction-service/event-parser';
13
- import { getTransactionId, isSubstrateTransaction } from '@subwallet/extension-base/services/transaction-service/helpers';
14
+ import { getBaseTransactionInfo, getTransactionId, isSubstrateTransaction } from '@subwallet/extension-base/services/transaction-service/helpers';
14
15
  import { getExplorerLink, parseTransactionData } from '@subwallet/extension-base/services/transaction-service/utils';
15
16
  import { anyNumberToBN } from '@subwallet/extension-base/utils/eth';
16
17
  import { parseTxAndSignature } from '@subwallet/extension-base/utils/eth/mergeTransactionAndSignature';
@@ -19,7 +20,7 @@ import keyring from '@subwallet/ui-keyring';
19
20
  import EventEmitter from 'eventemitter3';
20
21
  import RLP from 'rlp';
21
22
  import { BehaviorSubject } from 'rxjs';
22
- import { u8aToHex } from '@polkadot/util';
23
+ import { isHex, u8aToHex } from '@polkadot/util';
23
24
  export default class TransactionService {
24
25
  transactionSubject = new BehaviorSubject({});
25
26
  get transactions() {
@@ -109,6 +110,10 @@ export default class TransactionService {
109
110
  }
110
111
  }
111
112
  } catch (e) {
113
+ const error = e;
114
+ if (error.message.includes('gas required exceeds allowance')) {
115
+ validationResponse.errors.push(new TransactionError(BasicTxErrorType.NOT_ENOUGH_BALANCE));
116
+ }
112
117
  estimateFee.value = '0';
113
118
  }
114
119
  }
@@ -135,16 +140,22 @@ export default class TransactionService {
135
140
  const balanceNum = parseInt(balance.value);
136
141
  const edNum = parseInt(existentialDeposit);
137
142
  const transferNativeNum = parseInt(transferNative);
138
- if (!isTransferAll) {
139
- if (transferNativeNum + feeNum > balanceNum) {
143
+ if (transferNativeNum + feeNum > balanceNum) {
144
+ if (!isTransferAll) {
140
145
  validationResponse.errors.push(new TransactionError(BasicTxErrorType.NOT_ENOUGH_BALANCE));
141
146
  } else {
142
- if (balanceNum - (transferNativeNum + feeNum) <= edNum) {
143
- if (edAsWarning) {
144
- validationResponse.warnings.push(new TransactionWarning(BasicTxWarningCode.NOT_ENOUGH_EXISTENTIAL_DEPOSIT, ''));
145
- } else {
146
- validationResponse.errors.push(new TransactionError(BasicTxErrorType.NOT_ENOUGH_EXISTENTIAL_DEPOSIT, ''));
147
- }
147
+ if ([..._TRANSFER_CHAIN_GROUP.acala, ..._TRANSFER_CHAIN_GROUP.genshiro, ..._TRANSFER_CHAIN_GROUP.bitcountry, ..._TRANSFER_CHAIN_GROUP.statemine].includes(chain)) {
148
+ // Chain not have transfer all function
149
+ validationResponse.errors.push(new TransactionError(BasicTxErrorType.NOT_ENOUGH_BALANCE));
150
+ }
151
+ }
152
+ }
153
+ if (!isTransferAll) {
154
+ if (balanceNum - (transferNativeNum + feeNum) < edNum) {
155
+ if (edAsWarning) {
156
+ validationResponse.warnings.push(new TransactionWarning(BasicTxWarningCode.NOT_ENOUGH_EXISTENTIAL_DEPOSIT, ''));
157
+ } else {
158
+ validationResponse.errors.push(new TransactionError(BasicTxErrorType.NOT_ENOUGH_EXISTENTIAL_DEPOSIT, ''));
148
159
  }
149
160
  }
150
161
  }
@@ -206,6 +217,7 @@ export default class TransactionService {
206
217
  // @ts-ignore
207
218
  'transaction' in validatedTransaction && delete validatedTransaction.transaction;
208
219
  'additionalValidator' in validatedTransaction && delete validatedTransaction.additionalValidator;
220
+ 'eventsHandler' in validatedTransaction && delete validatedTransaction.eventsHandler;
209
221
  return validatedTransaction;
210
222
  }
211
223
  validatedTransaction.warnings = [];
@@ -227,11 +239,15 @@ export default class TransactionService {
227
239
  // @ts-ignore
228
240
  'transaction' in validatedTransaction && delete validatedTransaction.transaction;
229
241
  'additionalValidator' in validatedTransaction && delete validatedTransaction.additionalValidator;
242
+ 'eventsHandler' in validatedTransaction && delete validatedTransaction.eventsHandler;
230
243
  return validatedTransaction;
231
244
  }
232
245
  async sendTransaction(transaction) {
233
246
  // Send Transaction
234
247
  const emitter = transaction.chainType === 'substrate' ? this.signAndSendSubstrateTransaction(transaction) : await this.signAndSendEvmTransaction(transaction);
248
+ const {
249
+ eventsHandler
250
+ } = transaction;
235
251
  emitter.on('signed', data => {
236
252
  this.onSigned(data);
237
253
  });
@@ -255,6 +271,7 @@ export default class TransactionService {
255
271
 
256
272
  // Todo: handle any event with transaction.eventsHandler
257
273
 
274
+ eventsHandler === null || eventsHandler === void 0 ? void 0 : eventsHandler(emitter);
258
275
  return emitter;
259
276
  }
260
277
  removeTransaction(id) {
@@ -281,6 +298,7 @@ export default class TransactionService {
281
298
  }
282
299
  transactionToHistories(id, startBlock, nonce, eventLogs) {
283
300
  const transaction = this.getTransaction(id);
301
+ const extrinsicType = transaction.extrinsicType;
284
302
  const historyItem = {
285
303
  origin: 'app',
286
304
  chain: transaction.chain,
@@ -311,7 +329,7 @@ export default class TransactionService {
311
329
  };
312
330
 
313
331
  // Fill data by extrinsicType
314
- switch (transaction.extrinsicType) {
332
+ switch (extrinsicType) {
315
333
  case ExtrinsicType.TRANSFER_BALANCE:
316
334
  {
317
335
  const inputData = parseTransactionData(transaction.data);
@@ -350,9 +368,10 @@ export default class TransactionService {
350
368
  };
351
369
 
352
370
  // @ts-ignore
353
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
354
371
  historyItem.additionalInfo = {
355
- destinationChain: (inputData === null || inputData === void 0 ? void 0 : inputData.destinationNetworkKey) || ''
372
+ destinationChain: (inputData === null || inputData === void 0 ? void 0 : inputData.destinationNetworkKey) || '',
373
+ originalChain: inputData.originNetworkKey || '',
374
+ fee: transaction.estimateFee
356
375
  };
357
376
  eventLogs && parseXcmEventLogs(historyItem, eventLogs, transaction.chain, sendingTokenInfo, chainInfo);
358
377
  }
@@ -451,11 +470,22 @@ export default class TransactionService {
451
470
  // Return one more history record if transaction send to account in the wallets
452
471
  const toAccount = (historyItem === null || historyItem === void 0 ? void 0 : historyItem.to) && keyring.getPair(historyItem.to);
453
472
  if (toAccount) {
454
- return [historyItem, {
473
+ const receiverHistory = {
455
474
  ...historyItem,
456
475
  address: toAccount.address,
457
476
  direction: TransactionDirection.RECEIVED
458
- }];
477
+ };
478
+ switch (extrinsicType) {
479
+ case ExtrinsicType.TRANSFER_XCM:
480
+ {
481
+ const inputData = parseTransactionData(transaction.data);
482
+ receiverHistory.chain = inputData.destinationNetworkKey;
483
+ break;
484
+ }
485
+ default:
486
+ break;
487
+ }
488
+ return [historyItem, receiverHistory];
459
489
  }
460
490
  } catch (e) {
461
491
  console.warn(e);
@@ -543,10 +573,11 @@ export default class TransactionService {
543
573
  blockNumber: blockNumber || 0,
544
574
  blockHash: blockHash || ''
545
575
  }).catch(console.error);
576
+ const info = isHex(extrinsicHash) ? extrinsicHash : getBaseTransactionInfo(transaction, this.chainService.getChainInfoMap());
546
577
  this.notificationService.notify({
547
578
  type: NotificationType.SUCCESS,
548
579
  title: 'Transaction completed',
549
- message: `Transaction ${transaction === null || transaction === void 0 ? void 0 : transaction.extrinsicHash} completed`,
580
+ message: `Transaction ${info} completed`,
550
581
  action: {
551
582
  url: this.getTransactionLink(id)
552
583
  },
@@ -577,10 +608,11 @@ export default class TransactionService {
577
608
  blockNumber: blockNumber || 0,
578
609
  blockHash: blockHash || ''
579
610
  }).catch(console.error);
611
+ const info = isHex(transaction === null || transaction === void 0 ? void 0 : transaction.extrinsicHash) ? transaction === null || transaction === void 0 ? void 0 : transaction.extrinsicHash : getBaseTransactionInfo(transaction, this.chainService.getChainInfoMap());
580
612
  this.notificationService.notify({
581
613
  type: NotificationType.ERROR,
582
614
  title: 'Transaction failed',
583
- message: `Transaction ${transaction === null || transaction === void 0 ? void 0 : transaction.extrinsicHash} failed`,
615
+ message: `Transaction ${info} failed`,
584
616
  action: {
585
617
  url: this.getTransactionLink(id)
586
618
  },
@@ -674,7 +706,8 @@ export default class TransactionService {
674
706
  const eventData = {
675
707
  id,
676
708
  errors: [],
677
- warnings: []
709
+ warnings: [],
710
+ extrinsicHash: id
678
711
  };
679
712
  this.requestService.addConfirmation(id, url || EXTENSION_REQUEST_URL, 'evmSendTransactionRequest', payload, {}).then(async ({
680
713
  isApproved,
@@ -745,7 +778,8 @@ export default class TransactionService {
745
778
  const eventData = {
746
779
  id,
747
780
  errors: [],
748
- warnings: []
781
+ warnings: [],
782
+ extrinsicHash: id
749
783
  };
750
784
  transaction.signAsync(address, {
751
785
  signer: {
@@ -775,7 +809,7 @@ export default class TransactionService {
775
809
  }
776
810
  if (txState.status.isInBlock) {
777
811
  eventData.eventLogs = txState.events;
778
- if (!eventData.extrinsicHash || eventData.extrinsicHash === '') {
812
+ if (!eventData.extrinsicHash || eventData.extrinsicHash === '' || !isHex(eventData.extrinsicHash)) {
779
813
  eventData.extrinsicHash = txState.txHash.toHex();
780
814
  eventData.blockHash = txState.status.asInBlock.toHex();
781
815
  emitter.emit('extrinsicHash', eventData);
@@ -21,8 +21,8 @@ export interface SWTransaction extends ValidateTransactionResponse, Partial<Pick
21
21
  additionalValidator?: (inputTransaction: SWTransactionResponse) => Promise<void>;
22
22
  eventsHandler?: (eventEmitter: TransactionEmitter) => void;
23
23
  }
24
- export declare type SWTransactionResult = Omit<SWTransaction, 'transaction' | 'additionalValidator'>;
25
- declare type SwInputBase = Pick<SWTransaction, 'address' | 'url' | 'data' | 'extrinsicType' | 'chain' | 'chainType' | 'ignoreWarnings' | 'transferNativeAmount'> & Partial<Pick<SWTransaction, 'additionalValidator'>>;
24
+ export declare type SWTransactionResult = Omit<SWTransaction, 'transaction' | 'additionalValidator' | 'eventsHandler'>;
25
+ declare type SwInputBase = Pick<SWTransaction, 'address' | 'url' | 'data' | 'extrinsicType' | 'chain' | 'chainType' | 'ignoreWarnings' | 'transferNativeAmount'> & Partial<Pick<SWTransaction, 'additionalValidator' | 'eventsHandler'>>;
26
26
  export interface SWTransactionInput extends SwInputBase {
27
27
  transaction?: SWTransaction['transaction'] | null;
28
28
  warnings?: SWTransaction['warnings'];
@@ -0,0 +1,9 @@
1
+ import BigNumber from 'bignumber.js';
2
+ export declare const BN_TEN: BigNumber;
3
+ export interface NumberFormatter {
4
+ (input: string, metadata?: Record<string, number>): string;
5
+ }
6
+ export declare const balanceFormatter: NumberFormatter;
7
+ export declare const PREDEFINED_FORMATTER: Record<string, NumberFormatter>;
8
+ export declare const toBNString: (input: string | number | BigNumber, decimal: number) => string;
9
+ export declare const formatNumber: (input: string | number | BigNumber, decimal: number, formatter: NumberFormatter, metadata?: Record<string, number>) => string;
@@ -0,0 +1,100 @@
1
+ // Copyright 2019-2022 @subwallet/extension-koni authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import BigNumber from 'bignumber.js';
5
+ export const BN_TEN = new BigNumber(10);
6
+ // Clear zero from end, use with decimal only
7
+ const clearZero = result => {
8
+ let index = result.length - 1;
9
+ while (result[index] === '0') {
10
+ result = result.slice(0, index);
11
+ index--;
12
+ }
13
+ return result;
14
+ };
15
+ const NUM_1T = new BigNumber(1e12);
16
+ const TLIM = new BigNumber(1e17);
17
+ const NUM_1B = new BigNumber(1e9);
18
+ const BLIM = new BigNumber(1e14);
19
+ const NUM_1M = new BigNumber(1e6);
20
+ const NUM_100M = new BigNumber(1e8);
21
+ export const balanceFormatter = (input, metadata) => {
22
+ const absGteOne = new BigNumber(input).abs().gte(1);
23
+ const minNumberFormat = (metadata === null || metadata === void 0 ? void 0 : metadata.minNumberFormat) || 2;
24
+ const maxNumberFormat = (metadata === null || metadata === void 0 ? void 0 : metadata.maxNumberFormat) || 6;
25
+ const [int, decimal = '0'] = input.split('.');
26
+ let _decimal = '';
27
+ if (absGteOne) {
28
+ const intNumber = new BigNumber(int);
29
+ const max = BN_TEN.pow(maxNumberFormat);
30
+
31
+ // If count of number in integer part greater or equal maxNumberFormat, do not show decimal
32
+ if (intNumber.gte(max)) {
33
+ if (intNumber.gte(NUM_100M)) {
34
+ if (intNumber.gte(BLIM)) {
35
+ if (intNumber.gte(TLIM)) {
36
+ return `${intNumber.dividedBy(NUM_1T).toFixed(2)} T`;
37
+ }
38
+ return `${intNumber.dividedBy(NUM_1B).toFixed(2)} B`;
39
+ }
40
+ return `${intNumber.dividedBy(NUM_1M).toFixed(2)} M`;
41
+ }
42
+ return int;
43
+ }
44
+
45
+ // Get only minNumberFormat number at decimal
46
+ if (decimal.length <= minNumberFormat) {
47
+ _decimal = decimal;
48
+ } else {
49
+ _decimal = decimal.slice(0, minNumberFormat);
50
+ }
51
+
52
+ // Clear zero number for decimal
53
+ _decimal = clearZero(_decimal);
54
+ } else {
55
+ // Index of cursor
56
+ let index = 0;
57
+
58
+ // Count of not zero number in decimal
59
+ let current = 0;
60
+
61
+ // Find a not zero number in decimal
62
+ let metNotZero = false;
63
+
64
+ // Get at least minNumberFormat number not 0 from index 0
65
+ // If count of 0 number at prefix greater or equal maxNumberFormat should stop and return 0
66
+
67
+ // current === minNumberFormat: get enough number
68
+ // index === decimal.length: end of decimal
69
+ // index === maxNumberFormat: reach limit of 0 number at prefix
70
+ while (current < minNumberFormat && index < decimal.length && (index < maxNumberFormat || metNotZero)) {
71
+ const _char = decimal[index];
72
+ _decimal += _char;
73
+ index++;
74
+ if (_char !== '0') {
75
+ metNotZero = true;
76
+ }
77
+ if (metNotZero) {
78
+ current++;
79
+ }
80
+ }
81
+
82
+ // Clear zero number for decimal
83
+ _decimal = clearZero(_decimal);
84
+ }
85
+ if (_decimal) {
86
+ return `${int}.${_decimal}`;
87
+ }
88
+ return int;
89
+ };
90
+ export const PREDEFINED_FORMATTER = {
91
+ balance: balanceFormatter
92
+ };
93
+ export const toBNString = (input, decimal) => {
94
+ const raw = new BigNumber(input);
95
+ return raw.multipliedBy(BN_TEN.pow(decimal)).toFixed();
96
+ };
97
+ export const formatNumber = (input, decimal, formatter, metadata) => {
98
+ const raw = new BigNumber(input).dividedBy(BN_TEN.pow(decimal)).toFixed();
99
+ return formatter(raw, metadata);
100
+ };