diff --git a/package-lock.json b/package-lock.json index 933a9ff..b94e3d0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "graphql_tfgrid", - "version": "2.5.1", + "version": "2.5.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7b1f804..e7a48dc 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "graphql_tfgrid", "private": "true", - "version": "2.5.1", + "version": "2.5.2", "description": "GraphQL server and Substrate indexer. Generated with ♥ by Hydra-CLI", "author": "", "license": "ISC", diff --git a/processor-chart/Chart.yaml b/processor-chart/Chart.yaml index a56e6f4..e462188 100644 --- a/processor-chart/Chart.yaml +++ b/processor-chart/Chart.yaml @@ -2,4 +2,4 @@ apiVersion: v2 name: tfchain-processor description: A chart for the tfchain graphql processor and query node version: 1.0.4 -appVersion: "2.5.1" +appVersion: "2.5.2" diff --git a/src/mappings/nodes.ts b/src/mappings/nodes.ts index 7b1fa0a..eabc0e5 100644 --- a/src/mappings/nodes.ts +++ b/src/mappings/nodes.ts @@ -5,7 +5,7 @@ import { Node, Location, PublicConfig, NodeCertification, Interfaces, UptimeEven import { SmartContractModuleNodeMarkedAsDedicatedEvent, TfgridModuleNodeCertificationSetEvent, TfgridModuleNodeDeletedEvent, TfgridModuleNodePublicConfigStoredEvent, TfgridModuleNodeStoredEvent, TfgridModuleNodeUpdatedEvent, TfgridModuleNodeUptimeReportedEvent } from "../types/events"; export async function nodeStored(ctx: EventHandlerContext) { - const node = new TfgridModuleNodeStoredEvent(ctx) + const node = new TfgridModuleNodeStoredEvent(ctx) let nodeEvent if (node.isV9) { nodeEvent = node.asV9 @@ -18,9 +18,9 @@ export async function nodeStored(ctx: EventHandlerContext) { } else if (node.isV101) { nodeEvent = node.asV101 } - + if (!nodeEvent) return - + const newNode = new Node() newNode.id = ctx.event.id newNode.gridVersion = nodeEvent.version @@ -51,13 +51,13 @@ export async function nodeStored(ctx: EventHandlerContext) { const certificationTypeAsString = nodeAsV28.certificationType.__kind.toString() let certType = NodeCertification.Diy switch (certificationTypeAsString) { - case 'Diy': + case 'Diy': certType = NodeCertification.Diy - break - case 'Certified': + break + case 'Certified': certType = NodeCertification.Certified - break - } + break + } newNode.certification = certType } else { newNode.certification = NodeCertification.Diy @@ -65,7 +65,7 @@ export async function nodeStored(ctx: EventHandlerContext) { } if (node.isV43) { - const nodeAsV43 = node.asV43 + const nodeAsV43 = node.asV43 newNode.secure = nodeAsV43.secureBoot ? true : false newNode.virtualized = nodeAsV43.virtualized ? true : false newNode.serialNumber = nodeAsV43.serialNumber.toString() @@ -73,13 +73,13 @@ export async function nodeStored(ctx: EventHandlerContext) { const certificationTypeAsString = nodeAsV43.certificationType.__kind.toString() let certType = NodeCertification.Diy switch (certificationTypeAsString) { - case 'Diy': + case 'Diy': certType = NodeCertification.Diy - break - case 'Certified': + break + case 'Certified': certType = NodeCertification.Certified - break - } + break + } newNode.certification = certType } else { newNode.certification = NodeCertification.Diy @@ -103,13 +103,13 @@ export async function nodeStored(ctx: EventHandlerContext) { const certificationTypeAsString = nodeEvent.certification.__kind.toString() let certType = NodeCertification.Diy switch (certificationTypeAsString) { - case 'Diy': + case 'Diy': certType = NodeCertification.Diy - break - case 'Certified': + break + case 'Certified': certType = NodeCertification.Certified - break - } + break + } newNode.certification = certType } else { newNode.certification = NodeCertification.Diy @@ -160,7 +160,7 @@ export async function nodeStored(ctx: EventHandlerContext) { } export async function nodeUpdated(ctx: EventHandlerContext) { - const node = new TfgridModuleNodeUpdatedEvent(ctx) + const node = new TfgridModuleNodeUpdatedEvent(ctx) let nodeEvent if (node.isV9) { @@ -213,13 +213,13 @@ export async function nodeUpdated(ctx: EventHandlerContext) { const certificationTypeAsString = nodeAsV28.certificationType.__kind.toString() let certType = NodeCertification.Diy switch (certificationTypeAsString) { - case 'Diy': + case 'Diy': certType = NodeCertification.Diy - break - case 'Certified': + break + case 'Certified': certType = NodeCertification.Certified - break - } + break + } savedNode.certification = certType } else { savedNode.certification = NodeCertification.Diy @@ -227,7 +227,7 @@ export async function nodeUpdated(ctx: EventHandlerContext) { } if (node.isV43) { - const nodeAsV43 = node.asV43 + const nodeAsV43 = node.asV43 savedNode.secure = nodeAsV43.secureBoot ? true : false savedNode.virtualized = nodeAsV43.virtualized ? true : false savedNode.serialNumber = nodeAsV43.serialNumber.toString() @@ -235,13 +235,13 @@ export async function nodeUpdated(ctx: EventHandlerContext) { const certificationTypeAsString = nodeAsV43.certificationType.__kind.toString() let certType = NodeCertification.Diy switch (certificationTypeAsString) { - case 'Diy': + case 'Diy': certType = NodeCertification.Diy - break - case 'Certified': + break + case 'Certified': certType = NodeCertification.Certified - break - } + break + } savedNode.certification = certType } else { savedNode.certification = NodeCertification.Diy @@ -264,12 +264,12 @@ export async function nodeUpdated(ctx: EventHandlerContext) { const certificationTypeAsString = nodeEvent.certification.__kind.toString() let certType = NodeCertification.Diy switch (certificationTypeAsString) { - case 'Diy': + case 'Diy': certType = NodeCertification.Diy - break - case 'Certified': + break + case 'Certified': certType = NodeCertification.Certified - break + break } savedNode.certification = certType } else { @@ -289,20 +289,20 @@ export async function nodeUpdated(ctx: EventHandlerContext) { // if an interface with same name exists const found = savedNode.interfaces.findIndex(interf => interf.name === intf.name.toString()) if (found > 0) { - newInterface = savedNode.interfaces[found] + newInterface = savedNode.interfaces[found] } else { newInterface = new Interfaces() newInterface.id = ctx.event.id newInterface.node = savedNode } } - + if (!newInterface) return newInterface.name = intf.name.toString() newInterface.mac = intf.mac.toString() newInterface.ips = intf.ips.map(ip => ip.toString()).join(',') - + await ctx.store.save(newInterface) savedNode.interfaces.push(newInterface) }) @@ -314,7 +314,7 @@ export async function nodeDeleted(ctx: EventHandlerContext) { const nodeID = new TfgridModuleNodeDeletedEvent(ctx).asV9 const savedNode = await ctx.store.get(Node, { where: { nodeID: nodeID } }) - + if (savedNode) { const resourcesTotal = await ctx.store.get(NodeResourcesTotal, { where: { node: savedNode } }) if (resourcesTotal) { @@ -325,7 +325,7 @@ export async function nodeDeleted(ctx: EventHandlerContext) { await ctx.store.remove(pubConfig) } - const intfs = await ctx.store.find(Interfaces, { where: { node: savedNode }}) + const intfs = await ctx.store.find(Interfaces, { where: { node: savedNode } }) const promises = intfs.map(intf => { return ctx.store.remove(intf) }) @@ -365,26 +365,55 @@ export async function nodeUptimeReported(ctx: EventHandlerContext) { } export async function nodePublicConfigStored(ctx: EventHandlerContext) { - const [nodeID, config] = new TfgridModuleNodePublicConfigStoredEvent(ctx).asV12 + const storedEvent = new TfgridModuleNodePublicConfigStoredEvent(ctx) - const savedNode = await ctx.store.get(Node, { where: { nodeID: nodeID } }) - if (!savedNode) return + let nodeID, config + if (storedEvent.isV12) { + nodeID = storedEvent.asV12[0] + config = storedEvent.asV12[1] - let publicConfig = await ctx.store.get(PublicConfig, { where: { node: savedNode }}) + const savedNode = await ctx.store.get(Node, { where: { nodeID: nodeID } }) + if (!savedNode) return - if (!publicConfig) { - publicConfig = new PublicConfig() - publicConfig.id = ctx.event.id - publicConfig.node = savedNode - } + let publicConfig = await ctx.store.get(PublicConfig, { where: { node: savedNode } }) + + if (!publicConfig) { + publicConfig = new PublicConfig() + publicConfig.id = ctx.event.id + publicConfig.node = savedNode + } + + publicConfig.ipv4 = config.ipv4.toString() + publicConfig.ipv6 = config.ipv6.toString() + publicConfig.gw4 = config.gw4.toString() + publicConfig.gw6 = config.gw6.toString() + publicConfig.domain = config.domain.toString() || '' - publicConfig.ipv4 = config.ipv4.toString() - publicConfig.ipv6 = config.ipv6.toString() - publicConfig.gw4 = config.gw4.toString() - publicConfig.gw6 = config.gw6.toString() - publicConfig.domain = config.domain.toString() || '' + await ctx.store.save(publicConfig) - await ctx.store.save(publicConfig) + } else if (storedEvent.isV105) { + nodeID = storedEvent.asV105[0] + config = storedEvent.asV105[1] + + const savedNode = await ctx.store.get(Node, { where: { nodeID: nodeID } }) + if (!savedNode) return + + let publicConfig = await ctx.store.get(PublicConfig, { where: { node: savedNode } }) + + if (!publicConfig) { + publicConfig = new PublicConfig() + publicConfig.id = ctx.event.id + publicConfig.node = savedNode + } + + publicConfig.ipv4 = config?.ip4.ip.toString() + publicConfig.ipv6 = config?.ip6?.ip.toString() + publicConfig.gw4 = config?.ip4.gw.toString() + publicConfig.gw6 = config?.ip6?.gw.toString() + publicConfig.domain = config?.domain ? config.domain.toString() : '' + + await ctx.store.save(publicConfig) + } } // export async function nodeMarkedAsDedicated(ctx: EventHandlerContext) { @@ -405,12 +434,12 @@ export async function nodeCertificationSet(ctx: EventHandlerContext) { let certType = NodeCertification.Diy switch (certification.__kind.toString()) { - case 'Diy': + case 'Diy': certType = NodeCertification.Diy - break - case 'Certified': + break + case 'Certified': certType = NodeCertification.Certified - break + break } savedNode.certification = certType diff --git a/src/types/events.ts b/src/types/events.ts index 21446a5..e0f6bd3 100644 --- a/src/types/events.ts +++ b/src/types/events.ts @@ -1,5 +1,5 @@ import assert from 'assert' -import {EventContext, Result, deprecateLatest} from './support' +import { EventContext, Result, deprecateLatest } from './support' import * as v9 from './v9' import * as v12 from './v12' import * as v25 from './v25' @@ -37,14 +37,14 @@ export class BalancesTransferEvent { /** * Transfer succeeded. */ - get isV101(): boolean { + get isV101(): boolean { return this.ctx._chain.getEventHash('balances.Transfer') === '0ffdf35c495114c2d42a8bf6c241483fd5334ca0198662e14480ad040f1e3a66' } /** * Transfer succeeded. */ - get asV101(): {from: AccountId32, to: AccountId32, amount: bigint} { + get asV101(): { from: AccountId32, to: AccountId32, amount: bigint } { assert(this.isV101) return this.ctx._chain.decodeEvent(this.ctx.event) } @@ -54,7 +54,7 @@ export class BalancesTransferEvent { return this.isV101 } - get asLatest(): {from: AccountId32, to: AccountId32, amount: bigint} { + get asLatest(): { from: AccountId32, to: AccountId32, amount: bigint } { deprecateLatest() return this.asV101 } @@ -261,7 +261,7 @@ export class SmartContractModuleContractGracePeriodEndedEvent { /** * A Contract grace period was ended */ - get asV105(): {contractId: bigint, nodeId: number, twinId: number} { + get asV105(): { contractId: bigint, nodeId: number, twinId: number } { assert(this.isV105) return this.ctx._chain.decodeEvent(this.ctx.event) } @@ -271,7 +271,7 @@ export class SmartContractModuleContractGracePeriodEndedEvent { return this.isV105 } - get asLatest(): {contractId: bigint, nodeId: number, twinId: number} { + get asLatest(): { contractId: bigint, nodeId: number, twinId: number } { deprecateLatest() return this.asV105 } @@ -301,7 +301,7 @@ export class SmartContractModuleContractGracePeriodStartedEvent { /** * A Contract grace period is triggered */ - get asV105(): {contractId: bigint, nodeId: number, twinId: number, blockNumber: bigint} { + get asV105(): { contractId: bigint, nodeId: number, twinId: number, blockNumber: bigint } { assert(this.isV105) return this.ctx._chain.decodeEvent(this.ctx.event) } @@ -311,7 +311,7 @@ export class SmartContractModuleContractGracePeriodStartedEvent { return this.isV105 } - get asLatest(): {contractId: bigint, nodeId: number, twinId: number, blockNumber: bigint} { + get asLatest(): { contractId: bigint, nodeId: number, twinId: number, blockNumber: bigint } { deprecateLatest() return this.asV105 } @@ -341,7 +341,7 @@ export class SmartContractModuleNameContractCanceledEvent { /** * A Name contract is canceled */ - get asV105(): {contractId: bigint} { + get asV105(): { contractId: bigint } { assert(this.isV105) return this.ctx._chain.decodeEvent(this.ctx.event) } @@ -351,7 +351,7 @@ export class SmartContractModuleNameContractCanceledEvent { return this.isV105 } - get asLatest(): {contractId: bigint} { + get asLatest(): { contractId: bigint } { deprecateLatest() return this.asV105 } @@ -381,7 +381,7 @@ export class SmartContractModuleNodeContractCanceledEvent { /** * A Node contract is canceled */ - get asV105(): {contractId: bigint, nodeId: number, twinId: number} { + get asV105(): { contractId: bigint, nodeId: number, twinId: number } { assert(this.isV105) return this.ctx._chain.decodeEvent(this.ctx.event) } @@ -391,7 +391,7 @@ export class SmartContractModuleNodeContractCanceledEvent { return this.isV105 } - get asLatest(): {contractId: bigint, nodeId: number, twinId: number} { + get asLatest(): { contractId: bigint, nodeId: number, twinId: number } { deprecateLatest() return this.asV105 } @@ -824,14 +824,23 @@ export class TfgridModuleNodePublicConfigStoredEvent { return this.ctx._chain.decodeEvent(this.ctx.event) } + get isV105(): boolean { + return this.ctx._chain.getEventHash('tfgridModule.NodePublicConfigStored') === '3280822d064c517c372255a87e0f164783d75d41adc342fe0475179b687a0ad8' + } + + get asV105(): [number, (v105.PublicConfig | undefined)] { + assert(this.isV105) + return this.ctx._chain.decodeEvent(this.ctx.event) + } + get isLatest(): boolean { deprecateLatest() - return this.isV12 + return this.isV105 } - get asLatest(): [number, v12.PublicConfig] { + get asLatest(): [number, (v105.PublicConfig | undefined)] { deprecateLatest() - return this.asV12 + return this.asV105 } } @@ -1251,7 +1260,7 @@ export class SmartContractModuleRentContractCanceledEvent { /** * a Rent contract is canceled */ - get asV105(): {contractId: bigint} { + get asV105(): { contractId: bigint } { assert(this.isV105) return this.ctx._chain.decodeEvent(this.ctx.event) } @@ -1261,7 +1270,7 @@ export class SmartContractModuleRentContractCanceledEvent { return this.isV105 } - get asLatest(): {contractId: bigint} { + get asLatest(): { contractId: bigint } { deprecateLatest() return this.asV105 } diff --git a/src/types/v105.ts b/src/types/v105.ts index 23c999c..f07cdcf 100644 --- a/src/types/v105.ts +++ b/src/types/v105.ts @@ -1,4 +1,4 @@ -import type {Result} from './support' +import type { Result } from './support' export interface Contract { version: number @@ -88,3 +88,119 @@ export interface PublicIP { gateway: Uint8Array contractId: bigint } + +export interface Contract { + version: number + state: ContractState + contractId: bigint + twinId: number + contractType: ContractData + solutionProviderId: (bigint | undefined) +} + +export interface PublicConfig { + ip4: IP + ip6: (IP | undefined) + domain: (Domain | undefined) +} + +export interface Node { + version: number + id: number + farmId: number + twinId: number + resources: Resources + location: Location + country: Uint8Array + city: Uint8Array + publicConfig: (PublicConfig | undefined) + created: bigint + farmingPolicyId: number + interfaces: Interface[] + certification: NodeCertification + secureBoot: boolean + virtualized: boolean + serialNumber: Uint8Array + connectionPrice: number +} + +export interface IP { + ip: IP4 + gw: GW4 +} + +export type Domain = Uint8Array + +export interface Resources { + hru: bigint + sru: bigint + cru: bigint + mru: bigint +} + +export interface Location { + longitude: Uint8Array + latitude: Uint8Array +} + +export interface Interface { + name: InterfaceName + mac: InterfaceMac + ips: InterfaceIp[] +} + +export type NodeCertification = NodeCertification_Diy | NodeCertification_Certified + +export interface NodeCertification_Diy { + __kind: 'Diy' +} + +export interface NodeCertification_Certified { + __kind: 'Certified' +} + +export interface Cause_CanceledByUser { + __kind: 'CanceledByUser' +} + +export interface Cause_OutOfFunds { + __kind: 'OutOfFunds' +} + +export interface NodeContract { + nodeId: number + deploymentHash: H256 + deploymentData: Uint8Array + publicIps: number + publicIpsList: PublicIP[] +} + +export interface NameContract { + name: NameContractName +} + +export interface RentContract { + nodeId: number +} + +export type IP4 = Uint8Array + +export type GW4 = Uint8Array + +export type InterfaceName = Uint8Array + +export type InterfaceMac = Uint8Array + +export type InterfaceIp = Uint8Array + +export type H256 = Uint8Array + +export interface PublicIP { + ip: Uint8Array + gateway: GatewayIP + contractId: bigint +} + +export type NameContractName = Uint8Array + +export type GatewayIP = Uint8Array