Skip to content

Feat: add subgraph #68

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

Merged
merged 17 commits into from
Jun 3, 2022
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,7 @@ tags
# .pnp.*

# End of https://www.toptal.com/developers/gitignore/api/vim,node,visualstudiocode,yarn

# subgraph
subgraph/generated/*
subgraph/build/*
31 changes: 15 additions & 16 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

## Contribution Guidelines

👉 Please start by reading our guidelines here: https://kleros.gitbook.io/docs/contribution-guidelines/overview
Expand All @@ -7,32 +6,32 @@

You should usually open an issue in the following situations:

* Report an error you can’t solve yourself
* Discuss a high-level topic or idea (for example, community, vision or policies)
* Propose a new feature or other project idea
- Report an error you can’t solve yourself
- Discuss a high-level topic or idea (for example, community, vision or policies)
- Propose a new feature or other project idea

### Tips for communicating on issues:

* **If you see an open issue that you want to tackle,** comment on the issue to let people know you’re on it. That way, people are less likely to duplicate your work.
* **If an issue was opened a while ago,** it’s possible that it’s being addressed somewhere else, or has already been resolved, so comment to ask for confirmation before starting work.
* **If you opened an issue, but figured out the answer later on your own,** comment on the issue to let people know, then close the issue. Even documenting that outcome is a contribution to the project.
- **If you see an open issue that you want to tackle,** comment on the issue to let people know you’re on it. That way, people are less likely to duplicate your work.
- **If an issue was opened a while ago,** it’s possible that it’s being addressed somewhere else, or has already been resolved, so comment to ask for confirmation before starting work.
- **If you opened an issue, but figured out the answer later on your own,** comment on the issue to let people know, then close the issue. Even documenting that outcome is a contribution to the project.

## Opening a pull request

You should usually open a pull request in the following situations:

* Submit trivial fixes (for example, a typo, a broken link or an obvious error).
* Start work on a contribution that was already asked for, or that you’ve already discussed, in an issue.
- Submit trivial fixes (for example, a typo, a broken link or an obvious error).
- Start work on a contribution that was already asked for, or that you’ve already discussed, in an issue.

A pull request doesn’t have to represent finished work. It’s usually better to open a *draft* pull request early on, so others can watch or give feedback on your progress. Just mark it as a “WIP” (Work in Progress) in the subject line. You can always add more commits later.
A pull request doesn’t have to represent finished work. It’s usually better to open a _draft_ pull request early on, so others can watch or give feedback on your progress. Just mark it as a “WIP” (Work in Progress) in the subject line. You can always add more commits later.

As a contributor who is not an organization member, here’s how to submit a pull request:

* **Fork the repository** and clone it locally. Connect your local to the original repository by adding it as a remote. Pull in changes from this repository often so that you stay up to date so that when you submit your pull request, merge conflicts will be less likely.
* **Create a branch** for your edits.
* **Reference any relevant issues** or supporting documentation in your PR (for example, “Closes #37.”)
* **Include screenshots of the before and after** if your changes include differences in HTML/CSS. Drag and drop the images into the body of your pull request.
* **Test your changes!** Run your changes against any existing tests if they exist and create new ones when needed. Whether tests exist or not, make sure your changes don’t break the existing project.
* **Contribute in the style of the project** to the best of your abilities. This may mean using indents, semi-colons or comments differently than you would in your own repository, but makes it easier for the maintainer to merge, others to understand and maintain in the future.
- **Fork the repository** and clone it locally. Connect your local to the original repository by adding it as a remote. Pull in changes from this repository often so that you stay up to date so that when you submit your pull request, merge conflicts will be less likely.
- **Create a branch** for your edits.
- **Reference any relevant issues** or supporting documentation in your PR (for example, “Closes #37.”)
- **Include screenshots of the before and after** if your changes include differences in HTML/CSS. Drag and drop the images into the body of your pull request.
- **Test your changes!** Run your changes against any existing tests if they exist and create new ones when needed. Whether tests exist or not, make sure your changes don’t break the existing project.
- **Contribute in the style of the project** to the best of your abilities. This may mean using indents, semi-colons or comments differently than you would in your own repository, but makes it easier for the maintainer to merge, others to understand and maintain in the future.

If you are an organization member, a branch can be created directly in this repository, there is no need to fork it.
10 changes: 10 additions & 0 deletions subgraph/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"plugin:@typescript-eslint/recommended",
"prettier",
"prettier/@typescript-eslint"
]
}
3 changes: 3 additions & 0 deletions subgraph/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"printWidth": 80
}
16 changes: 16 additions & 0 deletions subgraph/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@kleros/kleros-v2-subgraph",
"license": "MIT",
"scripts": {
"codegen": "graph codegen",
"build": "graph build",
"deploy": "graph deploy --node https://api.studio.thegraph.com/deploy/ KlerosCore",
"create-local": "graph create --node http://localhost:8020/ KlerosCore",
"remove-local": "graph remove --node http://localhost:8020/ KlerosCore",
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 KlerosCore"
},
"dependencies": {
"@graphprotocol/graph-cli": "^0.29.1",
"@graphprotocol/graph-ts": "^0.26.0"
}
}
119 changes: 119 additions & 0 deletions subgraph/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
enum Period {
Evidence
Commit
Vote
Appeal
Execution
}

type Court @entity {
id: ID!
hiddenVotes: Boolean!
children: [Court!]!
childrenIDs: [String!]!
minStake: BigInt!
alpha: BigInt!
feeForJuror: BigInt!
jurorsForCourtJump: BigInt!
timesPerPeriod: [BigInt!]!
supportedDisputeKits: BigInt!
disputes: [Dispute!]! @derivedFrom(field: "subcourtID")
stakedJurors: [JurorTokensPerSubcourt!]! @derivedFrom(field: "subcourt")
tokens: [JurorTokensPerSubcourt!]! @derivedFrom(field: "subcourt")
}

type Juror @entity {
id: ID! # Set to address
tokens: [JurorTokensPerSubcourt!]! @derivedFrom(field: "juror")
shifts: [TokenAndETHShift!]! @derivedFrom(field: "juror")
draws: [Draw!]! @derivedFrom(field: "juror")
}

type TokenAndETHShift @entity {
id: ID! # Set to `${juror.id}-${dispute.id}`
juror: Juror!
dispute: Dispute!
tokenAmount: BigInt!
ethAmount: BigInt!
}

type JurorTokensPerSubcourt @entity {
id: ID! # Set to `${juror.id}-${court.id}`
juror: Juror!
subcourt: Court!
staked: BigInt!
locked: BigInt!
}

type Round @entity {
id: ID! # Set to `${dispute.id}-${currentRound}`
dispute: Dispute!
tokensAtStakePerJuror: BigInt!
totalFeesForJurors: BigInt!
repartitions: BigInt!
penalties: BigInt!
draws: [Draw!]! @derivedFrom(field: "round")
}

type Draw @entity {
id: ID! # Set to `${dispute.id}-${currentRound}-${voteID}`
round: Round!
juror: Juror!
voteID: BigInt!
}

type Dispute @entity {
id: ID!
subcourtID: Court!
arbitrated: Bytes!
disputeKit: Bytes!
period: Period!
ruled: Boolean!
lastPeriodChange: BigInt!
nbVotes: BigInt!
currentRound: Int!
rounds: [Round!]! @derivedFrom(field: "dispute")
shifts: [TokenAndETHShift!]! @derivedFrom(field: "dispute")
gatewayDispute: GatewayDispute! @derivedFrom(field: "homeDispute")
}

type GatewayDispute @entity {
id: ID!
homeDispute: Dispute!
arbitrator: Bytes!
disputeHash: Bytes!
arbitrationCost: BigInt!
relayer: Bytes!
}

type OutgoingMessage @entity {
id: ID! # Set to messageHash
target: Bytes!
messageHash: String!
message: String!
}

type PNKStakedDataPoint @entity {
id: ID! # Will be the timestamp except for the counter which will be 0
value: BigInt!
}

type ETHPaidDataPoint @entity {
id: ID! # Will be the timestamp except for the counter which will be 0
value: BigInt!
}

type PNKRedistributedDataPoint @entity {
id: ID! # Will be the timestamp except for the counter which will be 0
value: BigInt!
}

type ActiveJurorsDataPoint @entity {
id: ID! # Will be the timestamp except for the counter which will be 0
value: BigInt!
}

type CasesDataPoint @entity {
id: ID! # Will be the timestamp except for the counter which will be 0
value: BigInt!
}
16 changes: 16 additions & 0 deletions subgraph/src/FastBridgeSender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {
OutgoingMessage as OutgoingMessageEvent,
} from "../generated/FastBridgeSender/FastBridgeSender";
import {
OutgoingMessage
} from "../generated/schema";

export function handleOutgoingMessage(event: OutgoingMessageEvent): void {
const outgoingMessage = new OutgoingMessage(
event.params.messageHash.toHexString()
);
outgoingMessage.target = event.params.target;
outgoingMessage.messageHash = event.params.messageHash.toHex();
outgoingMessage.message = event.params.message.toHex();
outgoingMessage.save();
}
21 changes: 21 additions & 0 deletions subgraph/src/HomeGateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
HomeGateway,
Dispute as DisputeEvent,
} from "../generated/HomeGateway/HomeGateway";
import {
GatewayDispute
} from "../generated/schema";

export function handleDisputeEvent(event: DisputeEvent): void {
const contract = HomeGateway.bind(event.address);
const disputeID = event.params._disputeID;
const dispute = new GatewayDispute(disputeID.toString());
dispute.homeDispute = disputeID.toString();
dispute.arbitrator = contract.arbitrator();
const disputeHash = contract.disputeIDtoHash(disputeID);
dispute.disputeHash = disputeHash;
const relayedData = contract.disputeHashtoRelayedData(disputeHash);
dispute.arbitrationCost = relayedData.value0;
dispute.relayer = relayedData.value1;
dispute.save();
}
Loading