Skip to content

Commit e7cd7e2

Browse files
authored
Merge pull request #213 from threefoldtech/master-cleanup-improvements
cleanup: remove dead code, unused imports, and extract certification helpers
2 parents 82f7404 + 8ff1cc7 commit e7cd7e2

6 files changed

Lines changed: 37 additions & 244 deletions

File tree

src/mappings/contracts.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -394,38 +394,6 @@ export function collectContractBillReports(ctx: Ctx): ContractBillReport[] {
394394
return list
395395
}
396396

397-
export async function contractBilled(
398-
ctx: Ctx,
399-
item: EventItem<'SmartContractModule.ContractBilled', { event: { args: true } }>,
400-
) {
401-
const contract_billed_event = new SmartContractModuleContractBilledEvent(ctx, item.event).asV9
402-
403-
const newContractBilledReport = new ContractBillReport()
404-
405-
newContractBilledReport.id = item.event.id
406-
newContractBilledReport.contractID = contract_billed_event.contractId
407-
408-
let level = DiscountLevel.None
409-
switch (contract_billed_event.discountLevel.__kind) {
410-
case 'None': break
411-
case 'Default':
412-
level = DiscountLevel.Default
413-
break
414-
case 'Bronze':
415-
level = DiscountLevel.Bronze
416-
break
417-
case 'Silver':
418-
level = DiscountLevel.Silver
419-
break
420-
case 'Gold': level = DiscountLevel.Gold
421-
}
422-
newContractBilledReport.discountReceived = level
423-
newContractBilledReport.amountBilled = contract_billed_event.amountBilled
424-
newContractBilledReport.timestamp = contract_billed_event.timestamp
425-
426-
await ctx.store.save<ContractBillReport>(newContractBilledReport)
427-
}
428-
429397
export async function contractUpdateUsedResources(
430398
ctx: Ctx,
431399
item: EventItem<'SmartContractModule.UpdatedUsedResources', { event: { args: true } }>,

src/mappings/farms.ts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import { validateString } from "./nodes"
88

99
import * as ipaddr from 'ipaddr.js';
1010

11+
export function parseFarmCertification(kind: string): FarmCertification {
12+
switch (kind) {
13+
case 'Gold': return FarmCertification.Gold
14+
default: return FarmCertification.NotCertified
15+
}
16+
}
17+
1118
export class FarmWithIPs {
1219
constructor(farmID: number, ips: PublicIp[]) {
1320
this.farmID = farmID
@@ -58,8 +65,6 @@ export async function farmStored(
5865
: false
5966
newFarm.certification = FarmCertification.NotCertified
6067

61-
newFarm.publicIPs = []
62-
6368
await ctx.store.save<Farm>(newFarm)
6469

6570
const ipPromises = farmStoredEventParsed.publicIps.map((ip, index) => {
@@ -146,11 +151,7 @@ export async function farmUpdated(
146151
const rawArgs = item.event.args as any
147152
rawArgs.dedicatedFarm = rawArgs['dedicatedFarm:'] ?? rawArgs.dedicatedFarm ?? false
148153
farmUpdatedEventParsed = farmUpdatedEvent.asV63
149-
switch (farmUpdatedEvent.asV63.certification.__kind) {
150-
case "Gold": {
151-
certification = FarmCertification.Gold
152-
}
153-
}
154+
certification = parseFarmCertification(farmUpdatedEvent.asV63.certification.__kind)
154155
}
155156

156157
if (!farmUpdatedEventParsed) {
@@ -196,10 +197,6 @@ export async function farmUpdated(
196197
newIP.contractId = ip.contractId
197198
newIP.farm = savedFarm
198199
await ctx.store.save<PublicIp>(newIP)
199-
if (!savedFarm.publicIPs) {
200-
savedFarm.publicIPs = []
201-
}
202-
savedFarm.publicIPs.push(newIP)
203200
ctx.log.debug({ eventName: item.name, ip: ip.ip.toString() }, `PublicIP: ${ip.ip.toString()} added with farm id: ${savedFarm.farmID}`);
204201
}
205202
}
@@ -267,16 +264,6 @@ export async function farmCertificationSet(
267264
return
268265
}
269266

270-
let certType = FarmCertification.NotCertified
271-
switch (certification.__kind.toString()) {
272-
case 'NotCertified':
273-
certType = FarmCertification.NotCertified
274-
break
275-
case 'Gold':
276-
certType = FarmCertification.Gold
277-
break
278-
}
279-
280-
savedFarm.certification = certType
267+
savedFarm.certification = parseFarmCertification(certification.__kind.toString())
281268
await ctx.store.save<Farm>(savedFarm)
282269
}

src/mappings/nodes.ts

Lines changed: 20 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ import { PublicConfig as V105PublicConfig } from '../types/v105'
1313

1414
import { Ctx } from '../processor'
1515
import assert from "assert";
16-
import { allowedNodeEnvironmentFlags } from "process";
16+
17+
export function parseNodeCertification(kind: string): NodeCertification {
18+
switch (kind) {
19+
case 'Certified': return NodeCertification.Certified
20+
default: return NodeCertification.Diy
21+
}
22+
}
1723

1824
export async function nodeStored(
1925
ctx: Ctx,
@@ -137,8 +143,6 @@ export async function nodeStored(
137143
resourcesTotal.cru = nodeEvent.resources.cru
138144
await ctx.store.save<NodeResourcesTotal>(resourcesTotal)
139145

140-
newNode.interfaces = []
141-
142146
const interfacesPromisses = nodeEvent.interfaces.map(async (intf, index) => {
143147
const newInterface = new Interfaces()
144148
newInterface.id = item.event.id + '-' + index
@@ -226,21 +230,9 @@ export async function nodeUpdated(
226230
savedNode.country = validateString(ctx, nodeAsV28.country.toString())
227231
savedNode.city = validateString(ctx, nodeAsV28.city.toString())
228232

229-
if (nodeAsV28.certificationType) {
230-
const certificationTypeAsString = nodeAsV28.certificationType.__kind.toString()
231-
let certType = NodeCertification.Diy
232-
switch (certificationTypeAsString) {
233-
case 'Diy':
234-
certType = NodeCertification.Diy
235-
break
236-
case 'Certified':
237-
certType = NodeCertification.Certified
238-
break
239-
}
240-
savedNode.certification = certType
241-
} else {
242-
savedNode.certification = NodeCertification.Diy
243-
}
233+
savedNode.certification = nodeAsV28.certificationType
234+
? parseNodeCertification(nodeAsV28.certificationType.__kind.toString())
235+
: NodeCertification.Diy
244236
}
245237

246238
if (node.isV43) {
@@ -250,21 +242,9 @@ export async function nodeUpdated(
250242
savedNode.secure = nodeAsV43.secureBoot ? true : false
251243
savedNode.virtualized = nodeAsV43.virtualized ? true : false
252244
savedNode.serialNumber = validateString(ctx, nodeAsV43.serialNumber.toString())
253-
if (nodeAsV43.certificationType) {
254-
const certificationTypeAsString = nodeAsV43.certificationType.__kind.toString()
255-
let certType = NodeCertification.Diy
256-
switch (certificationTypeAsString) {
257-
case 'Diy':
258-
certType = NodeCertification.Diy
259-
break
260-
case 'Certified':
261-
certType = NodeCertification.Certified
262-
break
263-
}
264-
savedNode.certification = certType
265-
} else {
266-
savedNode.certification = NodeCertification.Diy
267-
}
245+
savedNode.certification = nodeAsV43.certificationType
246+
? parseNodeCertification(nodeAsV43.certificationType.__kind.toString())
247+
: NodeCertification.Diy
268248
}
269249

270250
if (node.isV63 || node.isV105) {
@@ -281,21 +261,9 @@ export async function nodeUpdated(
281261
savedNode.secure = nodeEvent.secureBoot ? true : false
282262
savedNode.virtualized = nodeEvent.virtualized ? true : false
283263
savedNode.serialNumber = validateString(ctx, nodeEvent.serialNumber.toString())
284-
if (nodeEvent.certification) {
285-
const certificationTypeAsString = nodeEvent.certification.__kind.toString()
286-
let certType = NodeCertification.Diy
287-
switch (certificationTypeAsString) {
288-
case 'Diy':
289-
certType = NodeCertification.Diy
290-
break
291-
case 'Certified':
292-
certType = NodeCertification.Certified
293-
break
294-
}
295-
savedNode.certification = certType
296-
} else {
297-
savedNode.certification = NodeCertification.Diy
298-
}
264+
savedNode.certification = nodeEvent.certification
265+
? parseNodeCertification(nodeEvent.certification.__kind.toString())
266+
: NodeCertification.Diy
299267
}
300268

301269
if (node.isV118) {
@@ -306,21 +274,9 @@ export async function nodeUpdated(
306274
savedNode.secure = nodeEvent.secureBoot ? true : false
307275
savedNode.virtualized = nodeEvent.virtualized ? true : false
308276
savedNode.serialNumber = nodeEvent.serialNumber ? validateString(ctx, nodeEvent.serialNumber.toString()) : 'Unknown'
309-
if (nodeEvent.certification) {
310-
const certificationTypeAsString = nodeEvent.certification.__kind.toString()
311-
let certType = NodeCertification.Diy
312-
switch (certificationTypeAsString) {
313-
case 'Diy':
314-
certType = NodeCertification.Diy
315-
break
316-
case 'Certified':
317-
certType = NodeCertification.Certified
318-
break
319-
}
320-
savedNode.certification = certType
321-
} else {
322-
savedNode.certification = NodeCertification.Diy
323-
}
277+
savedNode.certification = nodeEvent.certification
278+
? parseNodeCertification(nodeEvent.certification.__kind.toString())
279+
: NodeCertification.Diy
324280
}
325281

326282
// First remove all ifs
@@ -337,7 +293,6 @@ export async function nodeUpdated(
337293
newInterface.node = savedNode
338294
await ctx.store.save<Interfaces>(newInterface)
339295

340-
savedNode.interfaces.push(newInterface)
341296
}))
342297

343298
await ctx.store.save<Node>(savedNode)
@@ -499,17 +454,7 @@ export async function nodeCertificationSet(
499454
const savedNode = await ctx.store.get(Node, { where: { nodeID: nodeID }, relations: { location: true, interfaces: true } })
500455
if (!savedNode) return
501456

502-
let certType = NodeCertification.Diy
503-
switch (certification.__kind.toString()) {
504-
case 'Diy':
505-
certType = NodeCertification.Diy
506-
break
507-
case 'Certified':
508-
certType = NodeCertification.Certified
509-
break
510-
}
511-
512-
savedNode.certification = certType
457+
savedNode.certification = parseNodeCertification(certification.__kind.toString())
513458

514459
await ctx.store.save<Node>(savedNode)
515460
}
@@ -669,8 +614,6 @@ function getNodePublicConfig(ctx: Ctx, node: TfgridModuleNodeStoredEvent): NodeP
669614
if (nodeEvent.publicConfig.domain) {
670615
domain = nodeEvent.publicConfig.domain.toString()
671616
}
672-
let h = nodeEvent.publicConfig?.ip6?.ip.toString();
673-
let r = nodeEvent.publicConfig?.ip4.ip.toString();
674617
return {
675618
ip4: validateString(ctx, nodeEvent.publicConfig?.ip4.ip.toString()),
676619
gw4: validateString(ctx, nodeEvent.publicConfig?.ip4.gw.toString()),

src/mappings/policies.ts

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
TfgridModulePricingPolicyStoredEvent, TfgridModuleFarmingPolicyStoredEvent,
1212
TfgridModuleFarmingPolicyUpdatedEvent
1313
} from "../types/events";
14-
import { validateString } from "./nodes"
14+
import { validateString, parseNodeCertification } from "./nodes"
15+
import { parseFarmCertification } from "./farms"
1516

1617
export async function pricingPolicyStored(
1718
ctx: Ctx,
@@ -109,29 +110,9 @@ export async function farmingPolicyStored(
109110
newFarmingPolicy.immutable = farmingPolicyStoredEvent.immutable
110111
newFarmingPolicy.default = farmingPolicyStoredEvent.default
111112

112-
const certificationTypeAsString = farmingPolicyStoredEvent.nodeCertification.__kind.toString()
113-
let nodeCertType = NodeCertification.Diy
114-
switch (certificationTypeAsString) {
115-
case 'Diy':
116-
nodeCertType = NodeCertification.Diy
117-
break
118-
case 'Certified':
119-
nodeCertType = NodeCertification.Certified
120-
break
121-
}
122-
newFarmingPolicy.nodeCertification = nodeCertType
123-
124-
const farmCertificationTypeAsString = farmingPolicyStoredEvent.farmCertification.__kind.toString()
125-
let farmCertType = FarmCertification.NotCertified
126-
switch (farmCertificationTypeAsString) {
127-
case 'NotCertified':
128-
farmCertType = FarmCertification.NotCertified
129-
break
130-
case 'Gold':
131-
farmCertType = FarmCertification.Gold
132-
break
133-
}
134-
newFarmingPolicy.farmCertification = farmCertType
113+
newFarmingPolicy.nodeCertification = parseNodeCertification(farmingPolicyStoredEvent.nodeCertification.__kind.toString())
114+
115+
newFarmingPolicy.farmCertification = parseFarmCertification(farmingPolicyStoredEvent.farmCertification.__kind.toString())
135116

136117
await ctx.store.save<FarmingPolicy>(newFarmingPolicy)
137118
}
@@ -165,29 +146,9 @@ export async function farmingPolicyUpdated(
165146
savedPolicy.immutable = farmingPolicyUpdatedEvent.immutable
166147
savedPolicy.default = farmingPolicyUpdatedEvent.default
167148

168-
const certificationTypeAsString = farmingPolicyUpdatedEvent.nodeCertification.__kind.toString()
169-
let nodeCertType = NodeCertification.Diy
170-
switch (certificationTypeAsString) {
171-
case 'Diy':
172-
nodeCertType = NodeCertification.Diy
173-
break
174-
case 'Certified':
175-
nodeCertType = NodeCertification.Certified
176-
break
177-
}
178-
savedPolicy.nodeCertification = nodeCertType
179-
180-
const farmCertificationTypeAsString = farmingPolicyUpdatedEvent.farmCertification.__kind.toString()
181-
let farmCertType = FarmCertification.NotCertified
182-
switch (farmCertificationTypeAsString) {
183-
case 'NotCertified':
184-
farmCertType = FarmCertification.NotCertified
185-
break
186-
case 'Gold':
187-
farmCertType = FarmCertification.Gold
188-
break
189-
}
190-
savedPolicy.farmCertification = farmCertType
149+
savedPolicy.nodeCertification = parseNodeCertification(farmingPolicyUpdatedEvent.nodeCertification.__kind.toString())
150+
151+
savedPolicy.farmCertification = parseFarmCertification(farmingPolicyUpdatedEvent.farmCertification.__kind.toString())
191152

192153
await ctx.store.save<FarmingPolicy>(savedPolicy)
193154
}

src/mappings/tftPrice.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { Store } from '@subsquid/typeorm-store'
21
import { Ctx } from '../processor'
32
import { SubstrateBlock } from '@subsquid/substrate-processor';
43
import { EventItem } from '@subsquid/substrate-processor/lib/interfaces/dataSelection'
5-
import { In } from 'typeorm'
64
import { TftPriceModulePriceStoredEvent, TftPriceModuleAveragePriceStoredEvent } from '../types/events';
75
import { PriceStored, AveragePriceStored } from '../model';
86
import { ParserFixPointFn, parseI16F16 } from '@encointer/util'

0 commit comments

Comments
 (0)