Skip to content

Commit 69252c7

Browse files
committed
fix: api revamp
1 parent f254033 commit 69252c7

11 files changed

+225
-300
lines changed

.env.development

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
REACT_APP_USER_SETTINGS_URL=https://hgyxlve79a.execute-api.us-east-2.amazonaws.com/production/user-settings
2-
REACT_APP_JUSTIFICATIONS_URL=https://hgyxlve79a.execute-api.us-east-2.amazonaws.com/production/justifications
2+
REACT_APP_JUSTIFICATIONS_URL=https://kleros-api.netlify.app/.netlify/functions
3+
REACT_APP_METAEVIDENCE_URL=https://kleros-api.netlify.app/.netlify/functions/get-dispute-metaevidence

.env.production

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
REACT_APP_USER_SETTINGS_URL=https://hgyxlve79a.execute-api.us-east-2.amazonaws.com/production/user-settings
2-
REACT_APP_JUSTIFICATIONS_URL=https://hgyxlve79a.execute-api.us-east-2.amazonaws.com/production/justifications
2+
REACT_APP_JUSTIFICATIONS_URL=https://kleros-api.netlify.app/.netlify/functions
3+
REACT_APP_METAEVIDENCE_URL=https://kleros-api.netlify.app/.netlify/functions/get-dispute-metaevidence

netlify.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
REACT_APP_TOKEN_BRIDGE_ADDRESS='0x88ad09518695c6c3712AC10a214bE5109a655671'
1313
REACT_APP_DRAW_EVENT_LISTENER_BLOCK_NUMBER='7303699'
1414
REACT_APP_USER_SETTINGS_URL='https://8aoprv935h.execute-api.us-east-2.amazonaws.com/staging/user-settings'
15-
REACT_APP_JUSTIFICATIONS_URL='https://8aoprv935h.execute-api.us-east-2.amazonaws.com/staging/justifications'
15+
REACT_APP_JUSTIFICATIONS_URL='https://kleros-api.netlify.app/.netlify/functions'
16+
REACT_APP_METAEVIDENCE_URL='https://kleros-api.netlify.app/.netlify/functions/get-dispute-metaevidence'
1617
REACT_APP_APPLICATION_SERVER_KEY='BNqPp3phfZUqu4G1SCkqNuZ_FDL4YuSeAEV97U_G3UbrOuOsTeaI9jHE3QNRJ-O8EH3F2OUdnJJ4ZOhp80FthKU'
1718
REACT_APP_WEB3_FALLBACK_URL='wss://mainnet.infura.io/ws/v3/54fb3d87cd07464591ad2be29a1db32f'
1819
REACT_APP_WEB3_FALLBACK_HTTPS_URL='https://mainnet.infura.io/v3/54fb3d87cd07464591ad2be29a1db32f'
@@ -62,7 +63,8 @@
6263
[context.production.environment]
6364
REACT_APP_CONTEXT='production'
6465
REACT_APP_USER_SETTINGS_URL='https://hgyxlve79a.execute-api.us-east-2.amazonaws.com/production/user-settings'
65-
REACT_APP_JUSTIFICATIONS_URL='https://hgyxlve79a.execute-api.us-east-2.amazonaws.com/production/justifications'
66+
REACT_APP_JUSTIFICATIONS_URL='https://kleros-api.netlify.app/.netlify/functions'
67+
REACT_APP_METAEVIDENCE_URL='https://kleros-api.netlify.app/.netlify/functions/get-dispute-metaevidence'
6668

6769
[[redirects]]
6870
from = "/*"

src/bootstrap/api.js

+38-146
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,52 @@
1-
import { useCallback, useEffect, useState } from "react";
21
import web3DeriveAccount from "../temp/web3-derive-account";
3-
4-
const funcs = [
5-
{
6-
URL: process.env.REACT_APP_USER_SETTINGS_URL,
7-
createDerived: false,
8-
method: "GET",
9-
name: "getUserSettings",
10-
payload: "settings",
11-
signingMethod: "derived",
12-
},
13-
{
14-
URL: process.env.REACT_APP_USER_SETTINGS_URL,
15-
createDerived: true,
16-
method: "PATCH",
17-
name: "patchUserSettings",
18-
payload: "settings",
19-
signingMethod: "derived",
20-
},
21-
{
22-
URL: process.env.REACT_APP_JUSTIFICATIONS_URL,
23-
method: "GET",
24-
name: "getJustifications",
25-
signingMethod: null,
26-
},
27-
{
28-
URL: process.env.REACT_APP_JUSTIFICATIONS_URL,
29-
createDerived: true,
30-
method: "PUT",
31-
name: "putJustifications",
32-
payload: "justification",
33-
signingMethod: "derived",
34-
},
35-
];
2+
import axios from "axios";
363

374
const chainIdToNetwork = {
385
1: "mainnet",
396
5: "goerli",
407
100: "xdai",
41-
10200: "chiado",
428
};
439

44-
export const API = funcs.reduce((acc, f) => {
45-
acc[f.name] = async (web3, account, payload) => {
46-
let derivedAccount;
47-
if (f.signingMethod === "derived")
48-
derivedAccount = await web3DeriveAccount(
49-
web3,
50-
account,
51-
"To keep your data safe and to use certain features of Kleros, we ask that you sign these messages to create a secret key for your account. This key is unrelated from your main Ethereum account and will not be able to send any transactions.",
52-
f.createDerived
53-
);
54-
55-
const chainId = await web3.eth.getChainId();
56-
const network = chainIdToNetwork[chainId];
57-
if (!f.payload) {
58-
payload["network"] = network === "main" ? "mainnet" : network;
59-
}
60-
61-
// Use different signing method depending on the situation
62-
let signature;
63-
if (f.signingMethod === "derived" && derivedAccount) {
64-
signature = derivedAccount.sign(JSON.stringify(payload)).signature;
65-
} else if (f.signingMethod === "personal") {
66-
signature = await web3.eth.sign(JSON.stringify(payload), account);
67-
}
68-
69-
const func = () =>
70-
fetch(f.URL, {
71-
body: JSON.stringify({
72-
payload: f.payload
73-
? {
74-
address: account,
75-
network: network === "main" ? "mainnet" : network,
76-
signature,
77-
[f.payload]: payload,
78-
}
79-
: payload,
80-
}),
81-
headers: { "Content-Type": "application/json" },
82-
method: f.method === "GET" ? "POST" : f.method,
83-
}).then((res) => res.json());
84-
const res = await func();
85-
if (res.error && derivedAccount) {
86-
const settings = {
87-
derivedAccountAddress: {
88-
S: derivedAccount.address,
89-
},
90-
};
91-
await fetch(process.env.REACT_APP_USER_SETTINGS_URL, {
92-
body: JSON.stringify({
93-
payload: {
94-
address: account,
95-
settings,
96-
signature: await web3.eth.personal.sign(JSON.stringify(settings), account),
97-
},
98-
}),
99-
headers: { "Content-Type": "application/json" },
100-
method: "PATCH",
101-
}).then((res) => res.json());
102-
return func();
103-
}
104-
return res;
105-
};
106-
107-
Object.defineProperty(acc[f.name], "name", { value: f.name });
108-
109-
return acc;
110-
}, {});
111-
112-
export const useAPI = funcs.reduce((acc, f) => {
113-
acc[f.name] = function useHook(web3, account, payload) {
114-
const [state, setState] = useState(f.method === "GET" ? "pending" : undefined);
115-
let mounted = true;
116-
117-
const send = useCallback(
118-
async (payload) => {
119-
let state;
120-
try {
121-
if (state !== "pending") setState("pending");
122-
state = await API[f.name](web3, account, payload);
123-
} catch (err) {
124-
console.error(err);
125-
state = { error: "An unexpected error occurred." };
126-
}
127-
128-
if (mounted) {
129-
setState(state);
130-
}
10+
export const API = async ({ url, method, createDerived, web3, account, payload }) => {
11+
let derivedAccount;
12+
derivedAccount = await web3DeriveAccount(
13+
web3,
14+
account,
15+
"To keep your data safe and to use certain features of Kleros, we ask that you sign these messages to create a secret key for your account. This key is unrelated from your main Ethereum account and will not be able to send any transactions.",
16+
createDerived
17+
);
18+
19+
const network = chainIdToNetwork[await web3.eth.getChainId()];
20+
if (!payload) payload["network"] = network === "main" ? "mainnet" : network;
21+
22+
const signature = derivedAccount
23+
? derivedAccount.sign(JSON.stringify(payload)).signature
24+
: await web3.eth.sign(JSON.stringify(payload), account);
25+
26+
const func = () =>
27+
axios[method.toLocaleLowerCase()](url, {
28+
payload: {
29+
address: account,
30+
network: network === "main" ? "mainnet" : network,
31+
signature,
32+
...payload,
13133
},
132-
// eslint-disable-next-line react-hooks/exhaustive-deps
133-
[web3, account]
134-
);
135-
136-
useEffect(() => {
137-
if (f.method === "GET") {
138-
send(payload);
139-
return () => {
140-
// eslint-disable-next-line react-hooks/exhaustive-deps
141-
mounted = false;
142-
};
143-
}
144-
}, [web3, account, JSON.stringify(payload)]);
34+
});
14535

146-
return f.method === "GET" ? state : { send, state };
147-
};
36+
const res = func();
14837

149-
Object.defineProperty(acc[f.name], "name", { value: `use${ucFirst(f.name)}` });
150-
151-
return acc;
152-
}, {});
38+
if (!res.ok && derivedAccount) {
39+
const settings = { derivedAccountAddress: { S: derivedAccount.address } };
40+
await axios.patch(process.env.REACT_APP_USER_SETTINGS_URL, {
41+
payload: {
42+
address: account,
43+
settings,
44+
signature: await web3.eth.personal.sign(JSON.stringify(settings), account),
45+
},
46+
});
15347

154-
function ucFirst(str) {
155-
if (str.length === 0) {
156-
return "";
48+
return func();
15749
}
15850

159-
return str.charAt(0).toUpperCase() + str.substring(1);
160-
}
51+
return res;
52+
};

src/bootstrap/dataloader.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ import Dataloader from "dataloader";
33
import { getReadOnlyRpcUrl } from "./web3";
44
import axios from "axios";
55
import useSWR from "swr";
6-
7-
const subgraphURL = "https://api.thegraph.com/subgraphs/name";
8-
9-
const displaySubgraph = {
10-
1: "andreimvp/kleros-display-mainnet",
11-
100: "andreimvp/kleros-display",
12-
};
6+
import { displaySubgraph } from "./subgraph";
137

148
const getURIProtocol = (uri) => {
159
const uriParts = uri.replace(":", "").split("/");
@@ -94,7 +88,7 @@ const funcs = {
9488
async getMetaEvidence(chainID, arbitrated, arbitrator, disputeId) {
9589
try {
9690
const metaEvidenceUriData = await axios.get(
97-
`https://lemon.kleros.io/.netlify/functions/get-metaevidence?chainId=${chainID}&disputeId=${disputeId}`
91+
`${process.env.REACT_APP_METAEVIDENCE_URL}?chainId=${chainID}&disputeId=${disputeId}`
9892
);
9993

10094
const uri = metaEvidenceUriData.data?.metaEvidenceUri;
@@ -128,6 +122,7 @@ const funcs = {
128122
const injectedParameters = {
129123
arbitratorChainID: metaEvidenceJSON.arbitratorChainID || chainID,
130124
arbitrableChainID: metaEvidenceJSON.arbitrableChainID || chainID,
125+
disputeID: disputeId,
131126
};
132127

133128
injectedParameters.arbitrableContractAddress = injectedParameters.arbitrableContractAddress || arbitrated;
@@ -233,7 +228,7 @@ export const useDataloader = Object.keys(dataloaders).reduce((acc, f) => {
233228
const evidenceFetcher = async ([subgraph, disputeId]) => {
234229
const evidence = await axios
235230
.post(
236-
`${subgraphURL}/${subgraph}`,
231+
subgraph,
237232
{
238233
query: `
239234
query getDispute($id: String!) {

src/bootstrap/subgraph.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const displaySubgraph = {
2+
1: "https://api.thegraph.com/subgraphs/name/greenlucid/kleros-display-mainnet",
3+
100: "https://api.thegraph.com/subgraphs/name/greenlucid/kleros-display",
4+
11155111: "https://api.studio.thegraph.com/query/50849/kleros-sepolia-gf/version/latest",
5+
};

src/components/case-details-card.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,22 @@ export default function CaseDetailsCard({ ID }) {
209209

210210
const sendOrRevealVote = useCallback(
211211
async (choice) => {
212-
if (justification && justification.trim().length > 0) {
213-
API.putJustifications(web3, account, {
214-
appeal: disputeExtraInfo.votesLengths.length - 1,
215-
disputeID: ID,
216-
justification,
217-
voteIDs: votesData.voteIDs,
212+
if (justification && justification.trim().length > 0)
213+
API({
214+
account,
215+
web3,
216+
method: "PUT",
217+
URL: `${process.env.REACT_APP_JUSTIFICATIONS_URL}/put-justification`,
218+
createDerived: true,
219+
payload: {
220+
justification: {
221+
appeal: disputeExtraInfo.votesLengths.length - 1,
222+
disputeID: ID,
223+
justification,
224+
voteIDs: votesData.voteIDs,
225+
},
226+
},
218227
});
219-
}
220228

221229
sendVote(
222230
ID,

0 commit comments

Comments
 (0)