@subwallet/extension-base 1.1.28-0 → 1.1.28-beta.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 (292) hide show
  1. package/background/KoniTypes.d.ts +92 -68
  2. package/background/KoniTypes.js +16 -13
  3. package/background/errors/TransactionError.js +9 -0
  4. package/cjs/background/KoniTypes.js +17 -16
  5. package/cjs/background/errors/TransactionError.js +9 -0
  6. package/cjs/constants/index.js +9 -3
  7. package/cjs/koni/api/dotsama/balance.js +464 -0
  8. package/cjs/koni/api/nft/config.js +33 -23
  9. package/cjs/koni/api/nft/index.js +14 -0
  10. package/cjs/koni/api/nft/nft.js +1 -22
  11. package/cjs/koni/api/nft/ordinal_nft/constants.js +21 -0
  12. package/cjs/koni/api/nft/ordinal_nft/index.js +121 -0
  13. package/cjs/koni/api/nft/ordinal_nft/utils.js +41 -0
  14. package/cjs/koni/api/staking/bonding/amplitude.js +17 -15
  15. package/cjs/koni/api/staking/bonding/astar.js +11 -10
  16. package/cjs/koni/api/staking/bonding/index.js +4 -1
  17. package/cjs/koni/api/staking/bonding/paraChain.js +25 -23
  18. package/cjs/koni/api/staking/bonding/relayChain.js +48 -45
  19. package/cjs/koni/api/staking/bonding/utils.js +104 -86
  20. package/cjs/koni/api/staking/index.js +6 -5
  21. package/cjs/koni/api/staking/paraChain.js +6 -5
  22. package/cjs/koni/api/staking/relayChain.js +3 -2
  23. package/cjs/koni/api/yield/helper/utils.js +46 -0
  24. package/cjs/koni/background/cron.js +2 -0
  25. package/cjs/koni/background/handlers/Extension.js +323 -69
  26. package/cjs/koni/background/handlers/State.js +57 -4
  27. package/cjs/koni/background/handlers/index.js +4 -2
  28. package/cjs/koni/background/subscription.js +89 -19
  29. package/cjs/packageInfo.js +1 -1
  30. package/cjs/services/campaign-service/index.js +9 -6
  31. package/cjs/services/chain-service/constants.js +2 -16
  32. package/cjs/services/chain-service/utils.js +7 -1
  33. package/cjs/services/earning-service/constants/chains.js +29 -0
  34. package/cjs/services/earning-service/constants/index.js +27 -0
  35. package/cjs/services/earning-service/constants/step.js +18 -0
  36. package/cjs/services/earning-service/handlers/base.js +202 -0
  37. package/cjs/services/earning-service/handlers/index.js +60 -0
  38. package/cjs/services/earning-service/handlers/lending/base.js +20 -0
  39. package/cjs/services/earning-service/handlers/lending/index.js +13 -0
  40. package/cjs/services/earning-service/handlers/lending/interlay.js +168 -0
  41. package/cjs/services/earning-service/handlers/liquid-staking/acala.js +196 -0
  42. package/cjs/services/earning-service/handlers/liquid-staking/base.js +39 -0
  43. package/cjs/services/earning-service/handlers/liquid-staking/bifrost.js +257 -0
  44. package/cjs/services/earning-service/handlers/liquid-staking/index.js +34 -0
  45. package/cjs/services/earning-service/handlers/liquid-staking/parallel.js +180 -0
  46. package/cjs/services/earning-service/handlers/liquid-staking/stella-swap.js +373 -0
  47. package/cjs/services/earning-service/handlers/native-staking/amplitude.js +359 -0
  48. package/cjs/services/earning-service/handlers/native-staking/astar.js +426 -0
  49. package/cjs/services/earning-service/handlers/native-staking/base-para.js +137 -0
  50. package/cjs/services/earning-service/handlers/native-staking/base.js +109 -0
  51. package/cjs/services/earning-service/handlers/native-staking/index.js +34 -0
  52. package/cjs/services/earning-service/handlers/native-staking/para-chain.js +361 -0
  53. package/cjs/services/earning-service/handlers/native-staking/relay-chain.js +512 -0
  54. package/cjs/services/earning-service/handlers/nomination-pool/index.js +501 -0
  55. package/cjs/services/earning-service/handlers/special.js +387 -0
  56. package/cjs/services/earning-service/service.js +327 -0
  57. package/cjs/services/earning-service/utils/index.js +111 -0
  58. package/cjs/services/event-service/index.js +4 -0
  59. package/cjs/services/keyring-service/index.js +14 -2
  60. package/cjs/services/migration-service/scripts/DeleteEarningData.js +21 -0
  61. package/cjs/services/migration-service/scripts/EnableEarningChains.js +21 -0
  62. package/cjs/services/migration-service/scripts/MigrateEthProvider.js +17 -0
  63. package/cjs/services/migration-service/scripts/MigrateLedgerAccount.js +1 -1
  64. package/cjs/services/migration-service/scripts/MigratePioneerProvider.js +17 -0
  65. package/cjs/services/migration-service/scripts/MigrateProvider.js +29 -0
  66. package/cjs/services/migration-service/scripts/MigrateTransactionHistory.js +1 -1
  67. package/cjs/services/migration-service/scripts/index.js +4 -0
  68. package/cjs/services/mint-campaign-service/campaigns/UnlockDotCampaign.js +149 -0
  69. package/cjs/services/mint-campaign-service/campaigns/index.js +13 -0
  70. package/cjs/services/mint-campaign-service/constants.js +11 -0
  71. package/cjs/services/mint-campaign-service/index.js +18 -0
  72. package/cjs/services/notification-service/NotificationService.js +1 -1
  73. package/cjs/services/storage-service/DatabaseService.js +47 -0
  74. package/cjs/services/storage-service/databases/index.js +4 -0
  75. package/cjs/services/storage-service/db-stores/NominatorMetadata.js +3 -3
  76. package/cjs/services/storage-service/db-stores/YieldPoolStore.js +36 -0
  77. package/cjs/services/storage-service/db-stores/YieldPositionStore.js +65 -0
  78. package/cjs/services/storage-service/index.js +241 -0
  79. package/cjs/services/transaction-service/event-parser/index.js +58 -0
  80. package/cjs/services/transaction-service/helpers/index.js +3 -1
  81. package/cjs/services/transaction-service/index.js +180 -74
  82. package/cjs/services/transaction-service/utils.js +1 -0
  83. package/cjs/types/campaigns/index.js +16 -0
  84. package/cjs/types/campaigns/unlock-dot.js +1 -0
  85. package/cjs/types/index.js +44 -0
  86. package/cjs/types/ordinal.js +1 -0
  87. package/cjs/types/transaction.js +1 -0
  88. package/cjs/types/yield/actions/index.js +27 -0
  89. package/cjs/types/yield/actions/join/index.js +38 -0
  90. package/cjs/types/yield/actions/join/step.js +37 -0
  91. package/cjs/types/yield/actions/join/submit.js +1 -0
  92. package/cjs/types/yield/actions/join/validate.js +16 -0
  93. package/cjs/types/yield/actions/others.js +1 -0
  94. package/cjs/types/yield/index.js +27 -0
  95. package/cjs/types/yield/info/account/index.js +49 -0
  96. package/cjs/types/yield/info/account/info.js +1 -0
  97. package/cjs/types/yield/info/account/reward.js +1 -0
  98. package/cjs/types/yield/info/account/target.js +32 -0
  99. package/cjs/types/yield/info/account/unstake.js +27 -0
  100. package/cjs/types/yield/info/base.js +34 -0
  101. package/cjs/types/yield/info/chain/index.js +27 -0
  102. package/cjs/types/yield/info/chain/info.js +1 -0
  103. package/cjs/types/yield/info/chain/target.js +1 -0
  104. package/cjs/types/yield/info/index.js +49 -0
  105. package/cjs/types/yield/info/pallet.js +15 -0
  106. package/cjs/types.js +1 -0
  107. package/cjs/utils/address.js +34 -0
  108. package/cjs/utils/fetchStaticData.js +2 -1
  109. package/cjs/utils/index.js +89 -1
  110. package/cjs/utils/keyring.js +57 -0
  111. package/cjs/utils/object.js +12 -0
  112. package/constants/index.d.ts +2 -0
  113. package/constants/index.js +2 -0
  114. package/koni/api/nft/config.js +33 -23
  115. package/koni/api/nft/index.js +15 -1
  116. package/koni/api/nft/nft.js +2 -23
  117. package/koni/api/nft/ordinal_nft/constants.d.ts +9 -0
  118. package/koni/api/nft/ordinal_nft/constants.js +12 -0
  119. package/koni/api/nft/ordinal_nft/index.d.ts +8 -0
  120. package/koni/api/nft/ordinal_nft/index.js +114 -0
  121. package/koni/api/nft/ordinal_nft/utils.d.ts +2 -0
  122. package/koni/api/nft/ordinal_nft/utils.js +33 -0
  123. package/koni/api/staking/bonding/amplitude.js +11 -9
  124. package/koni/api/staking/bonding/astar.js +9 -8
  125. package/koni/api/staking/bonding/index.d.ts +1 -1
  126. package/koni/api/staking/bonding/index.js +5 -1
  127. package/koni/api/staking/bonding/paraChain.js +12 -10
  128. package/koni/api/staking/bonding/relayChain.d.ts +2 -2
  129. package/koni/api/staking/bonding/relayChain.js +33 -30
  130. package/koni/api/staking/bonding/utils.d.ts +15 -38
  131. package/koni/api/staking/bonding/utils.js +85 -69
  132. package/koni/api/staking/index.js +2 -1
  133. package/koni/api/staking/paraChain.js +7 -6
  134. package/koni/api/staking/relayChain.js +4 -3
  135. package/koni/api/yield/helper/utils.d.ts +10 -0
  136. package/koni/api/yield/helper/utils.js +32 -0
  137. package/koni/background/cron.js +2 -0
  138. package/koni/background/handlers/Extension.d.ts +14 -1
  139. package/koni/background/handlers/Extension.js +282 -30
  140. package/koni/background/handlers/State.d.ts +18 -2
  141. package/koni/background/handlers/State.js +56 -4
  142. package/koni/background/handlers/index.js +4 -2
  143. package/koni/background/subscription.d.ts +5 -1
  144. package/koni/background/subscription.js +91 -21
  145. package/package.json +311 -8
  146. package/packageInfo.js +1 -1
  147. package/services/campaign-service/index.js +9 -6
  148. package/services/chain-service/constants.d.ts +0 -11
  149. package/services/chain-service/constants.js +1 -14
  150. package/services/chain-service/utils.d.ts +1 -0
  151. package/services/chain-service/utils.js +5 -1
  152. package/services/earning-service/constants/abis/compound_finance_v2_abi.json +1235 -0
  153. package/services/earning-service/constants/abis/st_liquid_token_abi.json +1355 -0
  154. package/services/earning-service/constants/chains.d.ts +14 -0
  155. package/services/earning-service/constants/chains.js +21 -0
  156. package/services/earning-service/constants/index.d.ts +2 -0
  157. package/services/earning-service/constants/index.js +5 -0
  158. package/services/earning-service/constants/step.d.ts +3 -0
  159. package/services/earning-service/constants/step.js +10 -0
  160. package/services/earning-service/handlers/base.d.ts +105 -0
  161. package/services/earning-service/handlers/base.js +195 -0
  162. package/services/earning-service/handlers/index.d.ts +5 -0
  163. package/services/earning-service/handlers/index.js +8 -0
  164. package/services/earning-service/handlers/lending/base.d.ts +6 -0
  165. package/services/earning-service/handlers/lending/base.js +12 -0
  166. package/services/earning-service/handlers/lending/index.d.ts +1 -0
  167. package/services/earning-service/handlers/lending/index.js +4 -0
  168. package/services/earning-service/handlers/lending/interlay.d.ts +22 -0
  169. package/services/earning-service/handlers/lending/interlay.js +160 -0
  170. package/services/earning-service/handlers/liquid-staking/acala.d.ts +27 -0
  171. package/services/earning-service/handlers/liquid-staking/acala.js +188 -0
  172. package/services/earning-service/handlers/liquid-staking/base.d.ts +8 -0
  173. package/services/earning-service/handlers/liquid-staking/base.js +31 -0
  174. package/services/earning-service/handlers/liquid-staking/bifrost.d.ts +44 -0
  175. package/services/earning-service/handlers/liquid-staking/bifrost.js +246 -0
  176. package/services/earning-service/handlers/liquid-staking/index.d.ts +4 -0
  177. package/services/earning-service/handlers/liquid-staking/index.js +7 -0
  178. package/services/earning-service/handlers/liquid-staking/parallel.d.ts +27 -0
  179. package/services/earning-service/handlers/liquid-staking/parallel.js +172 -0
  180. package/services/earning-service/handlers/liquid-staking/stella-swap.d.ts +36 -0
  181. package/services/earning-service/handlers/liquid-staking/stella-swap.js +363 -0
  182. package/services/earning-service/handlers/native-staking/amplitude.d.ts +22 -0
  183. package/services/earning-service/handlers/native-staking/amplitude.js +350 -0
  184. package/services/earning-service/handlers/native-staking/astar.d.ts +18 -0
  185. package/services/earning-service/handlers/native-staking/astar.js +416 -0
  186. package/services/earning-service/handlers/native-staking/base-para.d.ts +11 -0
  187. package/services/earning-service/handlers/native-staking/base-para.js +129 -0
  188. package/services/earning-service/handlers/native-staking/base.d.ts +19 -0
  189. package/services/earning-service/handlers/native-staking/base.js +101 -0
  190. package/services/earning-service/handlers/native-staking/index.d.ts +4 -0
  191. package/services/earning-service/handlers/native-staking/index.js +7 -0
  192. package/services/earning-service/handlers/native-staking/para-chain.d.ts +15 -0
  193. package/services/earning-service/handlers/native-staking/para-chain.js +353 -0
  194. package/services/earning-service/handlers/native-staking/relay-chain.d.ts +21 -0
  195. package/services/earning-service/handlers/native-staking/relay-chain.js +503 -0
  196. package/services/earning-service/handlers/nomination-pool/index.d.ts +33 -0
  197. package/services/earning-service/handlers/nomination-pool/index.js +492 -0
  198. package/services/earning-service/handlers/special.d.ts +56 -0
  199. package/services/earning-service/handlers/special.js +379 -0
  200. package/services/earning-service/service.d.ts +36 -0
  201. package/services/earning-service/service.js +320 -0
  202. package/services/earning-service/utils/index.d.ts +14 -0
  203. package/services/earning-service/utils/index.js +100 -0
  204. package/services/event-service/index.d.ts +1 -0
  205. package/services/event-service/index.js +4 -0
  206. package/services/event-service/types.d.ts +8 -0
  207. package/services/keyring-service/index.d.ts +2 -1
  208. package/services/keyring-service/index.js +14 -2
  209. package/services/migration-service/scripts/DeleteEarningData.d.ts +4 -0
  210. package/services/migration-service/scripts/DeleteEarningData.js +13 -0
  211. package/services/migration-service/scripts/EnableEarningChains.d.ts +4 -0
  212. package/services/migration-service/scripts/EnableEarningChains.js +13 -0
  213. package/services/migration-service/scripts/MigrateLedgerAccount.js +1 -1
  214. package/services/migration-service/scripts/MigrateTransactionHistory.js +1 -1
  215. package/services/migration-service/scripts/index.js +4 -0
  216. package/services/mint-campaign-service/campaigns/UnlockDotCampaign.d.ts +13 -0
  217. package/services/mint-campaign-service/campaigns/UnlockDotCampaign.js +139 -0
  218. package/services/mint-campaign-service/campaigns/index.d.ts +1 -0
  219. package/services/mint-campaign-service/campaigns/index.js +4 -0
  220. package/services/mint-campaign-service/constants.d.ts +1 -0
  221. package/services/mint-campaign-service/constants.js +4 -0
  222. package/services/mint-campaign-service/index.d.ts +7 -0
  223. package/services/mint-campaign-service/index.js +11 -0
  224. package/services/notification-service/NotificationService.js +1 -1
  225. package/services/storage-service/DatabaseService.d.ts +17 -1
  226. package/services/storage-service/DatabaseService.js +47 -0
  227. package/services/storage-service/databases/index.d.ts +3 -1
  228. package/services/storage-service/databases/index.js +4 -0
  229. package/services/storage-service/db-stores/NominatorMetadata.js +3 -3
  230. package/services/storage-service/db-stores/YieldPoolStore.d.ts +10 -0
  231. package/services/storage-service/db-stores/YieldPoolStore.js +28 -0
  232. package/services/storage-service/db-stores/YieldPositionStore.d.ts +9 -0
  233. package/services/storage-service/db-stores/YieldPositionStore.js +57 -0
  234. package/services/subscan-service/index.d.ts +2 -2
  235. package/services/transaction-service/event-parser/index.d.ts +3 -1
  236. package/services/transaction-service/event-parser/index.js +57 -1
  237. package/services/transaction-service/helpers/index.js +3 -1
  238. package/services/transaction-service/index.d.ts +4 -15
  239. package/services/transaction-service/index.js +178 -72
  240. package/services/transaction-service/types.d.ts +2 -0
  241. package/services/transaction-service/utils.js +1 -0
  242. package/types/campaigns/index.d.ts +1 -0
  243. package/types/campaigns/index.js +4 -0
  244. package/types/campaigns/unlock-dot.d.ts +71 -0
  245. package/types/campaigns/unlock-dot.js +1 -0
  246. package/types/index.d.ts +5 -0
  247. package/types/index.js +5 -1
  248. package/types/ordinal.d.ts +69 -0
  249. package/types/ordinal.js +1 -0
  250. package/types/transaction.d.ts +3 -0
  251. package/types/transaction.js +1 -0
  252. package/types/yield/actions/index.d.ts +2 -0
  253. package/types/yield/actions/index.js +5 -0
  254. package/types/yield/actions/join/index.d.ts +3 -0
  255. package/types/yield/actions/join/index.js +6 -0
  256. package/types/yield/actions/join/step.d.ts +75 -0
  257. package/types/yield/actions/join/step.js +34 -0
  258. package/types/yield/actions/join/submit.d.ts +53 -0
  259. package/types/yield/actions/join/submit.js +1 -0
  260. package/types/yield/actions/join/validate.d.ts +18 -0
  261. package/types/yield/actions/join/validate.js +10 -0
  262. package/types/yield/actions/others.d.ts +60 -0
  263. package/types/yield/actions/others.js +1 -0
  264. package/types/yield/index.d.ts +2 -0
  265. package/types/yield/index.js +5 -0
  266. package/types/yield/info/account/index.d.ts +4 -0
  267. package/types/yield/info/account/index.js +7 -0
  268. package/types/yield/info/account/info.d.ts +112 -0
  269. package/types/yield/info/account/info.js +1 -0
  270. package/types/yield/info/account/reward.d.ts +41 -0
  271. package/types/yield/info/account/reward.js +1 -0
  272. package/types/yield/info/account/target.d.ts +43 -0
  273. package/types/yield/info/account/target.js +27 -0
  274. package/types/yield/info/account/unstake.d.ts +31 -0
  275. package/types/yield/info/account/unstake.js +22 -0
  276. package/types/yield/info/base.d.ts +28 -0
  277. package/types/yield/info/base.js +28 -0
  278. package/types/yield/info/chain/index.d.ts +2 -0
  279. package/types/yield/info/chain/index.js +5 -0
  280. package/types/yield/info/chain/info.d.ts +190 -0
  281. package/types/yield/info/chain/info.js +1 -0
  282. package/types/yield/info/chain/target.d.ts +33 -0
  283. package/types/yield/info/chain/target.js +1 -0
  284. package/types/yield/info/index.d.ts +4 -0
  285. package/types/yield/info/index.js +7 -0
  286. package/types/yield/info/pallet.d.ts +143 -0
  287. package/types/yield/info/pallet.js +9 -0
  288. package/utils/fetchStaticData.js +2 -1
  289. package/utils/index.d.ts +5 -0
  290. package/utils/index.js +53 -1
  291. package/utils/object.d.ts +1 -0
  292. package/utils/object.js +6 -0
@@ -0,0 +1,379 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { COMMON_CHAIN_SLUGS } from '@subwallet/chain-list';
5
+ import { TransactionError } from '@subwallet/extension-base/background/errors/TransactionError';
6
+ import { BasicTxErrorType, ExtrinsicType, StakingTxErrorType } from '@subwallet/extension-base/background/KoniTypes';
7
+ import { createXcmExtrinsic } from '@subwallet/extension-base/koni/api/xcm';
8
+ import { YIELD_POOL_STAT_REFRESH_INTERVAL } from '@subwallet/extension-base/koni/api/yield/helper/utils';
9
+ import { _getChainNativeTokenSlug } from '@subwallet/extension-base/services/chain-service/utils';
10
+ import { YieldStepType, YieldValidationStatus } from '@subwallet/extension-base/types';
11
+ import BN from 'bn.js';
12
+ import { t } from 'i18next';
13
+ import { BN_ZERO, noop } from '@polkadot/util';
14
+ import BasePoolHandler from "./base.js";
15
+ export default class BaseSpecialStakingPoolHandler extends BasePoolHandler {
16
+ /** Allow to create default unstake transaction */
17
+ allowDefaultUnstake = false;
18
+ /** Allow to create fast unstake transaction */
19
+ allowFastUnstake = true;
20
+ get baseMetadata() {
21
+ return {
22
+ altInputAssets: this.altInputAsset,
23
+ derivativeAssets: this.derivativeAssets,
24
+ inputAsset: this.inputAsset,
25
+ rewardAssets: this.rewardAssets,
26
+ feeAssets: this.feeAssets
27
+ };
28
+ }
29
+ get isPoolSupportAlternativeFee() {
30
+ return this.feeAssets.length > 1;
31
+ }
32
+ get group() {
33
+ const inputAsset = this.state.getAssetBySlug(this.inputAsset);
34
+ const groupSlug = inputAsset.multiChainAsset;
35
+ return groupSlug || this.inputAsset;
36
+ }
37
+
38
+ /* Subscribe pool info */
39
+
40
+ async subscribePoolInfo(callback) {
41
+ let cancel = false;
42
+ const getStatInterval = () => {
43
+ this.getPoolStat().then(rs => {
44
+ if (!cancel) {
45
+ callback(rs);
46
+ }
47
+ }).catch(console.error);
48
+ };
49
+ getStatInterval();
50
+ const interval = setInterval(() => {
51
+ if (cancel) {
52
+ clearInterval(interval);
53
+ } else {
54
+ getStatInterval();
55
+ }
56
+ }, YIELD_POOL_STAT_REFRESH_INTERVAL);
57
+ return new Promise(resolve => {
58
+ const rs = () => {
59
+ cancel = true;
60
+ clearInterval(interval);
61
+ };
62
+ resolve(rs);
63
+ });
64
+ }
65
+
66
+ /* Subscribe pool info */
67
+
68
+ /* Get pool reward */
69
+
70
+ async getPoolReward() {
71
+ return new Promise(resolve => resolve(noop));
72
+ }
73
+
74
+ /* Get pool reward */
75
+
76
+ /* Get pool targets */
77
+
78
+ async getPoolTargets() {
79
+ return new Promise(resolve => resolve([]));
80
+ }
81
+
82
+ /* Get pool targets */
83
+
84
+ /* Join pool action */
85
+
86
+ /* Generate steps */
87
+
88
+ /**
89
+ * @async
90
+ * @function getXcmStep
91
+ * */
92
+ async getXcmStep(params) {
93
+ const {
94
+ address,
95
+ amount
96
+ } = params;
97
+ const bnAmount = new BN(amount);
98
+ const inputTokenSlug = this.inputAsset; // assume that the pool only has 1 input token, will update later
99
+ const inputTokenInfo = this.state.getAssetBySlug(inputTokenSlug);
100
+ const inputTokenBalance = await this.state.balanceService.getTokenFreeBalance(address, inputTokenInfo.originChain, inputTokenSlug);
101
+ const bnInputTokenBalance = new BN(inputTokenBalance.value);
102
+ if (!bnInputTokenBalance.gte(bnAmount)) {
103
+ if (this.altInputAsset) {
104
+ const altInputTokenSlug = this.altInputAsset;
105
+ const altInputTokenInfo = this.state.getAssetBySlug(altInputTokenSlug);
106
+ const altInputTokenBalance = await this.state.balanceService.getTokenFreeBalance(address, altInputTokenInfo.originChain, altInputTokenSlug);
107
+ const bnAltInputTokenBalance = new BN(altInputTokenBalance.value || '0');
108
+ if (bnAltInputTokenBalance.gt(BN_ZERO)) {
109
+ const step = {
110
+ metadata: {
111
+ sendingValue: bnAmount.toString(),
112
+ originTokenInfo: altInputTokenInfo,
113
+ destinationTokenInfo: inputTokenInfo
114
+ },
115
+ name: 'Transfer DOT from Polkadot',
116
+ type: YieldStepType.XCM
117
+ };
118
+ const xcmOriginSubstrateApi = await this.state.getSubstrateApi(altInputTokenInfo.originChain).isReady;
119
+ const xcmTransfer = await createXcmExtrinsic({
120
+ originTokenInfo: altInputTokenInfo,
121
+ destinationTokenInfo: inputTokenInfo,
122
+ sendingValue: bnAmount.toString(),
123
+ recipient: address,
124
+ chainInfoMap: this.state.getChainInfoMap(),
125
+ substrateApi: xcmOriginSubstrateApi
126
+ });
127
+ const _xcmFeeInfo = await xcmTransfer.paymentInfo(address);
128
+ const xcmFeeInfo = _xcmFeeInfo.toPrimitive();
129
+ // TODO: calculate fee for destination chain
130
+
131
+ const fee = {
132
+ slug: altInputTokenSlug,
133
+ amount: Math.round(xcmFeeInfo.partialFee * 1.2).toString() // TODO
134
+ };
135
+
136
+ return [step, fee];
137
+ }
138
+ }
139
+ }
140
+ return undefined;
141
+ }
142
+ get defaultSubmitStep() {
143
+ return [this.submitJoinStepInfo, {
144
+ slug: this.feeAssets[0],
145
+ amount: '0'
146
+ }];
147
+ }
148
+
149
+ /**
150
+ * @function submitJoinStepInfo
151
+ * @description Base info of submit step
152
+ * @return Fee of the submitting step
153
+ * */
154
+
155
+ async getSubmitStep(params) {
156
+ const fee = await this.getSubmitStepFee(params);
157
+ return [this.submitJoinStepInfo, fee];
158
+ }
159
+
160
+ /* Generate steps */
161
+
162
+ /* Validate join action */
163
+
164
+ async validateTokenApproveStep(params, path) {
165
+ return Promise.resolve([new TransactionError(BasicTxErrorType.UNSUPPORTED)]);
166
+ }
167
+ async validateXcmStep(params, path, bnInputTokenBalance) {
168
+ const processValidation = {
169
+ ok: true,
170
+ status: YieldValidationStatus.OK
171
+ };
172
+ const bnAmount = new BN(params.amount);
173
+ const altInputTokenSlug = this.altInputAsset || '';
174
+ const altInputTokenInfo = this.state.getAssetBySlug(altInputTokenSlug);
175
+ const altInputTokenBalance = await this.state.balanceService.getTokenFreeBalance(params.address, altInputTokenInfo.originChain, altInputTokenSlug);
176
+ const missingAmount = bnAmount.sub(bnInputTokenBalance); // TODO: what if input token is not LOCAL ??
177
+ const xcmFee = new BN(path.totalFee[1].amount || '0');
178
+ const xcmAmount = missingAmount.add(xcmFee);
179
+ const bnAltInputTokenBalance = new BN(altInputTokenBalance.value || '0');
180
+ const altInputTokenMinAmount = new BN(altInputTokenInfo.minAmount || '0');
181
+ if (!bnAltInputTokenBalance.sub(xcmAmount).gte(altInputTokenMinAmount)) {
182
+ processValidation.failedStep = path.steps[1];
183
+ processValidation.ok = false;
184
+ processValidation.status = YieldValidationStatus.NOT_ENOUGH_BALANCE;
185
+ return [new TransactionError(YieldValidationStatus.NOT_ENOUGH_BALANCE, processValidation.message, processValidation)];
186
+ }
187
+ return [];
188
+ }
189
+ async validateJoinStep(id, params, path, bnInputTokenBalance, isXcmOk) {
190
+ const _poolInfo = await this.getPoolInfo();
191
+ if (!_poolInfo) {
192
+ return [new TransactionError(BasicTxErrorType.INTERNAL_ERROR)];
193
+ }
194
+ const poolInfo = _poolInfo;
195
+ const processValidation = {
196
+ ok: true,
197
+ status: YieldValidationStatus.OK
198
+ };
199
+ const feeTokenSlug = path.totalFee[id].slug;
200
+ const feeTokenInfo = this.state.getAssetBySlug(feeTokenSlug);
201
+ const defaultFeeTokenSlug = this.feeAssets[0];
202
+ const bnAmount = new BN(params.amount);
203
+ if (this.feeAssets.length === 1 && feeTokenSlug === defaultFeeTokenSlug) {
204
+ var _path$totalFee$id;
205
+ const bnFeeAmount = new BN(((_path$totalFee$id = path.totalFee[id]) === null || _path$totalFee$id === void 0 ? void 0 : _path$totalFee$id.amount) || '0');
206
+ const feeTokenBalance = await this.state.balanceService.getTokenFreeBalance(params.address, feeTokenInfo.originChain, feeTokenSlug);
207
+ const bnFeeTokenBalance = new BN(feeTokenBalance.value || '0');
208
+ const bnFeeTokenMinAmount = new BN((feeTokenInfo === null || feeTokenInfo === void 0 ? void 0 : feeTokenInfo.minAmount) || '0');
209
+ if (!bnFeeTokenBalance.sub(bnFeeAmount).gte(bnFeeTokenMinAmount)) {
210
+ processValidation.failedStep = path.steps[id];
211
+ processValidation.ok = false;
212
+ processValidation.status = YieldValidationStatus.NOT_ENOUGH_FEE;
213
+ return [new TransactionError(YieldValidationStatus.NOT_ENOUGH_FEE, processValidation.message, processValidation)];
214
+ }
215
+ }
216
+ if (!bnAmount.gte(new BN(poolInfo.metadata.minJoinPool || '0'))) {
217
+ processValidation.failedStep = path.steps[id];
218
+ processValidation.ok = false;
219
+ processValidation.status = YieldValidationStatus.NOT_ENOUGH_MIN_JOIN_POOL;
220
+ return [new TransactionError(YieldValidationStatus.NOT_ENOUGH_MIN_JOIN_POOL, processValidation.message, processValidation)];
221
+ }
222
+ if (!isXcmOk && bnAmount.gt(bnInputTokenBalance)) {
223
+ processValidation.failedStep = path.steps[id];
224
+ processValidation.ok = false;
225
+ processValidation.status = YieldValidationStatus.NOT_ENOUGH_BALANCE;
226
+ return [new TransactionError(YieldValidationStatus.NOT_ENOUGH_BALANCE, processValidation.message, processValidation)];
227
+ }
228
+ return [];
229
+ }
230
+ async validateYieldJoin(params, path) {
231
+ const inputTokenSlug = this.inputAsset;
232
+ const inputTokenInfo = this.state.getAssetBySlug(inputTokenSlug);
233
+ const balanceService = this.state.balanceService;
234
+ const inputTokenBalance = await balanceService.getTokenFreeBalance(params.address, inputTokenInfo.originChain, inputTokenSlug);
235
+ const bnInputTokenBalance = new BN(inputTokenBalance.value || '0');
236
+ let isXcmOk = false;
237
+ for (const step of path.steps) {
238
+ const getErrors = async () => {
239
+ switch (step.type) {
240
+ case YieldStepType.DEFAULT:
241
+ return Promise.resolve([]);
242
+ case YieldStepType.XCM:
243
+ return this.validateXcmStep(params, path, bnInputTokenBalance);
244
+ case YieldStepType.TOKEN_APPROVAL:
245
+ return this.validateTokenApproveStep(params, path);
246
+ default:
247
+ return this.validateJoinStep(step.id, params, path, bnInputTokenBalance, isXcmOk);
248
+ }
249
+ };
250
+ const errors = await getErrors();
251
+ if (errors.length) {
252
+ return errors;
253
+ } else if (step.type === YieldStepType.XCM) {
254
+ isXcmOk = true;
255
+ }
256
+ }
257
+ return [];
258
+ }
259
+
260
+ /* Validate join action */
261
+
262
+ /* Submit join action */
263
+
264
+ async handleTokenApproveStep(data, path) {
265
+ return Promise.reject(new TransactionError(BasicTxErrorType.UNSUPPORTED));
266
+ }
267
+ async handleXcmStep(data, path, xcmFee) {
268
+ const {
269
+ address,
270
+ amount
271
+ } = data;
272
+ const destinationTokenSlug = this.inputAsset;
273
+ const originChainInfo = this.state.getChainInfo(COMMON_CHAIN_SLUGS.POLKADOT);
274
+ const originTokenSlug = _getChainNativeTokenSlug(originChainInfo);
275
+ const originTokenInfo = this.state.getAssetBySlug(originTokenSlug);
276
+ const destinationTokenInfo = this.state.getAssetBySlug(destinationTokenSlug);
277
+ const substrateApi = this.state.getSubstrateApi(originChainInfo.slug);
278
+ const inputTokenBalance = await this.state.balanceService.getTokenFreeBalance(address, destinationTokenInfo.originChain, destinationTokenSlug);
279
+ const bnInputTokenBalance = new BN(inputTokenBalance.value);
280
+ const bnXcmFee = new BN(xcmFee);
281
+ const bnAmount = new BN(amount);
282
+ const bnTotalAmount = bnAmount.sub(bnInputTokenBalance).add(bnXcmFee);
283
+ const extrinsic = await createXcmExtrinsic({
284
+ chainInfoMap: this.state.getChainInfoMap(),
285
+ destinationTokenInfo,
286
+ originTokenInfo,
287
+ recipient: address,
288
+ sendingValue: bnTotalAmount.toString(),
289
+ substrateApi
290
+ });
291
+ const xcmData = {
292
+ originNetworkKey: originChainInfo.slug,
293
+ destinationNetworkKey: destinationTokenInfo.originChain,
294
+ from: address,
295
+ to: address,
296
+ value: bnTotalAmount.toString(),
297
+ tokenSlug: originTokenSlug,
298
+ showExtraWarning: true
299
+ };
300
+ return {
301
+ txChain: originChainInfo.slug,
302
+ extrinsicType: ExtrinsicType.TRANSFER_XCM,
303
+ extrinsic,
304
+ txData: xcmData,
305
+ transferNativeAmount: bnTotalAmount.toString()
306
+ };
307
+ }
308
+ handleYieldJoin(data, path, currentStep) {
309
+ const type = path.steps[currentStep].type;
310
+ switch (type) {
311
+ case YieldStepType.DEFAULT:
312
+ return Promise.reject(new TransactionError(BasicTxErrorType.UNSUPPORTED));
313
+ case YieldStepType.TOKEN_APPROVAL:
314
+ return this.handleTokenApproveStep(data, path);
315
+ case YieldStepType.XCM:
316
+ {
317
+ const xcmFee = path.totalFee[currentStep].amount || '0';
318
+ return this.handleXcmStep(data, path, xcmFee);
319
+ }
320
+ default:
321
+ return this.handleSubmitStep(data, path);
322
+ }
323
+ }
324
+
325
+ /* Submit join action */
326
+
327
+ /* Join pool action */
328
+
329
+ /* Leave pool action */
330
+
331
+ async validateYieldLeave(amount, address, fastLeave, selectedTarget) {
332
+ const poolInfo = await this.getPoolInfo();
333
+ const poolPosition = await this.getPoolPosition(address);
334
+ if (!poolInfo || !poolPosition) {
335
+ return [new TransactionError(BasicTxErrorType.INTERNAL_ERROR)];
336
+ }
337
+ if (!this.allowDefaultUnstake && !fastLeave) {
338
+ return [new TransactionError(BasicTxErrorType.INTERNAL_ERROR)];
339
+ }
340
+ if (!this.allowFastUnstake && fastLeave) {
341
+ return [new TransactionError(BasicTxErrorType.INTERNAL_ERROR)];
342
+ }
343
+ const errors = [];
344
+ const bnActiveStake = new BN(poolPosition.activeStake);
345
+ const bnRemainingStake = bnActiveStake.sub(new BN(amount));
346
+ const minStake = new BN(poolInfo.metadata.minJoinPool || '0');
347
+ const maxUnstake = poolInfo.metadata.maxWithdrawalRequestPerFarmer;
348
+ if (!(bnRemainingStake.isZero() || bnRemainingStake.gte(minStake))) {
349
+ errors.push(new TransactionError(StakingTxErrorType.INVALID_ACTIVE_STAKE));
350
+ }
351
+ if (poolPosition.unstakings.length > maxUnstake) {
352
+ errors.push(new TransactionError(StakingTxErrorType.EXCEED_MAX_UNSTAKING, t('You cannot unstake more than {{number}} times', {
353
+ replace: {
354
+ number: maxUnstake
355
+ }
356
+ })));
357
+ }
358
+ return Promise.resolve(errors);
359
+ }
360
+ handleYieldUnstake(amount, address, selectedTarget) {
361
+ return Promise.reject(new TransactionError(BasicTxErrorType.UNSUPPORTED));
362
+ }
363
+
364
+ /* Leave pool action */
365
+
366
+ /* Other action */
367
+
368
+ handleYieldCancelUnstake() {
369
+ return Promise.reject(new TransactionError(BasicTxErrorType.UNSUPPORTED));
370
+ }
371
+ handleYieldClaimReward(address, bondReward) {
372
+ return Promise.reject(new TransactionError(BasicTxErrorType.UNSUPPORTED));
373
+ }
374
+ handleYieldWithdraw(address, unstakingInfo) {
375
+ return Promise.reject(new TransactionError(BasicTxErrorType.UNSUPPORTED));
376
+ }
377
+
378
+ /* Other actions */
379
+ }
@@ -0,0 +1,36 @@
1
+ import { TransactionError } from '@subwallet/extension-base/background/errors/TransactionError';
2
+ import { ExtrinsicType } from '@subwallet/extension-base/background/KoniTypes';
3
+ import KoniState from '@subwallet/extension-base/koni/background/handlers/State';
4
+ import { EarningRewardItem, EarningRewardJson, HandleYieldStepData, HandleYieldStepParams, OptimalYieldPath, OptimalYieldPathParams, RequestStakeCancelWithdrawal, RequestStakeClaimReward, RequestYieldLeave, RequestYieldWithdrawal, TransactionData, ValidateYieldProcessParams, YieldPoolInfo, YieldPoolTarget, YieldPositionInfo } from '@subwallet/extension-base/types';
5
+ import { BehaviorSubject } from 'rxjs';
6
+ import { BasePoolHandler } from './handlers';
7
+ export default class EarningService {
8
+ protected readonly state: KoniState;
9
+ protected handlers: Record<string, BasePoolHandler>;
10
+ private earningRewardSubject;
11
+ constructor(state: KoniState);
12
+ initHandlers(): void;
13
+ getPoolHandler(slug: string): BasePoolHandler | undefined;
14
+ isPoolSupportAlternativeFee(slug: string): boolean;
15
+ subscribePoolsInfo(callback: (rs: YieldPoolInfo) => void): Promise<VoidFunction>;
16
+ subscribePoolPositions(addresses: string[], callback: (rs: YieldPositionInfo) => void): Promise<VoidFunction>;
17
+ updateEarningReward(stakingRewardData: EarningRewardItem, callback?: (earningRewardData: EarningRewardJson) => void): void;
18
+ getPoolReward(addresses: string[], callback: (result: EarningRewardItem) => void): Promise<VoidFunction>;
19
+ subscribeEarningReward(): BehaviorSubject<EarningRewardJson>;
20
+ getEarningRewards(): EarningRewardJson;
21
+ /**
22
+ * @async
23
+ * @function getPoolTargets
24
+ * @param {string} slug - Pool's slug
25
+ * @return {Promise<YieldPoolTarget[]>} List of pool's target
26
+ * */
27
+ getPoolTargets(slug: string): Promise<YieldPoolTarget[]>;
28
+ generateOptimalSteps(params: OptimalYieldPathParams): Promise<OptimalYieldPath>;
29
+ validateYieldJoin(params: ValidateYieldProcessParams): Promise<TransactionError[]>;
30
+ handleYieldJoin(params: HandleYieldStepParams): Promise<HandleYieldStepData>;
31
+ validateYieldLeave(params: RequestYieldLeave): Promise<TransactionError[]>;
32
+ handleYieldLeave(params: RequestYieldLeave): Promise<[ExtrinsicType, TransactionData]>;
33
+ handleYieldWithdraw(params: RequestYieldWithdrawal): Promise<TransactionData>;
34
+ handleYieldCancelUnstake(params: RequestStakeCancelWithdrawal): Promise<TransactionData>;
35
+ handleYieldClaimReward(params: RequestStakeClaimReward): Promise<TransactionData>;
36
+ }