-
Notifications
You must be signed in to change notification settings - Fork 3
/
farms.ts
385 lines (313 loc) · 13.3 KB
/
farms.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
import { Farm, FarmCertification, PublicIp } from "../model"
import { TfgridModuleFarmStoredEvent, TfgridModuleFarmDeletedEvent, TfgridModuleFarmUpdatedEvent, TfgridModuleFarmPayoutV2AddressRegisteredEvent, TfgridModuleFarmCertificationSetEvent } from "../types/events"
import { EventItem } from '@subsquid/substrate-processor/lib/interfaces/dataSelection'
import { Ctx } from '../processor'
import * as v63 from '../types/v63'
import { validateString } from "./nodes"
export class FarmWithIPs {
constructor(farmID: number, ips: PublicIp[]) {
this.farmID = farmID
this.publicIPs = ips
}
farmID: number;
publicIPs: PublicIp[];
}
export async function farmStored(
ctx: Ctx,
item: EventItem<'TfgridModule.FarmStored', { event: { args: true } }>
) {
const farmStoredEvent = new TfgridModuleFarmStoredEvent(ctx, item.event)
let farmStoredEventParsed
if (farmStoredEvent.isV9) {
farmStoredEventParsed = farmStoredEvent.asV9
} else if (farmStoredEvent.isV50) {
farmStoredEventParsed = farmStoredEvent.asV50
} else if (farmStoredEvent.isV63) {
farmStoredEventParsed = farmStoredEvent.asV63
} else if (farmStoredEvent.isV101) {
let eventValue = item.event.args as v63.Farm
eventValue.dedicatedFarm = false
farmStoredEventParsed = farmStoredEvent.asV101
}
if (!farmStoredEventParsed) return
const newFarm = new Farm()
newFarm.id = item.event.id
newFarm.gridVersion = farmStoredEventParsed.version
newFarm.farmID = farmStoredEventParsed.id
newFarm.name = validateString(ctx, farmStoredEventParsed.name.toString())
newFarm.twinID = farmStoredEventParsed.twinId
newFarm.pricingPolicyID = farmStoredEventParsed.pricingPolicyId
newFarm.dedicatedFarm = false
newFarm.certification = FarmCertification.NotCertified
newFarm.publicIPs = []
await ctx.store.save<Farm>(newFarm)
const ipPromises = farmStoredEventParsed.publicIps.map(ip => {
const newIP = new PublicIp()
newIP.id = item.event.id
newIP.ip = validateString(ctx, ip.ip.toString())
newIP.gateway = validateString(ctx, ip.gateway.toString())
newIP.contractId = ip.contractId
newIP.farm = newFarm
newFarm.publicIPs?.push(newIP)
return ctx.store.save<PublicIp>(newIP)
})
await Promise.all(ipPromises)
await ctx.store.save<Farm>(newFarm)
}
export async function farmUpdated(
ctx: Ctx,
item: EventItem<'TfgridModule.FarmUpdated', { event: { args: true } }>
) {
const farmUpdatedEvent = new TfgridModuleFarmUpdatedEvent(ctx, item.event)
let certification = FarmCertification.NotCertified
let farmUpdatedEventParsed
if (farmUpdatedEvent.isV9) {
farmUpdatedEventParsed = farmUpdatedEvent.asV9
} else if (farmUpdatedEvent.isV50) {
farmUpdatedEventParsed = farmUpdatedEvent.asV50
} else if (farmUpdatedEvent.isV63) {
farmUpdatedEventParsed = farmUpdatedEvent.asV63
switch (farmUpdatedEvent.asV101.certification.__kind) {
case "Gold": {
certification = FarmCertification.Gold
}
}
} else if (farmUpdatedEvent.isV101) {
let eventValue = item.event.args as v63.Farm
eventValue.dedicatedFarm = false
farmUpdatedEventParsed = farmUpdatedEvent.asV101
switch (farmUpdatedEvent.asV101.certification.__kind) {
case "Gold": {
certification = FarmCertification.Gold
}
}
}
if (!farmUpdatedEventParsed) return
const savedFarm = await ctx.store.get(Farm, { where: { farmID: farmUpdatedEventParsed.id } })
if (!savedFarm) return
savedFarm.gridVersion = farmUpdatedEventParsed.version
savedFarm.name = validateString(ctx, farmUpdatedEventParsed.name.toString())
savedFarm.twinID = farmUpdatedEventParsed.twinId
savedFarm.pricingPolicyID = farmUpdatedEventParsed.pricingPolicyId
savedFarm.certification = certification
let eventPublicIPs = farmUpdatedEventParsed.publicIps
await farmUpdatedEventParsed.publicIps.forEach(async ip => {
if (ip.ip.toString().indexOf('\x00') >= 0) {
return
}
const savedIP = await ctx.store.get(PublicIp, { where: { ip: ip.ip.toString() }, relations: { farm: true } })
// ip is already there in storage, don't save it again
if (savedIP) {
savedIP.ip = validateString(ctx, ip.ip.toString()) // not effective, but for since we already check for \x00
savedIP.gateway = validateString(ctx, ip.gateway.toString())
await ctx.store.save<PublicIp>(savedIP)
} else {
const newIP = new PublicIp()
newIP.id = item.event.id
newIP.ip = validateString(ctx, ip.ip.toString())
newIP.gateway = validateString(ctx, ip.gateway.toString())
newIP.contractId = ip.contractId
newIP.farm = savedFarm
await ctx.store.save<PublicIp>(newIP)
if (!savedFarm.publicIPs) {
savedFarm.publicIPs = []
}
savedFarm.publicIPs.push(newIP)
}
})
await ctx.store.save<Farm>(savedFarm)
const publicIPsOfFarm = await ctx.store.find<PublicIp>(PublicIp, { where: { farm: { id: savedFarm.id } }, relations: { farm: true } })
publicIPsOfFarm.forEach(async ip => {
if (eventPublicIPs.filter(eventIp => validateString(ctx, eventIp.ip.toString()) === ip.ip).length === 0) {
// IP got removed from farm
await ctx.store.remove<PublicIp>(ip)
}
})
let farm = item.event.args as Farm
if (farm.dedicatedFarm) {
savedFarm.dedicatedFarm = farm.dedicatedFarm
await ctx.store.save<Farm>(savedFarm)
}
}
export async function farmDeleted(
ctx: Ctx,
item: EventItem<'TfgridModule.FarmDeleted', { event: { args: true } }>
) {
const farmID = new TfgridModuleFarmDeletedEvent(ctx, item.event).asV49
const savedFarm = await ctx.store.get(Farm, { where: { farmID: farmID } })
if (savedFarm) {
await ctx.store.remove(savedFarm)
}
}
export async function farmPayoutV2AddressRegistered(
ctx: Ctx,
item: EventItem<'TfgridModule.FarmPayoutV2AddressRegistered', { event: { args: true } }>
) {
const [farmID, stellarAddress] = new TfgridModuleFarmPayoutV2AddressRegisteredEvent(ctx, item.event).asV49
const savedFarm = await ctx.store.get(Farm, { where: { farmID: farmID } })
if (savedFarm) {
let address = ''
if (!stellarAddress.includes(0)) {
address = validateString(ctx, stellarAddress.toString())
}
savedFarm.stellarAddress = address
await ctx.store.save<Farm>(savedFarm)
}
}
export async function farmCertificationSet(
ctx: Ctx,
item: EventItem<'TfgridModule.FarmCertificationSet', { event: { args: true } }>
) {
const [farmID, certification] = new TfgridModuleFarmCertificationSetEvent(ctx, item.event).asV63
const savedFarm = await ctx.store.get(Farm, { where: { farmID: farmID } })
if (!savedFarm) {
return
}
let certType = FarmCertification.NotCertified
switch (certification.__kind.toString()) {
case 'NotCertified':
certType = FarmCertification.NotCertified
break
case 'Gold':
certType = FarmCertification.Gold
break
}
savedFarm.certification = certType
await ctx.store.save<Farm>(savedFarm)
}
export async function farmCreateOrUpdateOrDelete(ctx: Ctx): Promise<[Farm[], Farm[], Farm[], FarmWithIPs[]]> {
let newFarms: Farm[] = []
let updatedFarms: Farm[] = []
let deletedFarms: Farm[] = []
let publicIPs: FarmWithIPs[] = []
for (let block of ctx.blocks) {
for (let item of block.items) {
if (item.name === "TfgridModule.FarmStored") {
const farmStoredEvent = new TfgridModuleFarmStoredEvent(ctx, item.event)
let farmStoredEventParsed
if (farmStoredEvent.isV9) {
farmStoredEventParsed = farmStoredEvent.asV9
} else if (farmStoredEvent.isV50) {
farmStoredEventParsed = farmStoredEvent.asV50
} else if (farmStoredEvent.isV63) {
farmStoredEventParsed = farmStoredEvent.asV63
} else if (farmStoredEvent.isV101) {
farmStoredEventParsed = farmStoredEvent.asV101
}
if (!farmStoredEventParsed) continue
const newFarm = new Farm()
let eventID = item.event.id
newFarm.id = eventID
newFarm.gridVersion = farmStoredEventParsed.version
newFarm.farmID = farmStoredEventParsed.id
newFarm.name = validateString(ctx, farmStoredEventParsed.name.toString())
newFarm.twinID = farmStoredEventParsed.twinId
newFarm.pricingPolicyID = farmStoredEventParsed.pricingPolicyId
newFarm.dedicatedFarm = false
newFarm.certification = FarmCertification.NotCertified
newFarm.publicIPs = []
let ips: PublicIp[] = []
farmStoredEventParsed.publicIps.forEach(ip => {
// ctx.log.info("storing ips")
const newIP = new PublicIp()
newIP.id = eventID
newIP.ip = validateString(ctx, ip.ip.toString())
newIP.gateway = validateString(ctx, ip.gateway.toString())
newIP.contractId = ip.contractId
newIP.farm = newFarm
ips.push(newIP)
})
// ctx.log.info(`storing farm: ${newFarm.id}`)
newFarms.push(newFarm)
publicIPs.push(new FarmWithIPs(newFarm.farmID, ips))
}
if (item.name === "TfgridModule.FarmUpdated") {
const farmUpdatedEvent = new TfgridModuleFarmUpdatedEvent(ctx, item.event)
let farmUpdatedEventParsed: any
if (farmUpdatedEvent.isV9) {
farmUpdatedEventParsed = farmUpdatedEvent.asV9
} else if (farmUpdatedEvent.isV50) {
farmUpdatedEventParsed = farmUpdatedEvent.asV50
} else if (farmUpdatedEvent.isV63) {
farmUpdatedEventParsed = farmUpdatedEvent.asV63
} else if (farmUpdatedEvent.isV101) {
farmUpdatedEventParsed = farmUpdatedEvent.asV101
}
if (!farmUpdatedEventParsed) {
ctx.log.error('cannot parse farm updated event')
continue
}
const eventID = item.event.id
const foundInNewListIndex: number = newFarms.findIndex(t => t.farmID == farmUpdatedEventParsed.id)
if (foundInNewListIndex != -1) {
const savedFarm: Farm = newFarms[foundInNewListIndex]
savedFarm.gridVersion = farmUpdatedEventParsed.version
savedFarm.name = validateString(ctx, farmUpdatedEventParsed.name.toString())
savedFarm.twinID = farmUpdatedEventParsed.twinId
savedFarm.pricingPolicyID = farmUpdatedEventParsed.pricingPolicyId
// const pubIps = updatePublicIPs(ctx, farmUpdatedEventParsed.publicIps, eventID, savedFarm)
newFarms[foundInNewListIndex] = savedFarm
publicIPs = getPublicIPs(ctx, savedFarm, publicIPs, farmUpdatedEventParsed.publicIps, eventID)
continue
}
const foundInUpdatedListIndex: number = updatedFarms.findIndex(t => t.farmID == farmUpdatedEventParsed.id)
if (foundInUpdatedListIndex != -1) {
let savedFarm: Farm = updatedFarms[foundInUpdatedListIndex]
savedFarm.gridVersion = farmUpdatedEventParsed.version
savedFarm.name = validateString(ctx, farmUpdatedEventParsed.name.toString())
savedFarm.twinID = farmUpdatedEventParsed.twinId
savedFarm.pricingPolicyID = farmUpdatedEventParsed.pricingPolicyId
updatedFarms[foundInUpdatedListIndex] = savedFarm
publicIPs = getPublicIPs(ctx, savedFarm, publicIPs, farmUpdatedEventParsed.publicIps, eventID)
continue
}
const savedFarm = await ctx.store.get(Farm, { where: { farmID: farmUpdatedEventParsed.id } })
if (!savedFarm) continue
savedFarm.gridVersion = farmUpdatedEventParsed.version
savedFarm.name = validateString(ctx, farmUpdatedEventParsed.name.toString())
savedFarm.twinID = farmUpdatedEventParsed.twinId
savedFarm.pricingPolicyID = farmUpdatedEventParsed.pricingPolicyId
// ctx.log.info(`updating farm: ${savedFarm.id}`)
publicIPs = getPublicIPs(ctx, savedFarm, publicIPs, farmUpdatedEventParsed.publicIps, eventID)
updatedFarms.push(savedFarm)
}
if (item.name === "TfgridModule.FarmDeleted") {
const farmID = new TfgridModuleFarmDeletedEvent(ctx, item.event).asV49
const savedFarm = await ctx.store.get(Farm, { where: { farmID: farmID } })
if (savedFarm) {
deletedFarms.push(savedFarm)
}
}
}
}
return [newFarms, updatedFarms, deletedFarms, publicIPs]
}
function getPublicIPs(ctx: Ctx, farm: Farm, savedFarmIps: FarmWithIPs[], newIps: PublicIp[], eventID: any): FarmWithIPs[] {
let toModify = savedFarmIps.filter(f => f.farmID === farm.farmID)
if (toModify.length === 0) {
return []
}
// For every IP in the updated event:
// Check if it's already in the savedFarmIps to be saved, if so, update the value
// If it's not there, add it
newIps.forEach((ip: PublicIp) => {
let foundIdx = toModify[0].publicIPs.findIndex(pubip => pubip.ip === ip.ip.toString())
// console.log(`found index: ${foundIdx}`)
if (foundIdx !== -1) {
toModify[0].publicIPs[foundIdx].contractId = ip.contractId
toModify[0].publicIPs[foundIdx].ip = validateString(ctx, ip.ip.toString())
toModify[0].publicIPs[foundIdx].gateway = validateString(ctx, ip.gateway.toString())
} else {
const newIP = new PublicIp()
newIP.id = eventID
newIP.ip = ip.ip.toString()
newIP.gateway = validateString(ctx, ip.gateway.toString())
newIP.contractId = ip.contractId
newIP.farm = farm
// ctx.log.info(`saving new ip: ${newIP.ip}`)
// ctx.log.warn(`farm ips: ${toModify[0].publicIPs}`)
toModify[0].publicIPs.push(newIP)
}
})
return savedFarmIps
}