Skip to content

Commit

Permalink
Removed old overlapping project attestation on receiving a new one
Browse files Browse the repository at this point in the history
  • Loading branch information
aminlatifi committed May 9, 2024
1 parent ea25d7a commit 2716060
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = class Data1715238240936 {
name = 'Data1715238240936'
module.exports = class Data1715254645119 {
name = 'Data1715254645119'

async up(db) {
await db.query(`CREATE TABLE "attestor" ("id" character varying NOT NULL, CONSTRAINT "PK_2ba0dae296b9deebeb9ecbbf508" PRIMARY KEY ("id"))`)
Expand All @@ -9,6 +9,8 @@ module.exports = class Data1715238240936 {
await db.query(`CREATE INDEX "IDX_22cd09c4533533cebedb5487f4" ON "attestor_organisation" ("attestor_id") `)
await db.query(`CREATE INDEX "IDX_b0d947390c1e10152bb1387fa2" ON "attestor_organisation" ("organisation_id") `)
await db.query(`CREATE TABLE "project" ("id" character varying NOT NULL, "source" text NOT NULL, "project_id" text NOT NULL, "title" text, "description" text, "total_vouches" integer NOT NULL, "total_flags" integer NOT NULL, "last_updated_timestamp" TIMESTAMP WITH TIME ZONE NOT NULL, CONSTRAINT "PK_4d68b1358bb5b766d3e78f32f57" PRIMARY KEY ("id"))`)
await db.query(`CREATE INDEX "IDX_399e8555e92ea7fd5f129fe178" ON "project" ("source") `)
await db.query(`CREATE INDEX "IDX_1a480c5734c5aacb9cef7b1499" ON "project" ("project_id") `)
await db.query(`CREATE TABLE "project_attestation" ("id" character varying NOT NULL, "recipient" text NOT NULL, "vouch" boolean NOT NULL, "tx_hash" text NOT NULL, "revoked" boolean NOT NULL, "attest_timestamp" TIMESTAMP WITH TIME ZONE NOT NULL, "comment" text, "attestor_organisation_id" character varying, "project_id" character varying, CONSTRAINT "PK_b54887e7eb9193e705303c2b0a0" PRIMARY KEY ("id"))`)
await db.query(`CREATE INDEX "IDX_d482a5af31e29569b8b42d9252" ON "project_attestation" ("attestor_organisation_id") `)
await db.query(`CREATE INDEX "IDX_1082147528db937cb5b50fb2a0" ON "project_attestation" ("project_id") `)
Expand All @@ -26,6 +28,8 @@ module.exports = class Data1715238240936 {
await db.query(`DROP INDEX "public"."IDX_22cd09c4533533cebedb5487f4"`)
await db.query(`DROP INDEX "public"."IDX_b0d947390c1e10152bb1387fa2"`)
await db.query(`DROP TABLE "project"`)
await db.query(`DROP INDEX "public"."IDX_399e8555e92ea7fd5f129fe178"`)
await db.query(`DROP INDEX "public"."IDX_1a480c5734c5aacb9cef7b1499"`)
await db.query(`DROP TABLE "project_attestation"`)
await db.query(`DROP INDEX "public"."IDX_d482a5af31e29569b8b42d9252"`)
await db.query(`DROP INDEX "public"."IDX_1082147528db937cb5b50fb2a0"`)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

module.exports = class AddTrace1715238240968 {
name = "AddTrace1715238240968";
module.exports = class AddTrace1715254645149 {
name = "AddTrace1715254645149";

async up(db) {
// add organisation with name "Trace" and schema id "0x2e22df9a11e06c306ed8f64ca45ceae02efcf8a443371395a78371bc4fb6f722"
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/authorizeAttestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DataHandlerContext, Log } from "@subsquid/evm-processor";
import { Store } from "@subsquid/typeorm-store";
import { Attestor, AttestorOrganisation, Organisation } from "../model";
import * as EASContract from "../abi/EAS";
import { getAttestor } from "./utils/modelHelper";

export const handleAuthorize = async (
ctx: DataHandlerContext<Store>,
Expand Down Expand Up @@ -30,8 +31,7 @@ export const handleAuthorize = async (
return;
}

const attestor = new Attestor({ id: accountAddress });
await ctx.store.upsert([attestor]);
const attestor = await getAttestor(ctx, accountAddress);

const key = uid.toLocaleLowerCase();

Expand Down
30 changes: 18 additions & 12 deletions src/controllers/projectVerificationAttestation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { DataHandlerContext, Log } from "@subsquid/evm-processor";
import { Store } from "@subsquid/typeorm-store";
import * as EASContract from "../abi/EAS";
import { getAttestationData } from "./utils/easHelper";
import {
getAttestationData,
removeDuplicateProjectAttestations,
} from "./utils/easHelper";
import { AttestorOrganisation, ProjectAttestation } from "../model";
import {
getProject,
Expand Down Expand Up @@ -30,10 +33,15 @@ export const handleProjectAttestation = async (
schemaUid
);

const attestorOrganisation = await ctx.store.get(
AttestorOrganisation,
refUID.toLowerCase()
);
const attestorOrganisation = await ctx.store.get(AttestorOrganisation, {
where: {
id: refUID.toLowerCase(),
},
relations: {
organisation: true,
attestor: true,
},
});

if (!attestorOrganisation) {
ctx.log.debug(
Expand Down Expand Up @@ -72,16 +80,14 @@ export const handleProjectAttestation = async (
);

// Delete the previous attestation
const oldAttestation = await ctx.store.findOneBy(ProjectAttestation, {
await removeDuplicateProjectAttestations(
ctx,
project,
attestorOrganisation,
});
if (oldAttestation) {
await ctx.store.remove(oldAttestation);
}
attestorOrganisation.attestor,
attestorOrganisation.organisation
);

const { vouch, comment } = projectVerificationAttestation;

const projectAttestation = new ProjectAttestation({
id: uid,
vouch,
Expand Down
33 changes: 33 additions & 0 deletions src/controllers/utils/easHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import * as EASContract from "../../abi/EAS";
import * as SchemaContract from "../../abi/Schema";
import { EAS_CONTRACT_ADDRESS, SCHEMA_CONTRACT_ADDRESS } from "../../constants";
import { Block } from "../../abi/abi.support";
import {
Attestor,
AttestorOrganisation,
Organisation,
Project,
ProjectAttestation,
} from "../../model";

export const getEasSchemaEncoder = async (
ctx: DataHandlerContext<Store>,
Expand Down Expand Up @@ -48,3 +55,29 @@ export const getAttestationData = async (
refUID,
};
};

export const removeDuplicateProjectAttestations = async (
ctx: DataHandlerContext<Store>,
project: Project,
attestor: Attestor,
organisation: Organisation
) => {
const attestorOrganisations = await ctx.store.findBy(AttestorOrganisation, {
attestor,
organisation,
});

for (const attestorOrganisation of attestorOrganisations) {
const projectAttestation = await ctx.store.findOneBy(ProjectAttestation, {
project,
attestorOrganisation,
});

if (projectAttestation) {
ctx.log.debug(
`Removing duplicate project attestation ${projectAttestation.id}`
);
await ctx.store.remove(projectAttestation);
}
}
};
4 changes: 2 additions & 2 deletions src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "@subsquid/evm-processor";

import * as EASContract from "./abi/EAS";
import { EAS_CONTRACT_ADDRESS } from "./constants";
import { EAS_CONTRACT_ADDRESS, START_BLOCK } from "./constants";

export const processor = new EvmBatchProcessor()
// Lookup archive by the network name in Subsquid registry
Expand All @@ -35,7 +35,7 @@ export const processor = new EvmBatchProcessor()
},
})
.setBlockRange({
from: 5815457,
from: START_BLOCK,
})
.addLog({
address: [EAS_CONTRACT_ADDRESS],
Expand Down

0 comments on commit 2716060

Please sign in to comment.