Skip to content

Commit cdbbcf2

Browse files
feat(subgraph): fetch-dispute-request-event-data-within-subgraph
1 parent 85f3668 commit cdbbcf2

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

subgraph/core/schema.graphql

+4
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ type Dispute @entity {
174174
jurors: [User!]! @derivedFrom(field: "disputes")
175175
shifts: [TokenAndETHShift!]! @derivedFrom(field: "dispute")
176176
disputeKitDispute: [DisputeKitDispute!]! @derivedFrom(field: "coreDispute")
177+
isCrossChain: Boolean
178+
arbitrableChainId:BigInt
179+
externalDisputeId:BigInt
180+
templateId:BigInt
177181
}
178182

179183
type PeriodIndexCounter @entity {

subgraph/core/src/entities/Dispute.ts

+70
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BigInt, ByteArray, crypto, dataSource, ethereum } from "@graphprotocol/graph-ts";
12
import { KlerosCore, DisputeCreation } from "../../generated/KlerosCore/KlerosCore";
23
import { Court, Dispute } from "../../generated/schema";
34
import { ZERO } from "../utils";
@@ -27,4 +28,73 @@ export function createDisputeFromEvent(event: DisputeCreation): void {
2728
const roundID = `${disputeID.toString()}-${ZERO.toString()}`;
2829
dispute.currentRound = roundID;
2930
dispute.save();
31+
32+
updateDisputeRequestData(event);
33+
}
34+
35+
const DisputeRequest = "DisputeRequest(address,uint256,uint256,uint256,string)";
36+
37+
const DisputeRequestSignature = crypto.keccak256(ByteArray.fromUTF8(DisputeRequest));
38+
39+
// note : we are using bytes32 in place of string as strings cannot be decoded and it breaks the function.
40+
// It is okay for us, as we are interested in the uint256 in front
41+
// _externalDisputeId,_templateId,_tenplateUri
42+
const DisputeRequestTypestring = "(uint256,uint256,bytes32)";
43+
44+
const CrossChainDisputeIncoming =
45+
"CrossChainDisputeIncoming(address,uint256,address,uint256,uint256,uint256,uint256,string)";
46+
47+
const CrossChainDisputeIncomingSignature = crypto.keccak256(ByteArray.fromUTF8(CrossChainDisputeIncoming));
48+
49+
// arbitrator, _arbitrableChainId, _externalDisputeId, _templateId, _templateUri
50+
// note : arbitrable is an indexed arg, so it will topic[1]
51+
const CrossChainDisputeIncomingTypestring = "(address,uint256,uint256,uint256,string)";
52+
53+
export const updateDisputeRequestData = (event: DisputeCreation): void => {
54+
const dispute = Dispute.load(event.params._disputeID.toString());
55+
56+
if (!dispute) return;
57+
58+
const receipt = event.receipt;
59+
if (!receipt) return;
60+
61+
const logs = receipt.logs;
62+
63+
// note that the topic at 0th index is always the event signature
64+
const disputeRequestEventIndex = logs.findIndex((log) => log.topics[0] == DisputeRequestSignature);
65+
66+
const crossChainDisputeEventIndex = logs.findIndex((log) => log.topics[0] == CrossChainDisputeIncomingSignature);
67+
68+
if (crossChainDisputeEventIndex !== -1) {
69+
const crossChainDisputeEvent = logs[crossChainDisputeEventIndex];
70+
71+
const decoded = ethereum.decode(CrossChainDisputeIncomingTypestring, crossChainDisputeEvent.data);
72+
73+
if (!decoded) return;
74+
75+
dispute.isCrossChain = true;
76+
dispute.arbitrableChainId = decoded.toTuple()[1].toBigInt();
77+
dispute.externalDisputeId = decoded.toTuple()[2].toBigInt();
78+
dispute.templateId = decoded.toTuple()[3].toBigInt();
79+
dispute.save();
80+
return;
81+
} else if (disputeRequestEventIndex !== -1) {
82+
const disputeRequestEvent = logs[disputeRequestEventIndex];
83+
84+
const decoded = ethereum.decode(DisputeRequestTypestring, disputeRequestEvent.data);
85+
if (!decoded) return;
86+
dispute.isCrossChain = false;
87+
dispute.arbitrableChainId = getChainId(dataSource.network());
88+
dispute.externalDisputeId = decoded.toTuple()[0].toBigInt();
89+
dispute.templateId = decoded.toTuple()[1].toBigInt();
90+
dispute.save();
91+
return;
92+
}
93+
};
94+
95+
// workaround, since hashmap don't work in subgraphs
96+
function getChainId(name: string): BigInt {
97+
if (name == "arbitrum-one") return BigInt.fromI32(42161);
98+
else if (name == "arbitrum-sepolia") return BigInt.fromI32(421614);
99+
else return BigInt.fromI32(1);
30100
}

subgraph/core/subgraph.yaml

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
specVersion: 0.0.4
1+
specVersion: 0.0.5
22
schema:
33
file: ./schema.graphql
44
features:
@@ -14,7 +14,7 @@ dataSources:
1414
startBlock: 3638878
1515
mapping:
1616
kind: ethereum/events
17-
apiVersion: 0.0.6
17+
apiVersion: 0.0.7
1818
language: wasm/assemblyscript
1919
entities:
2020
- User
@@ -39,6 +39,7 @@ dataSources:
3939
handler: handleAppealDecision
4040
- event: DisputeCreation(indexed uint256,indexed address)
4141
handler: handleDisputeCreation
42+
receipt: true
4243
- event: Draw(indexed address,indexed uint256,uint256,uint256)
4344
handler: handleDraw
4445
- event: NewPeriod(indexed uint256,uint8)
@@ -69,7 +70,7 @@ dataSources:
6970
startBlock: 3084568
7071
mapping:
7172
kind: ethereum/events
72-
apiVersion: 0.0.6
73+
apiVersion: 0.0.7
7374
language: wasm/assemblyscript
7475
entities:
7576
- Court
@@ -89,7 +90,7 @@ dataSources:
8990
startBlock: 3638835
9091
mapping:
9192
kind: ethereum/events
92-
apiVersion: 0.0.6
93+
apiVersion: 0.0.7
9394
language: wasm/assemblyscript
9495
entities:
9596
- ClassicDispute
@@ -124,7 +125,7 @@ dataSources:
124125
startBlock: 3638735
125126
mapping:
126127
kind: ethereum/events
127-
apiVersion: 0.0.6
128+
apiVersion: 0.0.7
128129
language: wasm/assemblyscript
129130
entities:
130131
- ClassicEvidenceGroup
@@ -145,7 +146,7 @@ dataSources:
145146
startBlock: 3638850
146147
mapping:
147148
kind: ethereum/events
148-
apiVersion: 0.0.6
149+
apiVersion: 0.0.7
149150
language: wasm/assemblyscript
150151
entities:
151152
- JurorTokensPerCourt

0 commit comments

Comments
 (0)