Skip to content

Commit f36ffd6

Browse files
committedOct 30, 2021
Added average RPL/ETH to network node balance checkpoint.
1 parent 29f4446 commit f36ffd6

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed
 

‎generated/schema.ts

+23
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export class RocketPoolProtocol extends Entity {
2222
this.set("stakers", Value.fromStringArray(new Array(0)));
2323
this.set("nodes", Value.fromStringArray(new Array(0)));
2424
this.set("nodeTimezones", Value.fromStringArray(new Array(0)));
25+
this.set(
26+
"networkNodeBalanceCheckpoints",
27+
Value.fromStringArray(new Array(0))
28+
);
2529
}
2630

2731
save(): void {
@@ -153,6 +157,15 @@ export class RocketPoolProtocol extends Entity {
153157
);
154158
}
155159
}
160+
161+
get networkNodeBalanceCheckpoints(): Array<string> {
162+
let value = this.get("networkNodeBalanceCheckpoints");
163+
return value!.toStringArray();
164+
}
165+
166+
set networkNodeBalanceCheckpoints(value: Array<string>) {
167+
this.set("networkNodeBalanceCheckpoints", Value.fromStringArray(value));
168+
}
156169
}
157170

158171
export class Staker extends Entity {
@@ -1566,6 +1579,7 @@ export class NetworkNodeBalanceCheckpoint extends Entity {
15661579
this.set("averageNodeTotalRewardsClaimed", Value.fromBigInt(BigInt.zero()));
15671580
this.set("averageNodeRewardClaim", Value.fromBigInt(BigInt.zero()));
15681581
this.set("rplPriceInETH", Value.fromBigInt(BigInt.zero()));
1582+
this.set("averageRplPriceInETH", Value.fromBigInt(BigInt.zero()));
15691583
this.set("queuedMinipools", Value.fromBigInt(BigInt.zero()));
15701584
this.set("stakingMinipools", Value.fromBigInt(BigInt.zero()));
15711585
this.set("stakingUnbondedMinipools", Value.fromBigInt(BigInt.zero()));
@@ -1786,6 +1800,15 @@ export class NetworkNodeBalanceCheckpoint extends Entity {
17861800
this.set("rplPriceInETH", Value.fromBigInt(value));
17871801
}
17881802

1803+
get averageRplPriceInETH(): BigInt {
1804+
let value = this.get("averageRplPriceInETH");
1805+
return value!.toBigInt();
1806+
}
1807+
1808+
set averageRplPriceInETH(value: BigInt) {
1809+
this.set("averageRplPriceInETH", Value.fromBigInt(value));
1810+
}
1811+
17891812
get queuedMinipools(): BigInt {
17901813
let value = this.get("queuedMinipools");
17911814
return value!.toBigInt();

‎schema.graphql

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ type RocketPoolProtocol @entity {
2626

2727
# Last known node network balance checkpoint.
2828
lastNetworkNodeBalanceCheckPoint: NetworkNodeBalanceCheckpoint
29+
30+
# All node network balance checkpoints for the RocketPool protocol.
31+
networkNodeBalanceCheckpoints: [NetworkNodeBalanceCheckpoint!]!
2932
}
3033

3134
# An address that is/was associated with an rETH balance.
@@ -452,6 +455,9 @@ type NetworkNodeBalanceCheckpoint @entity {
452455
# The RPL price in ETH at this checkpoint.
453456
rplPriceInETH: BigInt!
454457

458+
# The average RPL price in ETH up to this checkpoint.
459+
averageRplPriceInETH: BigInt!
460+
455461
# Current queued minipools accross all nodes at this checkpoint.
456462
queuedMinipools: BigInt!
457463

‎src/entityfactory.ts

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class RocketPoolEntityFactory {
3131
protocol.nodeTimezones = new Array<string>(0)
3232
protocol.lastRPLRewardInterval = null
3333
protocol.lastNetworkNodeBalanceCheckPoint = null
34+
protocol.networkNodeBalanceCheckpoints =new Array<string>(0)
3435
return protocol
3536
}
3637

@@ -354,6 +355,7 @@ class RocketPoolEntityFactory {
354355
checkpoint.averageNodeTotalRewardsClaimed = BigInt.fromI32(0) // Will be calculated.
355356
checkpoint.averageNodeRewardClaim = BigInt.fromI32(0) // Will be calculated.
356357
checkpoint.rplPriceInETH = newRplPriceInETH // From the associated price update.
358+
checkpoint.averageRplPriceInETH = BigInt.fromI32(0) // Will be calculated.
357359
checkpoint.queuedMinipools = BigInt.fromI32(0) // Will be calculated.
358360
checkpoint.stakingMinipools = BigInt.fromI32(0) // Will be calculated.
359361
checkpoint.stakingUnbondedMinipools = BigInt.fromI32(0) // Will be calculated.

‎src/mappings/rocketNetworkPricesMapping.ts

+82-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { rocketNetworkFees } from '../../generated/rocketNetworkPrices/rocketNet
33
import { rocketDAOProtocolSettingsMinipool } from '../../generated/rocketNetworkPrices/rocketDAOProtocolSettingsMinipool'
44
import { rocketDAOProtocolSettingsNode } from '../../generated/rocketNetworkPrices/rocketDAOProtocolSettingsNode'
55
import { rocketNodeStaking } from '../../generated/rocketNetworkPrices/rocketNodeStaking'
6-
import { Node, NetworkNodeBalanceCheckpoint } from '../../generated/schema'
6+
import {
7+
Node,
8+
NetworkNodeBalanceCheckpoint,
9+
RocketPoolProtocol,
10+
} from '../../generated/schema'
711
import { generalUtilities } from '../utilities/generalUtilities'
812
import { rocketPoolEntityFactory } from '../entityfactory'
913
import { NetworkNodeBalanceMetadata } from '../models/networkNodeBalanceMetadata'
@@ -16,6 +20,7 @@ import {
1620
import { BigInt, Address } from '@graphprotocol/graph-ts'
1721
import { nodeUtilities } from '../utilities/nodeutilities'
1822
import { EffectiveMinipoolRPLBounds } from '../models/effectiveMinipoolRPLBounds'
23+
import { ONE_ETHER_IN_WEI } from '../constants/generalconstants'
1924

2025
/**
2126
* When enough ODAO members submitted their votes and a consensus threshold is reached, a new RPL price is comitted to the smart contracts.
@@ -94,9 +99,18 @@ export function handlePricesUpdated(event: PricesUpdated): void {
9499
metadata.rplMetadata,
95100
)
96101

102+
// Determine the average RPL/ETH ratio for the current network node balance checkpoint.
103+
setAverageRplEthRatio(protocol, checkpoint);
104+
97105
// Update the link so the protocol points to the last network node balance checkpoint.
98106
protocol.lastNetworkNodeBalanceCheckPoint = checkpoint.id
99107

108+
// Add the new network node balance checkpoint to the protocol collection.
109+
let nodeBalanceCheckpoints = protocol.networkNodeBalanceCheckpoints
110+
if (nodeBalanceCheckpoints.indexOf(checkpoint.id) == -1)
111+
nodeBalanceCheckpoints.push(checkpoint.id)
112+
protocol.networkNodeBalanceCheckpoints = nodeBalanceCheckpoints
113+
100114
// Index these changes.
101115
checkpoint.save()
102116
if (previousCheckpoint !== null) previousCheckpoint.save()
@@ -184,6 +198,73 @@ function generateNodeBalanceCheckpoints(
184198
return networkMetadata
185199
}
186200

201+
/**
202+
* Loops through all network node balance checkpoints and determines the new average RPL price.
203+
*/
204+
function setAverageRplEthRatio(
205+
protocol: RocketPoolProtocol,
206+
currentCheckpoint: NetworkNodeBalanceCheckpoint,
207+
): void {
208+
// Preliminary null checks.
209+
if (
210+
protocol === null ||
211+
currentCheckpoint === null ||
212+
currentCheckpoint.rplPriceInETH == BigInt.fromI32(0)
213+
)
214+
return
215+
216+
// We will use this to calculate the average RPL/ETH price accross all the network node balance checkpoints.
217+
let totalRplPriceInEth = BigInt.fromI32(0)
218+
let totalCheckpointsWithAnRplPriceInETH = BigInt.fromI32(0)
219+
220+
// Loop through all of the node balance checkpoints up until this time.
221+
for (
222+
let index = 0;
223+
index < protocol.networkNodeBalanceCheckpoints.length;
224+
index++
225+
) {
226+
// Determine current network node balance checkpoint ID.
227+
let networkNodeBalanceCheckpointId = <string>(
228+
protocol.networkNodeBalanceCheckpoints[index]
229+
)
230+
if (networkNodeBalanceCheckpointId == null) continue
231+
232+
// Load the indexed network node balance checkpoint.
233+
let activeIterationCheckpoint = NetworkNodeBalanceCheckpoint.load(
234+
networkNodeBalanceCheckpointId,
235+
)
236+
if (
237+
activeIterationCheckpoint === null ||
238+
activeIterationCheckpoint.rplPriceInETH == BigInt.fromI32(0)
239+
)
240+
continue
241+
242+
// Increment running totals.
243+
totalCheckpointsWithAnRplPriceInETH = totalCheckpointsWithAnRplPriceInETH.plus(
244+
BigInt.fromI32(1),
245+
)
246+
totalRplPriceInEth = totalRplPriceInEth.plus(
247+
activeIterationCheckpoint.rplPriceInETH.div(ONE_ETHER_IN_WEI),
248+
)
249+
}
250+
251+
// Calculate the average rpl/eth price for the current node network balance checkpoint.
252+
if (
253+
totalRplPriceInEth > BigInt.fromI32(0) &&
254+
totalCheckpointsWithAnRplPriceInETH > BigInt.fromI32(0)
255+
) {
256+
currentCheckpoint.averageRplPriceInETH = totalRplPriceInEth.div(
257+
totalCheckpointsWithAnRplPriceInETH,
258+
).times(ONE_ETHER_IN_WEI)
259+
} else {
260+
/*
261+
If we didn't succeed in determining the average rpl/eth price from the history,
262+
then our average price is the current one.
263+
*/
264+
currentCheckpoint.averageRplPriceInETH = currentCheckpoint.rplPriceInETH
265+
}
266+
}
267+
187268
/**
188269
* Returns the minimum and maximum RPL needed to collateralize a new minipool based on the current smart contract state.
189270
*/

0 commit comments

Comments
 (0)
Please sign in to comment.