@subwallet/extension-base 1.0.1 → 1.0.2-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.
Files changed (169) hide show
  1. package/background/KoniTypes.d.ts +10 -3
  2. package/background/KoniTypes.js +6 -0
  3. package/cjs/background/KoniTypes.js +8 -1
  4. package/cjs/constants/index.js +6 -3
  5. package/cjs/koni/api/dotsama/balance.js +11 -0
  6. package/cjs/koni/api/staking/bonding/amplitude.js +6 -2
  7. package/cjs/koni/api/staking/bonding/astar.js +40 -243
  8. package/cjs/koni/api/staking/bonding/index.js +4 -4
  9. package/cjs/koni/api/staking/bonding/paraChain.js +16 -10
  10. package/cjs/koni/api/staking/bonding/relayChain.js +62 -7
  11. package/cjs/koni/api/staking/bonding/utils.js +29 -3
  12. package/cjs/koni/api/xcm/index.js +0 -111
  13. package/cjs/koni/api/xcm/polkadotXcm.js +1 -1
  14. package/cjs/koni/api/xcm/xTokens.js +2 -4
  15. package/cjs/koni/background/cron.js +61 -62
  16. package/cjs/koni/background/handlers/Extension.js +143 -149
  17. package/cjs/koni/background/handlers/State.js +129 -91
  18. package/cjs/koni/background/handlers/Tabs.js +40 -38
  19. package/cjs/koni/background/subscription.js +48 -43
  20. package/cjs/packageInfo.js +1 -1
  21. package/cjs/page/index.js +5 -0
  22. package/cjs/services/chain-service/constants.js +1 -11
  23. package/cjs/services/chain-service/index.js +39 -5
  24. package/cjs/services/chain-service/utils.js +4 -0
  25. package/cjs/services/event-service/index.js +71 -0
  26. package/cjs/services/event-service/types.js +1 -0
  27. package/cjs/services/history-service/index.js +36 -17
  28. package/cjs/services/history-service/subsquid-multi-chain-history.js +7 -2
  29. package/cjs/services/keyring-service/index.js +101 -0
  30. package/cjs/services/migration-service/index.js +13 -10
  31. package/cjs/{koni/migration/scripts/RemoveWrongCrowdloan.js → services/migration-service/scripts/AutoEnableChainsTokens.js} +11 -6
  32. package/cjs/services/migration-service/scripts/MigrateNetworkSettings.js +39 -19
  33. package/cjs/services/migration-service/scripts/MigrateSettings.js +31 -0
  34. package/cjs/services/migration-service/scripts/MigrateTransactionHistory.js +72 -0
  35. package/cjs/services/migration-service/scripts/index.js +13 -4
  36. package/cjs/services/price-service/coingecko.js +16 -3
  37. package/cjs/services/price-service/index.js +15 -17
  38. package/cjs/services/request-service/constants.js +8 -2
  39. package/cjs/services/request-service/handler/AuthRequestHandler.js +39 -12
  40. package/cjs/services/request-service/index.js +3 -0
  41. package/cjs/services/storage-service/DatabaseService.js +1 -1
  42. package/cjs/services/storage-service/databases/index.js +1 -1
  43. package/cjs/services/storage-service/db-stores/BaseStoreWithAddressAndChain.js +2 -8
  44. package/cjs/services/storage-service/db-stores/Nft.js +4 -1
  45. package/cjs/services/storage-service/db-stores/Transaction.js +10 -7
  46. package/cjs/services/subscan-service/index.js +107 -0
  47. package/cjs/services/subscan-service/subscan-chain-map.js +74 -0
  48. package/cjs/services/subscan-service/types.js +1 -0
  49. package/cjs/services/transaction-service/index.js +45 -10
  50. package/cjs/services/transaction-service/utils.js +4 -6
  51. package/cjs/utils/address.js +9 -1
  52. package/cjs/utils/index.js +24 -13
  53. package/constants/index.d.ts +1 -0
  54. package/constants/index.js +1 -0
  55. package/koni/api/dotsama/balance.js +11 -0
  56. package/koni/api/staking/bonding/amplitude.d.ts +1 -1
  57. package/koni/api/staking/bonding/amplitude.js +8 -4
  58. package/koni/api/staking/bonding/astar.d.ts +2 -11
  59. package/koni/api/staking/bonding/astar.js +35 -229
  60. package/koni/api/staking/bonding/index.d.ts +1 -1
  61. package/koni/api/staking/bonding/index.js +4 -4
  62. package/koni/api/staking/bonding/paraChain.d.ts +1 -1
  63. package/koni/api/staking/bonding/paraChain.js +17 -11
  64. package/koni/api/staking/bonding/relayChain.d.ts +1 -1
  65. package/koni/api/staking/bonding/relayChain.js +63 -8
  66. package/koni/api/staking/bonding/utils.d.ts +13 -1
  67. package/koni/api/staking/bonding/utils.js +27 -3
  68. package/koni/api/xcm/index.js +1 -112
  69. package/koni/api/xcm/polkadotXcm.js +1 -1
  70. package/koni/api/xcm/xTokens.js +3 -5
  71. package/koni/background/cron.js +60 -60
  72. package/koni/background/handlers/Extension.js +71 -76
  73. package/koni/background/handlers/State.d.ts +16 -14
  74. package/koni/background/handlers/State.js +126 -91
  75. package/koni/background/handlers/Tabs.js +22 -19
  76. package/koni/background/subscription.d.ts +1 -1
  77. package/koni/background/subscription.js +49 -44
  78. package/package.json +56 -16
  79. package/packageInfo.js +1 -1
  80. package/page/index.d.ts +2 -0
  81. package/page/index.js +4 -0
  82. package/services/chain-service/constants.d.ts +0 -1
  83. package/services/chain-service/constants.js +0 -9
  84. package/services/chain-service/index.d.ts +6 -4
  85. package/services/chain-service/index.js +39 -7
  86. package/services/chain-service/utils.d.ts +1 -0
  87. package/services/chain-service/utils.js +3 -0
  88. package/services/event-service/index.d.ts +22 -0
  89. package/services/event-service/index.js +59 -0
  90. package/services/event-service/types.d.ts +32 -0
  91. package/services/event-service/types.js +1 -0
  92. package/services/history-service/index.d.ts +5 -3
  93. package/services/history-service/index.js +36 -17
  94. package/services/history-service/subsquid-multi-chain-history.js +7 -2
  95. package/services/keyring-service/index.d.ts +19 -0
  96. package/services/keyring-service/index.js +93 -0
  97. package/services/migration-service/index.js +11 -9
  98. package/services/migration-service/scripts/AutoEnableChainsTokens.d.ts +4 -0
  99. package/services/migration-service/scripts/AutoEnableChainsTokens.js +13 -0
  100. package/services/migration-service/scripts/MigrateNetworkSettings.js +37 -18
  101. package/services/migration-service/scripts/MigrateSettings.d.ts +4 -0
  102. package/services/migration-service/scripts/MigrateSettings.js +23 -0
  103. package/services/migration-service/scripts/MigrateTransactionHistory.d.ts +4 -0
  104. package/services/migration-service/scripts/MigrateTransactionHistory.js +64 -0
  105. package/services/migration-service/scripts/index.d.ts +1 -0
  106. package/services/migration-service/scripts/index.js +10 -3
  107. package/services/price-service/coingecko.js +16 -3
  108. package/services/price-service/index.d.ts +5 -3
  109. package/services/price-service/index.js +15 -17
  110. package/services/request-service/constants.d.ts +1 -0
  111. package/services/request-service/constants.js +6 -1
  112. package/services/request-service/handler/AuthRequestHandler.d.ts +8 -1
  113. package/services/request-service/handler/AuthRequestHandler.js +40 -13
  114. package/services/request-service/index.d.ts +7 -1
  115. package/services/request-service/index.js +3 -0
  116. package/services/storage-service/DatabaseService.js +1 -1
  117. package/services/storage-service/databases/index.js +1 -1
  118. package/services/storage-service/db-stores/BaseStoreWithAddressAndChain.d.ts +1 -1
  119. package/services/storage-service/db-stores/BaseStoreWithAddressAndChain.js +2 -8
  120. package/services/storage-service/db-stores/Nft.d.ts +2 -1
  121. package/services/storage-service/db-stores/Nft.js +4 -1
  122. package/services/storage-service/db-stores/Transaction.js +10 -7
  123. package/services/subscan-service/index.d.ts +20 -0
  124. package/services/subscan-service/index.js +99 -0
  125. package/services/subscan-service/subscan-chain-map.d.ts +5 -0
  126. package/services/subscan-service/subscan-chain-map.js +63 -0
  127. package/services/subscan-service/types.d.ts +27 -0
  128. package/services/subscan-service/types.js +1 -0
  129. package/services/transaction-service/event-parser/index.d.ts +2 -2
  130. package/services/transaction-service/index.d.ts +4 -2
  131. package/services/transaction-service/index.js +45 -10
  132. package/services/transaction-service/utils.js +5 -6
  133. package/utils/address.d.ts +1 -0
  134. package/utils/address.js +9 -2
  135. package/utils/index.d.ts +1 -0
  136. package/utils/index.js +21 -12
  137. package/cjs/background/errors/EvmRpcError.js +0 -21
  138. package/cjs/background/errors/SubWalletProviderError.js +0 -17
  139. package/cjs/constants/ethereum.js +0 -19
  140. package/cjs/errors/SubWalletProviderError.js +0 -17
  141. package/cjs/koni/api/xcm/astar.js +0 -160
  142. package/cjs/koni/api/xcm/moonbeamXcm.js +0 -80
  143. package/cjs/koni/api/xcm/statemintXcm.js +0 -197
  144. package/cjs/koni/api/xcm/substrateXcm.js +0 -213
  145. package/cjs/koni/migration/Base.js +0 -20
  146. package/cjs/koni/migration/index.js +0 -45
  147. package/cjs/koni/migration/scripts/ChangeRouteToHome.js +0 -22
  148. package/cjs/koni/migration/scripts/ClearOldStorage.js +0 -24
  149. package/cjs/koni/migration/scripts/RemoveWrongTransactionHistoriesFromStore.js +0 -36
  150. package/cjs/koni/migration/scripts/ResetTransactionHistoryEventIdx.js +0 -21
  151. package/cjs/koni/migration/scripts/index.js +0 -22
  152. package/cjs/koni/page/index.js +0 -16
  153. package/cjs/services/asset-service/index.js +0 -91
  154. package/cjs/services/storage-service/db-stores/ExtraDelegationInfo.js +0 -17
  155. package/cjs/stores/Balance.js +0 -18
  156. package/cjs/stores/Crowdloan.js +0 -18
  157. package/cjs/stores/CustomEvmToken.js +0 -18
  158. package/cjs/stores/NetworkMap.js +0 -18
  159. package/cjs/stores/Nft.js +0 -18
  160. package/cjs/stores/NftCollection.js +0 -18
  161. package/cjs/stores/Price.js +0 -18
  162. package/cjs/stores/Staking.js +0 -18
  163. package/cjs/stores/StakingReward.js +0 -18
  164. package/cjs/utils/eth/parseTransactionData.js +0 -284
  165. package/koni/page/index.d.ts +0 -2
  166. package/koni/page/index.js +0 -9
  167. /package/cjs/{koni/page → page}/SubWalleEvmProvider.js +0 -0
  168. /package/{koni/page → page}/SubWalleEvmProvider.d.ts +0 -0
  169. /package/{koni/page → page}/SubWalleEvmProvider.js +0 -0
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "./cjs/detectPackage.js"
18
18
  ],
19
19
  "type": "module",
20
- "version": "1.0.1",
20
+ "version": "1.0.2-1",
21
21
  "main": "./cjs/index.js",
22
22
  "module": "./index.js",
23
23
  "types": "./index.d.ts",
@@ -434,16 +434,6 @@
434
434
  "require": "./cjs/koni/background/subscription.js",
435
435
  "default": "./koni/background/subscription.js"
436
436
  },
437
- "./koni/page": {
438
- "types": "./koni/page/index.d.ts",
439
- "require": "./cjs/koni/page/index.js",
440
- "default": "./koni/page/index.js"
441
- },
442
- "./koni/page/SubWalleEvmProvider": {
443
- "types": "./koni/page/SubWalleEvmProvider.d.ts",
444
- "require": "./cjs/koni/page/SubWalleEvmProvider.js",
445
- "default": "./koni/page/SubWalleEvmProvider.js"
446
- },
447
437
  "./package.json": "./package.json",
448
438
  "./packageInfo.js": {
449
439
  "types": "./packageInfo.d.ts",
@@ -485,6 +475,11 @@
485
475
  "require": "./cjs/page/Signer.js",
486
476
  "default": "./page/Signer.js"
487
477
  },
478
+ "./page/SubWalleEvmProvider": {
479
+ "types": "./page/SubWalleEvmProvider.d.ts",
480
+ "require": "./cjs/page/SubWalleEvmProvider.js",
481
+ "default": "./page/SubWalleEvmProvider.js"
482
+ },
488
483
  "./page/types": {
489
484
  "types": "./page/types.d.ts",
490
485
  "require": "./cjs/page/types.js",
@@ -1170,6 +1165,16 @@
1170
1165
  "require": "./cjs/services/chain-service/utils.js",
1171
1166
  "default": "./services/chain-service/utils.js"
1172
1167
  },
1168
+ "./services/event-service": {
1169
+ "types": "./services/event-service/index.d.ts",
1170
+ "require": "./cjs/services/event-service/index.js",
1171
+ "default": "./services/event-service/index.js"
1172
+ },
1173
+ "./services/event-service/types": {
1174
+ "types": "./services/event-service/types.d.ts",
1175
+ "require": "./cjs/services/event-service/types.js",
1176
+ "default": "./services/event-service/types.js"
1177
+ },
1173
1178
  "./services/history-service": {
1174
1179
  "types": "./services/history-service/index.d.ts",
1175
1180
  "require": "./cjs/services/history-service/index.js",
@@ -1180,6 +1185,11 @@
1180
1185
  "require": "./cjs/services/history-service/subsquid-multi-chain-history.js",
1181
1186
  "default": "./services/history-service/subsquid-multi-chain-history.js"
1182
1187
  },
1188
+ "./services/keyring-service": {
1189
+ "types": "./services/keyring-service/index.d.ts",
1190
+ "require": "./cjs/services/keyring-service/index.js",
1191
+ "default": "./services/keyring-service/index.js"
1192
+ },
1183
1193
  "./services/migration-service": {
1184
1194
  "types": "./services/migration-service/index.d.ts",
1185
1195
  "require": "./cjs/services/migration-service/index.js",
@@ -1195,6 +1205,11 @@
1195
1205
  "require": "./cjs/services/migration-service/scripts/index.js",
1196
1206
  "default": "./services/migration-service/scripts/index.js"
1197
1207
  },
1208
+ "./services/migration-service/scripts/AutoEnableChainsTokens": {
1209
+ "types": "./services/migration-service/scripts/AutoEnableChainsTokens.d.ts",
1210
+ "require": "./cjs/services/migration-service/scripts/AutoEnableChainsTokens.js",
1211
+ "default": "./services/migration-service/scripts/AutoEnableChainsTokens.js"
1212
+ },
1198
1213
  "./services/migration-service/scripts/ClearOldStorage": {
1199
1214
  "types": "./services/migration-service/scripts/ClearOldStorage.d.ts",
1200
1215
  "require": "./cjs/services/migration-service/scripts/ClearOldStorage.js",
@@ -1210,6 +1225,16 @@
1210
1225
  "require": "./cjs/services/migration-service/scripts/MigrateNetworkSettings.js",
1211
1226
  "default": "./services/migration-service/scripts/MigrateNetworkSettings.js"
1212
1227
  },
1228
+ "./services/migration-service/scripts/MigrateSettings": {
1229
+ "types": "./services/migration-service/scripts/MigrateSettings.d.ts",
1230
+ "require": "./cjs/services/migration-service/scripts/MigrateSettings.js",
1231
+ "default": "./services/migration-service/scripts/MigrateSettings.js"
1232
+ },
1233
+ "./services/migration-service/scripts/MigrateTransactionHistory": {
1234
+ "types": "./services/migration-service/scripts/MigrateTransactionHistory.d.ts",
1235
+ "require": "./cjs/services/migration-service/scripts/MigrateTransactionHistory.js",
1236
+ "default": "./services/migration-service/scripts/MigrateTransactionHistory.js"
1237
+ },
1213
1238
  "./services/notification-service/NotificationService": {
1214
1239
  "types": "./services/notification-service/NotificationService.d.ts",
1215
1240
  "require": "./cjs/services/notification-service/NotificationService.js",
@@ -1375,6 +1400,21 @@
1375
1400
  "require": "./cjs/services/storage-service/db-stores/Transaction.js",
1376
1401
  "default": "./services/storage-service/db-stores/Transaction.js"
1377
1402
  },
1403
+ "./services/subscan-service": {
1404
+ "types": "./services/subscan-service/index.d.ts",
1405
+ "require": "./cjs/services/subscan-service/index.js",
1406
+ "default": "./services/subscan-service/index.js"
1407
+ },
1408
+ "./services/subscan-service/subscan-chain-map": {
1409
+ "types": "./services/subscan-service/subscan-chain-map.d.ts",
1410
+ "require": "./cjs/services/subscan-service/subscan-chain-map.js",
1411
+ "default": "./services/subscan-service/subscan-chain-map.js"
1412
+ },
1413
+ "./services/subscan-service/types": {
1414
+ "types": "./services/subscan-service/types.d.ts",
1415
+ "require": "./cjs/services/subscan-service/types.js",
1416
+ "default": "./services/subscan-service/types.js"
1417
+ },
1378
1418
  "./services/transaction-service": {
1379
1419
  "types": "./services/transaction-service/index.d.ts",
1380
1420
  "require": "./cjs/services/transaction-service/index.js",
@@ -1611,11 +1651,11 @@
1611
1651
  "@sora-substrate/type-definitions": "^1.12.4",
1612
1652
  "@subsocial/types": "^0.6.8",
1613
1653
  "@substrate/connect": "^0.7.18",
1614
- "@subwallet/chain-list": "^0.0.21",
1615
- "@subwallet/extension-base": "^1.0.1",
1616
- "@subwallet/extension-chains": "^1.0.1",
1617
- "@subwallet/extension-dapp": "^1.0.1",
1618
- "@subwallet/extension-inject": "^1.0.1",
1654
+ "@subwallet/chain-list": "^0.0.27",
1655
+ "@subwallet/extension-base": "^1.0.2-1",
1656
+ "@subwallet/extension-chains": "^1.0.2-1",
1657
+ "@subwallet/extension-dapp": "^1.0.2-1",
1658
+ "@subwallet/extension-inject": "^1.0.2-1",
1619
1659
  "@subwallet/keyring": "^0.0.4",
1620
1660
  "@subwallet/ui-keyring": "^0.0.3",
1621
1661
  "@unique-nft/types": "^0.6.0-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.0.1'
10
+ version: '1.0.2-1'
11
11
  };
package/page/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { MessageTypes, MessageTypesWithNoSubscriptions, MessageTypesWithNullRequest, MessageTypesWithSubscriptions, RequestTypes, ResponseTypes, SubscriptionMessageTypes, TransportResponseMessage } from '../background/types';
2
+ import { EvmProvider } from '@subwallet/extension-inject/types';
2
3
  import Injected from './Injected';
3
4
  export interface Handler {
4
5
  resolve: (data?: any) => void;
@@ -13,3 +14,4 @@ export declare function enable(origin: string): Promise<Injected>;
13
14
  export declare function handleResponse<TMessageType extends MessageTypes>(data: TransportResponseMessage<TMessageType> & {
14
15
  subscription?: string;
15
16
  }): void;
17
+ export declare function initEvmProvider(version: string): EvmProvider;
package/page/index.js CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  import { ProviderError } from '@subwallet/extension-base/background/errors/ProviderError';
5
5
  import { ProviderErrorType } from '@subwallet/extension-base/background/KoniTypes';
6
+ import { SubWalletEvmProvider } from '@subwallet/extension-base/page/SubWalleEvmProvider';
6
7
  import { MESSAGE_ORIGIN_PAGE } from "../defaults.js";
7
8
  import { getId } from "../utils/getId.js";
8
9
  import Injected from "./Injected.js";
@@ -60,4 +61,7 @@ export function handleResponse(data) {
60
61
  } else {
61
62
  handler.resolve(data.response);
62
63
  }
64
+ }
65
+ export function initEvmProvider(version) {
66
+ return new SubWalletEvmProvider(sendMessage, version);
63
67
  }
@@ -69,7 +69,6 @@ export declare const _XCM_CHAIN_GROUP: {
69
69
  polkadotXcm: string[];
70
70
  xcmPallet: string[];
71
71
  };
72
- export declare const _XCM_CHAIN_USE_LIMITED_WEIGHT: string[];
73
72
  export declare const _XCM_TYPE: {
74
73
  RP: string;
75
74
  PP: string;
@@ -191,17 +191,8 @@ export const _XCM_CHAIN_GROUP = {
191
191
  polkadotXcm: ['astar', 'shiden', 'statemine', 'statemint'],
192
192
  xcmPallet: ['polkadot', 'kusama']
193
193
  // default is xTokens pallet
194
- // moonbeam: ['moonbeam', 'moonriver', 'moonbase'],
195
- // astar: ['astar', 'shiden'],
196
- // statemine: ['statemint', 'statemine'],
197
- // bifrost: ['bifrost'],
198
- // genshiro: ['genshiro_testnet', 'genshiro', 'equilibrium_parachain'],
199
- // kintsugi: ['kintsugi', 'kintsugi_test', 'interlay'],
200
- // acala: ['karura', 'acala', 'acala_testnet'],
201
- // astarEvm: ['astarEvm', 'shidenEvm']
202
194
  };
203
195
 
204
- export const _XCM_CHAIN_USE_LIMITED_WEIGHT = ['acala', 'karura', 'statemint'];
205
196
  export const _XCM_TYPE = {
206
197
  RP: `${_SubstrateChainType.RELAYCHAIN}-${_SubstrateChainType.PARACHAIN}`,
207
198
  // DMP
@@ -1,11 +1,13 @@
1
1
  import { _AssetRef, _AssetType, _ChainAsset, _ChainInfo, _MultiChainAsset } from '@subwallet/chain-list/types';
2
2
  import { AssetSetting, ValidateNetworkResponse } from '@subwallet/extension-base/background/KoniTypes';
3
3
  import { _ChainConnectionStatus, _ChainState, _EvmApi, _NetworkUpsertParams, _SubstrateApi, _ValidateCustomAssetRequest, _ValidateCustomAssetResponse } from '@subwallet/extension-base/services/chain-service/types';
4
+ import { EventService } from '@subwallet/extension-base/services/event-service';
4
5
  import DatabaseService from '@subwallet/extension-base/services/storage-service/DatabaseService';
5
6
  import { BehaviorSubject, Subject } from 'rxjs';
6
7
  export declare class ChainService {
7
8
  private dataMap;
8
9
  private dbService;
10
+ private eventService;
9
11
  private lockChainInfoMap;
10
12
  private substrateChainHandler;
11
13
  private evmChainHandler;
@@ -18,7 +20,7 @@ export declare class ChainService {
18
20
  private assetSettingSubject;
19
21
  private logger;
20
22
  private refreshChainStateTimeout;
21
- constructor(dbService: DatabaseService);
23
+ constructor(dbService: DatabaseService, eventService: EventService);
22
24
  getXcmRefMap(): Record<string, _AssetRef>;
23
25
  getEvmApi(slug: string): _EvmApi;
24
26
  getEvmApiMap(): Record<string, _EvmApi>;
@@ -50,7 +52,7 @@ export declare class ChainService {
50
52
  getChainInfoByKey(key: string): _ChainInfo;
51
53
  getActiveChainInfos(): Record<string, _ChainInfo>;
52
54
  getAssetBySlug(slug: string): _ChainAsset;
53
- getFungibleTokensByChain(chainSlug: string): Record<string, _ChainAsset>;
55
+ getFungibleTokensByChain(chainSlug: string, checkActive?: boolean): Record<string, _ChainAsset>;
54
56
  getXcmEqualAssetByChain(destinationChainSlug: string, originTokenSlug: string): _ChainAsset | undefined;
55
57
  getAssetByChainAndType(chainSlug: string, assetTypes: _AssetType[]): Record<string, _ChainAsset>;
56
58
  getSmartContractNfts(): _ChainAsset[];
@@ -93,10 +95,10 @@ export declare class ChainService {
93
95
  private refreshChainStateInterval;
94
96
  private updateApiMapStatus;
95
97
  initAssetSettings(): Promise<void>;
96
- setAssetSettings(assetSettings: Record<string, AssetSetting>): void;
98
+ setAssetSettings(assetSettings: Record<string, AssetSetting>, emitEvent?: boolean): void;
97
99
  getStoreAssetSettings(): Promise<Record<string, AssetSetting>>;
98
100
  getAssetSettings(): Promise<Record<string, AssetSetting>>;
99
- updateAssetSetting(assetSlug: string, assetSetting: AssetSetting): Promise<void>;
101
+ updateAssetSetting(assetSlug: string, assetSetting: AssetSetting): Promise<boolean | undefined>;
100
102
  updateAssetSettingByChain(chainSlug: string, visible: boolean): Promise<void>;
101
103
  subscribeAssetSettings(): BehaviorSubject<Record<string, AssetSetting>>;
102
104
  }
@@ -21,8 +21,6 @@ export class ChainService {
21
21
  assetRegistry: {},
22
22
  assetRefMap: {}
23
23
  };
24
- // to save chain, token settings from user
25
-
26
24
  lockChainInfoMap = false; // prevent unwanted changes (edit, enable, disable) to chainInfoMap
27
25
 
28
26
  // TODO: consider BehaviorSubject
@@ -35,8 +33,9 @@ export class ChainService {
35
33
  // Todo: Update to new store indexed DB
36
34
  store = new AssetSettingStore();
37
35
  assetSettingSubject = new BehaviorSubject({});
38
- constructor(dbService) {
36
+ constructor(dbService, eventService) {
39
37
  this.dbService = dbService;
38
+ this.eventService = eventService;
40
39
  this.substrateChainHandler = new SubstrateChainHandler();
41
40
  this.evmChainHandler = new EvmChainHandler();
42
41
  this.chainInfoMapSubject.next(this.dataMap.chainInfoMap);
@@ -208,10 +207,13 @@ export class ChainService {
208
207
  getAssetBySlug(slug) {
209
208
  return this.getAssetRegistry()[slug];
210
209
  }
211
- getFungibleTokensByChain(chainSlug) {
210
+ getFungibleTokensByChain(chainSlug, checkActive = false) {
212
211
  const result = {};
212
+ const assetSettings = this.assetSettingSubject.value;
213
213
  Object.values(this.getAssetRegistry()).forEach(chainAsset => {
214
- if (chainAsset.originChain === chainSlug && _isAssetFungibleToken(chainAsset)) {
214
+ var _assetSettings$chainA;
215
+ const _filterActive = !checkActive || ((_assetSettings$chainA = assetSettings[chainAsset.slug]) === null || _assetSettings$chainA === void 0 ? void 0 : _assetSettings$chainA.visible);
216
+ if (chainAsset.originChain === chainSlug && _isAssetFungibleToken(chainAsset) && _filterActive) {
215
217
  result[chainAsset.slug] = chainAsset;
216
218
  }
217
219
  });
@@ -275,6 +277,7 @@ export class ChainService {
275
277
  this.dbService.removeFromChainStore([slug]).catch(console.error);
276
278
  this.updateChainSubscription();
277
279
  this.lockChainInfoMap = false;
280
+ this.eventService.emit('chain.remove', slug);
278
281
  return true;
279
282
  }
280
283
  resetChainInfoMap(excludedChains) {
@@ -308,6 +311,10 @@ export class ChainService {
308
311
  token.slug = `${_CUSTOM_PREFIX}${defaultSlug}`;
309
312
  }
310
313
  }
314
+ if (token.originChain && _isAssetFungibleToken(token)) {
315
+ var _this$getChainInfoByK;
316
+ token.hasValue = !((_this$getChainInfoByK = this.getChainInfoByKey(token.originChain)) !== null && _this$getChainInfoByK !== void 0 && _this$getChainInfoByK.isTestnet);
317
+ }
311
318
  const assetRegistry = this.getAssetRegistry();
312
319
  assetRegistry[token.slug] = token;
313
320
  this.dbService.updateAssetStore(token).catch(e => this.logger.error(e));
@@ -335,6 +342,9 @@ export class ChainService {
335
342
  this.dbService.removeFromBalanceStore(targetAssets).catch(e => this.logger.error(e));
336
343
  this.dbService.removeFromAssetStore(targetAssets).catch(e => this.logger.error(e));
337
344
  this.assetRegistrySubject.next(assetRegistry);
345
+ targetAssets.forEach(assetSlug => {
346
+ this.eventService.emit('asset.remove', assetSlug);
347
+ });
338
348
  }
339
349
 
340
350
  // Business logic
@@ -404,6 +414,7 @@ export class ChainService {
404
414
  currentProvider: chainStateMap[chainSlug].currentProvider
405
415
  }).catch(console.error);
406
416
  this.lockChainInfoMap = false;
417
+ this.eventService.emit('chain.enable', chainSlug);
407
418
  return true;
408
419
  }
409
420
  enableChain(chainSlug) {
@@ -438,6 +449,7 @@ export class ChainService {
438
449
  }).catch(console.error);
439
450
  this.updateChainStateMapSubscription();
440
451
  this.lockChainInfoMap = false;
452
+ this.eventService.emit('chain.disable', chainSlug);
441
453
  return true;
442
454
  }
443
455
  checkExistedPredefinedChain(genesisHash, evmChainId) {
@@ -659,6 +671,8 @@ export class ChainService {
659
671
  ...targetChainInfo,
660
672
  active: targetChainState.active,
661
673
  currentProvider: targetChainState.currentProvider
674
+ }).then(() => {
675
+ this.eventService.emit('chain.update', chainSlug);
662
676
  }).catch(e => this.logger.error(e));
663
677
  }
664
678
  insertChain(params) {
@@ -743,6 +757,8 @@ export class ChainService {
743
757
  active: true,
744
758
  currentProvider: params.chainEditInfo.currentProvider,
745
759
  ...chainInfo
760
+ }).then(() => {
761
+ this.eventService.emit('chain.add', newChainSlug);
746
762
  }).catch(e => this.logger.error(e));
747
763
  return nativeTokenSlug;
748
764
  }
@@ -1086,12 +1102,25 @@ export class ChainService {
1086
1102
  };
1087
1103
  }
1088
1104
  });
1089
- this.setAssetSettings(assetSettings);
1105
+ this.setAssetSettings(assetSettings, false);
1090
1106
  }
1107
+ this.eventService.emit('asset.ready', true);
1091
1108
  console.log('Done init asset settings');
1092
1109
  }
1093
- setAssetSettings(assetSettings) {
1110
+ setAssetSettings(assetSettings, emitEvent = true) {
1111
+ const updateAssets = [];
1112
+ if (emitEvent) {
1113
+ Object.keys(assetSettings).forEach(slug => {
1114
+ var _this$assetSettingSub;
1115
+ if (((_this$assetSettingSub = this.assetSettingSubject.value[slug]) === null || _this$assetSettingSub === void 0 ? void 0 : _this$assetSettingSub.visible) !== assetSettings[slug].visible) {
1116
+ updateAssets.push(slug);
1117
+ }
1118
+ });
1119
+ }
1094
1120
  this.assetSettingSubject.next(assetSettings);
1121
+ updateAssets.forEach(slug => {
1122
+ this.eventService.emit(assetSettings[slug].visible ? 'asset.enable' : 'asset.disable', slug);
1123
+ });
1095
1124
  this.store.set('AssetSetting', assetSettings);
1096
1125
  }
1097
1126
  async getStoreAssetSettings() {
@@ -1108,6 +1137,7 @@ export class ChainService {
1108
1137
  }
1109
1138
  async updateAssetSetting(assetSlug, assetSetting) {
1110
1139
  const currentAssetSettings = await this.getAssetSettings();
1140
+ let needUpdateSubject;
1111
1141
 
1112
1142
  // Update settings
1113
1143
  currentAssetSettings[assetSlug] = assetSetting;
@@ -1119,8 +1149,10 @@ export class ChainService {
1119
1149
  // if chain not enabled, then automatically enable
1120
1150
  if (chainState && !chainState.active) {
1121
1151
  this.enableChain(chainState.slug);
1152
+ needUpdateSubject = true;
1122
1153
  }
1123
1154
  }
1155
+ return needUpdateSubject;
1124
1156
  }
1125
1157
  async updateAssetSettingByChain(chainSlug, visible) {
1126
1158
  const storedAssetSettings = await this.getAssetSettings();
@@ -34,6 +34,7 @@ export declare function _getNftTypesSupportedByChain(chainInfo: _ChainInfo): _As
34
34
  export declare function _getTokenTypesSupportedByChain(chainInfo: _ChainInfo): _AssetType[];
35
35
  export declare function _getChainNativeTokenBasicInfo(chainInfo: _ChainInfo): BasicTokenInfo;
36
36
  export declare function _getChainNativeTokenSlug(chainInfo: _ChainInfo): string;
37
+ export declare function _isLocalToken(tokenInfo: _ChainAsset): boolean;
37
38
  export declare function _isTokenEvmSmartContract(tokenInfo: _ChainAsset): boolean;
38
39
  export declare function _isTokenWasmSmartContract(tokenInfo: _ChainAsset): boolean;
39
40
  export declare function _isAssetSmartContractNft(assetInfo: _ChainAsset): boolean;
@@ -206,6 +206,9 @@ export function _getChainNativeTokenSlug(chainInfo) {
206
206
  }
207
207
  return `${chainInfo.slug}-${_AssetType.NATIVE}-${_getChainNativeTokenBasicInfo(chainInfo).symbol}`;
208
208
  }
209
+ export function _isLocalToken(tokenInfo) {
210
+ return tokenInfo.assetType === _AssetType.LOCAL;
211
+ }
209
212
  export function _isTokenEvmSmartContract(tokenInfo) {
210
213
  return [_AssetType.ERC721, _AssetType.ERC20].includes(tokenInfo.assetType);
211
214
  }
@@ -0,0 +1,22 @@
1
+ import { EventItem, EventRegistry, EventType } from '@subwallet/extension-base/services/event-service/types';
2
+ import EventEmitter from 'eventemitter3';
3
+ export declare class EventService extends EventEmitter<EventRegistry> {
4
+ private lazyTime;
5
+ private timeoutId;
6
+ private pendingEvents;
7
+ private lazyEmitter;
8
+ readonly waitKeyringReady: Promise<boolean>;
9
+ readonly waitAccountReady: Promise<boolean>;
10
+ readonly waitChainReady: Promise<boolean>;
11
+ readonly waitAssetReady: Promise<boolean>;
12
+ constructor(options?: {
13
+ lazyTime: number;
14
+ });
15
+ private generateWaitPromise;
16
+ private setLazyTimeout;
17
+ private emitLazy;
18
+ onLazy(callback: (events: EventItem<EventType>[], eventTypes: EventType[]) => void): void;
19
+ offLazy(callback: (events: EventItem<EventType>[], eventTypes: EventType[]) => void): void;
20
+ onceLazy(callback: (events: EventItem<EventType>[], eventTypes: EventType[]) => void): void;
21
+ emit<T extends EventType>(eventType: T, ...args: EventEmitter.EventArgs<EventRegistry, T>): boolean;
22
+ }
@@ -0,0 +1,59 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // Stateless service handle runtime event on background
5
+
6
+ import EventEmitter from 'eventemitter3';
7
+ export class EventService extends EventEmitter {
8
+ pendingEvents = [];
9
+ lazyEmitter = new EventEmitter();
10
+ constructor(options = {
11
+ lazyTime: 300
12
+ }) {
13
+ super();
14
+ this.lazyTime = options.lazyTime;
15
+ this.timeoutId = null;
16
+ this.waitKeyringReady = this.generateWaitPromise('keyring.ready');
17
+ this.waitAccountReady = this.generateWaitPromise('account.ready');
18
+ this.waitChainReady = this.generateWaitPromise('chain.ready');
19
+ this.waitAssetReady = this.generateWaitPromise('asset.ready');
20
+ }
21
+ generateWaitPromise(eventType) {
22
+ return new Promise(resolve => {
23
+ this.once(eventType, isReady => {
24
+ resolve(isReady);
25
+ });
26
+ });
27
+ }
28
+ setLazyTimeout() {
29
+ if (this.timeoutId) {
30
+ clearTimeout(this.timeoutId);
31
+ }
32
+ this.timeoutId = setTimeout(() => {
33
+ this.emitLazy();
34
+ }, this.lazyTime);
35
+ }
36
+ emitLazy() {
37
+ this.lazyEmitter.emit('lazy', this.pendingEvents, this.pendingEvents.map(e => e.type));
38
+ this.pendingEvents = [];
39
+ this.timeoutId = null;
40
+ }
41
+ onLazy(callback) {
42
+ this.lazyEmitter.on('lazy', callback);
43
+ }
44
+ offLazy(callback) {
45
+ this.lazyEmitter.off('lazy', callback);
46
+ }
47
+ onceLazy(callback) {
48
+ this.lazyEmitter.once('lazy', callback);
49
+ }
50
+ emit(eventType, ...args) {
51
+ console.debug('Emit event: ', eventType, ...args);
52
+ this.pendingEvents.push({
53
+ type: eventType,
54
+ data: args
55
+ });
56
+ this.setLazyTimeout();
57
+ return super.emit(eventType, ...args);
58
+ }
59
+ }
@@ -0,0 +1,32 @@
1
+ import { CurrentAccountInfo } from '@subwallet/extension-base/background/KoniTypes';
2
+ import { SWTransaction } from '@subwallet/extension-base/services/transaction-service/types';
3
+ export interface EventRegistry {
4
+ 'keyring.ready': [boolean];
5
+ 'account.updateCurrent': [CurrentAccountInfo];
6
+ 'account.ready': [boolean];
7
+ 'account.add': [string];
8
+ 'account.update': [string];
9
+ 'account.remove': [string];
10
+ 'chain.ready': [boolean];
11
+ 'chain.add': [string];
12
+ 'chain.update': [string];
13
+ 'chain.disable': [string];
14
+ 'chain.enable': [string];
15
+ 'chain.remove': [string];
16
+ 'asset.ready': [boolean];
17
+ 'asset.add': [string];
18
+ 'asset.update': [string];
19
+ 'asset.enable': [string];
20
+ 'asset.disable': [string];
21
+ 'asset.remove': [string];
22
+ 'transaction.done': [SWTransaction];
23
+ 'transaction.failed': [SWTransaction | undefined];
24
+ }
25
+ export declare type EventType = keyof EventRegistry;
26
+ export interface EventItem<T extends EventType> {
27
+ type: T;
28
+ data: EventRegistry[T];
29
+ }
30
+ export interface EventEmitterRegistry extends EventRegistry {
31
+ lazy: EventItem<EventType>[];
32
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,12 +1,14 @@
1
1
  import { TransactionHistoryItem } from '@subwallet/extension-base/background/KoniTypes';
2
2
  import { ChainService } from '@subwallet/extension-base/services/chain-service';
3
+ import { EventService } from '@subwallet/extension-base/services/event-service';
3
4
  import DatabaseService from '@subwallet/extension-base/services/storage-service/DatabaseService';
4
5
  import { BehaviorSubject } from 'rxjs';
5
6
  export declare class HistoryService {
6
7
  private dbService;
7
8
  private chainService;
9
+ private eventService;
8
10
  private historySubject;
9
- constructor(dbService: DatabaseService, chainService: ChainService);
11
+ constructor(dbService: DatabaseService, chainService: ChainService, eventService: EventService);
10
12
  private fetchPromise;
11
13
  private nextFetch;
12
14
  private fetchAndLoadHistories;
@@ -15,8 +17,8 @@ export declare class HistoryService {
15
17
  refreshHistoryInterval(): void;
16
18
  getHistories(): Promise<TransactionHistoryItem<import("@subwallet/extension-base/background/KoniTypes").ExtrinsicType.TRANSFER_BALANCE>[]>;
17
19
  getHistorySubject(): Promise<BehaviorSubject<TransactionHistoryItem<import("@subwallet/extension-base/background/KoniTypes").ExtrinsicType.TRANSFER_BALANCE>[]>>;
18
- insertHistory(historyItem: TransactionHistoryItem): Promise<void>;
19
- updateHistory(chain: string, extrinsicHash: string, historyItem: Partial<TransactionHistoryItem>): Promise<void>;
20
+ insertHistories(historyItems: TransactionHistoryItem[]): Promise<void>;
21
+ updateHistories(chain: string, extrinsicHash: string, updateData: Partial<TransactionHistoryItem>): Promise<void>;
20
22
  addHistoryItems(historyItems: TransactionHistoryItem[]): Promise<void>;
21
23
  removeHistoryByAddress(address: string): Promise<void>;
22
24
  }
@@ -2,25 +2,32 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import { CRON_REFRESH_HISTORY_INTERVAL } from '@subwallet/extension-base/constants';
5
+ import { quickFormatAddressToCompare } from '@subwallet/extension-base/utils/address';
6
+ import { keyring } from '@subwallet/ui-keyring';
5
7
  import { accounts } from '@subwallet/ui-keyring/observable/accounts';
6
8
  import { BehaviorSubject } from 'rxjs';
7
9
  import { fetchMultiChainHistories } from "./subsquid-multi-chain-history.js";
8
10
  export class HistoryService {
9
11
  historySubject = new BehaviorSubject([]);
10
- constructor(dbService, chainService) {
12
+ constructor(dbService, chainService, eventService) {
11
13
  this.dbService = dbService;
12
14
  this.chainService = chainService;
13
-
15
+ this.eventService = eventService;
14
16
  // Load history from database
15
17
  this.dbService.getHistories().then(histories => {
16
18
  this.historySubject.next(histories);
17
19
  }).catch(console.error);
18
20
 
19
- // Add some delay to avoid fetching many times when start extension background
20
- setTimeout(() => {
21
- // Create history interval and refresh it if changes accounts list
22
- accounts.subject.subscribe(this.refreshHistoryInterval.bind(this));
23
- }, 3333);
21
+ // Wait for keyring and chain ready and start
22
+ Promise.all([this.eventService.waitKeyringReady, this.eventService.waitChainReady]).then(() => {
23
+ this.getHistories().catch(console.log);
24
+ this.eventService.on('account.add', () => {
25
+ this.refreshHistoryInterval();
26
+ });
27
+ this.eventService.on('account.remove', address => {
28
+ this.removeHistoryByAddress(address).catch(console.error);
29
+ });
30
+ }).catch(console.error);
24
31
  }
25
32
  fetchPromise = null;
26
33
  nextFetch = undefined;
@@ -43,8 +50,8 @@ export class HistoryService {
43
50
  record.fromName = accountMap[(_record$from = record.from) === null || _record$from === void 0 ? void 0 : _record$from.toLowerCase()];
44
51
  record.toName = accountMap[(_record$to = record.to) === null || _record$to === void 0 ? void 0 : _record$to.toLowerCase()];
45
52
  });
46
- await this.dbService.upsertHistory(historyRecords);
47
- return await this.dbService.getHistories();
53
+ this.dbService.upsertHistory(historyRecords).catch(console.error);
54
+ return historyRecords;
48
55
  }
49
56
  async fetchHistories(addresses) {
50
57
  if (!this.fetchPromise) {
@@ -60,11 +67,14 @@ export class HistoryService {
60
67
  clearTimeout(this.nextFetch);
61
68
  this.invalidCache();
62
69
  this.getHistories().catch(console.error);
63
- this.nextFetch = setTimeout(this.refreshHistoryInterval.bind(this), CRON_REFRESH_HISTORY_INTERVAL);
70
+ this.nextFetch = setTimeout(() => {
71
+ this.refreshHistoryInterval();
72
+ }, CRON_REFRESH_HISTORY_INTERVAL);
64
73
  }
65
74
  async getHistories() {
66
- const addressList = Object.keys(accounts.subject.value);
67
- if (!this.fetchPromise) {
75
+ const addressList = keyring.getAccounts().map(a => a.address);
76
+ const currentHistories = this.historySubject.value;
77
+ if (!this.fetchPromise || currentHistories.length === 0) {
68
78
  const historyRecords = await this.fetchHistories(addressList);
69
79
  this.historySubject.next(historyRecords);
70
80
  }
@@ -74,11 +84,11 @@ export class HistoryService {
74
84
  await this.getHistories();
75
85
  return this.historySubject;
76
86
  }
77
- async insertHistory(historyItem) {
78
- await this.dbService.upsertHistory([historyItem]);
87
+ async insertHistories(historyItems) {
88
+ await this.dbService.upsertHistory(historyItems);
79
89
  this.historySubject.next(await this.dbService.getHistories());
80
90
  }
81
- async updateHistory(chain, extrinsicHash, historyItem) {
91
+ async updateHistories(chain, extrinsicHash, updateData) {
82
92
  const existedRecords = await this.dbService.getHistories({
83
93
  chain,
84
94
  extrinsicHash
@@ -86,13 +96,22 @@ export class HistoryService {
86
96
  const updatedRecords = existedRecords.map(r => {
87
97
  return {
88
98
  ...r,
89
- ...historyItem
99
+ ...updateData
90
100
  };
91
101
  });
92
102
  await this.addHistoryItems(updatedRecords);
93
103
  }
94
104
  async addHistoryItems(historyItems) {
95
- await this.dbService.upsertHistory(historyItems);
105
+ // Prevent override record with original is 'app'
106
+ const appRecords = this.historySubject.value.filter(item => item.origin === 'app');
107
+ const excludeKeys = appRecords.map(item => {
108
+ return `${item.chain}-${quickFormatAddressToCompare(item.address) || ''}-${item.extrinsicHash}`;
109
+ });
110
+ const updateRecords = historyItems.filter(item => {
111
+ const key = `${item.chain}-${quickFormatAddressToCompare(item.address) || ''}-${item.extrinsicHash}`;
112
+ return item.origin === 'app' || !excludeKeys.includes(key);
113
+ });
114
+ await this.dbService.upsertHistory(updateRecords);
96
115
  this.historySubject.next(await this.dbService.getHistories());
97
116
  }
98
117
  async removeHistoryByAddress(address) {