@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.
- package/background/KoniTypes.d.ts +10 -3
- package/background/KoniTypes.js +6 -0
- package/cjs/background/KoniTypes.js +8 -1
- package/cjs/constants/index.js +6 -3
- package/cjs/koni/api/dotsama/balance.js +11 -0
- package/cjs/koni/api/staking/bonding/amplitude.js +6 -2
- package/cjs/koni/api/staking/bonding/astar.js +40 -243
- package/cjs/koni/api/staking/bonding/index.js +4 -4
- package/cjs/koni/api/staking/bonding/paraChain.js +16 -10
- package/cjs/koni/api/staking/bonding/relayChain.js +62 -7
- package/cjs/koni/api/staking/bonding/utils.js +29 -3
- package/cjs/koni/api/xcm/index.js +0 -111
- package/cjs/koni/api/xcm/polkadotXcm.js +1 -1
- package/cjs/koni/api/xcm/xTokens.js +2 -4
- package/cjs/koni/background/cron.js +61 -62
- package/cjs/koni/background/handlers/Extension.js +143 -149
- package/cjs/koni/background/handlers/State.js +129 -91
- package/cjs/koni/background/handlers/Tabs.js +40 -38
- package/cjs/koni/background/subscription.js +48 -43
- package/cjs/packageInfo.js +1 -1
- package/cjs/page/index.js +5 -0
- package/cjs/services/chain-service/constants.js +1 -11
- package/cjs/services/chain-service/index.js +39 -5
- package/cjs/services/chain-service/utils.js +4 -0
- package/cjs/services/event-service/index.js +71 -0
- package/cjs/services/event-service/types.js +1 -0
- package/cjs/services/history-service/index.js +36 -17
- package/cjs/services/history-service/subsquid-multi-chain-history.js +7 -2
- package/cjs/services/keyring-service/index.js +101 -0
- package/cjs/services/migration-service/index.js +13 -10
- package/cjs/{koni/migration/scripts/RemoveWrongCrowdloan.js → services/migration-service/scripts/AutoEnableChainsTokens.js} +11 -6
- package/cjs/services/migration-service/scripts/MigrateNetworkSettings.js +39 -19
- package/cjs/services/migration-service/scripts/MigrateSettings.js +31 -0
- package/cjs/services/migration-service/scripts/MigrateTransactionHistory.js +72 -0
- package/cjs/services/migration-service/scripts/index.js +13 -4
- package/cjs/services/price-service/coingecko.js +16 -3
- package/cjs/services/price-service/index.js +15 -17
- package/cjs/services/request-service/constants.js +8 -2
- package/cjs/services/request-service/handler/AuthRequestHandler.js +39 -12
- package/cjs/services/request-service/index.js +3 -0
- package/cjs/services/storage-service/DatabaseService.js +1 -1
- package/cjs/services/storage-service/databases/index.js +1 -1
- package/cjs/services/storage-service/db-stores/BaseStoreWithAddressAndChain.js +2 -8
- package/cjs/services/storage-service/db-stores/Nft.js +4 -1
- package/cjs/services/storage-service/db-stores/Transaction.js +10 -7
- package/cjs/services/subscan-service/index.js +107 -0
- package/cjs/services/subscan-service/subscan-chain-map.js +74 -0
- package/cjs/services/subscan-service/types.js +1 -0
- package/cjs/services/transaction-service/index.js +45 -10
- package/cjs/services/transaction-service/utils.js +4 -6
- package/cjs/utils/address.js +9 -1
- package/cjs/utils/index.js +24 -13
- package/constants/index.d.ts +1 -0
- package/constants/index.js +1 -0
- package/koni/api/dotsama/balance.js +11 -0
- package/koni/api/staking/bonding/amplitude.d.ts +1 -1
- package/koni/api/staking/bonding/amplitude.js +8 -4
- package/koni/api/staking/bonding/astar.d.ts +2 -11
- package/koni/api/staking/bonding/astar.js +35 -229
- package/koni/api/staking/bonding/index.d.ts +1 -1
- package/koni/api/staking/bonding/index.js +4 -4
- package/koni/api/staking/bonding/paraChain.d.ts +1 -1
- package/koni/api/staking/bonding/paraChain.js +17 -11
- package/koni/api/staking/bonding/relayChain.d.ts +1 -1
- package/koni/api/staking/bonding/relayChain.js +63 -8
- package/koni/api/staking/bonding/utils.d.ts +13 -1
- package/koni/api/staking/bonding/utils.js +27 -3
- package/koni/api/xcm/index.js +1 -112
- package/koni/api/xcm/polkadotXcm.js +1 -1
- package/koni/api/xcm/xTokens.js +3 -5
- package/koni/background/cron.js +60 -60
- package/koni/background/handlers/Extension.js +71 -76
- package/koni/background/handlers/State.d.ts +16 -14
- package/koni/background/handlers/State.js +126 -91
- package/koni/background/handlers/Tabs.js +22 -19
- package/koni/background/subscription.d.ts +1 -1
- package/koni/background/subscription.js +49 -44
- package/package.json +56 -16
- package/packageInfo.js +1 -1
- package/page/index.d.ts +2 -0
- package/page/index.js +4 -0
- package/services/chain-service/constants.d.ts +0 -1
- package/services/chain-service/constants.js +0 -9
- package/services/chain-service/index.d.ts +6 -4
- package/services/chain-service/index.js +39 -7
- package/services/chain-service/utils.d.ts +1 -0
- package/services/chain-service/utils.js +3 -0
- package/services/event-service/index.d.ts +22 -0
- package/services/event-service/index.js +59 -0
- package/services/event-service/types.d.ts +32 -0
- package/services/event-service/types.js +1 -0
- package/services/history-service/index.d.ts +5 -3
- package/services/history-service/index.js +36 -17
- package/services/history-service/subsquid-multi-chain-history.js +7 -2
- package/services/keyring-service/index.d.ts +19 -0
- package/services/keyring-service/index.js +93 -0
- package/services/migration-service/index.js +11 -9
- package/services/migration-service/scripts/AutoEnableChainsTokens.d.ts +4 -0
- package/services/migration-service/scripts/AutoEnableChainsTokens.js +13 -0
- package/services/migration-service/scripts/MigrateNetworkSettings.js +37 -18
- package/services/migration-service/scripts/MigrateSettings.d.ts +4 -0
- package/services/migration-service/scripts/MigrateSettings.js +23 -0
- package/services/migration-service/scripts/MigrateTransactionHistory.d.ts +4 -0
- package/services/migration-service/scripts/MigrateTransactionHistory.js +64 -0
- package/services/migration-service/scripts/index.d.ts +1 -0
- package/services/migration-service/scripts/index.js +10 -3
- package/services/price-service/coingecko.js +16 -3
- package/services/price-service/index.d.ts +5 -3
- package/services/price-service/index.js +15 -17
- package/services/request-service/constants.d.ts +1 -0
- package/services/request-service/constants.js +6 -1
- package/services/request-service/handler/AuthRequestHandler.d.ts +8 -1
- package/services/request-service/handler/AuthRequestHandler.js +40 -13
- package/services/request-service/index.d.ts +7 -1
- package/services/request-service/index.js +3 -0
- package/services/storage-service/DatabaseService.js +1 -1
- package/services/storage-service/databases/index.js +1 -1
- package/services/storage-service/db-stores/BaseStoreWithAddressAndChain.d.ts +1 -1
- package/services/storage-service/db-stores/BaseStoreWithAddressAndChain.js +2 -8
- package/services/storage-service/db-stores/Nft.d.ts +2 -1
- package/services/storage-service/db-stores/Nft.js +4 -1
- package/services/storage-service/db-stores/Transaction.js +10 -7
- package/services/subscan-service/index.d.ts +20 -0
- package/services/subscan-service/index.js +99 -0
- package/services/subscan-service/subscan-chain-map.d.ts +5 -0
- package/services/subscan-service/subscan-chain-map.js +63 -0
- package/services/subscan-service/types.d.ts +27 -0
- package/services/subscan-service/types.js +1 -0
- package/services/transaction-service/event-parser/index.d.ts +2 -2
- package/services/transaction-service/index.d.ts +4 -2
- package/services/transaction-service/index.js +45 -10
- package/services/transaction-service/utils.js +5 -6
- package/utils/address.d.ts +1 -0
- package/utils/address.js +9 -2
- package/utils/index.d.ts +1 -0
- package/utils/index.js +21 -12
- package/cjs/background/errors/EvmRpcError.js +0 -21
- package/cjs/background/errors/SubWalletProviderError.js +0 -17
- package/cjs/constants/ethereum.js +0 -19
- package/cjs/errors/SubWalletProviderError.js +0 -17
- package/cjs/koni/api/xcm/astar.js +0 -160
- package/cjs/koni/api/xcm/moonbeamXcm.js +0 -80
- package/cjs/koni/api/xcm/statemintXcm.js +0 -197
- package/cjs/koni/api/xcm/substrateXcm.js +0 -213
- package/cjs/koni/migration/Base.js +0 -20
- package/cjs/koni/migration/index.js +0 -45
- package/cjs/koni/migration/scripts/ChangeRouteToHome.js +0 -22
- package/cjs/koni/migration/scripts/ClearOldStorage.js +0 -24
- package/cjs/koni/migration/scripts/RemoveWrongTransactionHistoriesFromStore.js +0 -36
- package/cjs/koni/migration/scripts/ResetTransactionHistoryEventIdx.js +0 -21
- package/cjs/koni/migration/scripts/index.js +0 -22
- package/cjs/koni/page/index.js +0 -16
- package/cjs/services/asset-service/index.js +0 -91
- package/cjs/services/storage-service/db-stores/ExtraDelegationInfo.js +0 -17
- package/cjs/stores/Balance.js +0 -18
- package/cjs/stores/Crowdloan.js +0 -18
- package/cjs/stores/CustomEvmToken.js +0 -18
- package/cjs/stores/NetworkMap.js +0 -18
- package/cjs/stores/Nft.js +0 -18
- package/cjs/stores/NftCollection.js +0 -18
- package/cjs/stores/Price.js +0 -18
- package/cjs/stores/Staking.js +0 -18
- package/cjs/stores/StakingReward.js +0 -18
- package/cjs/utils/eth/parseTransactionData.js +0 -284
- package/koni/page/index.d.ts +0 -2
- package/koni/page/index.js +0 -9
- /package/cjs/{koni/page → page}/SubWalleEvmProvider.js +0 -0
- /package/{koni/page → page}/SubWalleEvmProvider.d.ts +0 -0
- /package/{koni/page → page}/SubWalleEvmProvider.js +0 -0
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
// Copyright 2019-2022 @subwallet/extension-koni authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
import { StakingType, UnstakingStatus } from '@subwallet/extension-base/background/KoniTypes';
|
|
5
|
-
import { getFreeBalance } from '@subwallet/extension-base/koni/api/dotsama/balance';
|
|
4
|
+
import { StakingStatus, StakingType, UnstakingStatus } from '@subwallet/extension-base/background/KoniTypes';
|
|
6
5
|
import { _STAKING_ERA_LENGTH_MAP } from '@subwallet/extension-base/services/chain-service/constants';
|
|
7
|
-
import {
|
|
8
|
-
import { isUrl, parseNumberToDisplay, parseRawNumber } from '@subwallet/extension-base/utils';
|
|
6
|
+
import { isUrl, parseRawNumber } from '@subwallet/extension-base/utils';
|
|
9
7
|
import fetch from 'cross-fetch';
|
|
10
8
|
import { BN, BN_ZERO } from '@polkadot/util';
|
|
11
9
|
import { isEthereumAddress } from '@polkadot/util-crypto';
|
|
@@ -81,6 +79,8 @@ export async function getAstarNominatorMetadata(chainInfo, address, substrateApi
|
|
|
81
79
|
bnTotalActiveStake = bnTotalActiveStake.add(bnCurrentStake);
|
|
82
80
|
const dappInfo = dAppInfoMap[dappAddress];
|
|
83
81
|
nominationList.push({
|
|
82
|
+
status: StakingStatus.NOT_EARNING,
|
|
83
|
+
// TODO
|
|
84
84
|
chain,
|
|
85
85
|
validatorAddress: dappAddress,
|
|
86
86
|
activeStake: currentStake,
|
|
@@ -115,9 +115,11 @@ export async function getAstarNominatorMetadata(chainInfo, address, substrateApi
|
|
|
115
115
|
address: address,
|
|
116
116
|
activeStake: bnTotalActiveStake.toString(),
|
|
117
117
|
nominations: nominationList,
|
|
118
|
-
unstakings: unstakingList
|
|
118
|
+
unstakings: unstakingList,
|
|
119
|
+
status: StakingStatus.NOT_EARNING // TODO
|
|
119
120
|
};
|
|
120
121
|
}
|
|
122
|
+
|
|
121
123
|
export async function getAstarDappsInfo(networkKey, substrateApi) {
|
|
122
124
|
const chainApi = await substrateApi.isReady;
|
|
123
125
|
const rawMaxStakerPerContract = chainApi.api.consts.dappsStaking.maxNumberOfStakersPerContract.toHuman();
|
|
@@ -137,21 +139,26 @@ export async function getAstarDappsInfo(networkKey, substrateApi) {
|
|
|
137
139
|
const dappName = dapp.name;
|
|
138
140
|
const dappAddress = dapp.address;
|
|
139
141
|
const dappIcon = isUrl(dapp.iconUrl) ? dapp.iconUrl : undefined;
|
|
140
|
-
const
|
|
142
|
+
const contractParam = isEthereumAddress(dappAddress) ? {
|
|
141
143
|
Evm: dappAddress
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
144
|
+
} : {
|
|
145
|
+
Wasm: dappAddress
|
|
146
|
+
};
|
|
147
|
+
const _contractInfo = await chainApi.api.query.dappsStaking.contractEraStake(contractParam, era);
|
|
148
|
+
const contractInfo = _contractInfo.toPrimitive();
|
|
149
|
+
let totalStake = '0';
|
|
145
150
|
let stakerCount = 0;
|
|
146
151
|
if (contractInfo !== null) {
|
|
147
|
-
|
|
148
|
-
|
|
152
|
+
var _contractInfo$total;
|
|
153
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
|
|
154
|
+
totalStake = contractInfo === null || contractInfo === void 0 ? void 0 : (_contractInfo$total = contractInfo.total) === null || _contractInfo$total === void 0 ? void 0 : _contractInfo$total.toString();
|
|
155
|
+
stakerCount = contractInfo.numberOfStakers;
|
|
149
156
|
}
|
|
150
157
|
allDappsInfo.push({
|
|
151
158
|
commission: 0,
|
|
152
159
|
expectedReturn: 0,
|
|
153
160
|
address: dappAddress,
|
|
154
|
-
totalStake: totalStake
|
|
161
|
+
totalStake: totalStake,
|
|
155
162
|
ownStake: '0',
|
|
156
163
|
otherStake: totalStake.toString(),
|
|
157
164
|
nominatorCount: stakerCount,
|
|
@@ -166,172 +173,31 @@ export async function getAstarDappsInfo(networkKey, substrateApi) {
|
|
|
166
173
|
}));
|
|
167
174
|
return allDappsInfo;
|
|
168
175
|
}
|
|
169
|
-
export async function getAstarBondingTxInfo(chainInfo, substrateApi, stakerAddress, amount, dappInfo) {
|
|
170
|
-
const apiPromise = await substrateApi.isReady;
|
|
171
|
-
const {
|
|
172
|
-
decimals
|
|
173
|
-
} = _getChainNativeTokenBasicInfo(chainInfo);
|
|
174
|
-
const parsedAmount = amount * 10 ** decimals;
|
|
175
|
-
const binaryAmount = new BN(parsedAmount.toString());
|
|
176
|
-
const extrinsic = apiPromise.api.tx.dappsStaking.bondAndStake({
|
|
177
|
-
Evm: dappInfo.address
|
|
178
|
-
}, binaryAmount);
|
|
179
|
-
return extrinsic.paymentInfo(stakerAddress);
|
|
180
|
-
}
|
|
181
|
-
export async function handleAstarBondingTxInfo(chainInfo, amount, networkKey, stakerAddress, dappInfo, substrateApiMap, evmApiMap) {
|
|
182
|
-
const {
|
|
183
|
-
decimals,
|
|
184
|
-
symbol
|
|
185
|
-
} = _getChainNativeTokenBasicInfo(chainInfo);
|
|
186
|
-
try {
|
|
187
|
-
const [txInfo, balance] = await Promise.all([getAstarBondingTxInfo(chainInfo, substrateApiMap[networkKey], stakerAddress, amount, dappInfo), getFreeBalance(networkKey, stakerAddress, substrateApiMap, evmApiMap)]);
|
|
188
|
-
const feeString = parseNumberToDisplay(txInfo.partialFee, decimals) + ` ${symbol}`;
|
|
189
|
-
const rawFee = parseRawNumber(txInfo.partialFee.toString());
|
|
190
|
-
const binaryBalance = new BN(balance);
|
|
191
|
-
const sumAmount = txInfo.partialFee.addn(amount);
|
|
192
|
-
const balanceError = sumAmount.gt(binaryBalance);
|
|
193
|
-
return {
|
|
194
|
-
rawFee,
|
|
195
|
-
fee: feeString,
|
|
196
|
-
balanceError
|
|
197
|
-
};
|
|
198
|
-
} catch (e) {
|
|
199
|
-
return {
|
|
200
|
-
fee: `0.0000 ${symbol}`,
|
|
201
|
-
balanceError: false
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
176
|
export async function getAstarBondingExtrinsic(substrateApi, amount, dappInfo) {
|
|
206
177
|
const chainApi = await substrateApi.isReady;
|
|
207
178
|
const binaryAmount = new BN(amount);
|
|
208
|
-
|
|
179
|
+
const dappParam = isEthereumAddress(dappInfo.address) ? {
|
|
209
180
|
Evm: dappInfo.address
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const {
|
|
215
|
-
decimals
|
|
216
|
-
} = _getChainNativeTokenBasicInfo(chainInfo);
|
|
217
|
-
const parsedAmount = amount * 10 ** decimals;
|
|
218
|
-
const binaryAmount = new BN(parsedAmount.toString());
|
|
219
|
-
const extrinsic = apiPromise.api.tx.dappsStaking.unbondAndUnstake({
|
|
220
|
-
Evm: dappAddress
|
|
221
|
-
}, binaryAmount);
|
|
222
|
-
return extrinsic.paymentInfo(stakerAddress);
|
|
223
|
-
}
|
|
224
|
-
export async function handleAstarUnbondingTxInfo(chainInfo, amount, networkKey, stakerAddress, dappAddress, substrateApiMap, evmApiMap) {
|
|
225
|
-
const {
|
|
226
|
-
decimals,
|
|
227
|
-
symbol
|
|
228
|
-
} = _getChainNativeTokenBasicInfo(chainInfo);
|
|
229
|
-
try {
|
|
230
|
-
const [txInfo, balance] = await Promise.all([getAstarUnbondingTxInfo(chainInfo, substrateApiMap[networkKey], stakerAddress, amount, dappAddress), getFreeBalance(networkKey, stakerAddress, substrateApiMap, evmApiMap)]);
|
|
231
|
-
const feeString = parseNumberToDisplay(txInfo.partialFee, decimals) + ` ${symbol}`;
|
|
232
|
-
const rawFee = parseRawNumber(txInfo.partialFee.toString());
|
|
233
|
-
const binaryBalance = new BN(balance);
|
|
234
|
-
const balanceError = txInfo.partialFee.gt(binaryBalance);
|
|
235
|
-
return {
|
|
236
|
-
rawFee,
|
|
237
|
-
fee: feeString,
|
|
238
|
-
balanceError
|
|
239
|
-
};
|
|
240
|
-
} catch (e) {
|
|
241
|
-
return {
|
|
242
|
-
fee: `0.0000 ${symbol}`,
|
|
243
|
-
balanceError: false
|
|
244
|
-
};
|
|
245
|
-
}
|
|
181
|
+
} : {
|
|
182
|
+
Wasm: dappInfo.address
|
|
183
|
+
};
|
|
184
|
+
return chainApi.api.tx.dappsStaking.bondAndStake(dappParam, binaryAmount);
|
|
246
185
|
}
|
|
247
186
|
export async function getAstarUnbondingExtrinsic(substrateApi, amount, dappAddress) {
|
|
248
187
|
const apiPromise = await substrateApi.isReady;
|
|
249
188
|
const binaryAmount = new BN(amount);
|
|
250
|
-
|
|
189
|
+
const dappParam = isEthereumAddress(dappAddress) ? {
|
|
251
190
|
Evm: dappAddress
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
async function getAstarUnlockingInfo(substrateApi, address, networkKey) {
|
|
255
|
-
const chainApi = await substrateApi.isReady;
|
|
256
|
-
const [_stakingInfo, _era] = await Promise.all([chainApi.api.query.dappsStaking.ledger(address), chainApi.api.query.dappsStaking.currentEra()]);
|
|
257
|
-
const currentEra = parseRawNumber(_era.toHuman());
|
|
258
|
-
const stakingInfo = _stakingInfo.toHuman();
|
|
259
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
260
|
-
const unlockingChunks = stakingInfo.unbondingInfo.unlockingChunks;
|
|
261
|
-
let nextWithdrawalEra = -1;
|
|
262
|
-
let nextWithdrawalAmount = 0;
|
|
263
|
-
let redeemable = 0;
|
|
264
|
-
for (const chunk of unlockingChunks) {
|
|
265
|
-
const unlockEra = parseRawNumber(chunk.unlockEra);
|
|
266
|
-
const amount = parseRawNumber(chunk.amount);
|
|
267
|
-
|
|
268
|
-
// Find next withdrawal
|
|
269
|
-
if (nextWithdrawalEra === -1) {
|
|
270
|
-
nextWithdrawalEra = unlockEra;
|
|
271
|
-
nextWithdrawalAmount = amount;
|
|
272
|
-
} else if (unlockEra <= nextWithdrawalEra) {
|
|
273
|
-
nextWithdrawalEra = unlockEra;
|
|
274
|
-
nextWithdrawalAmount += amount;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
// Find redeemable
|
|
278
|
-
if (unlockEra - currentEra <= 0) {
|
|
279
|
-
redeemable += amount;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
const nextWithdrawal = (nextWithdrawalEra - currentEra) * _STAKING_ERA_LENGTH_MAP[networkKey];
|
|
283
|
-
return {
|
|
284
|
-
nextWithdrawal,
|
|
285
|
-
nextWithdrawalAmount,
|
|
286
|
-
redeemable
|
|
287
|
-
};
|
|
288
|
-
}
|
|
289
|
-
export async function handleAstarUnlockingInfo(substrateApi, chainInfo, networkKey, address, type) {
|
|
290
|
-
const {
|
|
291
|
-
nextWithdrawal,
|
|
292
|
-
nextWithdrawalAmount,
|
|
293
|
-
redeemable
|
|
294
|
-
} = await getAstarUnlockingInfo(substrateApi, address, networkKey);
|
|
295
|
-
const {
|
|
296
|
-
decimals
|
|
297
|
-
} = _getChainNativeTokenBasicInfo(chainInfo);
|
|
298
|
-
const parsedRedeemable = redeemable / 10 ** decimals;
|
|
299
|
-
const parsedNextWithdrawalAmount = nextWithdrawalAmount / 10 ** decimals;
|
|
300
|
-
return {
|
|
301
|
-
address,
|
|
302
|
-
type,
|
|
303
|
-
chain: networkKey,
|
|
304
|
-
nextWithdrawal: nextWithdrawal,
|
|
305
|
-
redeemable: parsedRedeemable,
|
|
306
|
-
nextWithdrawalAmount: parsedNextWithdrawalAmount
|
|
307
|
-
};
|
|
308
|
-
}
|
|
309
|
-
export async function getAstarWithdrawalTxInfo(substrateApi, address) {
|
|
310
|
-
const apiPromise = await substrateApi.isReady;
|
|
311
|
-
const extrinsic = apiPromise.api.tx.dappsStaking.withdrawUnbonded();
|
|
312
|
-
return extrinsic.paymentInfo(address);
|
|
313
|
-
}
|
|
314
|
-
export async function handleAstarWithdrawalTxInfo(networkKey, chainInfo, substrateApiMap, evmApiMap, address) {
|
|
315
|
-
const [txInfo, balance] = await Promise.all([getAstarWithdrawalTxInfo(substrateApiMap[networkKey], address), getFreeBalance(networkKey, address, substrateApiMap, evmApiMap)]);
|
|
316
|
-
const {
|
|
317
|
-
decimals,
|
|
318
|
-
symbol
|
|
319
|
-
} = _getChainNativeTokenBasicInfo(chainInfo);
|
|
320
|
-
const feeString = parseNumberToDisplay(txInfo.partialFee, decimals) + ` ${symbol}`;
|
|
321
|
-
const rawFee = parseRawNumber(txInfo.partialFee.toString());
|
|
322
|
-
const binaryBalance = new BN(balance);
|
|
323
|
-
const balanceError = txInfo.partialFee.gt(binaryBalance);
|
|
324
|
-
return {
|
|
325
|
-
rawFee,
|
|
326
|
-
fee: feeString,
|
|
327
|
-
balanceError
|
|
191
|
+
} : {
|
|
192
|
+
Wasm: dappAddress
|
|
328
193
|
};
|
|
194
|
+
return apiPromise.api.tx.dappsStaking.unbondAndUnstake(dappParam, binaryAmount);
|
|
329
195
|
}
|
|
330
196
|
export async function getAstarWithdrawalExtrinsic(substrateApi) {
|
|
331
197
|
const chainApi = await substrateApi.isReady;
|
|
332
198
|
return chainApi.api.tx.dappsStaking.withdrawUnbonded();
|
|
333
199
|
}
|
|
334
|
-
export async function
|
|
200
|
+
export async function getAstarClaimRewardExtrinsic(substrateApi, address) {
|
|
335
201
|
const apiPromise = await substrateApi.isReady;
|
|
336
202
|
const [_stakedDapps, _currentEra] = await Promise.all([apiPromise.api.query.dappsStaking.generalStakerInfo.entries(address), apiPromise.api.query.dappsStaking.currentEra()]);
|
|
337
203
|
const currentEra = parseRawNumber(_currentEra.toHuman());
|
|
@@ -343,6 +209,7 @@ export async function getAstarClaimRewardTxInfo(substrateApi, address) {
|
|
|
343
209
|
const stakes = stakeData.stakes;
|
|
344
210
|
const dappAddress = stakedDapp.Evm.toLowerCase();
|
|
345
211
|
let numberOfUnclaimedEra = 0;
|
|
212
|
+
const maxTx = 50;
|
|
346
213
|
for (let i = 0; i < stakes.length; i++) {
|
|
347
214
|
var _stakes;
|
|
348
215
|
const {
|
|
@@ -360,74 +227,13 @@ export async function getAstarClaimRewardTxInfo(substrateApi, address) {
|
|
|
360
227
|
const eraToClaim = isLastEra ? currentEra - parsedEra : nextEra - parsedEra;
|
|
361
228
|
numberOfUnclaimedEra += eraToClaim;
|
|
362
229
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
transactions.push(tx);
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
console.log('no of tx: ', transactions.length);
|
|
371
|
-
const extrinsic = apiPromise.api.tx.utility.batch(transactions);
|
|
372
|
-
return extrinsic.paymentInfo(address);
|
|
373
|
-
}
|
|
374
|
-
export async function handleAstarClaimRewardTxInfo(address, networkKey, chainInfo, substrateApiMap, evmApiMap) {
|
|
375
|
-
const {
|
|
376
|
-
decimals,
|
|
377
|
-
symbol
|
|
378
|
-
} = _getChainNativeTokenBasicInfo(chainInfo);
|
|
379
|
-
try {
|
|
380
|
-
const [txInfo, balance] = await Promise.all([getAstarClaimRewardTxInfo(substrateApiMap[networkKey], address), getFreeBalance(networkKey, address, substrateApiMap, evmApiMap)]);
|
|
381
|
-
const feeString = parseNumberToDisplay(txInfo.partialFee, decimals) + ` ${symbol}`;
|
|
382
|
-
const rawFee = parseRawNumber(txInfo.partialFee.toString());
|
|
383
|
-
const binaryBalance = new BN(balance);
|
|
384
|
-
const balanceError = txInfo.partialFee.gt(binaryBalance);
|
|
385
|
-
return {
|
|
386
|
-
rawFee,
|
|
387
|
-
fee: feeString,
|
|
388
|
-
balanceError
|
|
389
|
-
};
|
|
390
|
-
} catch (e) {
|
|
391
|
-
return {
|
|
392
|
-
fee: `0.0000 ${symbol}`,
|
|
393
|
-
balanceError: false
|
|
230
|
+
const dappParam = isEthereumAddress(dappAddress) ? {
|
|
231
|
+
Evm: dappAddress
|
|
232
|
+
} : {
|
|
233
|
+
Wasm: dappAddress
|
|
394
234
|
};
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
export async function getAstarClaimRewardExtrinsic(substrateApi, address) {
|
|
398
|
-
const apiPromise = await substrateApi.isReady;
|
|
399
|
-
const [_stakedDapps, _currentEra] = await Promise.all([apiPromise.api.query.dappsStaking.generalStakerInfo.entries(address), apiPromise.api.query.dappsStaking.currentEra()]);
|
|
400
|
-
const currentEra = parseRawNumber(_currentEra.toHuman());
|
|
401
|
-
const transactions = [];
|
|
402
|
-
for (const item of _stakedDapps) {
|
|
403
|
-
const data = item[0].toHuman();
|
|
404
|
-
const stakedDapp = data[1];
|
|
405
|
-
const stakeData = item[1].toHuman();
|
|
406
|
-
const stakes = stakeData.stakes;
|
|
407
|
-
const dappAddress = stakedDapp.Evm.toLowerCase();
|
|
408
|
-
let numberOfUnclaimedEra = 0;
|
|
409
|
-
const maxTx = 50;
|
|
410
|
-
for (let i = 0; i < stakes.length; i++) {
|
|
411
|
-
var _stakes2;
|
|
412
|
-
const {
|
|
413
|
-
era,
|
|
414
|
-
staked
|
|
415
|
-
} = stakes[i];
|
|
416
|
-
const bnStaked = new BN(staked.replaceAll(',', ''));
|
|
417
|
-
const parsedEra = parseRawNumber(era);
|
|
418
|
-
if (bnStaked.eq(new BN(0))) {
|
|
419
|
-
continue;
|
|
420
|
-
}
|
|
421
|
-
const nextEraData = (_stakes2 = stakes[i + 1]) !== null && _stakes2 !== void 0 ? _stakes2 : null;
|
|
422
|
-
const nextEra = nextEraData && parseRawNumber(nextEraData.era);
|
|
423
|
-
const isLastEra = i === stakes.length - 1;
|
|
424
|
-
const eraToClaim = isLastEra ? currentEra - parsedEra : nextEra - parsedEra;
|
|
425
|
-
numberOfUnclaimedEra += eraToClaim;
|
|
426
|
-
}
|
|
427
235
|
for (let i = 0; i < Math.min(numberOfUnclaimedEra, maxTx); i++) {
|
|
428
|
-
const tx = apiPromise.api.tx.dappsStaking.claimStaker(
|
|
429
|
-
Evm: dappAddress
|
|
430
|
-
});
|
|
236
|
+
const tx = apiPromise.api.tx.dappsStaking.claimStaker(dappParam);
|
|
431
237
|
transactions.push(tx);
|
|
432
238
|
}
|
|
433
239
|
}
|
|
@@ -5,7 +5,7 @@ export declare function getChainStakingMetadata(chain: string, substrateApi: _Su
|
|
|
5
5
|
export declare function getNominatorMetadata(chainInfo: _ChainInfo, address: string, substrateApi: _SubstrateApi): Promise<NominatorMetadata | undefined>;
|
|
6
6
|
export declare function getValidatorsInfo(networkKey: string, substrateApi: _SubstrateApi, decimals: number, chainStakingMetadata: ChainStakingMetadata): Promise<ValidatorInfo[]>;
|
|
7
7
|
export declare function getNominationPoolsInfo(chain: string, substrateApi: _SubstrateApi): Promise<import("@subwallet/extension-base/background/KoniTypes").NominationPoolInfo[]>;
|
|
8
|
-
export declare function getBondingExtrinsic(chainInfo: _ChainInfo, amount: string,
|
|
8
|
+
export declare function getBondingExtrinsic(chainInfo: _ChainInfo, amount: string, selectedValidators: ValidatorInfo[], substrateApi: _SubstrateApi, address: string, nominatorMetadata?: NominatorMetadata): Promise<import("@polkadot/api-base/types").SubmittableExtrinsic<"promise", import("@polkadot/types/types").ISubmittableResult>>;
|
|
9
9
|
export declare function getUnbondingExtrinsic(nominatorMetadata: NominatorMetadata, amount: string, chain: string, substrateApi: _SubstrateApi, selectedValidator?: string): Promise<import("@polkadot/api-base/types").SubmittableExtrinsic<"promise", import("@polkadot/types/types").ISubmittableResult>>;
|
|
10
10
|
export declare function getWithdrawalExtrinsic(substrateApi: _SubstrateApi, chain: string, nominatorMetadata: NominatorMetadata, validatorAddress?: string): Promise<import("@polkadot/api-base/types").SubmittableExtrinsic<"promise", import("@polkadot/types/types").ISubmittableResult>>;
|
|
11
11
|
export declare function getClaimRewardExtrinsic(substrateApi: _SubstrateApi, chain: string, address: string, stakingType: StakingType, bondReward?: boolean): Promise<import("@polkadot/api-base/types").SubmittableExtrinsic<"promise", import("@polkadot/types/types").ISubmittableResult>>;
|
|
@@ -42,15 +42,15 @@ export async function getValidatorsInfo(networkKey, substrateApi, decimals, chai
|
|
|
42
42
|
export async function getNominationPoolsInfo(chain, substrateApi) {
|
|
43
43
|
return getRelayPoolsInfo(chain, substrateApi);
|
|
44
44
|
}
|
|
45
|
-
export async function getBondingExtrinsic(chainInfo, amount,
|
|
45
|
+
export async function getBondingExtrinsic(chainInfo, amount, selectedValidators, substrateApi, address, nominatorMetadata) {
|
|
46
46
|
if (_STAKING_CHAIN_GROUP.para.includes(chainInfo.slug)) {
|
|
47
|
-
return getParaBondingExtrinsic(
|
|
47
|
+
return getParaBondingExtrinsic(chainInfo, substrateApi, amount, selectedValidators[0], nominatorMetadata); // only select 1 validator at a time
|
|
48
48
|
} else if (_STAKING_CHAIN_GROUP.astar.includes(chainInfo.slug)) {
|
|
49
49
|
return getAstarBondingExtrinsic(substrateApi, amount, selectedValidators[0]);
|
|
50
50
|
} else if (_STAKING_CHAIN_GROUP.amplitude.includes(chainInfo.slug)) {
|
|
51
|
-
return getAmplitudeBondingExtrinsic(
|
|
51
|
+
return getAmplitudeBondingExtrinsic(substrateApi, amount, selectedValidators[0], nominatorMetadata);
|
|
52
52
|
}
|
|
53
|
-
return getRelayBondingExtrinsic(substrateApi, amount, selectedValidators,
|
|
53
|
+
return getRelayBondingExtrinsic(substrateApi, amount, selectedValidators, chainInfo, address, nominatorMetadata);
|
|
54
54
|
}
|
|
55
55
|
export async function getUnbondingExtrinsic(nominatorMetadata, amount, chain, substrateApi, selectedValidator) {
|
|
56
56
|
if (_STAKING_CHAIN_GROUP.para.includes(chain)) {
|
|
@@ -4,7 +4,7 @@ import { _SubstrateApi } from '@subwallet/extension-base/services/chain-service/
|
|
|
4
4
|
export declare function getParaChainStakingMetadata(chain: string, substrateApi: _SubstrateApi): Promise<ChainStakingMetadata>;
|
|
5
5
|
export declare function getParaChainNominatorMetadata(chainInfo: _ChainInfo, address: string, substrateApi: _SubstrateApi): Promise<NominatorMetadata | undefined>;
|
|
6
6
|
export declare function getParachainCollatorsInfo(chain: string, substrateApi: _SubstrateApi): Promise<ValidatorInfo[]>;
|
|
7
|
-
export declare function getParaBondingExtrinsic(
|
|
7
|
+
export declare function getParaBondingExtrinsic(chainInfo: _ChainInfo, substrateApi: _SubstrateApi, amount: string, selectedCollatorInfo: ValidatorInfo, nominatorMetadata?: NominatorMetadata): Promise<import("@polkadot/api-base/types").SubmittableExtrinsic<"promise", import("@polkadot/types/types").ISubmittableResult>>;
|
|
8
8
|
export declare function getParaUnbondingExtrinsic(substrateApi: _SubstrateApi, amount: string, nominatorMetadata: NominatorMetadata, selectedValidator: string): Promise<import("@polkadot/api-base/types").SubmittableExtrinsic<"promise", import("@polkadot/types/types").ISubmittableResult>>;
|
|
9
9
|
export declare function getParaWithdrawalExtrinsic(substrateApi: _SubstrateApi, address: string, collatorAddress: string): Promise<import("@polkadot/api-base/types").SubmittableExtrinsic<"promise", import("@polkadot/types/types").ISubmittableResult>>;
|
|
10
10
|
export declare function getTuringCompoundExtrinsic(substrateApi: _SubstrateApi, address: string, collatorAddress: string, accountMinimum: string, bondedAmount: string): Promise<import("@polkadot/api-base/types").SubmittableExtrinsic<"promise", import("@polkadot/types/types").ISubmittableResult>>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Copyright 2019-2022 @subwallet/extension-koni authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
import { StakingType, UnstakingStatus } from '@subwallet/extension-base/background/KoniTypes';
|
|
4
|
+
import { StakingStatus, StakingType, UnstakingStatus } from '@subwallet/extension-base/background/KoniTypes';
|
|
5
5
|
import { getBondedValidators, getParaCurrentInflation, isUnstakeAll, parseIdentity } from '@subwallet/extension-base/koni/api/staking/bonding/utils';
|
|
6
6
|
import { _STAKING_ERA_LENGTH_MAP } from '@subwallet/extension-base/services/chain-service/constants';
|
|
7
7
|
import { _isChainEvmCompatible } from '@subwallet/extension-base/services/chain-service/utils';
|
|
@@ -97,6 +97,7 @@ export async function getParaChainNominatorMetadata(chainInfo, address, substrat
|
|
|
97
97
|
bnTotalActiveStake = bnTotalActiveStake.add(bnActiveStake);
|
|
98
98
|
nominationList.push({
|
|
99
99
|
chain,
|
|
100
|
+
status: StakingStatus.NOT_EARNING,
|
|
100
101
|
validatorAddress: delegation.owner,
|
|
101
102
|
validatorIdentity: identity,
|
|
102
103
|
activeStake: bnActiveStake.toString(),
|
|
@@ -119,15 +120,16 @@ export async function getParaChainNominatorMetadata(chainInfo, address, substrat
|
|
|
119
120
|
}
|
|
120
121
|
export async function getParachainCollatorsInfo(chain, substrateApi) {
|
|
121
122
|
const apiProps = await substrateApi.isReady;
|
|
122
|
-
const
|
|
123
|
+
const allCollators = [];
|
|
123
124
|
const [_allCollators, _collatorCommission] = await Promise.all([apiProps.api.query.parachainStaking.candidateInfo.entries(), apiProps.api.query.parachainStaking.collatorCommission()]);
|
|
124
125
|
const maxDelegationPerCollator = apiProps.api.consts.parachainStaking.maxTopDelegationsPerCandidate.toString();
|
|
125
126
|
const rawCollatorCommission = _collatorCommission.toHuman();
|
|
126
127
|
const collatorCommission = parseFloat(rawCollatorCommission.split('%')[0]);
|
|
127
128
|
for (const collator of _allCollators) {
|
|
128
|
-
const
|
|
129
|
+
const _collatorAddress = collator[0].toHuman();
|
|
130
|
+
const collatorAddress = _collatorAddress[0];
|
|
129
131
|
const collatorInfo = collator[1].toPrimitive();
|
|
130
|
-
|
|
132
|
+
allCollators.push({
|
|
131
133
|
commission: 0,
|
|
132
134
|
expectedReturn: 0,
|
|
133
135
|
address: collatorAddress,
|
|
@@ -143,9 +145,9 @@ export async function getParachainCollatorsInfo(chain, substrateApi) {
|
|
|
143
145
|
});
|
|
144
146
|
}
|
|
145
147
|
const extraInfoMap = {};
|
|
146
|
-
await Promise.all(
|
|
148
|
+
await Promise.all(allCollators.map(async collator => {
|
|
147
149
|
var _apiProps$api$query, _apiProps$api$query$i;
|
|
148
|
-
const [_info, _identity] = await Promise.all([apiProps.api.query.parachainStaking.candidateInfo(
|
|
150
|
+
const [_info, _identity] = await Promise.all([apiProps.api.query.parachainStaking.candidateInfo(collator.address), (_apiProps$api$query = apiProps.api.query) === null || _apiProps$api$query === void 0 ? void 0 : (_apiProps$api$query$i = _apiProps$api$query.identity) === null || _apiProps$api$query$i === void 0 ? void 0 : _apiProps$api$query$i.identityOf(collator.address) // some chains might not have identity pallet
|
|
149
151
|
]);
|
|
150
152
|
const rawInfo = _info.toHuman();
|
|
151
153
|
const rawIdentity = _identity ? _identity.toHuman() : null;
|
|
@@ -161,7 +163,7 @@ export async function getParachainCollatorsInfo(chain, substrateApi) {
|
|
|
161
163
|
isReasonable = rawIdentity.judgements.length > 0;
|
|
162
164
|
identity = parseIdentity(rawIdentity);
|
|
163
165
|
}
|
|
164
|
-
extraInfoMap[
|
|
166
|
+
extraInfoMap[collator.address] = {
|
|
165
167
|
identity,
|
|
166
168
|
isVerified: isReasonable,
|
|
167
169
|
bond: bond.toString(),
|
|
@@ -170,7 +172,7 @@ export async function getParachainCollatorsInfo(chain, substrateApi) {
|
|
|
170
172
|
active
|
|
171
173
|
};
|
|
172
174
|
}));
|
|
173
|
-
for (const validator of
|
|
175
|
+
for (const validator of allCollators) {
|
|
174
176
|
validator.minBond = extraInfoMap[validator.address].minDelegation.toString();
|
|
175
177
|
validator.ownStake = extraInfoMap[validator.address].bond.toString();
|
|
176
178
|
validator.blocked = !extraInfoMap[validator.address].active;
|
|
@@ -181,16 +183,20 @@ export async function getParachainCollatorsInfo(chain, substrateApi) {
|
|
|
181
183
|
validator.nominatorCount = extraInfoMap[validator.address].delegationCount;
|
|
182
184
|
validator.commission = collatorCommission;
|
|
183
185
|
}
|
|
184
|
-
return
|
|
186
|
+
return allCollators;
|
|
185
187
|
}
|
|
186
|
-
export async function getParaBondingExtrinsic(
|
|
188
|
+
export async function getParaBondingExtrinsic(chainInfo, substrateApi, amount, selectedCollatorInfo, nominatorMetadata) {
|
|
187
189
|
const apiPromise = await substrateApi.isReady;
|
|
188
190
|
const binaryAmount = new BN(amount);
|
|
191
|
+
if (!nominatorMetadata) {
|
|
192
|
+
return apiPromise.api.tx.parachainStaking.delegate(selectedCollatorInfo.address, binaryAmount, new BN(selectedCollatorInfo.nominatorCount), 0);
|
|
193
|
+
}
|
|
189
194
|
const {
|
|
190
195
|
bondedValidators,
|
|
191
196
|
nominationCount
|
|
192
197
|
} = getBondedValidators(nominatorMetadata.nominations);
|
|
193
|
-
|
|
198
|
+
const parsedSelectedCollatorAddress = reformatAddress(selectedCollatorInfo.address, 0);
|
|
199
|
+
if (!bondedValidators.includes(parsedSelectedCollatorAddress)) {
|
|
194
200
|
return apiPromise.api.tx.parachainStaking.delegate(selectedCollatorInfo.address, binaryAmount, new BN(selectedCollatorInfo.nominatorCount), nominationCount);
|
|
195
201
|
} else {
|
|
196
202
|
return apiPromise.api.tx.parachainStaking.delegatorBondMore(selectedCollatorInfo.address, binaryAmount);
|
|
@@ -22,7 +22,7 @@ export declare function getRelayChainNominatorMetadata(chainInfo: _ChainInfo, ad
|
|
|
22
22
|
export declare function getRelayChainPoolMemberMetadata(chainInfo: _ChainInfo, address: string, substrateApi: _SubstrateApi): Promise<NominatorMetadata | undefined>;
|
|
23
23
|
export declare function getRelayValidatorsInfo(chain: string, substrateApi: _SubstrateApi, decimals: number, chainStakingMetadata: ChainStakingMetadata): Promise<ValidatorInfo[]>;
|
|
24
24
|
export declare function getRelayPoolsInfo(chain: string, substrateApi: _SubstrateApi): Promise<NominationPoolInfo[]>;
|
|
25
|
-
export declare function getRelayBondingExtrinsic(substrateApi: _SubstrateApi, amount: string, targetValidators: ValidatorInfo[],
|
|
25
|
+
export declare function getRelayBondingExtrinsic(substrateApi: _SubstrateApi, amount: string, targetValidators: ValidatorInfo[], chainInfo: _ChainInfo, address: string, nominatorMetadata?: NominatorMetadata, bondDest?: string): Promise<import("@polkadot/api-base/types").SubmittableExtrinsic<"promise", import("@polkadot/types/types").ISubmittableResult>>;
|
|
26
26
|
export declare function getRelayUnbondingExtrinsic(substrateApi: _SubstrateApi, amount: string, nominatorMetadata: NominatorMetadata): Promise<import("@polkadot/api-base/types").SubmittableExtrinsic<"promise", import("@polkadot/types/types").ISubmittableResult>>;
|
|
27
27
|
export declare function getRelayWithdrawalExtrinsic(substrateApi: _SubstrateApi, address: string): Promise<import("@polkadot/api-base/types").SubmittableExtrinsic<"promise", import("@polkadot/types/types").ISubmittableResult>>;
|
|
28
28
|
export declare function getRelayCancelWithdrawalExtrinsic(substrateApi: _SubstrateApi, selectedUnstaking: UnstakingInfo): Promise<import("@polkadot/api-base/types").SubmittableExtrinsic<"promise", import("@polkadot/types/types").ISubmittableResult>>;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// Copyright 2019-2022 @subwallet/extension-koni authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
import { StakingType, UnstakingStatus } from '@subwallet/extension-base/background/KoniTypes';
|
|
5
|
-
import { calculateAlephZeroValidatorReturn, calculateChainStakedReturn, calculateInflation, calculateValidatorStakedReturn, getCommission, parseIdentity, transformPoolName } from '@subwallet/extension-base/koni/api/staking/bonding/utils';
|
|
4
|
+
import { StakingStatus, StakingType, UnstakingStatus } from '@subwallet/extension-base/background/KoniTypes';
|
|
5
|
+
import { calculateAlephZeroValidatorReturn, calculateChainStakedReturn, calculateInflation, calculateValidatorStakedReturn, getCommission, parseIdentity, parsePoolStashAddress, transformPoolName } from '@subwallet/extension-base/koni/api/staking/bonding/utils';
|
|
6
6
|
import { _STAKING_CHAIN_GROUP, _STAKING_ERA_LENGTH_MAP } from '@subwallet/extension-base/services/chain-service/constants';
|
|
7
|
+
import { _getChainSubstrateAddressPrefix } from '@subwallet/extension-base/services/chain-service/utils';
|
|
8
|
+
import { reformatAddress } from '@subwallet/extension-base/utils';
|
|
7
9
|
import { BN, BN_ZERO } from '@polkadot/util';
|
|
8
10
|
import { isEthereumAddress } from '@polkadot/util-crypto';
|
|
9
11
|
export async function getRelayChainStakingMetadata(chain, substrateApi) {
|
|
@@ -48,30 +50,51 @@ export async function getRelayChainNominatorMetadata(chainInfo, address, substra
|
|
|
48
50
|
const chain = chainInfo.slug;
|
|
49
51
|
const chainApi = await substrateApi.isReady;
|
|
50
52
|
const [_ledger, _nominations, _currentEra, _bonded] = await Promise.all([chainApi.api.query.staking.ledger(address), chainApi.api.query.staking.nominators(address), chainApi.api.query.staking.currentEra(), chainApi.api.query.staking.bonded(address)]);
|
|
51
|
-
const
|
|
52
|
-
const
|
|
53
|
+
const _maxNominatorRewardedPerValidator = chainApi.api.consts.staking.maxNominatorRewardedPerValidator.toString();
|
|
54
|
+
const maxNominatorRewardedPerValidator = parseInt(_maxNominatorRewardedPerValidator);
|
|
55
|
+
const ledger = _ledger.toPrimitive();
|
|
56
|
+
const nominations = _nominations.toPrimitive();
|
|
53
57
|
const currentEra = _currentEra.toString();
|
|
54
58
|
const bonded = _bonded.toHuman();
|
|
55
59
|
if (!ledger) {
|
|
56
60
|
return;
|
|
57
61
|
}
|
|
62
|
+
let stakingStatus = StakingStatus.NOT_EARNING;
|
|
58
63
|
const activeStake = ledger.active.toString();
|
|
59
64
|
const nominationList = [];
|
|
60
65
|
const unstakingList = [];
|
|
61
66
|
if (nominations) {
|
|
62
67
|
const validatorList = nominations.targets;
|
|
63
68
|
await Promise.all(validatorList.map(async validatorAddress => {
|
|
64
|
-
|
|
69
|
+
let nominationStatus = StakingStatus.NOT_EARNING;
|
|
70
|
+
const [_identityInfo, _eraStaker] = await Promise.all([chainApi.api.query.identity.identityOf(validatorAddress), chainApi.api.query.staking.erasStakers(currentEra, validatorAddress)]);
|
|
71
|
+
const eraStaker = _eraStaker.toPrimitive();
|
|
72
|
+
const identityInfo = _identityInfo.toHuman();
|
|
65
73
|
const identity = parseIdentity(identityInfo);
|
|
74
|
+
const topNominators = eraStaker.others.map(nominator => {
|
|
75
|
+
return nominator.who;
|
|
76
|
+
});
|
|
77
|
+
if (topNominators.slice(0, maxNominatorRewardedPerValidator).includes(reformatAddress(address, _getChainSubstrateAddressPrefix(chainInfo)))) {
|
|
78
|
+
// if address in top nominators
|
|
79
|
+
nominationStatus = StakingStatus.EARNING_REWARD;
|
|
80
|
+
}
|
|
66
81
|
nominationList.push({
|
|
67
82
|
chain,
|
|
68
83
|
validatorAddress,
|
|
84
|
+
status: nominationStatus,
|
|
69
85
|
validatorIdentity: identity,
|
|
70
86
|
activeStake: '0' // relaychain allocates stake accordingly
|
|
71
87
|
});
|
|
72
88
|
}));
|
|
73
89
|
}
|
|
74
90
|
|
|
91
|
+
for (const nomination of nominationList) {
|
|
92
|
+
if (nomination.status === StakingStatus.EARNING_REWARD) {
|
|
93
|
+
// only need 1 earning nomination to count
|
|
94
|
+
stakingStatus = StakingStatus.EARNING_REWARD;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
75
98
|
ledger.unlocking.forEach(unlockingChunk => {
|
|
76
99
|
const isClaimable = unlockingChunk.era - parseInt(currentEra) <= 0;
|
|
77
100
|
const remainingEra = unlockingChunk.era - (parseInt(currentEra) + 1);
|
|
@@ -86,6 +109,7 @@ export async function getRelayChainNominatorMetadata(chainInfo, address, substra
|
|
|
86
109
|
return {
|
|
87
110
|
chain,
|
|
88
111
|
type: StakingType.NOMINATED,
|
|
112
|
+
status: stakingStatus,
|
|
89
113
|
address: address,
|
|
90
114
|
activeStake,
|
|
91
115
|
nominations: nominationList,
|
|
@@ -96,17 +120,39 @@ export async function getRelayChainNominatorMetadata(chainInfo, address, substra
|
|
|
96
120
|
export async function getRelayChainPoolMemberMetadata(chainInfo, address, substrateApi) {
|
|
97
121
|
const chainApi = await substrateApi.isReady;
|
|
98
122
|
const [_poolMemberInfo, _currentEra] = await Promise.all([chainApi.api.query.nominationPools.poolMembers(address), chainApi.api.query.staking.currentEra()]);
|
|
123
|
+
const _maxNominatorRewardedPerValidator = chainApi.api.consts.staking.maxNominatorRewardedPerValidator.toString();
|
|
124
|
+
const maxNominatorRewardedPerValidator = parseInt(_maxNominatorRewardedPerValidator);
|
|
125
|
+
const poolsPalletId = chainApi.api.consts.nominationPools.palletId.toString();
|
|
99
126
|
const poolMemberInfo = _poolMemberInfo.toPrimitive();
|
|
100
127
|
const currentEra = _currentEra.toString();
|
|
101
128
|
if (!poolMemberInfo) {
|
|
102
129
|
return;
|
|
103
130
|
}
|
|
131
|
+
let stakingStatus = StakingStatus.NOT_EARNING;
|
|
104
132
|
const _poolMetadata = await chainApi.api.query.nominationPools.metadata(poolMemberInfo.poolId);
|
|
105
133
|
const poolMetadata = _poolMetadata.toPrimitive();
|
|
106
134
|
const poolName = transformPoolName(poolMetadata.isUtf8 ? poolMetadata.toUtf8() : poolMetadata.toString());
|
|
135
|
+
const poolStashAccount = parsePoolStashAddress(chainApi.api, 0, poolMemberInfo.poolId, poolsPalletId);
|
|
136
|
+
const _nominations = await chainApi.api.query.staking.nominators(poolStashAccount);
|
|
137
|
+
const nominations = _nominations.toJSON();
|
|
138
|
+
if (nominations) {
|
|
139
|
+
const validatorList = nominations.targets;
|
|
140
|
+
await Promise.all(validatorList.map(async validatorAddress => {
|
|
141
|
+
const _eraStaker = await chainApi.api.query.staking.erasStakers(currentEra, validatorAddress);
|
|
142
|
+
const eraStaker = _eraStaker.toPrimitive();
|
|
143
|
+
const topNominators = eraStaker.others.map(nominator => {
|
|
144
|
+
return nominator.who;
|
|
145
|
+
}).slice(0, maxNominatorRewardedPerValidator);
|
|
146
|
+
if (topNominators.includes(reformatAddress(poolStashAccount, _getChainSubstrateAddressPrefix(chainInfo)))) {
|
|
147
|
+
// if address in top nominators
|
|
148
|
+
stakingStatus = StakingStatus.EARNING_REWARD;
|
|
149
|
+
}
|
|
150
|
+
}));
|
|
151
|
+
}
|
|
107
152
|
const joinedPoolInfo = {
|
|
108
153
|
activeStake: poolMemberInfo.points.toString(),
|
|
109
154
|
chain: chainInfo.slug,
|
|
155
|
+
status: stakingStatus,
|
|
110
156
|
validatorIdentity: poolName,
|
|
111
157
|
validatorAddress: poolMemberInfo.poolId.toString(),
|
|
112
158
|
// use poolId
|
|
@@ -128,6 +174,7 @@ export async function getRelayChainPoolMemberMetadata(chainInfo, address, substr
|
|
|
128
174
|
chain: chainInfo.slug,
|
|
129
175
|
type: StakingType.POOLED,
|
|
130
176
|
address,
|
|
177
|
+
status: stakingStatus,
|
|
131
178
|
activeStake: poolMemberInfo.points.toString(),
|
|
132
179
|
nominations: [joinedPoolInfo],
|
|
133
180
|
// can only join 1 pool at a time
|
|
@@ -236,22 +283,30 @@ export async function getRelayPoolsInfo(chain, substrateApi) {
|
|
|
236
283
|
}));
|
|
237
284
|
return nominationPools;
|
|
238
285
|
}
|
|
239
|
-
export async function getRelayBondingExtrinsic(substrateApi, amount, targetValidators,
|
|
286
|
+
export async function getRelayBondingExtrinsic(substrateApi, amount, targetValidators, chainInfo, address, nominatorMetadata, bondDest = 'Staked') {
|
|
240
287
|
const chainApi = await substrateApi.isReady;
|
|
241
288
|
const binaryAmount = new BN(amount);
|
|
242
289
|
let bondTx;
|
|
243
290
|
let nominateTx;
|
|
291
|
+
const validatorParamList = targetValidators.map(validator => {
|
|
292
|
+
return validator.address;
|
|
293
|
+
});
|
|
294
|
+
if (!nominatorMetadata) {
|
|
295
|
+
bondTx = chainApi.api.tx.staking.bond(address, binaryAmount, bondDest);
|
|
296
|
+
nominateTx = chainApi.api.tx.staking.nominate(validatorParamList);
|
|
297
|
+
return chainApi.api.tx.utility.batchAll([bondTx, nominateTx]);
|
|
298
|
+
}
|
|
244
299
|
if (!nominatorMetadata.isBondedBefore) {
|
|
245
300
|
// first time
|
|
246
301
|
bondTx = chainApi.api.tx.staking.bond(nominatorMetadata.address, binaryAmount, bondDest);
|
|
247
|
-
nominateTx = chainApi.api.tx.staking.nominate(
|
|
302
|
+
nominateTx = chainApi.api.tx.staking.nominate(validatorParamList);
|
|
248
303
|
return chainApi.api.tx.utility.batchAll([bondTx, nominateTx]);
|
|
249
304
|
} else {
|
|
250
305
|
if (binaryAmount.gt(BN_ZERO)) {
|
|
251
306
|
bondTx = chainApi.api.tx.staking.bondExtra(binaryAmount);
|
|
252
307
|
}
|
|
253
308
|
if (nominatorMetadata.isBondedBefore && targetValidators.length > 0) {
|
|
254
|
-
nominateTx = chainApi.api.tx.staking.nominate(
|
|
309
|
+
nominateTx = chainApi.api.tx.staking.nominate(validatorParamList);
|
|
255
310
|
}
|
|
256
311
|
}
|
|
257
312
|
if (bondTx && !nominateTx) {
|