Skip to content

Commit

Permalink
Merge pull request #29 from kleros/feat/fix-request-requester
Browse files Browse the repository at this point in the history
Chore/migrate to file data source and fix request requester
  • Loading branch information
jaybuidl authored Jan 22, 2025
2 parents 782de7b + 05a353c commit 6a3e461
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 53 deletions.
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
"fix-schema.ts": "replace-in-file '/(;\\s*if \\()value === null/mg' '$1value === null || value.kind === ValueKind.NULL' generated/schema.ts --isRegex",
"codegen:gnosis": "node render-templates.js gnosis && graph codegen && yarn fix-schema.ts",
"build:gnosis": "yarn codegen:gnosis && graph build",
"publish:gnosis": "yarn build:gnosis && graph deploy --product hosted-service kleros/legacy-curate-gnosis",
"publish:gnosis-goldsky": "yarn build:gnosis && yarn goldsky subgraph deploy legacy-curate-gnosis/latest",
"publish:gnosis": "yarn build:gnosis && graph deploy --studio legacy-curate-gnosis",
"codegen:gnosis-goldsky": "node render-templates.js xdai && graph codegen && yarn fix-schema.ts",
"build:gnosis-goldsky": "yarn codegen:gnosis-goldsky && graph build",
"publish:gnosis-goldsky": "yarn build:gnosis-goldsky && yarn goldsky subgraph deploy legacy-curate-gnosis/v${npm_package_version}",
"codegen:mainnet": "node render-templates.js mainnet && graph codegen && yarn fix-schema.ts",
"build:mainnet": "yarn codegen:mainnet && graph build",
"publish:mainnet": "yarn build:mainnet && graph deploy --product hosted-service kleros/curate",
"publish:mainnet": "yarn build:mainnet && graph deploy --studio legacy-curate-mainnet",
"codegen:goerli": "node render-templates.js goerli && graph codegen && yarn fix-schema.ts",
"build:goerli": "yarn codegen:goerli && graph build",
"publish:goerli": "yarn build:goerli && graph deploy --product hosted-service kleros/curate-goerli",
"codegen:sepolia": "node render-templates.js sepolia && graph codegen && yarn fix-schema.ts",
"build:sepolia": "yarn codegen:sepolia && graph build",
"publish:sepolia": "yarn build:sepolia && graph deploy --studio kleros/curate-sepolia",
"publish:sepolia": "yarn build:sepolia && graph deploy --studio legacy-curate-sepolia",
"lint:eslint": "eslint . --ext .ts,.tsx",
"lint:prettier": "prettier --config .prettierrc 'src/**/*.ts' --check",
"lint": "yarn lint:eslint && yarn lint:prettier && yarn lint:secrets",
Expand All @@ -40,7 +42,7 @@
"@assemblyscript/loader": "^0.14.11",
"@assemblyscript/node": "github:AssemblyScript/node",
"@gnosis.pm/truffle-nice-tools": "^1.3.1",
"@goldskycom/cli": "^11.2.0",
"@goldskycom/cli": "^11.3.0",
"@graphprotocol/graph-cli": "^0.71.0",
"@kleros/erc-792": "3.0.0",
"@kleros/gtcr-encoder": "^1.1.3",
Expand All @@ -65,6 +67,9 @@
"typescript": "^4.3.5",
"wait-on": "^5.2.0"
},
"resolutions": {
"ejs": "^3.1.10"
},
"volta": {
"node": "20.11.0",
"yarn": "1.22.19"
Expand Down
13 changes: 5 additions & 8 deletions render-templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const mustache = require('mustache');

const chainNameToChainId = {
gnosis: 100,
xdai: 100, // For Goldsky: https://docs.goldsky.com/chains/supported-networks
mainnet: 1,
goerli: 5,
sepolia: 11155111,
Expand All @@ -12,14 +13,10 @@ async function main() {
const networkName = process.argv[2];
const chainId = chainNameToChainId[networkName];
const deployments = JSON.parse(fs.readFileSync('networks.json', 'utf8'));
const {
address: lgtcrFactoryAddr,
startBlock: lgtcrFactoryStartBlock,
} = deployments['LightGTCRFactory'][chainId];
const {
address: gtcrFactoryAddr,
startBlock: gtcrFactoryStartBlock,
} = deployments['GTCRFactory'][chainId];
const { address: lgtcrFactoryAddr, startBlock: lgtcrFactoryStartBlock } =
deployments['LightGTCRFactory'][chainId];
const { address: gtcrFactoryAddr, startBlock: gtcrFactoryStartBlock } =
deployments['GTCRFactory'][chainId];
const templateData = {
network: networkName,
};
Expand Down
30 changes: 26 additions & 4 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type Evidence @entity {
metadata: EvidenceMetadata
}

type EvidenceMetadata @entity {
type EvidenceMetadata @entity(immutable: true) {
id: ID!
"Name of the evidence"
name: String
Expand Down Expand Up @@ -116,8 +116,24 @@ type LRegistry @entity {
numberOfChallengedClearing: BigInt!
"Connected TCR. Can be the 0 address. In practice, will never be null."
connectedTCR: Bytes
metadata: LRegistryMetadata
}

type LRegistryMetadata @entity {
id: ID!
title: String
description: String
itemName: String
itemNamePlural: String
isConnectedTCR: Boolean
requireRemovalEvidence: Boolean
isTCRofTcrs: Boolean
parentTCRAddress: String
relTcrDisabled: Boolean
registry: LRegistry @derivedFrom(field: "metadata")
}


type MetaEvidence @entity {
"The meta evidence ID."
id: ID!
Expand Down Expand Up @@ -155,8 +171,8 @@ type LItem @entity {
metadata: LItemMetadata
}

type LItemMetadata @entity {
"ipfs cid"
type LItemMetadata @entity(immutable: true) {
"ipfs cid - Litem ID"
id: ID!
"The parsed data describing the item."
props: [ItemProp!]! @derivedFrom(field: "item")
Expand All @@ -183,8 +199,14 @@ type _Schema_
algorithm: rank
include: [{ entity: "LItemMetadata", fields: [{ name: "keywords" }] }]
)
@fulltext(
name: "registrySearch"
language: en
algorithm: rank
include: [{ entity: "LRegistryMetadata", fields: [{ name: "title" }, { name: "description" }, { name: "itemName" }] }]
)

type ItemProp @entity {
type ItemProp @entity(immutable: true) {
id: ID!
type: String!
label: String!
Expand Down
30 changes: 21 additions & 9 deletions src/GeneralizedTCRMapping.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/* eslint-disable prefer-const */
import { Bytes, BigInt, Address, log } from '@graphprotocol/graph-ts';
import {
Bytes,
BigInt,
Address,
log,
DataSourceContext,
} from '@graphprotocol/graph-ts';
import {
Item,
Request,
Expand Down Expand Up @@ -127,13 +133,15 @@ export function handleRequestSubmitted(event: RequestEvidenceGroupID): void {

let requestID =
graphItemID + '-' + itemInfo.value2.minus(BigInt.fromI32(1)).toString();
let requestIndex = item.numberOfRequests.minus(BigInt.fromI32(1));
let requestInfo = tcr.getRequestInfo(event.params._itemID, requestIndex);

let request = new Request(requestID);
request.disputed = false;
request.arbitrator = tcr.arbitrator();
request.arbitratorExtraData = tcr.arbitratorExtraData();
request.challenger = ZERO_ADDRESS;
request.requester = event.transaction.from;
request.requester = requestInfo.value4[1];
request.item = item.id;
request.registry = registry.id;
request.registryAddress = event.address;
Expand Down Expand Up @@ -257,16 +265,16 @@ export function handleRequestChallenged(event: Dispute): void {
log.error(`Request of requestID {} not found.`, [requestID]);
return;
}
let requestInfo = tcr.getRequestInfo(
itemID,
itemInfo.value2.minus(BigInt.fromI32(1)),
);

request.disputed = true;
request.challenger = event.transaction.from;
request.challenger = requestInfo.value4[2];
request.numberOfRounds = BigInt.fromI32(2);
request.disputeID = event.params._disputeID;

let requestInfo = tcr.getRequestInfo(
itemID,
itemInfo.value2.minus(BigInt.fromI32(1)),
);
let roundID =
requestID + '-' + requestInfo.value5.minus(BigInt.fromI32(2)).toString();
let round = Round.load(roundID);
Expand Down Expand Up @@ -612,8 +620,12 @@ export function handleEvidence(event: EvidenceEvent): void {
);

const ipfsHash = extractPath(event.params._evidence);
evidence.metadata = ipfsHash;
EvidenceMetadataTemplate.create(ipfsHash);
evidence.metadata = `${ipfsHash}-${evidence.id}`;

const context = new DataSourceContext();
context.setString('evidenceId', evidence.id);

EvidenceMetadataTemplate.createWithContext(ipfsHash, context);

evidenceGroup.save();
evidence.save();
Expand Down
30 changes: 25 additions & 5 deletions src/LightGeneralizedTCRMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
LIArbitrator as IArbitratorDataSourceTemplate,
LGTCREvidence as EvidenceMetadataTemplate,
LItemMetadata as LItemMetadataTemplate,
LRegistryMetadata as LRegistryMetadataTemplate,
} from '../generated/templates';
import {
Contribution,
Expand Down Expand Up @@ -250,7 +251,9 @@ export function handleNewItem(event: NewItem): void {
item.latestRequestSubmissionTime = BigInt.fromI32(0);

const ipfsHash = extractPath(event.params._data);
item.metadata = ipfsHash;
item.metadata = `${ipfsHash}-${graphItemID}`;

log.debug('Creating datasource for ipfs hash : {}', [ipfsHash]);

const context = new DataSourceContext();
context.setString('graphItemID', graphItemID);
Expand Down Expand Up @@ -298,14 +301,15 @@ export function handleRequestSubmitted(event: RequestSubmitted): void {
let newStatus = getExtendedStatus(item.disputed, item.status);

let requestIndex = item.numberOfRequests.minus(BigInt.fromI32(1));
let requestInfo = tcr.getRequestInfo(event.params._itemID, requestIndex);
let requestID = graphItemID + '-' + requestIndex.toString();

let request = new LRequest(requestID);
request.disputed = false;
request.arbitrator = tcr.arbitrator();
request.arbitratorExtraData = tcr.arbitratorExtraData();
request.challenger = ZERO_ADDRESS;
request.requester = event.transaction.from;
request.requester = requestInfo.value4[1];
request.item = item.id;
request.registry = registry.id;
request.registryAddress = event.address;
Expand Down Expand Up @@ -446,6 +450,7 @@ export function handleRequestChallenged(event: Dispute): void {
let newStatus = getExtendedStatus(item.disputed, item.status);

let requestIndex = item.numberOfRequests.minus(BigInt.fromI32(1));
let requestInfo = tcr.getRequestInfo(itemID, requestIndex);
let requestID = graphItemID + '-' + requestIndex.toString();
let request = LRequest.load(requestID);
if (!request) {
Expand All @@ -454,7 +459,7 @@ export function handleRequestChallenged(event: Dispute): void {
}

request.disputed = true;
request.challenger = event.transaction.from;
request.challenger = requestInfo.value4[2];
request.numberOfRounds = BigInt.fromI32(2);
request.disputeID = event.params._disputeID;

Expand Down Expand Up @@ -790,6 +795,18 @@ export function handleMetaEvidence(event: MetaEvidenceEvent): void {
}

metaEvidence.URI = event.params._evidence;

const ipfsHash = extractPath(event.params._evidence);
registry.metadata = `${ipfsHash}-${event.address.toHexString()}-${
registry.metaEvidenceCount
}`;

const context = new DataSourceContext();
context.setString('address', event.address.toHexString());
context.setBigInt('count', registry.metaEvidenceCount);

LRegistryMetadataTemplate.createWithContext(ipfsHash, context);

metaEvidence.save();

if (
Expand Down Expand Up @@ -848,8 +865,11 @@ export function handleEvidence(event: EvidenceEvent): void {
);

const ipfsHash = extractPath(event.params._evidence);
evidence.metadata = ipfsHash;
EvidenceMetadataTemplate.create(ipfsHash);
evidence.metadata = `${ipfsHash}-${evidence.id}`;

const context = new DataSourceContext();
context.setString('evidenceId', evidence.id);
EvidenceMetadataTemplate.createWithContext(ipfsHash, context);

evidenceGroup.save();
evidence.save();
Expand Down
6 changes: 5 additions & 1 deletion src/fileHandlers/EvidenceMetadataHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { EvidenceMetadata } from '../../generated/schema';

export function handleGTCREvidenceMetadata(content: Bytes): void {
const id = dataSource.stringParam();
const evidence = new EvidenceMetadata(id);

const context = dataSource.context();
const evidenceId = context.getString('evidenceId');

const evidence = new EvidenceMetadata(`${id}-${evidenceId}`);
const value = json.fromBytes(content).toObject();

log.debug(`ipfs hash : {}, content : {}`, [id, content.toString()]);
Expand Down
5 changes: 4 additions & 1 deletion src/fileHandlers/LEvidenceMetadataHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { EvidenceMetadata } from '../../generated/schema';

export function handleLGTCREvidenceMetadata(content: Bytes): void {
const id = dataSource.stringParam();
const evidence = new EvidenceMetadata(id);
const context = dataSource.context();
const evidenceId = context.getString('evidenceId');

const evidence = new EvidenceMetadata(`${id}-${evidenceId}`);
const value = json.fromBytes(content).toObject();

log.debug(`ipfs hash : {}, content : {}`, [id, content.toString()]);
Expand Down
11 changes: 7 additions & 4 deletions src/fileHandlers/LItemMetadataHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ import { ItemProp, LItemMetadata } from '../../generated/schema';
import { JSONValueToBool, JSONValueToMaybeString } from '../utils';

export function handleLItemMetadata(content: Bytes): void {
const id = dataSource.stringParam();
const metadata = new LItemMetadata(id);
const ipfsHash = dataSource.stringParam();

const value = json.fromBytes(content).toObject();
const context = dataSource.context();

const context = dataSource.context();
const graphItemID = context.getString('graphItemID');
const address = context.getString('address');

const id = `${ipfsHash}-${graphItemID}`;

const metadata = new LItemMetadata(id);

metadata.keywords = address;

log.debug(`ipfs hash : {}, content : {}`, [id, content.toString()]);
log.debug(`ipfs hash : {}, content : {}`, [ipfsHash, content.toString()]);

if (!value) {
log.warning(`Error converting object for graphItemId {}`, [graphItemID]);
Expand Down
56 changes: 56 additions & 0 deletions src/fileHandlers/LRegistryMetadataHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Bytes, dataSource, json, log } from '@graphprotocol/graph-ts';
import { LRegistryMetadata } from '../../generated/schema';
import { JSONValueToBool, JSONValueToMaybeString } from '../utils';

export function handleLRegistryMetadata(content: Bytes): void {
const ipfsHash = dataSource.stringParam();

const value = json.fromBytes(content).toObject();

const context = dataSource.context();
const count = context.getBigInt('count');
const address = context.getString('address');

const id = `${ipfsHash}-${address}-${count.toString()}`;

const metadata = new LRegistryMetadata(id);

log.debug(`ipfs hash : {}, content : {}`, [ipfsHash, content.toString()]);

if (!value) {
log.warning(`Error converting object for hash {}`, [ipfsHash]);
metadata.save();
return;
}

const metadataValue = value.get('metadata');
if (!metadataValue) {
log.error(`Error getting metadata values from ipfs hash {}`, [ipfsHash]);
metadata.save();
return;
}

const data = metadataValue.toObject();

const title = data.get('tcrTitle');
const description = data.get('tcrDescription');
const itemName = data.get('itemName');
const itemNamePlural = data.get('itemNamePlural');
const isConnectedTCR = data.get('isConnectedTCR');
const requireRemovalEvidence = data.get('requireRemovalEvidence');
const isTCRofTcrs = data.get('isTCRofTcrs');
const parentTCRAddress = data.get('parentTCRAddress');
const relTcrDisabled = data.get('relTcrDisabled');

metadata.title = JSONValueToMaybeString(title);
metadata.description = JSONValueToMaybeString(description);
metadata.itemName = JSONValueToMaybeString(itemName);
metadata.parentTCRAddress = JSONValueToMaybeString(parentTCRAddress);
metadata.itemNamePlural = JSONValueToMaybeString(itemNamePlural);
metadata.isConnectedTCR = JSONValueToBool(isConnectedTCR);
metadata.requireRemovalEvidence = JSONValueToBool(requireRemovalEvidence);
metadata.isTCRofTcrs = JSONValueToBool(isTCRofTcrs);
metadata.relTcrDisabled = JSONValueToBool(relTcrDisabled);

metadata.save();
}
Loading

0 comments on commit 6a3e461

Please sign in to comment.