Skip to content

Commit

Permalink
Refactor: New set of GraphQL entities (Simplified) (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomenezes authored Feb 7, 2024
1 parent 887616c commit d6142f4
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 129 deletions.
31 changes: 0 additions & 31 deletions db/migrations/1706166961258-Data.js

This file was deleted.

31 changes: 31 additions & 0 deletions db/migrations/1706849548900-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = class Data1706849548900 {
name = 'Data1706849548900'

async up(db) {
await db.query(`CREATE TABLE "authority" ("id" character varying NOT NULL, CONSTRAINT "PK_b0f9bb35ff132fc6bd92d0582ce" PRIMARY KEY ("id"))`)
await db.query(`CREATE TABLE "node" ("id" character varying NOT NULL, "runway" numeric, "location" text, "type" character varying(9) NOT NULL, "application_id" character varying, "provider_id" character varying, CONSTRAINT "PK_8c8caf5f29d25264abe9eaf94dd" PRIMARY KEY ("id"))`)
await db.query(`CREATE INDEX "IDX_7a1582c5c19e59eaee8baf3b7c" ON "node" ("application_id") `)
await db.query(`CREATE INDEX "IDX_bc6bc99eaa9a0b593c745054dd" ON "node" ("provider_id") `)
await db.query(`CREATE TABLE "node_provider" ("id" character varying NOT NULL, "payee" text NOT NULL, "price" numeric NOT NULL, "paused" boolean NOT NULL, "type" character varying(9) NOT NULL, "authority_id" character varying, "token_id" character varying, CONSTRAINT "PK_bc6bc99eaa9a0b593c745054dd4" PRIMARY KEY ("id"))`)
await db.query(`CREATE INDEX "IDX_bf6c4c99e8df6a9004e9e8d0e3" ON "node_provider" ("authority_id") `)
await db.query(`CREATE INDEX "IDX_12cd0de307dec107ad3184038a" ON "node_provider" ("token_id") `)
await db.query(`ALTER TABLE "node" ADD CONSTRAINT "FK_7a1582c5c19e59eaee8baf3b7c2" FOREIGN KEY ("application_id") REFERENCES "application"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
await db.query(`ALTER TABLE "node" ADD CONSTRAINT "FK_bc6bc99eaa9a0b593c745054dd4" FOREIGN KEY ("provider_id") REFERENCES "node_provider"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
await db.query(`ALTER TABLE "node_provider" ADD CONSTRAINT "FK_bf6c4c99e8df6a9004e9e8d0e3a" FOREIGN KEY ("authority_id") REFERENCES "authority"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
await db.query(`ALTER TABLE "node_provider" ADD CONSTRAINT "FK_12cd0de307dec107ad3184038a1" FOREIGN KEY ("token_id") REFERENCES "token"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
}

async down(db) {
await db.query(`DROP TABLE "authority"`)
await db.query(`DROP TABLE "node"`)
await db.query(`DROP INDEX "public"."IDX_7a1582c5c19e59eaee8baf3b7c"`)
await db.query(`DROP INDEX "public"."IDX_bc6bc99eaa9a0b593c745054dd"`)
await db.query(`DROP TABLE "node_provider"`)
await db.query(`DROP INDEX "public"."IDX_bf6c4c99e8df6a9004e9e8d0e3"`)
await db.query(`DROP INDEX "public"."IDX_12cd0de307dec107ad3184038a"`)
await db.query(`ALTER TABLE "node" DROP CONSTRAINT "FK_7a1582c5c19e59eaee8baf3b7c2"`)
await db.query(`ALTER TABLE "node" DROP CONSTRAINT "FK_bc6bc99eaa9a0b593c745054dd4"`)
await db.query(`ALTER TABLE "node_provider" DROP CONSTRAINT "FK_bf6c4c99e8df6a9004e9e8d0e3a"`)
await db.query(`ALTER TABLE "node_provider" DROP CONSTRAINT "FK_12cd0de307dec107ad3184038a1"`)
}
}
18 changes: 13 additions & 5 deletions schema.graphql
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
enum FunctionType {
READER
VALIDATOR
}

type Authority @entity @cardinality(value: 5) {
id: ID!
}

type ValidatorNodeProvider @entity @cardinality(value: 100) {
type NodeProvider @entity @cardinality(value: 100) {
id: ID!
authority: Authority!
authority: Authority
token: Token!
payee: String!
price: BigInt!
paused: Boolean!
nodes: [ValidatorNode!] @derivedFrom(field: "provider")
nodes: [Node!] @derivedFrom(field: "provider")
type: FunctionType!
}

type ValidatorNode @entity @cardinality(value: 100) {
type Node @entity @cardinality(value: 100) {
id: ID!
application: Application!
provider: ValidatorNodeProvider!
provider: NodeProvider!
runway: BigInt
location: String
type: FunctionType!
}

type ApplicationFactory @entity @cardinality(value: 5) {
Expand All @@ -31,6 +38,7 @@ type Application @entity @cardinality(value: 100) {
timestamp: BigInt!
factory: ApplicationFactory
inputs: [Input!] @derivedFrom(field: "application")
nodes: [Node!] @derivedFrom(field: "application")
}

type Token @entity @cardinality(value: 10) {
Expand Down
30 changes: 15 additions & 15 deletions src/handlers/EventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
Erc721Deposit,
Input,
NFT,
Node,
NodeProvider,
Token,
ValidatorNode,
ValidatorNodeProvider,
} from '../model';
import ApplicationCreated from './ApplicationCreated';
import AuthorityCreated from './AuthorityCreated';
Expand All @@ -32,8 +32,8 @@ export default class EventHandler {
private readonly nfts: Map<string, NFT>;
private readonly erc721Deposits: Map<string, Erc721Deposit>;
private readonly authorities: Map<string, Authority>;
private readonly validatorNodeProviders: Map<string, ValidatorNodeProvider>;
private readonly validatorNodes: Map<string, ValidatorNode>;
private readonly nodeProviders: Map<string, NodeProvider>;
private readonly nodes: Map<string, Node>;
private readonly applicationCreated: Handler;
private readonly inputAdded: Handler;
private readonly ownershipTransferred: Handler;
Expand All @@ -53,8 +53,8 @@ export default class EventHandler {
this.nfts = new Map();
this.erc721Deposits = new Map();
this.authorities = new Map();
this.validatorNodeProviders = new Map();
this.validatorNodes = new Map();
this.nodeProviders = new Map();
this.nodes = new Map();

this.applicationCreated = new ApplicationCreated(
this.factories,
Expand All @@ -74,25 +74,25 @@ export default class EventHandler {
this.authorityCreated = new AuthorityCreated(this.authorities);
this.validatorNodeProviderCreated = new ValidatorNodeProviderCreated(
this.authorities,
this.validatorNodeProviders,
this.nodeProviders,
this.tokens,
);

this.validatorNodeProviderPaused = new ValidatorNodeProviderPaused(
this.validatorNodeProviders,
this.nodeProviders,
);
this.validatorNodeProviderUnpaused = new validatorNodeProviderUnpaused(
this.validatorNodeProviders,
this.nodeProviders,
);
this.validatorNodeFinancialRunway = new ValidatorNodeFinancialRunway(
this.applications,
this.validatorNodes,
this.validatorNodeProviders,
this.nodes,
this.nodeProviders,
);
this.validatorNodeMachineLocation = new ValidatorNodeMachineLocation(
this.applications,
this.validatorNodes,
this.validatorNodeProviders,
this.nodes,
this.nodeProviders,
);
}

Expand All @@ -119,8 +119,8 @@ export default class EventHandler {
nfts: this.nfts,
erc721Deposits: this.erc721Deposits,
authorities: this.authorities,
validatorNodeProviders: this.validatorNodeProviders,
validatorNodes: this.validatorNodes,
nodeProviders: this.nodeProviders,
nodes: this.nodes,
};
}

Expand Down
13 changes: 7 additions & 6 deletions src/handlers/ValidatorNodeFinancialRunway.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BlockData, DataHandlerContext, Log } from '@subsquid/evm-processor';
import { Store } from '@subsquid/typeorm-store';
import { events as ValidatorNodeProviderEvents } from '../abi/ValidatorNodeProvider';
import { Application, ValidatorNode, ValidatorNodeProvider } from '../model';
import { Application, FunctionType, Node, NodeProvider } from '../model';
import Handler from './Handler';

export default class ValidatorNodeFinancialRunway implements Handler {
constructor(
private apps: Map<string, Application>,
private nodes: Map<string, ValidatorNode>,
private providers: Map<string, ValidatorNodeProvider>,
private nodes: Map<string, Node>,
private providers: Map<string, NodeProvider>,
) {}

async handle(log: Log, block: BlockData, ctx: DataHandlerContext<Store>) {
Expand All @@ -18,7 +18,7 @@ export default class ValidatorNodeFinancialRunway implements Handler {
const providerAddress = log.address.toLowerCase();
const provider =
this.providers.get(providerAddress) ??
(await ctx.store.get(ValidatorNodeProvider, providerAddress));
(await ctx.store.get(NodeProvider, providerAddress));

if (provider) {
const { dapp, until } =
Expand All @@ -27,13 +27,14 @@ export default class ValidatorNodeFinancialRunway implements Handler {
const nodeId = `${providerAddress}-${appId}`;
let node =
this.nodes.get(nodeId) ??
(await ctx.store.get(ValidatorNode, nodeId));
(await ctx.store.get(Node, nodeId));

if (!node) {
const application =
this.apps.get(appId) ??
(await ctx.store.get(Application, appId));
node = new ValidatorNode({
node = new Node({
type: FunctionType.VALIDATOR,
id: nodeId,
provider,
application,
Expand Down
13 changes: 7 additions & 6 deletions src/handlers/ValidatorNodeMachineLocation.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BlockData, DataHandlerContext, Log } from '@subsquid/evm-processor';
import { Store } from '@subsquid/typeorm-store';
import { events as ValidatorNodeProviderEvents } from '../abi/ValidatorNodeProvider';
import { Application, ValidatorNode, ValidatorNodeProvider } from '../model';
import { Application, FunctionType, Node, NodeProvider } from '../model';
import Handler from './Handler';

export default class ValidatorNodeMachineLocation implements Handler {
constructor(
private apps: Map<string, Application>,
private nodes: Map<string, ValidatorNode>,
private providers: Map<string, ValidatorNodeProvider>,
private nodes: Map<string, Node>,
private providers: Map<string, NodeProvider>,
) {}

async handle(log: Log, block: BlockData, ctx: DataHandlerContext<Store>) {
Expand All @@ -18,7 +18,7 @@ export default class ValidatorNodeMachineLocation implements Handler {
const providerAddress = log.address.toLowerCase();
const provider =
this.providers.get(providerAddress) ??
(await ctx.store.get(ValidatorNodeProvider, providerAddress));
(await ctx.store.get(NodeProvider, providerAddress));

if (provider) {
const { dapp, location } =
Expand All @@ -27,13 +27,14 @@ export default class ValidatorNodeMachineLocation implements Handler {
const nodeId = `${providerAddress}-${appId}`;
let node =
this.nodes.get(nodeId) ??
(await ctx.store.get(ValidatorNode, nodeId));
(await ctx.store.get(Node, nodeId));

if (!node) {
const application =
this.apps.get(appId) ??
(await ctx.store.get(Application, appId));
node = new ValidatorNode({
node = new Node({
type: FunctionType.VALIDATOR,
id: nodeId,
provider,
application,
Expand Down
7 changes: 4 additions & 3 deletions src/handlers/ValidatorNodeProviderCreated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { BlockData, DataHandlerContext, Log } from '@subsquid/evm-processor';
import { Store } from '@subsquid/typeorm-store';
import { events as MarketplaceEvents } from '../abi/Marketplace';
import { MarketplaceAddress } from '../config';
import { Authority, Token, ValidatorNodeProvider } from '../model';
import { Authority, FunctionType, NodeProvider, Token } from '../model';
import Handler from './Handler';
import TokenHelper from './helpers/TokenHelper';

export default class ValidatorNodeProviderCreated implements Handler {
constructor(
private authorities: Map<string, Authority>,
private providers: Map<string, ValidatorNodeProvider>,
private providers: Map<string, NodeProvider>,
private tokens: Map<string, Token>,
) {}

Expand Down Expand Up @@ -49,7 +49,8 @@ export default class ValidatorNodeProviderCreated implements Handler {
ctx.log.info(`${providerId} (ValidatorNodeProvider) stored`);
this.providers.set(
providerId,
new ValidatorNodeProvider({
new NodeProvider({
type: FunctionType.VALIDATOR,
id: providerId,
token: tokenInstance,
paused: false,
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/ValidatorNodeProviderPaused.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { BlockData, DataHandlerContext, Log } from '@subsquid/evm-processor';
import { Store } from '@subsquid/typeorm-store';
import { events as ValidatorNodeProviderEvents } from '../abi/ValidatorNodeProvider';
import { ValidatorNodeProvider } from '../model';
import { NodeProvider } from '../model';
import Handler from './Handler';

export default class ValidatorNodeProviderPaused implements Handler {
constructor(private providers: Map<string, ValidatorNodeProvider>) {}
constructor(private providers: Map<string, NodeProvider>) {}

async handle(log: Log, block: BlockData, ctx: DataHandlerContext<Store>) {
if (log.topics[0] === ValidatorNodeProviderEvents.Paused.topic) {
const address = log.address.toLowerCase();
const provider =
this.providers.get(address) ??
(await ctx.store.get(ValidatorNodeProvider, address));
(await ctx.store.get(NodeProvider, address));

if (provider) {
provider.paused = true;
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/ValidatorNodeProviderUnpaused.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { BlockData, DataHandlerContext, Log } from '@subsquid/evm-processor';
import { Store } from '@subsquid/typeorm-store';
import { events as ValidatorNodeProviderEvents } from '../abi/ValidatorNodeProvider';
import { ValidatorNodeProvider } from '../model';
import { NodeProvider } from '../model';
import Handler from './Handler';

export default class validatorNodeProviderUnpaused implements Handler {
constructor(private providers: Map<string, ValidatorNodeProvider>) {}
constructor(private providers: Map<string, NodeProvider>) {}

async handle(log: Log, block: BlockData, ctx: DataHandlerContext<Store>) {
if (log.topics[0] === ValidatorNodeProviderEvents.Unpaused.topic) {
const address = log.address.toLowerCase();
const provider =
this.providers.get(address) ??
(await ctx.store.get(ValidatorNodeProvider, address));
(await ctx.store.get(NodeProvider, address));

if (provider) {
provider.paused = false;
Expand Down
8 changes: 4 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ processor.run(new TypeormDatabase({ supportHotBlocks: true }), async (ctx) => {
nfts,
erc721Deposits,
authorities,
validatorNodeProviders,
validatorNodes,
nodeProviders,
nodes,
} = eventHandler.getValues();

const total = eventHandler.getTotalHandled();
Expand All @@ -56,6 +56,6 @@ processor.run(new TypeormDatabase({ supportHotBlocks: true }), async (ctx) => {
await ctx.store.upsert([...deposits.values()]);
await ctx.store.upsert([...erc721Deposits.values()]);
await ctx.store.upsert([...inputs.values()]);
await ctx.store.upsert([...validatorNodeProviders.values()]);
await ctx.store.upsert([...validatorNodes.values()]);
await ctx.store.upsert([...nodeProviders.values()]);
await ctx.store.upsert([...nodes.values()]);
});
Loading

0 comments on commit d6142f4

Please sign in to comment.