-
Notifications
You must be signed in to change notification settings - Fork 3
/
reserved.js
417 lines (359 loc) · 11.6 KB
/
reserved.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
const { BN, u8aEq } = polkadotUtil;
const { decodeAddress } = polkadotUtilCrypto;
const { WsProvider, ApiPromise } = polkadotApi;
let output = document.getElementById("output");
let log = document.getElementById("log");
let address = document.getElementById("address");
// Global Variables
var global = {
address: '',
endpoint: '',
chainDecimals: '',
chainToken: 'Units',
};
// Convert a big number balance to expected float with correct units.
function toUnit(balance) {
let decimals = global.chainDecimals;
base = new BN(10).pow(new BN(decimals));
dm = balance.divmod(base);
modulus = dm.mod.abs().toString();
if (!dm.mod.isZero()) {
// make sure there are enough front zeros
const additionalZeros = decimals - modulus.length;
for (let i = 0; i < additionalZeros; i++) {
modulus = '0' + modulus;
}
// remove all trailing zeros
while (modulus.slice(-1) == "0") {
modulus = modulus.slice(0, modulus.length - 1);
}
}
let sign = "";
if (dm.mod < 0) {
sign = "-";
}
return sign + dm.div.toString() + "." + modulus + " " + global.chainToken
}
// Connect to Substrate endpoint
async function connect() {
let endpoint = document.getElementById('endpoint').value;
if (!window.substrate || global.endpoint != endpoint) {
const provider = new WsProvider(endpoint);
log.innerHTML = 'Connecting to Endpoint...';
window.substrate = await ApiPromise.create({ provider });
global.endpoint = endpoint;
global.chainDecimals = substrate.registry.chainDecimals;
global.chainToken = substrate.registry.chainTokens[0];
log.innerHTML = 'Connected';
}
}
async function getDemocracyReserved() {
if (!substrate.query.democracy) {
console.log("No democracy pallet.");
return new BN();
}
let deposit = new BN();
// Get all DepositOf
let deposits = await substrate.query.democracy.depositOf.entries();
for (const [key, d] of deposits) {
let users = d.value[0];
let amount = d.value[1];
// Find where the user matches
for (user of users) {
if (u8aEq(user, global.address)) {
// Add the deposit amount
deposit = deposit.add(amount);
}
}
}
// Get Preimage Deposit
let preimageDeposit = new BN();
let preimages = await substrate.query.democracy.preimages.entries();
for (let [key, preimage] of preimages) {
preimage = preimage.value;
if (preimage.isAvailable) {
// TODO: Check if works
preimage = preimage.asAvailable;
if (u8aEq(preimage.provider, global.address)) {
preimageDeposit = preimageDeposit.add(preimage.deposit);
}
}
}
output.innerText += `Democracy: Deposit = ${toUnit(deposit)}, Preimage = ${toUnit(preimageDeposit)}\n`;
return deposit.add(preimageDeposit);
}
async function getPhragmenElectionReserved() {
if (!substrate.query.phragmenElection) {
console.log("No phragmen elections pallet.");
return new BN();
}
let voter = await substrate.query.phragmenElection.voting(global.address);
let deposit = new BN();
if (voter.deposit) {
deposit = deposit.add(voter.deposit);
console.log(deposit);
}
output.innerText += `Phragmen Elections: Deposit = ${toUnit(deposit)}\n`
return deposit;
}
async function getIdentityReserved() {
if (!substrate.query.identity) {
console.log("No identity pallet.");
return new BN();
}
let registration = await substrate.query.identity.identityOf(global.address);
registration = registration.value;
let subIdentities = await substrate.query.identity.subsOf(global.address);
// Deposit for existing identity
let deposit = new BN();
// Deposit for any subidentities
let subDeposit = new BN();
if (registration.deposit) {
deposit = deposit.add(registration.deposit);
subDeposit = subDeposit.add(subIdentities[0]);
}
// Reserved fees for judgements
let fees = new BN();
if (registration.judgements) {
for (const judgement of registration.judgements) {
if (judgement.isFeePaid) {
// TODO: Need to check this
fees = fees.add(judgement.asFeePaid)
}
}
}
output.innerText += `Identity: Deposit = ${toUnit(deposit)}, Sub-identities Deposit = ${toUnit(subDeposit)}, Fees = ${toUnit(fees)}\n`;
return deposit.add(fees).add(subDeposit);
}
async function getRegistrarReserved() {
if (!substrate.query.registrar) {
console.log("No registrar pallet.");
return new BN();
}
let deposit = new BN();
let paras = await substrate.query.registrar.paras.entries();
for (let [key, para] of paras) {
para = para.value;
let who = para.manager;
let value = para.deposit;
if (u8aEq(who, global.address)) {
deposit = deposit.add(value);
}
}
output.innerText += `Registrar: Deposit = ${toUnit(deposit)}\n`
return deposit;
}
async function getCrowdloanReserved() {
if (!substrate.query.crowdloan) {
console.log("No crowdloan pallet.");
return new BN();
}
let deposit = new BN();
let crowdloans = await substrate.query.crowdloan.funds.entries();
for (let [key, crowdloan] of crowdloans) {
crowdloan = crowdloan.value;
let who = crowdloan.depositor;
let value = crowdloan.deposit;
if (u8aEq(who, global.address)) {
deposit = deposit.add(value);
}
}
output.innerText += `Crowdloan: Deposit = ${toUnit(deposit)}\n`
return deposit;
}
async function getIndicesReserved() {
if (!substrate.query.indices) {
console.log("No indices pallet.");
return new BN();
}
let deposit = new BN();
// Each index has a user and a deposit amount
let indices = await substrate.query.indices.accounts.entries();
for (let [key, index] of indices) {
index = index.value;
let who = index[0];
let amount = index[1];
if (u8aEq(who, global.address)) {
deposit = deposit.add(amount);
}
}
output.innerText += `Indices: Deposit = ${toUnit(deposit)}\n`;
return deposit;
}
async function getMultisigReserved() {
if (!substrate.query.multisig) {
console.log("No multisig pallet.");
return new BN();
}
let multisigDeposit = new BN();
let multisigs = await substrate.query.multisig.multisigs.entries();
for (let [key, multisig] of multisigs) {
multisig = multisig.value;
let who = multisig.depositor;
let amount = multisig.deposit;
if (u8aEq(who, global.address)) {
multisigDeposit = multisigDeposit.add(amount);
}
}
let callsDeposit = new BN();
let calls = await substrate.query.multisig.calls.entries();
for (let [key, call] of calls) {
call = call.value;
let who = call[1];
let amount = call[2];
if (u8aEq(who, global.address)) {
callsDeposit = callsDeposit.add(amount);
}
}
output.innerText += `Multisig: Deposit = ${toUnit(multisigDeposit)}, Calls = ${toUnit(callsDeposit)}\n`;
return multisigDeposit.add(callsDeposit);
}
async function getProxyReserved() {
if (!substrate.query.proxy) {
console.log("No proxy pallet.");
return new BN();
}
let proxyDeposit = new BN();
let proxies = await substrate.query.proxy.proxies(global.address);
let value = proxies[1];
proxyDeposit = proxyDeposit.add(value)
let announcementDeposit = new BN();
let announcements = await substrate.query.proxy.announcements(global.address);
let announcementValue = proxies[1];
announcementDeposit = announcementDeposit.add(announcementValue)
// TODO Anon vs delegator
output.innerText += `Proxy: Deposit = ${toUnit(proxyDeposit)}, Announcement = ${toUnit(announcementDeposit)}\n`;
return proxyDeposit.add(announcementDeposit);
}
async function getRecoveryReserved() {
if (!substrate.query.recovery) {
console.log("No recovery pallet.");
return new BN();
}
// User has a recovery setup
let recoverableDeposit = new BN();
let recoverable = await substrate.query.recovery.recoverable(global.address);
if (recoverable.isSome) {
recoverable = recoverable.value;
if (recoverable.deposit) {
recoverableDeposit = recoverableDeposit.add(recoverable.deposit);
}
}
// Active Recoveries
let activeDeposit = new BN();
let activeRecoveries = await substrate.query.recovery.activeRecoveries.entries();
for (let [keys, active] of activeRecoveries) {
// TODO: Confirm works
let who = keys.args[1];
let value = active.deposit;
if (u8aEq(who, global.address)) {
activeDeposit = activeDeposit.add(value);
}
}
output.innerText += `Recovery: Recoverable = ${toUnit(recoverableDeposit)}, Active: ${toUnit(activeDeposit)}\n`;
return recoverableDeposit.add(activeDeposit);
}
async function getSocietyReserved() {
if (!substrate.query.society) {
console.log("No society pallet.");
return new BN();
}
let bidDeposit = new BN();
let bids = await substrate.query.society.bids();
for (bid of bids) {
if (u8aEq(bid.who, global.address)) {
if (bid.kind.isDeposit) {
bidDeposit = bidDeposit.add(bid.kind.asDeposit);
}
}
}
output.innerText += `Society: Bid Deposit = ${toUnit(bidDeposit)}\n`;
return bidDeposit;
}
async function getTreasuryReserved() {
if (!substrate.query.treasury) {
console.log("No treasury pallet.");
return new BN();
}
let proposalDeposit = new BN();
let proposals = await substrate.query.treasury.proposals.entries();
for (let [key, proposal] of proposals) {
proposal = proposal.value;
let who = proposal.proposer;
let value = proposal.bond;
if (u8aEq(who, global.address)) {
proposalDeposit = proposalDeposit.add(value);
}
}
let tipDeposit = new BN();
let tips = await substrate.query.tips.tips.entries();
for (let [key, tip] of tips) {
tip = tip.value;
let who = tip.who;
let value = tip.deposit;
if (u8aEq(who, global.address)) {
tipDeposit = tipDeposit.add(value);
}
}
let curatorDeposit = new BN();
let bountyDeposit = new BN();
let bounties = await substrate.query.bounties.bounties.entries();
for (let [key, bounty] of bounties) {
bounty = bounty.value;
let status = bounty.status;
// Bounty is not funded yet, so there is still a deposit for proposer.
if (status.isProposed || status.isFunded) {
let who = bounty.proposer;
let value = bounty.bond;
if (u8aEq(who, global.address)) {
bountyDeposit = bountyDeposit.add(value);
}
} else {
// Curator has a deposit.
let value = bounty.curatorDeposit;
if (!value.isZero()) {
let status = bounty.status;
if (status.value && status.value.curator) {
let who = status.value.curator;
if (u8aEq(who, global.address)) {
curatorDeposit = curatorDeposit.add(value);
}
}
}
}
}
output.innerText += `Treasury: Proposal = ${toUnit(proposalDeposit)}, Tip = ${toUnit(tipDeposit)}, Bounty = ${toUnit(bountyDeposit)}, Curator = ${toUnit(curatorDeposit)}\n`;
return proposalDeposit.add(tipDeposit).add(curatorDeposit);
}
async function getActualReserved() {
let account = await substrate.derive.balances.all(global.address);
return account.reservedBalance;
}
function clear() {
output.innerText = "";
}
async function calculateReserved() {
await connect();
clear();
let reserved = new BN();
global.address = decodeAddress(address.value);
// Calculate the reserved balance pallet to pallet
reserved = reserved.add(await getDemocracyReserved());
reserved = reserved.add(await getPhragmenElectionReserved());
reserved = reserved.add(await getIdentityReserved());
reserved = reserved.add(await getRegistrarReserved());
reserved = reserved.add(await getCrowdloanReserved());
reserved = reserved.add(await getIndicesReserved());
reserved = reserved.add(await getMultisigReserved());
reserved = reserved.add(await getProxyReserved());
reserved = reserved.add(await getRecoveryReserved());
reserved = reserved.add(await getSocietyReserved());
reserved = reserved.add(await getTreasuryReserved());
output.innerText += `Final: ${toUnit(reserved)}\n`;
// Get the current reserved balance for account
let actualReserved = await getActualReserved();
output.innerText += `Actual: ${toUnit(actualReserved)}\n`;
// Show difference between calculated and actual
let diff = actualReserved.sub(reserved)
output.innerText += `Difference: ${toUnit(diff)}\n`;
}