Skip to content

Commit

Permalink
Feat: As a user, I want to know how many attestations were issued thr…
Browse files Browse the repository at this point in the history
…ough a given Schema (#543)
  • Loading branch information
Stasn13 authored Mar 13, 2024
1 parent d0a2589 commit 1880ff2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Schema @entity {
description: String!
context: String!
schema: String!
attestationCounter: Int
}

type Counter @entity {
Expand Down
17 changes: 15 additions & 2 deletions subgraph/src/attestation-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function handleAttestationRegistered(event: AttestationRegisteredEvent):
const attestationData = attestationRegistryContract.getAttestation(event.params.attestationId);
const attestation = new Attestation(event.params.attestationId.toHex());

incrementAttestationCount(attestationData.portal.toHexString());
incrementAttestationCount(attestationData.portal.toHexString(), attestationData.schemaId.toHex());

attestation.schemaId = attestationData.schemaId;
attestation.replacedBy = attestationData.replacedBy;
Expand Down Expand Up @@ -157,7 +157,7 @@ function valueToString(value: ethereum.Value): string {
}
}

function incrementAttestationCount(portalAddress: string): void {
function incrementAttestationCount(portalAddress: string, schemaId: string): void {
let counter = Counter.load("counter");

if (!counter) {
Expand All @@ -184,4 +184,17 @@ function incrementAttestationCount(portalAddress: string): void {

portal.save();
}

// Increment attestation counter for corresponding schema
const schema = Schema.load(schemaId);

if (schema) {
if (!schema.attestationCounter) {
schema.attestationCounter = 1;
} else {
schema.attestationCounter += 1;
}

schema.save();
}
}
1 change: 1 addition & 0 deletions subgraph/src/schema-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function handleSchemaCreated(event: SchemaCreatedEvent): void {
schema.description = event.params.description;
schema.context = event.params.context;
schema.schema = event.params.schemaString;
schema.attestationCounter = 0;

schema.save();
}
Expand Down
23 changes: 23 additions & 0 deletions subgraph/tests/attestation-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,29 @@ describe("AttestationRegistry", () => {
assert.fieldEquals("Portal", portal.toHexString(), "attestationCounter", "1");
});

test("Should increment the attestations counter in schema", () => {
assert.entityCount("Attestation", 0);
assert.entityCount("Schema", 0);

const schemaEntity = new Schema(schemaId.toHexString());
schemaEntity.name = "name";
schemaEntity.description = "description";
schemaEntity.context = "context";
schemaEntity.schema = "schemaString";
schemaEntity.attestationCounter = 0;
schemaEntity.save();

assert.entityCount("Schema", 1);
assert.fieldEquals("Schema", schemaId.toHexString(), "attestationCounter", "0");

const attestationRegisteredEvent = createAttestationRegisteredEvent(attestationId);

handleAttestationRegistered(attestationRegisteredEvent);

assert.entityCount("Attestation", 1);
assert.fieldEquals("Schema", schemaId.toHexString(), "attestationCounter", "1");
});

test("Should decode a basic attestation data", () => {
const schema = new Schema(schemaId.toHexString());
schema.name = "name";
Expand Down
1 change: 1 addition & 0 deletions subgraph/tests/schema-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe("handleSchemaCreated()", () => {
assert.fieldEquals("Schema", schemaId, "description", schemaDescription);
assert.fieldEquals("Schema", schemaId, "context", schemaContext);
assert.fieldEquals("Schema", schemaId, "schema", schemaString);
assert.fieldEquals("Schema", schemaId, "attestationCounter", "0");
});

test("Should increment the schemas Counter", () => {
Expand Down

0 comments on commit 1880ff2

Please sign in to comment.