@subwallet/extension-base 1.0.7-0 → 1.0.7-2
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 +7 -8
- package/cjs/constants/index.js +1 -1
- package/cjs/koni/api/staking/bonding/amplitude.js +83 -0
- package/cjs/koni/api/staking/bonding/astar.js +109 -5
- package/cjs/koni/api/staking/bonding/index.js +35 -0
- package/cjs/koni/api/staking/bonding/paraChain.js +97 -0
- package/cjs/koni/api/staking/bonding/relayChain.js +203 -15
- package/cjs/koni/api/staking/bonding/utils.js +7 -0
- package/cjs/koni/api/staking/index.js +11 -11
- package/cjs/koni/api/staking/paraChain.js +200 -130
- package/cjs/koni/api/staking/relayChain.js +66 -68
- package/cjs/koni/api/staking/subsquidStaking.js +6 -11
- package/cjs/koni/background/cron.js +0 -25
- package/cjs/koni/background/handlers/Extension.js +98 -74
- package/cjs/koni/background/handlers/State.js +17 -19
- package/cjs/koni/background/subscription.js +57 -12
- package/cjs/packageInfo.js +1 -1
- package/cjs/services/chain-service/constants.js +3 -1
- package/cjs/services/chain-service/utils.js +6 -1
- package/cjs/services/storage-service/DatabaseService.js +7 -3
- package/cjs/services/storage-service/db-stores/ChainStakingMetadata.js +5 -0
- package/cjs/services/storage-service/db-stores/NominatorMetadata.js +4 -4
- package/cjs/services/transaction-service/index.js +6 -1
- package/constants/index.d.ts +1 -1
- package/constants/index.js +1 -1
- package/koni/api/staking/bonding/amplitude.d.ts +4 -0
- package/koni/api/staking/bonding/amplitude.js +81 -0
- package/koni/api/staking/bonding/astar.d.ts +4 -0
- package/koni/api/staking/bonding/astar.js +107 -5
- package/koni/api/staking/bonding/index.d.ts +1 -0
- package/koni/api/staking/bonding/index.js +38 -4
- package/koni/api/staking/bonding/paraChain.d.ts +4 -0
- package/koni/api/staking/bonding/paraChain.js +95 -0
- package/koni/api/staking/bonding/relayChain.d.ts +5 -0
- package/koni/api/staking/bonding/relayChain.js +198 -14
- package/koni/api/staking/bonding/utils.d.ts +5 -0
- package/koni/api/staking/bonding/utils.js +6 -0
- package/koni/api/staking/index.d.ts +4 -4
- package/koni/api/staking/index.js +11 -11
- package/koni/api/staking/paraChain.d.ts +5 -5
- package/koni/api/staking/paraChain.js +201 -131
- package/koni/api/staking/relayChain.d.ts +4 -4
- package/koni/api/staking/relayChain.js +66 -67
- package/koni/api/staking/subsquidStaking.d.ts +1 -1
- package/koni/api/staking/subsquidStaking.js +6 -11
- package/koni/background/cron.js +1 -26
- package/koni/background/handlers/Extension.js +29 -7
- package/koni/background/handlers/State.d.ts +2 -2
- package/koni/background/handlers/State.js +17 -19
- package/koni/background/subscription.d.ts +2 -1
- package/koni/background/subscription.js +58 -13
- package/package.json +5 -5
- package/packageInfo.js +1 -1
- package/services/chain-service/constants.d.ts +1 -0
- package/services/chain-service/constants.js +3 -2
- package/services/chain-service/types.d.ts +8 -0
- package/services/chain-service/utils.js +6 -1
- package/services/storage-service/DatabaseService.d.ts +2 -2
- package/services/storage-service/DatabaseService.js +7 -3
- package/services/storage-service/db-stores/ChainStakingMetadata.d.ts +1 -0
- package/services/storage-service/db-stores/ChainStakingMetadata.js +3 -0
- package/services/storage-service/db-stores/NominatorMetadata.d.ts +2 -2
- package/services/storage-service/db-stores/NominatorMetadata.js +4 -4
- package/services/transaction-service/index.js +6 -1
|
@@ -16,6 +16,9 @@ exports.getRelayPoolsInfo = getRelayPoolsInfo;
|
|
|
16
16
|
exports.getRelayUnbondingExtrinsic = getRelayUnbondingExtrinsic;
|
|
17
17
|
exports.getRelayValidatorsInfo = getRelayValidatorsInfo;
|
|
18
18
|
exports.getRelayWithdrawalExtrinsic = getRelayWithdrawalExtrinsic;
|
|
19
|
+
exports.subscribeRelayChainNominatorMetadata = subscribeRelayChainNominatorMetadata;
|
|
20
|
+
exports.subscribeRelayChainPoolMemberMetadata = subscribeRelayChainPoolMemberMetadata;
|
|
21
|
+
exports.subscribeRelayChainStakingMetadata = subscribeRelayChainStakingMetadata;
|
|
19
22
|
exports.validatePoolBondingCondition = validatePoolBondingCondition;
|
|
20
23
|
exports.validateRelayBondingCondition = validateRelayBondingCondition;
|
|
21
24
|
exports.validateRelayUnbondingCondition = validateRelayUnbondingCondition;
|
|
@@ -55,7 +58,7 @@ function validatePoolBondingCondition(chainInfo, amount, selectedPool, address,
|
|
|
55
58
|
if (nominatorMetadata) {
|
|
56
59
|
const bnCurrentActiveStake = new _util.BN(nominatorMetadata.activeStake);
|
|
57
60
|
bnTotalStake = bnTotalStake.add(bnCurrentActiveStake);
|
|
58
|
-
if (nominatorMetadata.unstakings.length > 0) {
|
|
61
|
+
if (nominatorMetadata.unstakings.length > 0 && bnCurrentActiveStake.isZero()) {
|
|
59
62
|
errors.push(new _TransactionError.TransactionError(_KoniTypes.StakingTxErrorType.EXIST_UNSTAKING_REQUEST));
|
|
60
63
|
}
|
|
61
64
|
}
|
|
@@ -87,6 +90,35 @@ function validateRelayBondingCondition(chainInfo, amount, selectedValidators, ad
|
|
|
87
90
|
}
|
|
88
91
|
return errors;
|
|
89
92
|
}
|
|
93
|
+
function subscribeRelayChainStakingMetadata(chainInfo, substrateApi, callback) {
|
|
94
|
+
return substrateApi.api.query.staking.currentEra(async _currentEra => {
|
|
95
|
+
var _substrateApi$api$que, _substrateApi$api$que2, _substrateApi$api$que3, _substrateApi$api$que4, _substrateApi$api$que5, _substrateApi$api$que6;
|
|
96
|
+
const currentEra = _currentEra.toString();
|
|
97
|
+
const maxNominations = substrateApi.api.consts.staking.maxNominations.toString();
|
|
98
|
+
const maxUnlockingChunks = substrateApi.api.consts.staking.maxUnlockingChunks.toString();
|
|
99
|
+
const unlockingEras = substrateApi.api.consts.staking.bondingDuration.toString();
|
|
100
|
+
const [_minNominatorBond, _minPoolJoin, _minimumActiveStake] = await Promise.all([substrateApi.api.query.staking.minNominatorBond(), (_substrateApi$api$que = substrateApi.api.query) === null || _substrateApi$api$que === void 0 ? void 0 : (_substrateApi$api$que2 = _substrateApi$api$que.nominationPools) === null || _substrateApi$api$que2 === void 0 ? void 0 : _substrateApi$api$que2.minJoinBond(), ((_substrateApi$api$que3 = substrateApi.api.query) === null || _substrateApi$api$que3 === void 0 ? void 0 : (_substrateApi$api$que4 = _substrateApi$api$que3.staking) === null || _substrateApi$api$que4 === void 0 ? void 0 : _substrateApi$api$que4.minimumActiveStake) && ((_substrateApi$api$que5 = substrateApi.api.query) === null || _substrateApi$api$que5 === void 0 ? void 0 : (_substrateApi$api$que6 = _substrateApi$api$que5.staking) === null || _substrateApi$api$que6 === void 0 ? void 0 : _substrateApi$api$que6.minimumActiveStake())]);
|
|
101
|
+
const minActiveStake = (_minimumActiveStake === null || _minimumActiveStake === void 0 ? void 0 : _minimumActiveStake.toString()) || '0';
|
|
102
|
+
const minNominatorBond = _minNominatorBond.toString();
|
|
103
|
+
const bnMinActiveStake = new _util.BN(minActiveStake);
|
|
104
|
+
const bnMinNominatorBond = new _util.BN(minNominatorBond);
|
|
105
|
+
const minStake = bnMinActiveStake.gt(bnMinNominatorBond) ? bnMinActiveStake : bnMinNominatorBond;
|
|
106
|
+
const minPoolJoin = (_minPoolJoin === null || _minPoolJoin === void 0 ? void 0 : _minPoolJoin.toString()) || undefined;
|
|
107
|
+
const unlockingPeriod = parseInt(unlockingEras) * (_constants._STAKING_ERA_LENGTH_MAP[chainInfo.slug] || _constants._STAKING_ERA_LENGTH_MAP.default); // in hours
|
|
108
|
+
|
|
109
|
+
callback(chainInfo.slug, {
|
|
110
|
+
chain: chainInfo.slug,
|
|
111
|
+
type: _KoniTypes.StakingType.NOMINATED,
|
|
112
|
+
era: parseInt(currentEra),
|
|
113
|
+
minStake: minStake.toString(),
|
|
114
|
+
maxValidatorPerNominator: parseInt(maxNominations),
|
|
115
|
+
maxWithdrawalRequestPerValidator: parseInt(maxUnlockingChunks),
|
|
116
|
+
allowCancelUnstaking: true,
|
|
117
|
+
unstakingPeriod: unlockingPeriod,
|
|
118
|
+
minJoinNominationPool: minPoolJoin
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
}
|
|
90
122
|
async function getRelayChainStakingMetadata(chainInfo, substrateApi) {
|
|
91
123
|
var _chainApi$api$query$a, _chainApi$api$query, _chainApi$api$query$s, _chainApi$api$query2, _chainApi$api$query2$, _chainApi$api$query3, _chainApi$api$query3$;
|
|
92
124
|
const chain = chainInfo.slug;
|
|
@@ -98,16 +130,17 @@ async function getRelayChainStakingMetadata(chainInfo, substrateApi) {
|
|
|
98
130
|
const unlockingEras = chainApi.api.consts.staking.bondingDuration.toString();
|
|
99
131
|
const [_totalEraStake, _totalIssuance, _auctionCounter, _minimumActiveStake, _minNominatorBond, _minPoolJoin, _eraStakers] = await Promise.all([chainApi.api.query.staking.erasTotalStake(parseInt(currentEra)), chainApi.api.query.balances.totalIssuance(), (_chainApi$api$query$a = chainApi.api.query.auctions) === null || _chainApi$api$query$a === void 0 ? void 0 : _chainApi$api$query$a.auctionCounter(), ((_chainApi$api$query = chainApi.api.query) === null || _chainApi$api$query === void 0 ? void 0 : (_chainApi$api$query$s = _chainApi$api$query.staking) === null || _chainApi$api$query$s === void 0 ? void 0 : _chainApi$api$query$s.minimumActiveStake) && ((_chainApi$api$query2 = chainApi.api.query) === null || _chainApi$api$query2 === void 0 ? void 0 : (_chainApi$api$query2$ = _chainApi$api$query2.staking) === null || _chainApi$api$query2$ === void 0 ? void 0 : _chainApi$api$query2$.minimumActiveStake()), chainApi.api.query.staking.minNominatorBond(), (_chainApi$api$query3 = chainApi.api.query) === null || _chainApi$api$query3 === void 0 ? void 0 : (_chainApi$api$query3$ = _chainApi$api$query3.nominationPools) === null || _chainApi$api$query3$ === void 0 ? void 0 : _chainApi$api$query3$.minJoinBond(), chainApi.api.query.staking.erasStakers.entries(parseInt(currentEra))]);
|
|
100
132
|
const eraStakers = _eraStakers;
|
|
133
|
+
let allCurrentNominators = [];
|
|
101
134
|
const nominatorList = [];
|
|
102
135
|
for (const item of eraStakers) {
|
|
103
136
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
|
|
104
137
|
const rawValidatorStat = item[1].toHuman();
|
|
105
138
|
const eraNominators = rawValidatorStat.others;
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
139
|
+
allCurrentNominators = allCurrentNominators.concat(eraNominators);
|
|
140
|
+
}
|
|
141
|
+
for (const nominator of allCurrentNominators) {
|
|
142
|
+
if (!nominatorList.includes(nominator.who)) {
|
|
143
|
+
nominatorList.push(nominator.who);
|
|
111
144
|
}
|
|
112
145
|
}
|
|
113
146
|
const minActiveStake = (_minimumActiveStake === null || _minimumActiveStake === void 0 ? void 0 : _minimumActiveStake.toString()) || '0';
|
|
@@ -129,7 +162,7 @@ async function getRelayChainStakingMetadata(chainInfo, substrateApi) {
|
|
|
129
162
|
chain,
|
|
130
163
|
type: _KoniTypes.StakingType.NOMINATED,
|
|
131
164
|
era: parseInt(currentEra),
|
|
132
|
-
expectedReturn,
|
|
165
|
+
expectedReturn: !_constants._STAKING_CHAIN_GROUP.ternoa.includes(chain) ? expectedReturn : undefined,
|
|
133
166
|
// in %, annually
|
|
134
167
|
inflation,
|
|
135
168
|
minStake: minStake.toString(),
|
|
@@ -141,14 +174,98 @@ async function getRelayChainStakingMetadata(chainInfo, substrateApi) {
|
|
|
141
174
|
nominatorCount: nominatorList.length
|
|
142
175
|
};
|
|
143
176
|
}
|
|
177
|
+
async function subscribeRelayChainNominatorMetadata(chainInfo, address, substrateApi, ledger) {
|
|
178
|
+
var _chainApi$api$query4, _chainApi$api$query4$, _chainApi$api$query5, _chainApi$api$query5$, _chainApi$api$query6, _chainApi$api$query6$, _chainApi$api$query7, _chainApi$api$query7$, _chainApi$api$query8, _chainApi$api$query8$, _chainApi$api$query9, _chainApi$api$query9$;
|
|
179
|
+
const chain = chainInfo.slug;
|
|
180
|
+
const chainApi = await substrateApi.isReady;
|
|
181
|
+
const [_nominations, _currentEra, _bonded, _minimumActiveStake, _minNominatorBond] = await Promise.all([(_chainApi$api$query4 = chainApi.api.query) === null || _chainApi$api$query4 === void 0 ? void 0 : (_chainApi$api$query4$ = _chainApi$api$query4.staking) === null || _chainApi$api$query4$ === void 0 ? void 0 : _chainApi$api$query4$.nominators(address), (_chainApi$api$query5 = chainApi.api.query) === null || _chainApi$api$query5 === void 0 ? void 0 : (_chainApi$api$query5$ = _chainApi$api$query5.staking) === null || _chainApi$api$query5$ === void 0 ? void 0 : _chainApi$api$query5$.currentEra(), (_chainApi$api$query6 = chainApi.api.query) === null || _chainApi$api$query6 === void 0 ? void 0 : (_chainApi$api$query6$ = _chainApi$api$query6.staking) === null || _chainApi$api$query6$ === void 0 ? void 0 : _chainApi$api$query6$.bonded(address), ((_chainApi$api$query7 = chainApi.api.query) === null || _chainApi$api$query7 === void 0 ? void 0 : (_chainApi$api$query7$ = _chainApi$api$query7.staking) === null || _chainApi$api$query7$ === void 0 ? void 0 : _chainApi$api$query7$.minimumActiveStake) && ((_chainApi$api$query8 = chainApi.api.query) === null || _chainApi$api$query8 === void 0 ? void 0 : (_chainApi$api$query8$ = _chainApi$api$query8.staking) === null || _chainApi$api$query8$ === void 0 ? void 0 : _chainApi$api$query8$.minimumActiveStake()), (_chainApi$api$query9 = chainApi.api.query) === null || _chainApi$api$query9 === void 0 ? void 0 : (_chainApi$api$query9$ = _chainApi$api$query9.staking) === null || _chainApi$api$query9$ === void 0 ? void 0 : _chainApi$api$query9$.minNominatorBond()]);
|
|
182
|
+
const minActiveStake = (_minimumActiveStake === null || _minimumActiveStake === void 0 ? void 0 : _minimumActiveStake.toString()) || '0';
|
|
183
|
+
const minNominatorBond = _minNominatorBond.toString();
|
|
184
|
+
const bnMinActiveStake = new _util.BN(minActiveStake);
|
|
185
|
+
const bnMinNominatorBond = new _util.BN(minNominatorBond);
|
|
186
|
+
const minStake = bnMinActiveStake.gt(bnMinNominatorBond) ? bnMinActiveStake : bnMinNominatorBond;
|
|
187
|
+
const _maxNominatorRewardedPerValidator = chainApi.api.consts.staking.maxNominatorRewardedPerValidator.toString();
|
|
188
|
+
const maxNominatorRewardedPerValidator = parseInt(_maxNominatorRewardedPerValidator);
|
|
189
|
+
const nominations = _nominations.toPrimitive();
|
|
190
|
+
const currentEra = _currentEra.toString();
|
|
191
|
+
const bonded = _bonded.toHuman();
|
|
192
|
+
const activeStake = ledger.active.toString();
|
|
193
|
+
const nominationList = [];
|
|
194
|
+
const unstakingList = [];
|
|
195
|
+
if (nominations) {
|
|
196
|
+
const validatorList = nominations.targets;
|
|
197
|
+
await Promise.all(validatorList.map(async validatorAddress => {
|
|
198
|
+
let nominationStatus = _KoniTypes.StakingStatus.NOT_EARNING;
|
|
199
|
+
const [_identityInfo, _eraStaker] = await Promise.all([chainApi.api.query.identity.identityOf(validatorAddress), chainApi.api.query.staking.erasStakers(currentEra, validatorAddress)]);
|
|
200
|
+
const eraStaker = _eraStaker.toPrimitive();
|
|
201
|
+
const identityInfo = _identityInfo.toHuman();
|
|
202
|
+
const identity = (0, _utils.parseIdentity)(identityInfo);
|
|
203
|
+
const topNominators = eraStaker.others.map(nominator => {
|
|
204
|
+
return nominator.who;
|
|
205
|
+
});
|
|
206
|
+
if (!topNominators.includes((0, _utils3.reformatAddress)(address, (0, _utils2._getChainSubstrateAddressPrefix)(chainInfo)))) {
|
|
207
|
+
// if nominator has target but not in nominator list
|
|
208
|
+
nominationStatus = _KoniTypes.StakingStatus.WAITING;
|
|
209
|
+
} else if (topNominators.slice(0, maxNominatorRewardedPerValidator).includes((0, _utils3.reformatAddress)(address, (0, _utils2._getChainSubstrateAddressPrefix)(chainInfo)))) {
|
|
210
|
+
// if address in top nominators
|
|
211
|
+
nominationStatus = _KoniTypes.StakingStatus.EARNING_REWARD;
|
|
212
|
+
}
|
|
213
|
+
nominationList.push({
|
|
214
|
+
chain,
|
|
215
|
+
validatorAddress,
|
|
216
|
+
status: nominationStatus,
|
|
217
|
+
validatorIdentity: identity,
|
|
218
|
+
activeStake: '0' // relaychain allocates stake accordingly
|
|
219
|
+
});
|
|
220
|
+
}));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
let stakingStatus = _KoniTypes.StakingStatus.NOT_EARNING;
|
|
224
|
+
const bnActiveStake = new _util.BN(activeStake);
|
|
225
|
+
let waitingNominationCount = 0;
|
|
226
|
+
if (bnActiveStake.gte(minStake)) {
|
|
227
|
+
for (const nomination of nominationList) {
|
|
228
|
+
if (nomination.status === _KoniTypes.StakingStatus.EARNING_REWARD) {
|
|
229
|
+
// only need 1 earning nomination to count
|
|
230
|
+
stakingStatus = _KoniTypes.StakingStatus.EARNING_REWARD;
|
|
231
|
+
} else if (nomination.status === _KoniTypes.StakingStatus.WAITING) {
|
|
232
|
+
waitingNominationCount += 1;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (waitingNominationCount === nominationList.length) {
|
|
236
|
+
stakingStatus = _KoniTypes.StakingStatus.WAITING;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
ledger.unlocking.forEach(unlockingChunk => {
|
|
240
|
+
const isClaimable = unlockingChunk.era - parseInt(currentEra) <= 0;
|
|
241
|
+
const remainingEra = unlockingChunk.era - (parseInt(currentEra) + 1);
|
|
242
|
+
const waitingTime = remainingEra * _constants._STAKING_ERA_LENGTH_MAP[chain];
|
|
243
|
+
unstakingList.push({
|
|
244
|
+
chain,
|
|
245
|
+
status: isClaimable ? _KoniTypes.UnstakingStatus.CLAIMABLE : _KoniTypes.UnstakingStatus.UNLOCKING,
|
|
246
|
+
claimable: unlockingChunk.value.toString(),
|
|
247
|
+
waitingTime: waitingTime > 0 ? waitingTime : 0
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
return {
|
|
251
|
+
chain,
|
|
252
|
+
type: _KoniTypes.StakingType.NOMINATED,
|
|
253
|
+
status: stakingStatus,
|
|
254
|
+
address: address,
|
|
255
|
+
activeStake,
|
|
256
|
+
nominations: nominationList,
|
|
257
|
+
unstakings: unstakingList,
|
|
258
|
+
isBondedBefore: bonded !== null
|
|
259
|
+
};
|
|
260
|
+
}
|
|
144
261
|
async function getRelayChainNominatorMetadata(chainInfo, address, substrateApi) {
|
|
145
|
-
var _chainApi$api$
|
|
262
|
+
var _chainApi$api$query10, _chainApi$api$query11, _chainApi$api$query12, _chainApi$api$query13, _chainApi$api$query14, _chainApi$api$query15, _chainApi$api$query16, _chainApi$api$query17, _chainApi$api$query18, _chainApi$api$query19, _chainApi$api$query20, _chainApi$api$query21, _chainApi$api$query22, _chainApi$api$query23;
|
|
146
263
|
if ((0, _utilCrypto.isEthereumAddress)(address)) {
|
|
147
264
|
return;
|
|
148
265
|
}
|
|
149
266
|
const chain = chainInfo.slug;
|
|
150
267
|
const chainApi = await substrateApi.isReady;
|
|
151
|
-
const [_ledger, _nominations, _currentEra, _bonded, _minimumActiveStake, _minNominatorBond] = await Promise.all([(_chainApi$api$
|
|
268
|
+
const [_ledger, _nominations, _currentEra, _bonded, _minimumActiveStake, _minNominatorBond] = await Promise.all([(_chainApi$api$query10 = chainApi.api.query) === null || _chainApi$api$query10 === void 0 ? void 0 : (_chainApi$api$query11 = _chainApi$api$query10.staking) === null || _chainApi$api$query11 === void 0 ? void 0 : _chainApi$api$query11.ledger(address), (_chainApi$api$query12 = chainApi.api.query) === null || _chainApi$api$query12 === void 0 ? void 0 : (_chainApi$api$query13 = _chainApi$api$query12.staking) === null || _chainApi$api$query13 === void 0 ? void 0 : _chainApi$api$query13.nominators(address), (_chainApi$api$query14 = chainApi.api.query) === null || _chainApi$api$query14 === void 0 ? void 0 : (_chainApi$api$query15 = _chainApi$api$query14.staking) === null || _chainApi$api$query15 === void 0 ? void 0 : _chainApi$api$query15.currentEra(), (_chainApi$api$query16 = chainApi.api.query) === null || _chainApi$api$query16 === void 0 ? void 0 : (_chainApi$api$query17 = _chainApi$api$query16.staking) === null || _chainApi$api$query17 === void 0 ? void 0 : _chainApi$api$query17.bonded(address), ((_chainApi$api$query18 = chainApi.api.query) === null || _chainApi$api$query18 === void 0 ? void 0 : (_chainApi$api$query19 = _chainApi$api$query18.staking) === null || _chainApi$api$query19 === void 0 ? void 0 : _chainApi$api$query19.minimumActiveStake) && ((_chainApi$api$query20 = chainApi.api.query) === null || _chainApi$api$query20 === void 0 ? void 0 : (_chainApi$api$query21 = _chainApi$api$query20.staking) === null || _chainApi$api$query21 === void 0 ? void 0 : _chainApi$api$query21.minimumActiveStake()), (_chainApi$api$query22 = chainApi.api.query) === null || _chainApi$api$query22 === void 0 ? void 0 : (_chainApi$api$query23 = _chainApi$api$query22.staking) === null || _chainApi$api$query23 === void 0 ? void 0 : _chainApi$api$query23.minNominatorBond()]);
|
|
152
269
|
const minActiveStake = (_minimumActiveStake === null || _minimumActiveStake === void 0 ? void 0 : _minimumActiveStake.toString()) || '0';
|
|
153
270
|
const minNominatorBond = _minNominatorBond.toString();
|
|
154
271
|
const bnMinActiveStake = new _util.BN(minActiveStake);
|
|
@@ -240,6 +357,68 @@ async function getRelayChainNominatorMetadata(chainInfo, address, substrateApi)
|
|
|
240
357
|
isBondedBefore: bonded !== null
|
|
241
358
|
};
|
|
242
359
|
}
|
|
360
|
+
async function subscribeRelayChainPoolMemberMetadata(chainInfo, address, substrateApi, poolMemberInfo) {
|
|
361
|
+
const _maxNominatorRewardedPerValidator = substrateApi.api.consts.staking.maxNominatorRewardedPerValidator.toString();
|
|
362
|
+
const maxNominatorRewardedPerValidator = parseInt(_maxNominatorRewardedPerValidator);
|
|
363
|
+
const poolsPalletId = substrateApi.api.consts.nominationPools.palletId.toString();
|
|
364
|
+
const poolStashAccount = (0, _utils.parsePoolStashAddress)(substrateApi.api, 0, poolMemberInfo.poolId, poolsPalletId);
|
|
365
|
+
const [_nominations, _poolMetadata, _currentEra] = await Promise.all([substrateApi.api.query.staking.nominators(poolStashAccount), substrateApi.api.query.nominationPools.metadata(poolMemberInfo.poolId), substrateApi.api.query.staking.currentEra()]);
|
|
366
|
+
const poolMetadata = _poolMetadata.toPrimitive();
|
|
367
|
+
const currentEra = _currentEra.toString();
|
|
368
|
+
const nominations = _nominations.toJSON();
|
|
369
|
+
let stakingStatus = _KoniTypes.StakingStatus.NOT_EARNING;
|
|
370
|
+
const poolName = (0, _utils.transformPoolName)(poolMetadata.isUtf8 ? poolMetadata.toUtf8() : poolMetadata.toString());
|
|
371
|
+
if (nominations) {
|
|
372
|
+
const validatorList = nominations.targets;
|
|
373
|
+
await Promise.all(validatorList.map(async validatorAddress => {
|
|
374
|
+
const _eraStaker = await substrateApi.api.query.staking.erasStakers(currentEra, validatorAddress);
|
|
375
|
+
const eraStaker = _eraStaker.toPrimitive();
|
|
376
|
+
const topNominators = eraStaker.others.map(nominator => {
|
|
377
|
+
return nominator.who;
|
|
378
|
+
}).slice(0, maxNominatorRewardedPerValidator);
|
|
379
|
+
if (topNominators.includes((0, _utils3.reformatAddress)(poolStashAccount, (0, _utils2._getChainSubstrateAddressPrefix)(chainInfo)))) {
|
|
380
|
+
// if address in top nominators
|
|
381
|
+
stakingStatus = _KoniTypes.StakingStatus.EARNING_REWARD;
|
|
382
|
+
}
|
|
383
|
+
}));
|
|
384
|
+
}
|
|
385
|
+
const joinedPoolInfo = {
|
|
386
|
+
activeStake: poolMemberInfo.points.toString(),
|
|
387
|
+
chain: chainInfo.slug,
|
|
388
|
+
status: stakingStatus,
|
|
389
|
+
validatorIdentity: poolName,
|
|
390
|
+
validatorAddress: poolMemberInfo.poolId.toString(),
|
|
391
|
+
// use poolId
|
|
392
|
+
hasUnstaking: poolMemberInfo.unbondingEras && Object.keys(poolMemberInfo.unbondingEras).length > 0
|
|
393
|
+
};
|
|
394
|
+
const unstakings = [];
|
|
395
|
+
Object.entries(poolMemberInfo.unbondingEras).forEach(_ref => {
|
|
396
|
+
let [unlockingEra, amount] = _ref;
|
|
397
|
+
const isClaimable = parseInt(unlockingEra) - parseInt(currentEra) <= 0;
|
|
398
|
+
const remainingEra = parseInt(unlockingEra) - (parseInt(currentEra) + 1);
|
|
399
|
+
const waitingTime = remainingEra * _constants._STAKING_ERA_LENGTH_MAP[chainInfo.slug];
|
|
400
|
+
unstakings.push({
|
|
401
|
+
chain: chainInfo.slug,
|
|
402
|
+
status: isClaimable ? _KoniTypes.UnstakingStatus.CLAIMABLE : _KoniTypes.UnstakingStatus.UNLOCKING,
|
|
403
|
+
claimable: amount.toString(),
|
|
404
|
+
waitingTime: waitingTime > 0 ? waitingTime : 0
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
const bnActiveStake = new _util.BN(poolMemberInfo.points.toString());
|
|
408
|
+
if (!bnActiveStake.gt(_util.BN_ZERO)) {
|
|
409
|
+
stakingStatus = _KoniTypes.StakingStatus.NOT_EARNING;
|
|
410
|
+
}
|
|
411
|
+
return {
|
|
412
|
+
chain: chainInfo.slug,
|
|
413
|
+
type: _KoniTypes.StakingType.POOLED,
|
|
414
|
+
address,
|
|
415
|
+
status: stakingStatus,
|
|
416
|
+
activeStake: poolMemberInfo.points.toString(),
|
|
417
|
+
nominations: [joinedPoolInfo],
|
|
418
|
+
// can only join 1 pool at a time
|
|
419
|
+
unstakings
|
|
420
|
+
};
|
|
421
|
+
}
|
|
243
422
|
async function getRelayChainPoolMemberMetadata(chainInfo, address, substrateApi) {
|
|
244
423
|
const chainApi = await substrateApi.isReady;
|
|
245
424
|
const [_poolMemberInfo, _currentEra] = await Promise.all([chainApi.api.query.nominationPools.poolMembers(address), chainApi.api.query.staking.currentEra()]);
|
|
@@ -291,8 +470,8 @@ async function getRelayChainPoolMemberMetadata(chainInfo, address, substrateApi)
|
|
|
291
470
|
hasUnstaking: poolMemberInfo.unbondingEras && Object.keys(poolMemberInfo.unbondingEras).length > 0
|
|
292
471
|
};
|
|
293
472
|
const unstakings = [];
|
|
294
|
-
Object.entries(poolMemberInfo.unbondingEras).forEach(
|
|
295
|
-
let [unlockingEra, amount] =
|
|
473
|
+
Object.entries(poolMemberInfo.unbondingEras).forEach(_ref2 => {
|
|
474
|
+
let [unlockingEra, amount] = _ref2;
|
|
296
475
|
const isClaimable = parseInt(unlockingEra) - parseInt(currentEra) <= 0;
|
|
297
476
|
const remainingEra = parseInt(unlockingEra) - (parseInt(currentEra) + 1);
|
|
298
477
|
const waitingTime = remainingEra * _constants._STAKING_ERA_LENGTH_MAP[chainInfo.slug];
|
|
@@ -324,7 +503,8 @@ async function getRelayValidatorsInfo(chain, substrateApi, decimals, chainStakin
|
|
|
324
503
|
const currentEra = _era.toString();
|
|
325
504
|
const allValidators = [];
|
|
326
505
|
const validatorInfoList = [];
|
|
327
|
-
const [_totalEraStake, _eraStakers, _minBond] = await Promise.all([chainApi.api.query.staking.erasTotalStake(parseInt(currentEra)), chainApi.api.query.staking.erasStakers.entries(parseInt(currentEra)), chainApi.api.query.staking.minNominatorBond()]);
|
|
506
|
+
const [_totalEraStake, _eraStakers, _minBond, _stakingRewards] = await Promise.all([chainApi.api.query.staking.erasTotalStake(parseInt(currentEra)), chainApi.api.query.staking.erasStakers.entries(parseInt(currentEra)), chainApi.api.query.staking.minNominatorBond(), chainApi.api.query.stakingRewards && chainApi.api.query.stakingRewards.data()]);
|
|
507
|
+
const stakingRewards = _stakingRewards === null || _stakingRewards === void 0 ? void 0 : _stakingRewards.toPrimitive();
|
|
328
508
|
const maxNominatorRewarded = chainApi.api.consts.staking.maxNominatorRewardedPerValidator.toString();
|
|
329
509
|
const bnTotalEraStake = new _util.BN(_totalEraStake.toString());
|
|
330
510
|
const eraStakers = _eraStakers;
|
|
@@ -367,9 +547,9 @@ async function getRelayValidatorsInfo(chain, substrateApi, decimals, chainStakin
|
|
|
367
547
|
}
|
|
368
548
|
const extraInfoMap = {};
|
|
369
549
|
await Promise.all(allValidators.map(async address => {
|
|
370
|
-
var _chainApi$api$
|
|
550
|
+
var _chainApi$api$query24, _chainApi$api$query25, _identityInfo$judgeme;
|
|
371
551
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
372
|
-
const [_commissionInfo, _identityInfo] = await Promise.all([chainApi.api.query.staking.validators(address), (_chainApi$api$
|
|
552
|
+
const [_commissionInfo, _identityInfo] = await Promise.all([chainApi.api.query.staking.validators(address), (_chainApi$api$query24 = chainApi.api.query) === null || _chainApi$api$query24 === void 0 ? void 0 : (_chainApi$api$query25 = _chainApi$api$query24.identity) === null || _chainApi$api$query25 === void 0 ? void 0 : _chainApi$api$query25.identityOf(address)]);
|
|
373
553
|
const commissionInfo = _commissionInfo.toHuman();
|
|
374
554
|
const identityInfo = _identityInfo ? _identityInfo.toHuman() : null;
|
|
375
555
|
let identity;
|
|
@@ -387,7 +567,15 @@ async function getRelayValidatorsInfo(chain, substrateApi, decimals, chainStakin
|
|
|
387
567
|
for (const validator of validatorInfoList) {
|
|
388
568
|
const commission = extraInfoMap[validator.address].commission;
|
|
389
569
|
const bnValidatorStake = totalStakeMap[validator.address].div(bnDecimals);
|
|
390
|
-
|
|
570
|
+
if (_constants._STAKING_CHAIN_GROUP.aleph.includes(chain)) {
|
|
571
|
+
validator.expectedReturn = (0, _utils.calculateAlephZeroValidatorReturn)(chainStakingMetadata.expectedReturn, (0, _utils.getCommission)(commission));
|
|
572
|
+
} else if (_constants._STAKING_CHAIN_GROUP.ternoa.includes(chain)) {
|
|
573
|
+
const rewardPerValidator = new _util.BN(stakingRewards.sessionExtraRewardPayout).divn(allValidators.length).div(bnDecimals);
|
|
574
|
+
const validatorStake = totalStakeMap[validator.address].div(bnDecimals).toNumber();
|
|
575
|
+
validator.expectedReturn = (0, _utils.calculateTernoaValidatorReturn)(rewardPerValidator.toNumber(), validatorStake, (0, _utils.getCommission)(commission));
|
|
576
|
+
} else {
|
|
577
|
+
validator.expectedReturn = (0, _utils.calculateValidatorStakedReturn)(chainStakingMetadata.expectedReturn, bnValidatorStake, bnAvgStake, (0, _utils.getCommission)(commission));
|
|
578
|
+
}
|
|
391
579
|
validator.commission = parseFloat(commission.split('%')[0]);
|
|
392
580
|
validator.blocked = extraInfoMap[validator.address].blocked;
|
|
393
581
|
validator.identity = extraInfoMap[validator.address].identity;
|
|
@@ -9,6 +9,7 @@ exports.calcInflationUniformEraPayout = calcInflationUniformEraPayout;
|
|
|
9
9
|
exports.calculateAlephZeroValidatorReturn = calculateAlephZeroValidatorReturn;
|
|
10
10
|
exports.calculateChainStakedReturn = calculateChainStakedReturn;
|
|
11
11
|
exports.calculateInflation = calculateInflation;
|
|
12
|
+
exports.calculateTernoaValidatorReturn = calculateTernoaValidatorReturn;
|
|
12
13
|
exports.calculateValidatorStakedReturn = calculateValidatorStakedReturn;
|
|
13
14
|
exports.getBondedValidators = getBondedValidators;
|
|
14
15
|
exports.getCommission = getCommission;
|
|
@@ -113,6 +114,12 @@ function calculateChainStakedReturn(inflation, totalEraStake, totalIssuance, net
|
|
|
113
114
|
function calculateAlephZeroValidatorReturn(chainStakedReturn, commission) {
|
|
114
115
|
return chainStakedReturn * (100 - commission) / 100;
|
|
115
116
|
}
|
|
117
|
+
function calculateTernoaValidatorReturn(rewardPerValidator, validatorStake, commission) {
|
|
118
|
+
const percentRewardForNominators = (100 - commission) / 100;
|
|
119
|
+
const rewardForNominators = rewardPerValidator * percentRewardForNominators;
|
|
120
|
+
const stakeRatio = rewardForNominators / validatorStake;
|
|
121
|
+
return stakeRatio * 365 * 100;
|
|
122
|
+
}
|
|
116
123
|
function calculateValidatorStakedReturn(chainStakedReturn, totalValidatorStake, avgStake, commission) {
|
|
117
124
|
const bnAdjusted = avgStake.mul(_util.BN_HUNDRED).div(totalValidatorStake);
|
|
118
125
|
const adjusted = bnAdjusted.toNumber() * chainStakedReturn;
|
|
@@ -15,7 +15,7 @@ var _utils2 = require("@subwallet/extension-base/utils");
|
|
|
15
15
|
// Copyright 2019-2022 @subwallet/extension-koni authors & contributors
|
|
16
16
|
// SPDX-License-Identifier: Apache-2.0
|
|
17
17
|
|
|
18
|
-
function stakingOnChainApi(addresses, substrateApiMap,
|
|
18
|
+
function stakingOnChainApi(addresses, substrateApiMap, chainInfoMap, stakingCallback, nominatorStateCallback) {
|
|
19
19
|
const filteredApiMap = [];
|
|
20
20
|
const [substrateAddresses, evmAddresses] = (0, _utils2.categoryAddresses)(addresses);
|
|
21
21
|
Object.entries(chainInfoMap).forEach(_ref => {
|
|
@@ -38,20 +38,20 @@ function stakingOnChainApi(addresses, substrateApiMap, callback, chainInfoMap) {
|
|
|
38
38
|
const parentApi = await apiPromise.isReady;
|
|
39
39
|
const useAddresses = (0, _utils._isChainEvmCompatible)(chainInfoMap[chain]) ? evmAddresses : substrateAddresses;
|
|
40
40
|
if (_constants._STAKING_CHAIN_GROUP.amplitude.includes(chain)) {
|
|
41
|
-
const unsub = await (0, _paraChain.getAmplitudeStakingOnChain)(parentApi, useAddresses, chainInfoMap, chain,
|
|
41
|
+
const unsub = await (0, _paraChain.getAmplitudeStakingOnChain)(parentApi, useAddresses, chainInfoMap, chain, stakingCallback, nominatorStateCallback);
|
|
42
42
|
unsubList.push(unsub);
|
|
43
43
|
} else if (_constants._STAKING_CHAIN_GROUP.astar.includes(chain)) {
|
|
44
|
-
const unsub = await (0, _paraChain.getAstarStakingOnChain)(parentApi, useAddresses, chainInfoMap, chain,
|
|
44
|
+
const unsub = await (0, _paraChain.getAstarStakingOnChain)(parentApi, useAddresses, chainInfoMap, chain, stakingCallback, nominatorStateCallback);
|
|
45
45
|
unsubList.push(unsub);
|
|
46
46
|
} else if (_constants._STAKING_CHAIN_GROUP.para.includes(chain)) {
|
|
47
|
-
const unsub = await (0, _paraChain.getParaStakingOnChain)(parentApi, useAddresses, chainInfoMap, chain,
|
|
47
|
+
const unsub = await (0, _paraChain.getParaStakingOnChain)(parentApi, useAddresses, chainInfoMap, chain, stakingCallback, nominatorStateCallback);
|
|
48
48
|
unsubList.push(unsub);
|
|
49
49
|
} else if (_constants._STAKING_CHAIN_GROUP.relay.includes(chain)) {
|
|
50
|
-
const unsub = await (0, _relayChain.getRelayStakingOnChain)(parentApi, useAddresses, chainInfoMap, chain,
|
|
50
|
+
const unsub = await (0, _relayChain.getRelayStakingOnChain)(parentApi, useAddresses, chainInfoMap, chain, stakingCallback, nominatorStateCallback);
|
|
51
51
|
unsubList.push(unsub);
|
|
52
52
|
}
|
|
53
53
|
if (_constants._STAKING_CHAIN_GROUP.nominationPool.includes(chain)) {
|
|
54
|
-
const unsub = await (0, _relayChain.getRelayPoolingOnChain)(parentApi, useAddresses, chainInfoMap, chain,
|
|
54
|
+
const unsub = await (0, _relayChain.getRelayPoolingOnChain)(parentApi, useAddresses, chainInfoMap, chain, stakingCallback, nominatorStateCallback);
|
|
55
55
|
unsubList.push(unsub);
|
|
56
56
|
}
|
|
57
57
|
});
|
|
@@ -61,11 +61,11 @@ function stakingOnChainApi(addresses, substrateApiMap, callback, chainInfoMap) {
|
|
|
61
61
|
});
|
|
62
62
|
};
|
|
63
63
|
}
|
|
64
|
-
async function getNominationStakingRewardData(addresses, chainInfoMap) {
|
|
64
|
+
async function getNominationStakingRewardData(addresses, chainInfoMap, callback) {
|
|
65
65
|
// might retrieve from other sources
|
|
66
|
-
|
|
66
|
+
await (0, _subsquidStaking.getAllSubsquidStaking)(addresses, chainInfoMap, callback);
|
|
67
67
|
}
|
|
68
|
-
async function getPoolingStakingRewardData(addresses, networkMap, dotSamaApiMap) {
|
|
68
|
+
async function getPoolingStakingRewardData(addresses, networkMap, dotSamaApiMap, callback) {
|
|
69
69
|
const activeNetworks = [];
|
|
70
70
|
Object.entries(networkMap).forEach(_ref3 => {
|
|
71
71
|
let [key, chainInfo] = _ref3;
|
|
@@ -74,7 +74,7 @@ async function getPoolingStakingRewardData(addresses, networkMap, dotSamaApiMap)
|
|
|
74
74
|
}
|
|
75
75
|
});
|
|
76
76
|
if (activeNetworks.length === 0) {
|
|
77
|
-
return
|
|
77
|
+
return;
|
|
78
78
|
}
|
|
79
|
-
|
|
79
|
+
await (0, _relayChain.getNominationPoolReward)(addresses, networkMap, dotSamaApiMap, callback);
|
|
80
80
|
}
|