Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: As a User, I want to check the number of Attestation, Modules, Portals and Schemas from the subgraph #237

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ type Schema @entity {
context: String!
schema: String!
}

type Counter @entity{
id: ID!
attestations: Int
modules: Int
portals: Int
schemas: Int
}
20 changes: 19 additions & 1 deletion subgraph/src/attestation-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import {
AttestationRegistered as AttestationRegisteredEvent,
AttestationRegistry,
} from "../generated/AttestationRegistry/AttestationRegistry";
import { Attestation, Schema } from "../generated/schema";
import { Attestation, Counter, Schema } from "../generated/schema";
import { BigInt, ByteArray, Bytes, ethereum } from "@graphprotocol/graph-ts";

export function handleAttestationRegistered(event: AttestationRegisteredEvent): void {
const attestationRegistryContract = AttestationRegistry.bind(event.address);
const attestationData = attestationRegistryContract.getAttestation(event.params.attestationId);
const attestation = new Attestation(event.params.attestationId.toHex());

incrementAttestationCount();

attestation.schemaId = attestationData.schemaId;
attestation.replacedBy = attestationData.replacedBy;
attestation.attester = attestationData.attester;
Expand Down Expand Up @@ -110,3 +112,19 @@ function valueToString(value: ethereum.Value): string {
return "UNKNOWN TYPE";
}
}

function incrementAttestationCount(): void {
let counter = Counter.load("counter");

if (!counter) {
counter = new Counter("counter");
}

if (!counter.attestations) {
counter.attestations = 1;
} else {
counter.attestations += 1;
}

counter.save();
}
20 changes: 19 additions & 1 deletion subgraph/src/module-registry.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import { ModuleRegistered as ModuleRegisteredEvent, ModuleRegistry } from "../generated/ModuleRegistry/ModuleRegistry";
import { Module } from "../generated/schema";
import { Counter, Module } from "../generated/schema";

export function handleModuleRegistered(event: ModuleRegisteredEvent): void {
const contract = ModuleRegistry.bind(event.address);
const moduleData = contract.modules(event.params.moduleAddress);
const module = new Module(event.params.moduleAddress.toHex());

incrementModulesCount();

module.moduleAddress = moduleData.getModuleAddress();
module.name = moduleData.getName();
module.description = moduleData.getDescription();

module.save();
}

function incrementModulesCount(): void {
let counter = Counter.load("counter");

if (!counter) {
counter = new Counter("counter");
}

if (!counter.modules) {
counter.modules = 1;
} else {
counter.modules += 1;
}

counter.save();
}
20 changes: 19 additions & 1 deletion subgraph/src/portal-registry.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Bytes } from "@graphprotocol/graph-ts";
import { PortalRegistered as PortalRegisteredEvent, PortalRegistry } from "../generated/PortalRegistry/PortalRegistry";
import { Portal } from "../generated/schema";
import { Counter, Portal } from "../generated/schema";

export function handlePortalRegistered(event: PortalRegisteredEvent): void {
const contract = PortalRegistry.bind(event.address);
const portalData = contract.getPortalByAddress(event.params.portalAddress);
const portal = new Portal(event.params.portalAddress.toHex());

incrementPortalsCount();

portal.ownerAddress = portalData.ownerAddress;
portal.modules = changetype<Bytes[]>(portalData.modules);
portal.isRevocable = portalData.isRevocable;
Expand All @@ -16,3 +18,19 @@ export function handlePortalRegistered(event: PortalRegisteredEvent): void {

portal.save();
}

function incrementPortalsCount(): void {
let counter = Counter.load("counter");

if (!counter) {
counter = new Counter("counter");
}

if (!counter.portals) {
counter.portals = 1;
} else {
counter.portals += 1;
}

counter.save();
}
20 changes: 19 additions & 1 deletion subgraph/src/schema-registry.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import { SchemaCreated as SchemaCreatedEvent, SchemaRegistry } from "../generated/SchemaRegistry/SchemaRegistry";
import { Schema } from "../generated/schema";
import { Counter, Schema } from "../generated/schema";

export function handleSchemaCreated(event: SchemaCreatedEvent): void {
const contract = SchemaRegistry.bind(event.address);
const schemaData = contract.getSchema(event.params.id);
const schema = new Schema(event.params.id.toHex());

incrementSchemasCount();

schema.name = schemaData.name;
schema.description = schemaData.description;
schema.context = schemaData.context;
schema.schema = schemaData.schema;

schema.save();
}

function incrementSchemasCount(): void {
let counter = Counter.load("counter");

if (!counter) {
counter = new Counter("counter");
}

if (!counter.schemas) {
counter.schemas = 1;
} else {
counter.schemas += 1;
}

counter.save();
}
Loading