-
Notifications
You must be signed in to change notification settings - Fork 11
/
dataGatherer.ts
207 lines (172 loc) · 8.4 KB
/
dataGatherer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*eslint @typescript-eslint/no-use-before-define: ["error", { "variables": false }]*/
import { DeriveStakingAccount, DeriveEraExposure } from '@polkadot/api-derive/staking/types';
import { MyDeriveStakingAccount, WriteCSVRequest, ChainData, Voter, VotersMap } from "./types";
import { Logger } from '@w3f/logger';
import { ApiPromise } from '@polkadot/api';
import { EraRewardPoints } from '@polkadot/types/interfaces';
import { delay, getDisplayName, getErrorMessage } from './utils';
import BN from 'bn.js';
export const gatherChainData = async (request: WriteCSVRequest, logger: Logger): Promise<ChainData> =>{
logger.info(`Data gathering triggered...`)
const data = await _handleConnectionRetries(_gatherData,request,logger)
logger.info(`Data have been gathered.`)
return data
}
/* eslint-disable @typescript-eslint/no-explicit-any */
const _handleConnectionRetries = async (f: { (request: WriteCSVRequest, logger: Logger): Promise<ChainData> }, request: WriteCSVRequest, logger: Logger): Promise<ChainData> => {
let attempts = 0
for(;;){
try {
const data = await f(request,logger)
return data
} catch (error) {
logger.error(`Could not process the Data gathering...`);
const errorMessage = getErrorMessage(error)
logger.error(errorMessage)
if(
!errorMessage.includes("Unable to decode using the supplied passphrase") && //there is no way to recover from this
++attempts < 5
){
logger.warn(`Retrying...`)
await delay(5000) //wait x seconds before retrying
}
else{
process.exit(-1);
}
}
}
}
/* eslint-enable @typescript-eslint/no-explicit-any */
const _gatherData = async (request: WriteCSVRequest, logger: Logger): Promise<ChainData> =>{
console.time('_gatherData');
logger.debug(`gathering some data from the chain...`)
const {api,apiChunkSize,eraIndex} = request
const eraPointsPromise = api.query.staking.erasRewardPoints(eraIndex);
const eraExposures = await api.derive.staking.eraExposure(eraIndex)
const totalIssuance = await api.query.balances.totalIssuance()
const validatorRewardsPreviousEra = (await api.query.staking.erasValidatorReward(eraIndex.sub(new BN(1)))).unwrap();
console.time('get nominators');
logger.debug(`nominators...`);
const nominatorStakingPromise = _getNominatorStaking(api,apiChunkSize,logger)
const [nominatorStaking,eraPoints] = [await nominatorStakingPromise, await eraPointsPromise]
console.timeEnd('get nominators')
console.time('build voters map');
logger.debug(`voters map...`);
const votersMap = _buildVotersMap(nominatorStaking)
console.timeEnd('build voters map')
console.time('get validators')
logger.debug(`validators...`);
const myValidatorStaking = await _getMyValidatorStaking(api,apiChunkSize,votersMap,eraPoints, eraExposures, logger)
console.timeEnd('get validators')
console.time('get waiting validators')
logger.debug(`waiting validators...`);
const myWaitingValidatorStaking = await _getMyWaitingValidatorStaking(api,apiChunkSize,votersMap,eraPoints, eraExposures, logger)
console.timeEnd('get waiting validators')
console.timeEnd('_gatherData')
return {
eraPoints,
totalIssuance,
validatorRewardsPreviousEra,
nominatorStaking,
myValidatorStaking,
myWaitingValidatorStaking
} as ChainData
}
const _getNominatorStaking = async (api: ApiPromise, apiChunkSize: number, logger: Logger): Promise<DeriveStakingAccount[]> =>{
logger.debug(`getting the nominator entries...`)
const nominators = await api.query.staking.nominators.entries();
logger.debug(`got ${nominators.length} entries !!`)
const nominatorAddresses = nominators.map(([address]) => ""+address.toHuman()[0]);
logger.debug(`the nominator addresses size is ${nominatorAddresses.length}`)
//A too big nominators set could make crush the API => Chunk splitting
const size = apiChunkSize
const nominatorAddressesChucked = []
for (let i = 0; i < nominatorAddresses.length; i += size) {
const chunk = nominatorAddresses.slice(i, i + size)
nominatorAddressesChucked.push(chunk)
}
const nominatorsStakings: DeriveStakingAccount[] = []
for (const chunk of nominatorAddressesChucked) {
logger.debug(`the handled chunk size is ${chunk.length}`)
nominatorsStakings.push(...await api.derive.staking.accounts(chunk))
}
return nominatorsStakings
}
const _getMyValidatorStaking = async (api: ApiPromise, apiChunkSize: number, voters: VotersMap, eraPoints: EraRewardPoints, eraExposures: DeriveEraExposure, logger: Logger): Promise<MyDeriveStakingAccount[]> =>{
const validatorsAddresses = await api.query.session.validators();
logger.debug(`the validator addresses size is ${validatorsAddresses.length}`)
//A too big nominators set could make crush the API => Chunk splitting
const size = apiChunkSize
const validatorsAddressesChucked = []
for (let i = 0; i < validatorsAddresses.length; i += size) {
const chunk = validatorsAddresses.slice(i, i + size)
validatorsAddressesChucked.push(chunk)
}
const validatorsStakings: DeriveStakingAccount[] = []
for (const chunk of validatorsAddressesChucked) {
logger.debug(`the handled chunk size is ${chunk.length}`)
validatorsStakings.push(...await api.derive.staking.accounts(chunk))
}
return await _buildMyValidatorStaking(api,validatorsStakings,voters,eraPoints,eraExposures)
}
const _getMyWaitingValidatorStaking = async (api: ApiPromise, apiChunkSize: number, voters: VotersMap, eraPoints: EraRewardPoints, eraExposures: DeriveEraExposure, logger: Logger): Promise<MyDeriveStakingAccount[]> => {
const validatorsAddresses = await _getWaitingValidatorsAccountId(api)
logger.debug(`the waiting validator addresses size is ${validatorsAddresses.length}`)
//A too big nominators set could make crush the API => Chunk splitting
const size = apiChunkSize
const validatorsAddressesChucked = []
for (let i = 0; i < validatorsAddresses.length; i += size) {
const chunk = validatorsAddresses.slice(i, i + size)
validatorsAddressesChucked.push(chunk)
}
const validatorsStakings: DeriveStakingAccount[] = []
for (const chunk of validatorsAddressesChucked) {
logger.debug(`the handled chunk size is ${chunk.length}`)
validatorsStakings.push(...await api.derive.staking.accounts(chunk))
}
return await _buildMyValidatorStaking(api,validatorsStakings,voters,eraPoints,eraExposures)
}
const _buildMyValidatorStaking = async (api: ApiPromise, validatorsStakings: DeriveStakingAccount[], votersMap: VotersMap, eraPoints: EraRewardPoints, eraExposures: DeriveEraExposure): Promise<MyDeriveStakingAccount[]> =>{
const myValidatorStaking = Promise.all ( validatorsStakings.map( async validatorStaking => {
const validatorAddress = validatorStaking.accountId
const infoPromise = api.derive.accounts.info(validatorAddress);
const validatorEraPoints = eraPoints.toJSON()['individual'][validatorAddress.toHuman()] ? eraPoints.toJSON()['individual'][validatorAddress.toHuman()] : 0
const exposure = eraExposures.validators[validatorAddress.toHuman()] ? eraExposures.validators[validatorAddress.toHuman()] : {total:0,own:0,others:[]}
const voters: Voter[] = votersMap.has(validatorAddress.toHuman()) ? votersMap.get(validatorAddress.toHuman()) : []
const {identity} = await infoPromise
return {
...validatorStaking,
displayName: getDisplayName(identity),
voters: voters,
exposure: exposure,
eraPoints: validatorEraPoints,
} as MyDeriveStakingAccount
}))
return myValidatorStaking
}
const _getWaitingValidatorsAccountId = async (api: ApiPromise): Promise<string[]> => {
const skStashes = await api.query.staking.validators.keys()
const stashes = skStashes.map(sk => sk.args[0].toString())
const active = (await api.query.session.validators()).map(a => a.toString());
const waiting = stashes.filter((s) => !active.includes(s));
return waiting.map(account => account.toString())
}
const _buildVotersMap = (nominatorsStakings: DeriveStakingAccount[]): VotersMap => {
const voters: VotersMap = new Map<string,Voter[]>()
nominatorsStakings.forEach( nominator => {
nominator.nominators.forEach ( nominated => {
const key = nominated.toHuman()
const value = {
address: nominator.accountId.toHuman(),
value: nominator.stakingLedger.total
}
if (voters.has(key)){
voters.get(key).push(value)
}
else{
voters.set(key,[value])
}
})
})
return voters
}