Skip to content

Commit c59e102

Browse files
feat(kleros-sdk): get-dispute-function
1 parent cdbbcf2 commit c59e102

File tree

8 files changed

+256
-15
lines changed

8 files changed

+256
-15
lines changed

kleros-sdk/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"dependencies": {
3636
"@kleros/kleros-v2-contracts": "workspace:^",
3737
"@reality.eth/reality-eth-lib": "^3.2.30",
38+
"graphql-request": "^7.1.0",
3839
"mustache": "^4.2.0",
3940
"zod": "^3.22.4"
4041
}

kleros-sdk/src/dataMappings/executeActions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const executeAction = async (mapping: ActionMapping, context: Record<stri
3737
}
3838
};
3939

40-
export const executeActions = async (mappings, initialContext = {}) => {
40+
export const executeActions = async (mappings, initialContext: Record<string, any> = {}) => {
4141
const context = { ...initialContext };
4242

4343
for (const mapping of mappings) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { request } from "graphql-request";
2+
3+
type DisputeDetailsQueryResponse = {
4+
dispute: {
5+
arbitrated: {
6+
id: string;
7+
};
8+
arbitrableChainId: number;
9+
externalDisputeId: number;
10+
templateId: number;
11+
};
12+
};
13+
14+
const fetchDisputeDetails = (endpoint: string, id: number) => {
15+
const query = `
16+
query DisputeDetails {
17+
dispute(id: ${id}) {
18+
arbitrated {
19+
id
20+
}
21+
arbitrableChainId
22+
externalDisputeId
23+
templateId
24+
}
25+
}
26+
`;
27+
28+
try {
29+
return request<DisputeDetailsQueryResponse>(endpoint, query)
30+
.then((res) => res)
31+
.catch((err) => {
32+
throw new Error(`Error querying Dispute Details , endpoint : ${endpoint}, message : ${err?.message}`);
33+
});
34+
} catch (error: any) {
35+
console.log(`Query Error : ${error?.message}`);
36+
return undefined;
37+
}
38+
};
39+
40+
export default fetchDisputeDetails;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { request } from "graphql-request";
2+
3+
type DisputeTemplateQueryResponse = {
4+
disputeTemplate: {
5+
templateData: string;
6+
templateDataMappings: string;
7+
};
8+
};
9+
10+
const fetchDisputeTemplateFromId = (endpoint: string, id: number) => {
11+
const query = `
12+
query DisputeTemplate {
13+
disputeTemplate(id: ${id}) {
14+
templateData
15+
templateDataMappings
16+
}
17+
}
18+
`;
19+
20+
try {
21+
return request<DisputeTemplateQueryResponse>(endpoint, query)
22+
.then((res) => res)
23+
.catch((err) => {
24+
throw new Error(`Error querying Dispute Template Registry , endpoint : ${endpoint}, message : ${err?.message}`);
25+
});
26+
} catch (error: any) {
27+
console.log(`Query Error : ${error?.message}`);
28+
return undefined;
29+
}
30+
};
31+
32+
export default fetchDisputeTemplateFromId;

kleros-sdk/src/sdk.ts

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
import { createPublicClient, webSocket } from "viem";
2-
import { arbitrumSepolia } from "viem/chains";
1+
import { createPublicClient, type PublicClient } from "viem";
2+
import { SdkConfig } from "./types";
33

4-
let publicClient;
4+
let publicClient: PublicClient | undefined;
55

6-
export const configureSDK = (config: { apiKey?: string }) => {
7-
if (config.apiKey) {
8-
const ALCHEMY_API_KEY = config.apiKey;
9-
const transport = webSocket(`wss://arb-sepolia.g.alchemy.com/v2/${ALCHEMY_API_KEY}`);
10-
publicClient = createPublicClient({
11-
chain: arbitrumSepolia,
12-
transport,
13-
});
6+
export const configureSDK = (config: SdkConfig) => {
7+
if (config.client) {
8+
publicClient = createPublicClient(config.client);
149
}
1510
};
1611

17-
export const getPublicClient = () => {
12+
export const getPublicClient = (): PublicClient | undefined => {
1813
if (!publicClient) {
1914
throw new Error("SDK not configured. Please call `configureSDK` before using.");
2015
}

kleros-sdk/src/types/index.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { PublicClientConfig } from "viem";
2+
3+
export type SdkConfig = {
4+
client: PublicClientConfig;
5+
};
6+
7+
type GetDisputeParametersOptions = {
8+
sdkConfig: SdkConfig;
9+
additionalContext: Record<string, any>;
10+
};
11+
12+
export type GetDisputeParameters = {
13+
disputeId: number;
14+
coreSubgraph: string;
15+
dtrSubgraph: string;
16+
options: GetDisputeParametersOptions | undefined;
17+
};

kleros-sdk/src/utils/getDispute.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { type GetDisputeParameters } from "src/types";
2+
import { configureSDK } from "src/sdk";
3+
import fetchDisputeDetails from "src/requests/fetchDisputeDetails";
4+
import fetchDisputeTemplateFromId from "src/requests/fetchDisputeTemplateFromId";
5+
import { executeActions } from "dataMappings/executeActions";
6+
import { populateTemplate } from "dataMappings/utils/populateTemplate";
7+
import { DisputeDetails } from "dataMappings/utils/disputeDetailsTypes";
8+
9+
/**
10+
* Retrieves dispute parameters based on the provided dispute ID and subgraph endpoints.
11+
*
12+
* @param {GetDisputeParameters} disputeParameters - The parameters required to get the dispute.
13+
* @param {number} disputeParameters.disputeId - A unique numeric identifier of the dispute in the Kleros Core contract.
14+
* @param {string} disputeParameters.coreSubgraph - Endpoint for the Kleros core subgraph to use.
15+
* @param {string} disputeParameters.dtrSubgraph - Endpoint for the Kleros dispute template registry subgraph.
16+
* @param {GetDisputeParametersOptions | undefined} disputeParameters.options - Optional parameters to configure the SDK and provide additional context, if not configured already.
17+
*/
18+
export const getDispute = async (disputeParameters: GetDisputeParameters): Promise<DisputeDetails | undefined> => {
19+
if (disputeParameters.options?.sdkConfig) {
20+
configureSDK(disputeParameters.options.sdkConfig);
21+
}
22+
const { disputeId, dtrSubgraph, coreSubgraph, options } = disputeParameters;
23+
24+
const disputeDetails = await fetchDisputeDetails(coreSubgraph, disputeId);
25+
26+
if (!disputeDetails || !disputeDetails.dispute) return;
27+
28+
const template = await fetchDisputeTemplateFromId(dtrSubgraph, disputeDetails.dispute.templateId);
29+
30+
if (!template) return;
31+
32+
const { templateData, templateDataMappings } = template.disputeTemplate;
33+
34+
const initialContext = {
35+
arbitrableAddress: disputeDetails.dispute.arbitrated.id,
36+
arbitrableChainID: disputeDetails.dispute.arbitrableChainId,
37+
externalDisputeID: disputeDetails.dispute.externalDisputeId,
38+
...options?.additionalContext,
39+
};
40+
41+
const data = templateDataMappings ? await executeActions(JSON.parse(templateDataMappings), initialContext) : {};
42+
43+
const populatedTemplate = populateTemplate(templateData, data);
44+
45+
return populatedTemplate;
46+
};

yarn.lock

+112-2
Original file line numberDiff line numberDiff line change
@@ -6437,6 +6437,7 @@ __metadata:
64376437
"@reality.eth/reality-eth-lib": "npm:^3.2.30"
64386438
"@types/mustache": "npm:^4.2.5"
64396439
"@vitest/ui": "npm:^1.1.3"
6440+
graphql-request: "npm:^7.1.0"
64406441
mocha: "npm:^10.2.0"
64416442
mustache: "npm:^4.2.0"
64426443
ts-node: "npm:^10.9.2"
@@ -7334,6 +7335,34 @@ __metadata:
73347335
languageName: node
73357336
linkType: hard
73367337

7338+
"@molt/command@npm:^0.9.0":
7339+
version: 0.9.0
7340+
resolution: "@molt/command@npm:0.9.0"
7341+
dependencies:
7342+
"@molt/types": "npm:0.2.0"
7343+
alge: "npm:0.8.1"
7344+
chalk: "npm:^5.3.0"
7345+
lodash.camelcase: "npm:^4.3.0"
7346+
lodash.snakecase: "npm:^4.1.1"
7347+
readline-sync: "npm:^1.4.10"
7348+
string-length: "npm:^6.0.0"
7349+
strip-ansi: "npm:^7.1.0"
7350+
ts-toolbelt: "npm:^9.6.0"
7351+
type-fest: "npm:^4.3.1"
7352+
zod: "npm:^3.22.2"
7353+
checksum: e6ec7c6c2c6a64a1b28e09074196e5aa891bdcbff4129e54c59b902e1b6f4151033fd92c8535666be32267163d04ce3ce454ca0df0a4d5669a8506817131984e
7354+
languageName: node
7355+
linkType: hard
7356+
7357+
"@molt/types@npm:0.2.0":
7358+
version: 0.2.0
7359+
resolution: "@molt/types@npm:0.2.0"
7360+
dependencies:
7361+
ts-toolbelt: "npm:^9.6.0"
7362+
checksum: 0c7eab1dda0d689fda7025b4e4b37ffc290a7e2f7bfb0463a8bffc5834c30af66ab92d63bc2f71dc515cdd0121fc3587596690353a7e1c40d530395135540063
7363+
languageName: node
7364+
linkType: hard
7365+
73377366
"@motionone/animation@npm:^10.15.1":
73387367
version: 10.15.1
73397368
resolution: "@motionone/animation@npm:10.15.1"
@@ -13031,6 +13060,18 @@ __metadata:
1303113060
languageName: node
1303213061
linkType: hard
1303313062

13063+
"alge@npm:0.8.1":
13064+
version: 0.8.1
13065+
resolution: "alge@npm:0.8.1"
13066+
dependencies:
13067+
lodash.ismatch: "npm:^4.4.0"
13068+
remeda: "npm:^1.0.0"
13069+
ts-toolbelt: "npm:^9.6.0"
13070+
zod: "npm:^3.17.3"
13071+
checksum: 11483523289bc7750b6462698a2b61c816bf3c7eada2ade206bc02b964679b6bf4b298cc561ae267f382573668b68ff311a0aec60c5ae011aecde491932240e8
13072+
languageName: node
13073+
linkType: hard
13074+
1303413075
"amdefine@npm:>=0.0.4":
1303513076
version: 1.0.1
1303613077
resolution: "amdefine@npm:1.0.1"
@@ -15089,7 +15130,7 @@ __metadata:
1508915130
languageName: node
1509015131
linkType: hard
1509115132

15092-
"chalk@npm:5.3.0":
15133+
"chalk@npm:5.3.0, chalk@npm:^5.3.0":
1509315134
version: 5.3.0
1509415135
resolution: "chalk@npm:5.3.0"
1509515136
checksum: 6373caaab21bd64c405bfc4bd9672b145647fc9482657b5ea1d549b3b2765054e9d3d928870cdf764fb4aad67555f5061538ff247b8310f110c5c888d92397ea
@@ -21343,6 +21384,31 @@ __metadata:
2134321384
languageName: node
2134421385
linkType: hard
2134521386

21387+
"graphql-request@npm:^7.1.0":
21388+
version: 7.1.0
21389+
resolution: "graphql-request@npm:7.1.0"
21390+
dependencies:
21391+
"@graphql-typed-document-node/core": "npm:^3.2.0"
21392+
"@molt/command": "npm:^0.9.0"
21393+
zod: "npm:^3.23.8"
21394+
peerDependencies:
21395+
"@dprint/formatter": ^0.3.0
21396+
"@dprint/typescript": ^0.91.1
21397+
dprint: ^0.46.2
21398+
graphql: 14 - 16
21399+
peerDependenciesMeta:
21400+
"@dprint/formatter":
21401+
optional: true
21402+
"@dprint/typescript":
21403+
optional: true
21404+
dprint:
21405+
optional: true
21406+
bin:
21407+
graffle: build/cli/generate.js
21408+
checksum: 6e61cd8200f9842569a657b0fdc0cb45a8433d8afa730ace911fa9a09318fb3cae3ccd9396a9ab2e1795bf9ccbce976fc90fb0bfac9dfe2fc821e74845be57d9
21409+
languageName: node
21410+
linkType: hard
21411+
2134621412
"graphql-tag@npm:^2.11.0":
2134721413
version: 2.12.6
2134821414
resolution: "graphql-tag@npm:2.12.6"
@@ -31351,6 +31417,13 @@ __metadata:
3135131417
languageName: node
3135231418
linkType: hard
3135331419

31420+
"readline-sync@npm:^1.4.10":
31421+
version: 1.4.10
31422+
resolution: "readline-sync@npm:1.4.10"
31423+
checksum: 5eb6465f5c5391e32cb525022a307a910a565828cd53da87ac05fca291607df54099dd65bc9c7a513ac53a5eb4e11454d48f0dcf4ad54126d36dec5fcec4a8f0
31424+
languageName: node
31425+
linkType: hard
31426+
3135431427
"real-require@npm:^0.1.0":
3135531428
version: 0.1.0
3135631429
resolution: "real-require@npm:0.1.0"
@@ -31598,6 +31671,13 @@ __metadata:
3159831671
languageName: node
3159931672
linkType: hard
3160031673

31674+
"remeda@npm:^1.0.0":
31675+
version: 1.61.0
31676+
resolution: "remeda@npm:1.61.0"
31677+
checksum: 0b0ec8688e340c7525a8788e99701b174ddca2eaf230d47f9e087d9aaccd54c7181a1f58bf4f95cb9c12bc1d26f03b17e3d3d9046a59225074a7ac3fa1356da3
31678+
languageName: node
31679+
linkType: hard
31680+
3160131681
"remedial@npm:^1.0.7":
3160231682
version: 1.0.8
3160331683
resolution: "remedial@npm:1.0.8"
@@ -33913,6 +33993,15 @@ __metadata:
3391333993
languageName: node
3391433994
linkType: hard
3391533995

33996+
"string-length@npm:^6.0.0":
33997+
version: 6.0.0
33998+
resolution: "string-length@npm:6.0.0"
33999+
dependencies:
34000+
strip-ansi: "npm:^7.1.0"
34001+
checksum: b171e9e193ec292ad71f8b2a36e20478bf1bac8cd5beec062fb126942d627dfd9311959e271e9ab7b0a383d16b85b9e25a183d15786e30f22104d152e7627f99
34002+
languageName: node
34003+
linkType: hard
34004+
3391634005
"string-natural-compare@npm:^3.0.1":
3391734006
version: 3.0.1
3391834007
resolution: "string-natural-compare@npm:3.0.1"
@@ -34128,7 +34217,7 @@ __metadata:
3412834217
languageName: node
3412934218
linkType: hard
3413034219

34131-
"strip-ansi@npm:^7.0.1":
34220+
"strip-ansi@npm:^7.0.1, strip-ansi@npm:^7.1.0":
3413234221
version: 7.1.0
3413334222
resolution: "strip-ansi@npm:7.1.0"
3413434223
dependencies:
@@ -35300,6 +35389,13 @@ __metadata:
3530035389
languageName: node
3530135390
linkType: hard
3530235391

35392+
"ts-toolbelt@npm:^9.6.0":
35393+
version: 9.6.0
35394+
resolution: "ts-toolbelt@npm:9.6.0"
35395+
checksum: 2c2dea2631dbd7372a79cccc6d09a377a6ca2f319f767fd239d2e312cd1d9165a90f8c1777a047227bfdcda6aeba3addbadce88fdfc7f43caf4534d385a43c82
35396+
languageName: node
35397+
linkType: hard
35398+
3530335399
"tsconfck@npm:^3.0.3":
3530435400
version: 3.0.3
3530535401
resolution: "tsconfck@npm:3.0.3"
@@ -35509,6 +35605,13 @@ __metadata:
3550935605
languageName: node
3551035606
linkType: hard
3551135607

35608+
"type-fest@npm:^4.3.1":
35609+
version: 4.26.1
35610+
resolution: "type-fest@npm:4.26.1"
35611+
checksum: b82676194f80af228cb852e320d2ea8381c89d667d2e4d9f2bdfc8f254bccc039c7741a90c53617a4de0c9fdca8265ed18eb0888cd628f391c5c381c33a9f94b
35612+
languageName: node
35613+
linkType: hard
35614+
3551235615
"type-is@npm:~1.6.18":
3551335616
version: 1.6.18
3551435617
resolution: "type-is@npm:1.6.18"
@@ -38133,6 +38236,13 @@ __metadata:
3813338236
languageName: node
3813438237
linkType: hard
3813538238

38239+
"zod@npm:^3.17.3, zod@npm:^3.23.8":
38240+
version: 3.23.8
38241+
resolution: "zod@npm:3.23.8"
38242+
checksum: 846fd73e1af0def79c19d510ea9e4a795544a67d5b34b7e1c4d0425bf6bfd1c719446d94cdfa1721c1987d891321d61f779e8236fde517dc0e524aa851a6eff1
38243+
languageName: node
38244+
linkType: hard
38245+
3813638246
"zod@npm:^3.21.4":
3813738247
version: 3.21.4
3813838248
resolution: "zod@npm:3.21.4"

0 commit comments

Comments
 (0)