Skip to content

Commit 320e734

Browse files
authored
Merge pull request #68 from kleros/feat/subgraph
Feat: add subgraph
2 parents 8f32dfa + 3a44687 commit 320e734

File tree

12 files changed

+2630
-508
lines changed

12 files changed

+2630
-508
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,7 @@ tags
196196
# .pnp.*
197197

198198
# End of https://www.toptal.com/developers/gitignore/api/vim,node,visualstudiocode,yarn
199+
200+
# subgraph
201+
subgraph/generated/*
202+
subgraph/build/*

CONTRIBUTING.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
## Contribution Guidelines
32

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

87
You should usually open an issue in the following situations:
98

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

1413
### Tips for communicating on issues:
1514

16-
* **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.
17-
* **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.
18-
* **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.
15+
- **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.
16+
- **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.
17+
- **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.
1918

2019
## Opening a pull request
2120

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

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

27-
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.
26+
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.
2827

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

31-
* **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.
32-
* **Create a branch** for your edits.
33-
* **Reference any relevant issues** or supporting documentation in your PR (for example, “Closes #37.”)
34-
* **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.
35-
* **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.
36-
* **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.
30+
- **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.
31+
- **Create a branch** for your edits.
32+
- **Reference any relevant issues** or supporting documentation in your PR (for example, “Closes #37.”)
33+
- **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.
34+
- **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.
35+
- **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.
3736

3837
If you are an organization member, a branch can be created directly in this repository, there is no need to fork it.

subgraph/.eslintrc.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"plugins": ["@typescript-eslint"],
5+
"extends": [
6+
"plugin:@typescript-eslint/recommended",
7+
"prettier",
8+
"prettier/@typescript-eslint"
9+
]
10+
}

subgraph/.prettierrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"printWidth": 80
3+
}

subgraph/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@kleros/kleros-v2-subgraph",
3+
"license": "MIT",
4+
"scripts": {
5+
"codegen": "graph codegen",
6+
"build": "graph build",
7+
"deploy": "graph deploy --node https://api.studio.thegraph.com/deploy/ KlerosCore",
8+
"create-local": "graph create --node http://localhost:8020/ KlerosCore",
9+
"remove-local": "graph remove --node http://localhost:8020/ KlerosCore",
10+
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 KlerosCore"
11+
},
12+
"dependencies": {
13+
"@graphprotocol/graph-cli": "^0.29.1",
14+
"@graphprotocol/graph-ts": "^0.26.0"
15+
}
16+
}

subgraph/schema.graphql

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
enum Period {
2+
Evidence
3+
Commit
4+
Vote
5+
Appeal
6+
Execution
7+
}
8+
9+
type Court @entity {
10+
id: ID!
11+
hiddenVotes: Boolean!
12+
children: [Court!]!
13+
childrenIDs: [String!]!
14+
minStake: BigInt!
15+
alpha: BigInt!
16+
feeForJuror: BigInt!
17+
jurorsForCourtJump: BigInt!
18+
timesPerPeriod: [BigInt!]!
19+
supportedDisputeKits: BigInt!
20+
disputes: [Dispute!]! @derivedFrom(field: "subcourtID")
21+
stakedJurors: [JurorTokensPerSubcourt!]! @derivedFrom(field: "subcourt")
22+
tokens: [JurorTokensPerSubcourt!]! @derivedFrom(field: "subcourt")
23+
}
24+
25+
type Juror @entity {
26+
id: ID! # Set to address
27+
tokens: [JurorTokensPerSubcourt!]! @derivedFrom(field: "juror")
28+
shifts: [TokenAndETHShift!]! @derivedFrom(field: "juror")
29+
draws: [Draw!]! @derivedFrom(field: "juror")
30+
}
31+
32+
type TokenAndETHShift @entity {
33+
id: ID! # Set to `${juror.id}-${dispute.id}`
34+
juror: Juror!
35+
dispute: Dispute!
36+
tokenAmount: BigInt!
37+
ethAmount: BigInt!
38+
}
39+
40+
type JurorTokensPerSubcourt @entity {
41+
id: ID! # Set to `${juror.id}-${court.id}`
42+
juror: Juror!
43+
subcourt: Court!
44+
staked: BigInt!
45+
locked: BigInt!
46+
}
47+
48+
type Round @entity {
49+
id: ID! # Set to `${dispute.id}-${currentRound}`
50+
dispute: Dispute!
51+
tokensAtStakePerJuror: BigInt!
52+
totalFeesForJurors: BigInt!
53+
repartitions: BigInt!
54+
penalties: BigInt!
55+
draws: [Draw!]! @derivedFrom(field: "round")
56+
}
57+
58+
type Draw @entity {
59+
id: ID! # Set to `${dispute.id}-${currentRound}-${voteID}`
60+
round: Round!
61+
juror: Juror!
62+
voteID: BigInt!
63+
}
64+
65+
type Dispute @entity {
66+
id: ID!
67+
subcourtID: Court!
68+
arbitrated: Bytes!
69+
disputeKit: Bytes!
70+
period: Period!
71+
ruled: Boolean!
72+
lastPeriodChange: BigInt!
73+
nbVotes: BigInt!
74+
currentRound: Int!
75+
rounds: [Round!]! @derivedFrom(field: "dispute")
76+
shifts: [TokenAndETHShift!]! @derivedFrom(field: "dispute")
77+
gatewayDispute: GatewayDispute! @derivedFrom(field: "homeDispute")
78+
}
79+
80+
type GatewayDispute @entity {
81+
id: ID!
82+
homeDispute: Dispute!
83+
arbitrator: Bytes!
84+
disputeHash: Bytes!
85+
arbitrationCost: BigInt!
86+
relayer: Bytes!
87+
}
88+
89+
type OutgoingMessage @entity {
90+
id: ID! # Set to messageHash
91+
target: Bytes!
92+
messageHash: String!
93+
message: String!
94+
}
95+
96+
type PNKStakedDataPoint @entity {
97+
id: ID! # Will be the timestamp except for the counter which will be 0
98+
value: BigInt!
99+
}
100+
101+
type ETHPaidDataPoint @entity {
102+
id: ID! # Will be the timestamp except for the counter which will be 0
103+
value: BigInt!
104+
}
105+
106+
type PNKRedistributedDataPoint @entity {
107+
id: ID! # Will be the timestamp except for the counter which will be 0
108+
value: BigInt!
109+
}
110+
111+
type ActiveJurorsDataPoint @entity {
112+
id: ID! # Will be the timestamp except for the counter which will be 0
113+
value: BigInt!
114+
}
115+
116+
type CasesDataPoint @entity {
117+
id: ID! # Will be the timestamp except for the counter which will be 0
118+
value: BigInt!
119+
}

subgraph/src/FastBridgeSender.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {
2+
OutgoingMessage as OutgoingMessageEvent,
3+
} from "../generated/FastBridgeSender/FastBridgeSender";
4+
import {
5+
OutgoingMessage
6+
} from "../generated/schema";
7+
8+
export function handleOutgoingMessage(event: OutgoingMessageEvent): void {
9+
const outgoingMessage = new OutgoingMessage(
10+
event.params.messageHash.toHexString()
11+
);
12+
outgoingMessage.target = event.params.target;
13+
outgoingMessage.messageHash = event.params.messageHash.toHex();
14+
outgoingMessage.message = event.params.message.toHex();
15+
outgoingMessage.save();
16+
}

subgraph/src/HomeGateway.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {
2+
HomeGateway,
3+
Dispute as DisputeEvent,
4+
} from "../generated/HomeGateway/HomeGateway";
5+
import {
6+
GatewayDispute
7+
} from "../generated/schema";
8+
9+
export function handleDisputeEvent(event: DisputeEvent): void {
10+
const contract = HomeGateway.bind(event.address);
11+
const disputeID = event.params._disputeID;
12+
const dispute = new GatewayDispute(disputeID.toString());
13+
dispute.homeDispute = disputeID.toString();
14+
dispute.arbitrator = contract.arbitrator();
15+
const disputeHash = contract.disputeIDtoHash(disputeID);
16+
dispute.disputeHash = disputeHash;
17+
const relayedData = contract.disputeHashtoRelayedData(disputeHash);
18+
dispute.arbitrationCost = relayedData.value0;
19+
dispute.relayer = relayedData.value1;
20+
dispute.save();
21+
}

0 commit comments

Comments
 (0)