-
Notifications
You must be signed in to change notification settings - Fork 3
/
reference.js
224 lines (209 loc) · 7.6 KB
/
reference.js
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const createApi = async () => {
// console.log(`Connecting to API...`);
let electedInfo;
const wsProvider = new WsProvider('wss://kusama-rpc.polkadot.io');
const api = await ApiPromise.create({ provider: wsProvider });
await api.isReady;
// console.log(`API is ready`);
// console.clear();
// Fetch recent reward events from Polkascan
const res = await fetch(
'https://polkascan.io/kusama-cc3/api/v1/event?&filter[module_id]=staking&filter[event_id]=Reward&page[size]=10'
);
const json = await res.json();
const rewardData = await json.data;
// Retrieve currentElected validators for current block
const currentValidators = await api.query.staking.currentElected();
// Retrieve all validators
const allValidators = await api.query.staking.validators();
// Parse validators
const parsedValidators = JSON.parse(JSON.stringify(allValidators))[0];
// Retrieve session validators
const sessionValidators = await api.query.session.validators();
const intentions = await parsedValidators.filter(
validator => !sessionValidators.includes(validator)
);
const validatorsAndIntentions = [...sessionValidators, ...intentions];
// Retrieve the last known era reward
const reward = await rewardData[0].attributes.attributes[0].value;
// Retrieve the hashes of the end of era blocks
const hash = await Promise.all(
rewardData.map(data =>
api.rpc.chain.getBlockHash(data.attributes.block_id - 1)
)
);
// Retrieve the era points for all end of era blocks
const eraPoints = await Promise.all(
hash.map(data =>
api.query.staking.currentEraPointsEarned.at(`${data.toString()}`)
)
);
// Retrieve an array of the list of all elected validators at the end of era blocks
const validatorList = await Promise.all(
hash.map(data => api.query.staking.currentElected.at(`${data.toString()}`))
);
let result = {};
await Promise.all(
validatorList.map(async validator => {
await Promise.all(
validator.map(async address => {
const commission = await api.query.staking.validators(address);
const name = await api.query.nicks.nameOf(`${address.toString()}`);
result[address] = {
stashId: address.toString(),
stashIdTruncated: `${address
.toString()
.slice(0, 4)}...${address.toString().slice(-6, -1)}`,
points: [],
poolReward: '',
totalStake: '',
commission: commission[0].commission.toNumber() / 10 ** 7,
name: name.raw[0]
? hexToString(name.raw[0].toString())
: `Validator (...${address.toString().slice(-6, -1)})`
};
})
);
})
);
eraPoints.map((eraPoint, index) => {
eraPoint.individual.map((point, validatorIndex) => {
result[validatorList[index][validatorIndex]].points.push(
point.toNumber() / eraPoint.total.toNumber()
);
return 0;
});
return 0;
});
const validatorData = await Promise.all(
Object.keys(result).map(async (key, index) => {
const validatorPoolReward =
((result[key].points.reduce((acc, curr) => acc + curr, 0) /
result[key].points.length) *
reward) /
10 ** 12;
electedInfo = await api.derive.staking.electedInfo();
const stakeInfo = await api.derive.staking.account(key.toString());
const totalStake =
stakeInfo !== undefined
? stakeInfo.stakers.total.toString() / 10 ** 12
: undefined;
result[key].totalStake = totalStake;
result[key].poolReward = isNaN(validatorPoolReward)
? 'Not enough data'
: (1 - result[key].commission / 100) * validatorPoolReward;
// setElectedInfo(electedInfo);
return result[key];
})
);
const filteredValidatorData = validatorData.filter(curr =>
currentValidators.includes(curr.stashId)
);
// setApiConnected(true);
// setValidatorData(filteredValidatorData);
// setIntentionData(intentions);
// setValidatorsAndIntentions(validatorsAndIntentions);
return {
filteredValidatorData,
intentions,
validatorsAndIntentions,
electedInfo
};
};
let counter = 0;
app.get('/manualfetch', async (req, res) => {
try {
console.log('res.headersSent', res.headersSent);
const result = await createApi();
const savedValidator = await Validator.insertMany(
JSON.parse(JSON.stringify(result.filteredValidatorData))
);
const electedInfoData = new EI(
JSON.parse(JSON.stringify(result.electedInfo))
);
const savedElectedInfo = await electedInfoData.save();
const intentionData = new Intention({
intentions: JSON.parse(JSON.stringify(result.intentions)),
validatorsAndIntentions: JSON.parse(
JSON.stringify(result.validatorsAndIntentions)
)
});
const savedIntention = await intentionData.save();
counter++;
console.log('Counter', counter);
res.json({ savedValidator, savedElectedInfo, savedIntention });
} catch (err) {
res.status(400).json({ err: err, message: 'error bro' });
}
});
// app.get('/nominator', async (req, res) => {
// try {
// await Nominator.deleteMany({});
// const validators = await Validator.find();
// const validatorInfo = await ValidatorInfo.find();
// const newData = validators.map(validator => {
// const temp = validatorInfo.find(currentValidator => {
// if (currentValidator.stashId === validator.stashId) {
// return true;
// }
// });
// return {
// electedInfo: temp,
// validator: validator
// };
// });
// const nominators = [];
// newData.forEach(data => {
// data.electedInfo.stakers.others.forEach(nom => {
// const tempObj = { who: nom.who, value: nom.value };
// if (!_.some(nominators, tempObj)) {
// nominators.push(tempObj);
// }
// });
// });
// const final = [];
// nominators.map(nom => {
// let temp = [];
// newData.forEach(data => {
// data.electedInfo.stakers.others.forEach(curr => {
// if (nom.who.toString() === curr.who.toString()) {
// temp.push({
// validator: data,
// staked: curr.value / 10 ** 12
// });
// }
// });
// });
// if (temp.length > 0) {
// //for reference: https://docs.google.com/document/d/13dLBH5Ngu63lCQryRW3BiiJlXlYllg_fAzAObmOh0gw/edit?pli=1
// let sum = 0;
// for (let i = 0; i < temp.length; i++) {
// //Logic for calculating expected daily ROI
// //Commission is already taken into account while calculating poolReward
// const { totalStake, poolReward } = temp[i].validator.validator;
// sum += (temp[0].staked / totalStake) * poolReward;
// }
// const ERA_PER_DAY = 4;
// const expectedDailyRoi = (sum * ERA_PER_DAY).toFixed(3);
// const total = temp.reduce((acc, curr) => {
// return acc + curr.staked;
// }, 0);
// const highest = Math.max(...temp.map(validator => validator.staked));
// const other = total - highest;
// final.push({
// nominatorId: nom.who,
// validators: temp,
// totalStaked: parseFloat(total.toFixed(3)),
// highestStaked: parseFloat(highest.toFixed(3)),
// othersStaked: parseFloat(other.toFixed(3)),
// expectedDailyRoi: parseFloat(expectedDailyRoi)
// });
// temp = [];
// }
// });
// // await Nominator.insertMany(final);
// res.json(final.length);
// } catch (err) {
// console.log('err', err);
// }
// });