forked from renproject/command-center
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contractReads.ts
563 lines (506 loc) · 19.4 KB
/
contractReads.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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import { ApolloClient } from "@apollo/react-hooks";
import { RenNetworkDetails } from "@renproject/contracts";
import { Currency } from "@renproject/react-components";
import BigNumber from "bignumber.js";
import { List, OrderedMap, OrderedSet } from "immutable";
import Web3 from "web3";
import { sha3, toChecksumAddress } from "web3-utils";
import { updatePrices } from "../../controllers/common/tokenBalanceUtils";
import { retryNTimes } from "../../controllers/pages/renvmStatsPage/renvmContainer";
import { getLightnode } from "../../store/mapContainer";
import { DarknodesState } from "../../store/networkContainer";
import { darknodeIDHexToBase58 } from "../darknode/darknodeID";
import { queryStat } from "../darknode/jsonrpc";
import { isDefined } from "../general/isDefined";
import { safePromiseAllList } from "../general/promiseAll";
import { Darknode, queryDarknode } from "../graphQL/queries/darknode";
import { TokenAmount } from "../graphQL/queries/queries";
import { RenVM } from "../graphQL/queries/renVM";
import { catchBackgroundException } from "../react/errors";
import { getDarknodePayment, getDarknodeRegistry } from "./contract";
import { getDarknodeStatus, isRegisteredInEpoch } from "./darknodeStatus";
import { FeeTokens, Token, TokenPrices, TokenString } from "./tokens";
export const NULL = "0x0000000000000000000000000000000000000000";
/**
* Remove "0x" prefix from a hex string if there is one.
*
* @example
* strip0x("0x1234") // "1234"
* strip0x("1234") // "1234"
*
* @param hex The string to remove "0x" from.
*/
export const strip0x = (hex: string): string =>
hex.substring(0, 2) === "0x" ? hex.slice(2) : hex;
/**
* Add a "0x" prefix to a hex value, converting to a string if required.
*
* @example
* Ox("1234") // 0x1234
* Ox("0x1234") // 0x1234
* Ox(Buffer([0x12, 0x34])) // 0x1234
*
* @param hex The string or Buffer to add "0x" to.
*/
export const Ox = (hex: string | Buffer | number | BigNumber): string => {
const hexString =
typeof hex === "string"
? hex
: Buffer.isBuffer(hex)
? hex.toString("hex")
: new BigNumber(hex).toString(16);
return hexString.substring(0, 2) === "0x" ? hexString : `0x${hexString}`;
};
// ////////////////////////////////////////////////////////////////////////// //
// Darknode Registry contract /////////////////////////////////////////////// //
// ////////////////////////////////////////////////////////////////////////// //
export enum RegistrationStatus {
Unknown = "",
Unregistered = "unregistered",
RegistrationPending = "registration pending",
Registered = "registered",
DeregistrationPending = "deregistration pending",
Deregistered = "awaiting refund period",
Refundable = "refundable",
}
/**
* Find the darknodes by reading the logs of the Darknode Registry.
*
* @param web3 A Web3 instance.
* @param renNetwork A Ren network object.
* @param fromBlock The starting block to look at logs for.
* @returns An immutable set of darknode IDs (as hex strings).
*/
const retrieveDarknodesInLogs = async (
web3: Web3,
renNetwork: RenNetworkDetails,
fromBlock: string | number,
operatorAddress: string,
) => {
let darknodes = OrderedSet();
/**
* Sample log:
* ```json
* {
* address: "0x75Fa8349fc9C7C640A4e9F1A1496fBB95D2Dc3d5",
* blockHash: "0xfab9c0e4d7ccca3e56d6961fbe17917923898828b3f929093e6b976b8727db39",
* blockNumber: 9740948,
* data: "0x000000000000000000000000945458e071eca54bb534d8ac7c8cd1a3eb318d92000000000000000000000000000000000000\
* 00000000152d02c7e14af6800000",
* id: "log_98d2346b",
* logIndex: 2,
* removed: false,
* topics: ["0xd2819ba4c736158371edf0be38fd8d1fc435609832e392f118c4c79160e5bd7b"],
* transactionHash: "0x8ed0e53dffda6c356e25cb1ac3ebe7a69bcab8ebf668a7b2e770480bdb47598b",
* transactionIndex: 2,
* transactionLogIndex: "0x2",
* type: "mined",
* }
* ```
*/
let recentRegistrationEvents = await web3.eth.getPastLogs({
address: renNetwork.addresses.ren.DarknodeRegistry.address,
fromBlock,
toBlock: "latest",
// topics: [sha3("LogDarknodeRegistered(address,uint256)"), "0x000000000000000000000000" +
// address.slice(2), null, null] as any,
topics: [sha3("LogDarknodeRegistered(address,uint256)")],
});
const recentRegistrationEvents2 = await web3.eth.getPastLogs({
address: renNetwork.addresses.ren.DarknodeRegistry.address,
fromBlock,
toBlock: "latest",
// topics: [sha3("LogDarknodeRegistered(address,uint256)"), "0x000000000000000000000000" +
// address.slice(2), null, null] as any,
topics: [
sha3("LogDarknodeRegistered(address,address,uint256)"),
"0x000000000000000000000000" + strip0x(operatorAddress),
],
});
recentRegistrationEvents = recentRegistrationEvents.concat(
recentRegistrationEvents2,
);
for (const event of recentRegistrationEvents) {
// The log data returns back like this:
// 0x000000000000000000000000945458e071eca54bb534d8ac7c8cd1a3eb318d9200000000000000000000000000000000000000000000152d02c7e14af6800000
// and we want to extract this: 0x945458e071eca54bb534d8ac7c8cd1a3eb318d92 (20 bytes, 40 characters long)
const prefixLength = 2;
const dataLength = 64;
const addressLength = 40;
const start = dataLength + prefixLength - addressLength; // 26
let darknodeID;
let operator;
if (event.topics.length === 3) {
darknodeID = toChecksumAddress(
`0x${(event.topics[2] as string).substr(start, addressLength)}`,
);
operator = toChecksumAddress(
`0x${(event.topics[1] as string).substr(start, addressLength)}`,
);
} else {
darknodeID = toChecksumAddress(
`0x${event.data.substr(start, addressLength)}`,
);
}
darknodes = darknodes.add({ darknodeID, operator });
}
// Note: Deregistration events are not included because we are unable to retrieve the operator
// const recentDeregistrationEvents = await web3.eth.getPastLogs({
// address: contracts.DarknodeRegistry.address,
// fromBlock: renNetwork.addresses.ren.DarknodeRegistry.block || "0x600000",
// toBlock: "latest",
// topics: [sha3("LogDarknodeDeregistered(address)"), null],
// });
// for (const event of recentDeregistrationEvents) {
// const darknodeID = toChecksumAddress("0x" + event.data.substr(26, 40));
// darknodes.push(darknodeID);
// }
return darknodes;
};
/**
* Find the darknodes for an operator.
*
* Currently, the LogDarknodeRegistered logs don't include the registrar, so
* instead we loop through every darknode and get it's owner first.
* This would be a lot faster if the logs indexed the operator! 🤦
*
* @param web3 A Web3 instance.
* @param renNetwork A Ren network object.
* @param operatorAddress The address of the operator to look up darknodes for.
* @param onDarknode An optional callback to retrieve darknodes as they are
* found, instead of waiting for all of them to be returned together.
* @returns An immutable set of darknode IDs (as hex strings).
*/
export const getOperatorDarknodes = async (
web3: Web3,
renNetwork: RenNetworkDetails,
operatorAddress: string,
reportProgress?: (progress: number, total: number) => void,
onDarknode?: (darknodeID: string) => void,
): Promise<OrderedSet<string>> => {
let darknodes = OrderedSet();
const darknodeRegistry = getDarknodeRegistry(web3, renNetwork);
operatorAddress = toChecksumAddress(operatorAddress);
const fromBlock = renNetwork.addresses.ren.DarknodeRegistry.block || "0x00";
darknodes = darknodes.concat(
await retrieveDarknodesInLogs(
web3,
renNetwork,
fromBlock,
operatorAddress,
),
);
const operatorPromises = darknodes
.map(
async ({
operator,
darknodeID,
}: {
operator: string;
darknodeID: string;
}) => {
if (operator) {
return [darknodeID, operator];
} else {
// For backwards compatibility.
return [
darknodeID,
await retryNTimes(
async () =>
await darknodeRegistry.methods
.getDarknodeOperator(darknodeID)
.call(/**/),
2,
),
] as [string, string];
}
},
)
.toArray();
let operatorDarknodes = OrderedSet<string>();
for (let i = 0; i < operatorPromises.length; i++) {
if (reportProgress) {
reportProgress(i, operatorPromises.length);
}
const [darknodeID, operator] = await operatorPromises[i];
if (
operator.toLowerCase() === operatorAddress.toLowerCase() &&
!operatorDarknodes.contains(operatorAddress)
) {
operatorDarknodes = operatorDarknodes.add(darknodeID);
if (onDarknode) {
onDarknode(darknodeID);
}
}
}
if (reportProgress) {
reportProgress(operatorPromises.length, operatorPromises.length);
}
return operatorDarknodes;
};
// ////////////////////////////////////////////////////////////////////////// //
// Darknode Payment contract //////////////////////////////////////////////// //
// ////////////////////////////////////////////////////////////////////////// //
// Sum up fees into the total ETH value (in wei).
export const sumUpFeeMap = (
feesEarned: OrderedMap<Token, TokenAmount | null>,
tokenPrices: TokenPrices,
): [BigNumber | null] => {
// let totalEth: BigNumber | null = null;
const feesEarnedInEth = FeeTokens.map((tokenDetails, token) => {
const price = tokenPrices.get(token, undefined);
const decimals = tokenDetails
? new BigNumber(tokenDetails.decimals.toString()).toNumber()
: 0;
const tokenFees = feesEarned.get(token, null);
return tokenFees
? tokenFees.amount
.div(Math.pow(10, decimals))
.multipliedBy(price ? price.get(Currency.ETH, 0) : 0)
: tokenFees;
});
const totalEth = feesEarnedInEth.reduce((acc, inEth) => {
return inEth ? (acc || new BigNumber(0)).plus(inEth) : acc;
}, null as BigNumber | null);
// Convert to wei
return [
totalEth ? totalEth.multipliedBy(new BigNumber(10).pow(18)) : totalEth,
];
};
// ////////////////////////////////////////////////////////////////////////// //
// Darknode Payment contract //////////////////////////////////////////////// //
// ////////////////////////////////////////////////////////////////////////// //
/**
* Retrieves the balances from the DarknodePayment contract.
*
* @param web3 A Web3 instance.
* @param renNetwork A Ren network object.
* @param darknodeID The ID of the darknode as a hex string.
* @returns Returns a promise to an immutable map from token codes to balances
* as BigNumbers.
*/
const getBalances = (
darknode: Darknode | null,
): OrderedMap<TokenString, TokenAmount | null> => {
let balances = OrderedMap<string, TokenAmount | null>();
if (isDefined(darknode)) {
balances = darknode.balances
.filter((asset) => asset.asset !== null)
.reduce((map, asset, token) => map.set(token, asset), balances);
}
return balances;
};
const getBalancesWithInfura = async (
web3: Web3,
renNetwork: RenNetworkDetails,
darknodeID: string,
renVM: RenVM,
tokenPrices: TokenPrices | null,
): Promise<OrderedMap<TokenString, TokenAmount | null>> => {
const darknodePayment = getDarknodePayment(web3, renNetwork);
let feesEarned = OrderedMap<TokenString, TokenAmount | null>();
// const address = (await web3.eth.getAccounts())[0];
const balances = await safePromiseAllList(
List(
renVM.assets
.map(async (asset) => {
const { symbol, tokenAddress, decimals } = asset;
const token = symbol
.replace(/^ren/, "")
.replace(/^test/, "")
.replace(/^dev/, "");
let tokenBalance;
try {
const balance1Call = await retryNTimes(
async () =>
await darknodePayment.methods
.darknodeBalances(darknodeID, tokenAddress)
.call(/**/),
2,
);
tokenBalance = new BigNumber(
(balance1Call || "0").toString(),
);
} catch (error) {
catchBackgroundException(
error,
"Error in contractReads > darknodeBalances",
);
tokenBalance = new BigNumber(0);
}
let amountInEth: BigNumber | undefined;
let amountInUsd: BigNumber | undefined;
if (tokenPrices) {
const price = tokenPrices.get(
token as Token,
undefined,
);
amountInEth = tokenBalance
.div(Math.pow(10, decimals))
.multipliedBy(
price ? price.get(Currency.ETH, 0) : 0,
);
amountInUsd = tokenBalance
.div(Math.pow(10, decimals))
.multipliedBy(
price ? price.get(Currency.USD, 0) : 0,
);
}
const balance = {
symbol: token,
amount: tokenBalance,
amountInEth: amountInEth || new BigNumber(0),
amountInUsd: amountInUsd || new BigNumber(0),
asset,
};
return {
balance, // .plus(balance2),
token,
};
})
.values(),
),
null,
);
for (const balance of balances.toArray()) {
if (balance && balance.token && balance.balance.amount.gt(1)) {
feesEarned = feesEarned.set(balance.token, balance.balance);
}
}
return feesEarned;
};
export enum DarknodeFeeStatus {
BLACKLISTED = "BLACKLISTED",
CLAIMED = "CLAIMED",
NOT_CLAIMED = "NOT_CLAIMED",
NOT_WHITELISTED = "NOT_WHITELISTED",
}
const getDarknodeCycleRewards = (
renVM: RenVM,
darknode: Darknode | null,
registrationStatus: RegistrationStatus,
) => {
// Cycle status ///////////////////////////////////////////////////////// //
let previousStatus;
const isRegisteredInPreviousEpoch =
darknode && isRegisteredInEpoch(darknode, renVM.previousEpoch);
const currentStatus =
registrationStatus === RegistrationStatus.Registered
? DarknodeFeeStatus.NOT_CLAIMED
: DarknodeFeeStatus.NOT_WHITELISTED;
if (!isRegisteredInPreviousEpoch) {
previousStatus = DarknodeFeeStatus.NOT_WHITELISTED;
} else {
const claimed =
darknode &&
(renVM.previousCycle === darknode.lastClaimedEpoch ||
renVM.previousCycle === darknode.previousLastClaimedEpoch);
if (claimed) {
previousStatus = DarknodeFeeStatus.CLAIMED;
} else {
previousStatus = DarknodeFeeStatus.NOT_CLAIMED;
}
}
let cycleStatus = OrderedMap<string, DarknodeFeeStatus>();
if (isDefined(renVM.currentCycle)) {
cycleStatus = cycleStatus.set(
renVM.currentCycle.toString(),
currentStatus,
);
}
if (isDefined(renVM.previousCycle)) {
cycleStatus = cycleStatus.set(
renVM.previousCycle.toString(),
previousStatus,
);
}
return cycleStatus;
};
/**
* Fetches various pieces of information about a darknode, including:
* 1. balances and fees
* 2. its status
* 3. its gas usage information
* 4. its network information (NOTE: not implemented yet)
*
* @param client GraphQL client connected to RenVM subgraph.
* @param web3 A Web3 instance.
* @param renNetwork A Ren network object.
* @param darknodeID The ID of the darknode as a hex string.
* @param tokenPrices
* @returns A promise to the darknode state record.
*/
export const fetchDarknodeDetails = async (
client: ApolloClient<object>,
renVM: RenVM,
web3: Web3,
renNetwork: RenNetworkDetails,
darknodeID: string,
tokenPrices: TokenPrices | null,
useInfura: boolean,
): Promise<DarknodesState> => {
darknodeID = toChecksumAddress(darknodeID.toLowerCase());
const darknode = await queryDarknode(client, darknodeID);
// Get registration status
const registrationStatus = darknode
? getDarknodeStatus(darknode, renVM)
: RegistrationStatus.Unregistered;
// Get eth Balance
const πEthBalance = web3.eth
.getBalance(darknodeID)
.then((ethBalanceBN) =>
ethBalanceBN
? new BigNumber(ethBalanceBN.toString())
: new BigNumber(0),
)
.catch(() => null);
// Call queryStats
const πNodeStatistics = queryStat(
getLightnode(renNetwork),
darknodeIDHexToBase58(darknodeID),
).catch((_error) => /* ignore */ null);
const cycleStatus = getDarknodeCycleRewards(
renVM,
darknode,
registrationStatus,
);
// Get earned fees TODO: fees darknode fees are here
let feesEarned = !useInfura
? getBalances(darknode)
: await getBalancesWithInfura(
web3,
renNetwork,
darknodeID,
renVM,
tokenPrices,
);
feesEarned = updatePrices(feesEarned, tokenPrices);
let feesEarnedInEth: BigNumber | null = null;
if (tokenPrices) {
feesEarnedInEth = feesEarned
.map((fee) => (fee ? fee.amountInEth : new BigNumber(0)))
.reduce((acc, fee) => acc.plus(fee), new BigNumber(0));
}
let feesEarnedInUsd: BigNumber | null = null;
if (tokenPrices) {
feesEarnedInUsd = feesEarned
.map((fee) => (fee ? fee.amountInUsd : new BigNumber(0)))
.reduce((acc, fee) => acc.plus(fee), new BigNumber(0));
}
return new DarknodesState({
ID: darknodeID,
multiAddress: "" as string,
ethBalance: await πEthBalance,
feesEarned,
feesEarnedInEth,
feesEarnedInUsd,
cycleStatus,
averageGasUsage: 0,
lastTopUp: null,
expectedExhaustion: null,
peers: 0,
registrationStatus,
operator: darknode ? darknode.operator : undefined,
nodeStatistics: await πNodeStatistics,
});
};