@@ -3,7 +3,11 @@ import { rocketNetworkFees } from '../../generated/rocketNetworkPrices/rocketNet
3
3
import { rocketDAOProtocolSettingsMinipool } from '../../generated/rocketNetworkPrices/rocketDAOProtocolSettingsMinipool'
4
4
import { rocketDAOProtocolSettingsNode } from '../../generated/rocketNetworkPrices/rocketDAOProtocolSettingsNode'
5
5
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'
7
11
import { generalUtilities } from '../utilities/generalUtilities'
8
12
import { rocketPoolEntityFactory } from '../entityfactory'
9
13
import { NetworkNodeBalanceMetadata } from '../models/networkNodeBalanceMetadata'
@@ -16,6 +20,7 @@ import {
16
20
import { BigInt , Address } from '@graphprotocol/graph-ts'
17
21
import { nodeUtilities } from '../utilities/nodeutilities'
18
22
import { EffectiveMinipoolRPLBounds } from '../models/effectiveMinipoolRPLBounds'
23
+ import { ONE_ETHER_IN_WEI } from '../constants/generalconstants'
19
24
20
25
/**
21
26
* 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 {
94
99
metadata . rplMetadata ,
95
100
)
96
101
102
+ // Determine the average RPL/ETH ratio for the current network node balance checkpoint.
103
+ setAverageRplEthRatio ( protocol , checkpoint ) ;
104
+
97
105
// Update the link so the protocol points to the last network node balance checkpoint.
98
106
protocol . lastNetworkNodeBalanceCheckPoint = checkpoint . id
99
107
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
+
100
114
// Index these changes.
101
115
checkpoint . save ( )
102
116
if ( previousCheckpoint !== null ) previousCheckpoint . save ( )
@@ -184,6 +198,73 @@ function generateNodeBalanceCheckpoints(
184
198
return networkMetadata
185
199
}
186
200
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
+
187
268
/**
188
269
* Returns the minimum and maximum RPL needed to collateralize a new minipool based on the current smart contract state.
189
270
*/
0 commit comments