Skip to content

Commit

Permalink
Merge pull request #32 from threefoldtech/development_grace_period
Browse files Browse the repository at this point in the history
feat: support contract grace period state
  • Loading branch information
DylanVerstraete authored Jun 1, 2022
2 parents 99422cb + 60f1733 commit 5e5d267
Show file tree
Hide file tree
Showing 20 changed files with 462 additions and 24 deletions.
21 changes: 21 additions & 0 deletions db/migrations/1653490002499-graceperiod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = class graceperiod1653490002499 {
name = 'graceperiod1653490002499'

async up(db) {
await db.query(`ALTER TABLE "node_contract" DROP COLUMN "state"`)
await db.query(`ALTER TABLE "node_contract" ADD "state" character varying(11) NOT NULL`)
await db.query(`ALTER TABLE "name_contract" DROP COLUMN "state"`)
await db.query(`ALTER TABLE "name_contract" ADD "state" character varying(11) NOT NULL`)
await db.query(`ALTER TABLE "rent_contract" DROP COLUMN "state"`)
await db.query(`ALTER TABLE "rent_contract" ADD "state" character varying(11) NOT NULL`)
}

async down(db) {
await db.query(`ALTER TABLE "node_contract" ADD "state" character varying(10) NOT NULL`)
await db.query(`ALTER TABLE "node_contract" DROP COLUMN "state"`)
await db.query(`ALTER TABLE "name_contract" ADD "state" character varying(10) NOT NULL`)
await db.query(`ALTER TABLE "name_contract" DROP COLUMN "state"`)
await db.query(`ALTER TABLE "rent_contract" ADD "state" character varying(10) NOT NULL`)
await db.query(`ALTER TABLE "rent_contract" DROP COLUMN "state"`)
}
}
4 changes: 2 additions & 2 deletions indexer/chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: tfchainindexer
description: Helm Chart for the tfchain hydra indexer
version: 2.1.0
version: 2.2.0
apiVersion: v2
appVersion: "2.1.5"
appVersion: "2.2.0"
7 changes: 6 additions & 1 deletion indexer/chart/templates/indexer-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,12 @@ data:
\ \"nu\": \"Policy\",\n \"ipu\": \"Policy\",\n \"uniqueName\":
\"Policy\",\n \"domainName\": \"Policy\",\n \"foundationAccount\":
\"AccountId\",\n \"certifiedSalesAccount\": \"AccountId\",\n \"discountForDedicatedNodes\":
\"u8\"\n }\n }\n }\n ]\n }\n }\n}"
\"u8\"\n }\n }\n },\n {\n \"minmax\":
[\n 59,\n null\n ],\n \"types\": {\n \"ContractState\":
{\n \"_enum\": {\n \"Created\": null,\n \"Deleted\":
null,\n \"OutOfFunds\": null,\n \"GracePeriod\":
\"u64\"\n }\n }\n }\n }\n ]\n }\n
\ }\n}"
kind: ConfigMap
metadata:
creationTimestamp: null
Expand Down
16 changes: 16 additions & 0 deletions indexer/typesBundle.json
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,22 @@
"discountForDedicatedNodes": "u8"
}
}
},
{
"minmax": [
59,
null
],
"types": {
"ContractState": {
"_enum": {
"Created": null,
"Deleted": null,
"OutOfFunds": null,
"GracePeriod": "u64"
}
}
}
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "graphql_tfgrid",
"private": "true",
"version": "2.1.5",
"version": "2.2.0",
"description": "GraphQL server and Substrate indexer. Generated with ♥ by Hydra-CLI",
"author": "",
"license": "ISC",
Expand Down
2 changes: 1 addition & 1 deletion processor-chart/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.1.5"
appVersion: "2.2.0"
3 changes: 2 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ type RentContract @entity {
enum ContractState {
Created,
Deleted,
OutOfFunds
OutOfFunds,
GracePeriod
}

type NruConsumption @entity {
Expand Down
58 changes: 56 additions & 2 deletions src/mappings/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Store
} from "@subsquid/substrate-processor";
import { ContractState, PublicIp, NameContract, NodeContract, ContractBillReport, DiscountLevel, ContractResources, NodeResourcesTotal, Node, RentContract, Farm, NruConsumption } from "../model";
import { SmartContractModuleContractCreatedEvent, SmartContractModuleContractUpdatedEvent, SmartContractModuleNodeContractCanceledEvent, SmartContractModuleNameContractCanceledEvent, SmartContractModuleContractBilledEvent, SmartContractModuleUpdatedUsedResourcesEvent, SmartContractModuleNruConsumptionReportReceivedEvent, SmartContractModuleRentContractCanceledEvent } from "../types/events";
import { SmartContractModuleContractCreatedEvent, SmartContractModuleContractUpdatedEvent, SmartContractModuleNodeContractCanceledEvent, SmartContractModuleNameContractCanceledEvent, SmartContractModuleContractBilledEvent, SmartContractModuleUpdatedUsedResourcesEvent, SmartContractModuleNruConsumptionReportReceivedEvent, SmartContractModuleRentContractCanceledEvent, SmartContractModuleContractGracePeriodStartedEvent, SmartContractModuleContractGracePeriodEndedEvent } from "../types/events";

export async function contractCreated(ctx: EventHandlerContext) {
let contractCreatedEvent = new SmartContractModuleContractCreatedEvent(ctx)
Expand All @@ -15,6 +15,8 @@ export async function contractCreated(ctx: EventHandlerContext) {
contractEvent = contractCreatedEvent.asV25
} else if (contractCreatedEvent.isV50) {
contractEvent = contractCreatedEvent.asV50
} else if (contractCreatedEvent.isV59) {
contractEvent = contractCreatedEvent.asV59
}

if (!contractEvent) return
Expand Down Expand Up @@ -86,6 +88,8 @@ export async function contractUpdated(ctx: EventHandlerContext) {
contractEvent = contractUpdatedEvent.asV25
} else if (contractUpdatedEvent.isV50) {
contractEvent = contractUpdatedEvent.asV50
} else if (contractUpdatedEvent.isV59) {
contractEvent = contractUpdatedEvent.asV59
}

if (!contractEvent) return
Expand Down Expand Up @@ -228,7 +232,6 @@ export async function contractUpdateUsedResources(ctx: EventHandlerContext) {

const contractUsedResources = new ContractResources()


const savedContract = await ctx.store.get(NodeContract, { where: { contractID: usedResources.contractId } })
if (!savedContract) return

Expand Down Expand Up @@ -269,3 +272,54 @@ export async function nruConsumptionReportReceived(ctx: EventHandlerContext) {

await ctx.store.save<NruConsumption>(nruConsumption)
}

export async function contractGracePeriodStarted(ctx: EventHandlerContext) {
const contractGracePeriodStartedEvent = new SmartContractModuleContractGracePeriodStartedEvent(ctx).asV59

let savedContract
savedContract = await ctx.store.get(NodeContract, { where: { contractID: contractGracePeriodStartedEvent[0] } })

if (!savedContract) {
savedContract = await ctx.store.get(RentContract, { where: { contractID: contractGracePeriodStartedEvent[0] } })
if (!savedContract) {
savedContract = await ctx.store.get(NameContract, { where: { contractID: contractGracePeriodStartedEvent[0] } })
if (!savedContract) return
else {
savedContract.state = ContractState.GracePeriod
await ctx.store.save<NameContract>(savedContract)
}
} else {
savedContract.state = ContractState.GracePeriod
await ctx.store.save<RentContract>(savedContract)
}
} else {
savedContract.state = ContractState.GracePeriod
await ctx.store.save<NodeContract>(savedContract)
}
}


export async function contractGracePeriodEnded(ctx: EventHandlerContext) {
const contractGracePeriodEnded = new SmartContractModuleContractGracePeriodEndedEvent(ctx).asV59

let savedContract
savedContract = await ctx.store.get(NodeContract, { where: { contractID: contractGracePeriodEnded[0] } })

if (!savedContract) {
savedContract = await ctx.store.get(RentContract, { where: { contractID: contractGracePeriodEnded[0] } })
if (!savedContract) {
savedContract = await ctx.store.get(NameContract, { where: { contractID: contractGracePeriodEnded[0] } })
if (!savedContract) return
else {
savedContract.state = ContractState.GracePeriod
await ctx.store.save<NameContract>(savedContract)
}
} else {
savedContract.state = ContractState.GracePeriod
await ctx.store.save<RentContract>(savedContract)
}
} else {
savedContract.state = ContractState.GracePeriod
await ctx.store.save<NodeContract>(savedContract)
}
}
1 change: 1 addition & 0 deletions src/model/generated/_contractState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export enum ContractState {
Created = "Created",
Deleted = "Deleted",
OutOfFunds = "OutOfFunds",
GracePeriod = "GracePeriod",
}
2 changes: 1 addition & 1 deletion src/model/generated/nameContract.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class NameContract {
@Column_("text", {nullable: false})
name!: string

@Column_("varchar", {length: 10, nullable: false})
@Column_("varchar", {length: 11, nullable: false})
state!: ContractState

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
Expand Down
2 changes: 1 addition & 1 deletion src/model/generated/nodeContract.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class NodeContract {
@Column_("integer", {nullable: false})
numberOfPublicIPs!: number

@Column_("varchar", {length: 10, nullable: false})
@Column_("varchar", {length: 11, nullable: false})
state!: ContractState

@Index_()
Expand Down
2 changes: 1 addition & 1 deletion src/model/generated/rentContract.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class RentContract {
@Column_("integer", {nullable: false})
nodeID!: number

@Column_("varchar", {length: 10, nullable: false})
@Column_("varchar", {length: 11, nullable: false})
state!: ContractState

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
Expand Down
4 changes: 3 additions & 1 deletion src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { nodeStored, nodeUpdated, nodeDeleted, nodeUptimeReported, nodePublicCon
import { farmingPolicyStored, pricingPolicyStored } from './mappings/policies';
import { farmDeleted, farmPayoutV2AddressRegistered, farmStored, farmUpdated } from './mappings/farms';
import { entityDeleted, entityStored, entityUpdated } from './mappings/entity';
import { contractBilled, contractCreated, contractUpdated, contractUpdateUsedResources, nameContractCanceled, nodeContractCanceled, nruConsumptionReportReceived, rentContractCanceled } from './mappings/contracts';
import { contractBilled, contractCreated, contractUpdated, contractUpdateUsedResources, nameContractCanceled, nodeContractCanceled, nruConsumptionReportReceived, rentContractCanceled, contractGracePeriodStarted, contractGracePeriodEnded } from './mappings/contracts';
import { burnProcessed, mintCompleted, refundProcessed } from './mappings/bridge';

import {
Expand Down Expand Up @@ -56,6 +56,8 @@ processor.addEventHandler('smartContractModule.RentContractCanceled', ctx => ren
processor.addEventHandler('smartContractModule.ContractBilled', ctx => contractBilled(ctx));
processor.addEventHandler('smartContractModule.UpdatedUsedResources', ctx => contractUpdateUsedResources(ctx));
processor.addEventHandler('smartContractModule.NruConsumptionReportReceived', ctx => nruConsumptionReportReceived(ctx))
processor.addEventHandler('smartContractModule.ContractGracePeriodStarted', ctx => contractGracePeriodStarted(ctx))
processor.addEventHandler('smartContractModule.ContractGracePeriodEnded', ctx => contractGracePeriodEnded(ctx))

processor.addEventHandler('tftBridgeModule.MintCompleted', ctx => mintCompleted(ctx));
processor.addEventHandler('tfgridModule.BurnTransactionProcessed', ctx => burnProcessed(ctx));
Expand Down
81 changes: 75 additions & 6 deletions src/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as v43 from './v43'
import * as v49 from './v49'
import * as v50 from './v50'
import * as v51 from './v51'
import * as v59 from './v59'

export class BalancesTransferEvent {
constructor(private ctx: EventContext) {
Expand Down Expand Up @@ -97,14 +98,23 @@ export class SmartContractModuleContractCreatedEvent {
return this.ctx._chain.decodeEvent(this.ctx.event)
}

get isV59(): boolean {
return this.ctx._chain.getEventHash('smartContractModule.ContractCreated') === '5446945d28da7c765303d310f9ca55fc9bb46e7b5f281ac184521f913c430ee0'
}

get asV59(): v59.Contract {
assert(this.isV59)
return this.ctx._chain.decodeEvent(this.ctx.event)
}

get isLatest(): boolean {
deprecateLatest()
return this.isV50
return this.isV59
}

get asLatest(): v50.Contract {
get asLatest(): v59.Contract {
deprecateLatest()
return this.asV50
return this.asV59
}
}

Expand Down Expand Up @@ -140,14 +150,73 @@ export class SmartContractModuleContractUpdatedEvent {
return this.ctx._chain.decodeEvent(this.ctx.event)
}

get isV59(): boolean {
return this.ctx._chain.getEventHash('smartContractModule.ContractUpdated') === '5446945d28da7c765303d310f9ca55fc9bb46e7b5f281ac184521f913c430ee0'
}

get asV59(): v59.Contract {
assert(this.isV59)
return this.ctx._chain.decodeEvent(this.ctx.event)
}

get isLatest(): boolean {
deprecateLatest()
return this.isV50
return this.isV59
}

get asLatest(): v50.Contract {
get asLatest(): v59.Contract {
deprecateLatest()
return this.asV50
return this.asV59
}
}

export class SmartContractModuleContractGracePeriodEndedEvent {
constructor(private ctx: EventContext) {
assert(this.ctx.event.name === 'smartContractModule.ContractGracePeriodEnded')
}

get isV59(): boolean {
return this.ctx._chain.getEventHash('smartContractModule.ContractGracePeriodEnded') === 'da3d413ec5a77ddce8b8c12c9cdd58340716a71ab4effe398cbaf249a16d0542'
}

get asV59(): [bigint, number, number] {
assert(this.isV59)
return this.ctx._chain.decodeEvent(this.ctx.event)
}

get isLatest(): boolean {
deprecateLatest()
return this.isV59
}

get asLatest(): [bigint, number, number] {
deprecateLatest()
return this.asV59
}
}

export class SmartContractModuleContractGracePeriodStartedEvent {
constructor(private ctx: EventContext) {
assert(this.ctx.event.name === 'smartContractModule.ContractGracePeriodStarted')
}

get isV59(): boolean {
return this.ctx._chain.getEventHash('smartContractModule.ContractGracePeriodStarted') === 'e6ffbb3cdd02a660fb04c1fb606cae70e4a388774028c58e59a98d56f3654371'
}

get asV59(): [bigint, number, number, bigint] {
assert(this.isV59)
return this.ctx._chain.decodeEvent(this.ctx.event)
}

get isLatest(): boolean {
deprecateLatest()
return this.isV59
}

get asLatest(): [bigint, number, number, bigint] {
deprecateLatest()
return this.asV59
}
}

Expand Down
Loading

0 comments on commit 5e5d267

Please sign in to comment.