Skip to content

Commit d6f4300

Browse files
committed
fix(web): upload-evidence-as-uri
1 parent 8c5d5d7 commit d6f4300

File tree

2 files changed

+63
-55
lines changed

2 files changed

+63
-55
lines changed

web/src/components/ActionButton/Modal/ChallengeItemModal.tsx

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import DepositRequired from "./DepositRequired";
88
import { StyledModal } from "./StyledModal";
99
import Info from "./Info";
1010
import {
11-
useCurateV2ChallengeRequest,
11+
prepareWriteCurateV2,
1212
useCurateV2GetArbitratorExtraData,
1313
useCurateV2RemovalChallengeBaseDeposit,
1414
useCurateV2SubmissionChallengeBaseDeposit,
15-
usePrepareCurateV2ChallengeRequest,
1615
} from "hooks/contracts/generated";
1716
import { useArbitrationCost } from "hooks/useArbitrationCostFromKlerosCore";
18-
import { useAccount, useBalance, usePublicClient } from "wagmi";
17+
import { useAccount, useBalance, usePublicClient, useWalletClient } from "wagmi";
1918
import { wrapWithToast } from "utils/wrapWithToast";
2019
import { IBaseModal } from ".";
2120
import EvidenceUpload, { Evidence } from "./EvidenceUpload";
21+
import { uploadFileToIPFS } from "utils/uploadFileToIPFS";
2222

2323
const ReStyledModal = styled(StyledModal)`
2424
gap: 32px;
@@ -50,6 +50,7 @@ const ChallengeItemModal: React.FC<IChallengeItemModal> = ({
5050
useClickAway(containerRef, () => toggleModal());
5151
const { address } = useAccount();
5252
const publicClient = usePublicClient();
53+
const { data: walletClient } = useWalletClient();
5354

5455
const [isChallengingItem, setIsChallengingItem] = useState(false);
5556
const [isEvidenceUploading, setIsEvidenceUploading] = useState(false);
@@ -109,16 +110,6 @@ const ChallengeItemModal: React.FC<IChallengeItemModal> = ({
109110
return userBalance?.value < depositRequired;
110111
}, [depositRequired, userBalance, isEvidenceUploading, isEvidenceValid]);
111112

112-
const { config, isError } = usePrepareCurateV2ChallengeRequest({
113-
enabled: address && registryAddress && !isLoading && !isDisabled,
114-
//@ts-ignore
115-
address: registryAddress,
116-
args: [itemId as `0x${string}`, JSON.stringify(evidence)],
117-
value: depositRequired,
118-
});
119-
120-
const { writeAsync: challengeRequest } = useCurateV2ChallengeRequest(config);
121-
122113
return (
123114
<>
124115
<Overlay />
@@ -130,24 +121,37 @@ const ChallengeItemModal: React.FC<IChallengeItemModal> = ({
130121
<Buttons
131122
buttonText="Challenge"
132123
toggleModal={toggleModal}
133-
isDisabled={isDisabled || isError || isChallengingItem}
124+
isDisabled={isDisabled || isChallengingItem}
134125
isLoading={isLoading}
135-
callback={() => {
136-
if (!challengeRequest) return;
126+
callback={async () => {
137127
setIsChallengingItem(true);
138-
wrapWithToast(
139-
async () =>
140-
await challengeRequest().then((response) => {
141-
return response.hash;
142-
}),
143-
publicClient
144-
)
145-
.then((res) => {
146-
console.log({ res });
147-
refetch();
148-
toggleModal();
128+
129+
const evidenceFile = new File([JSON.stringify(evidence)], "evidence.json", {
130+
type: "application/json",
131+
});
132+
133+
uploadFileToIPFS(evidenceFile)
134+
.then(async (res) => {
135+
if (res.status === 200 && walletClient) {
136+
const response = await res.json();
137+
const fileURI = response["cids"][0];
138+
139+
const { request } = await prepareWriteCurateV2({
140+
//@ts-ignore
141+
address: registryAddress,
142+
functionName: "challengeRequest",
143+
args: [itemId as `0x${string}`, fileURI],
144+
value: depositRequired,
145+
});
146+
147+
wrapWithToast(async () => await walletClient.writeContract(request), publicClient).then((res) => {
148+
console.log({ res });
149+
refetch();
150+
toggleModal();
151+
});
152+
}
149153
})
150-
.catch(() => {})
154+
.catch((err) => console.log(err))
151155
.finally(() => setIsChallengingItem(false));
152156
}}
153157
/>

web/src/components/ActionButton/Modal/RemoveModal.tsx

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import DepositRequired from "./DepositRequired";
88
import { StyledModal } from "./StyledModal";
99
import Info from "./Info";
1010
import { IBaseModal } from ".";
11-
import { useAccount, useBalance, usePublicClient } from "wagmi";
11+
import { useAccount, useBalance, usePublicClient, useWalletClient } from "wagmi";
1212
import {
13+
prepareWriteCurateV2,
1314
useCurateV2GetArbitratorExtraData,
1415
useCurateV2RemovalBaseDeposit,
15-
useCurateV2RemoveItem,
16-
usePrepareCurateV2RemoveItem,
1716
} from "hooks/contracts/generated";
1817
import { useArbitrationCost } from "hooks/useArbitrationCostFromKlerosCore";
1918
import { wrapWithToast } from "utils/wrapWithToast";
2019
import EvidenceUpload, { Evidence } from "./EvidenceUpload";
20+
import { uploadFileToIPFS } from "utils/uploadFileToIPFS";
2121

2222
const ReStyledModal = styled(StyledModal)`
2323
gap: 32px;
@@ -36,6 +36,7 @@ const RemoveModal: React.FC<IRemoveModal> = ({ toggleModal, isItem, registryAddr
3636
useClickAway(containerRef, () => toggleModal());
3737
const { address } = useAccount();
3838
const publicClient = usePublicClient();
39+
const { data: walletClient } = useWalletClient();
3940

4041
const [isRemovingItem, setIsRemovingItem] = useState(false);
4142
const [isEvidenceUploading, setIsEvidenceUploading] = useState(false);
@@ -85,16 +86,6 @@ const RemoveModal: React.FC<IRemoveModal> = ({ toggleModal, isItem, registryAddr
8586
return userBalance?.value < depositRequired;
8687
}, [depositRequired, userBalance, isEvidenceUploading, isEvidenceValid]);
8788

88-
const { config, isError } = usePrepareCurateV2RemoveItem({
89-
enabled: address && registryAddress && !isLoading && !isDisabled,
90-
//@ts-ignore
91-
address: registryAddress,
92-
args: [itemId as `0x${string}`, JSON.stringify(evidence)],
93-
value: depositRequired,
94-
});
95-
96-
const { writeAsync: removeItem } = useCurateV2RemoveItem(config);
97-
9889
return (
9990
<>
10091
<Overlay />
@@ -106,24 +97,37 @@ const RemoveModal: React.FC<IRemoveModal> = ({ toggleModal, isItem, registryAddr
10697
<Buttons
10798
buttonText="Remove"
10899
toggleModal={toggleModal}
109-
isDisabled={isDisabled || isError || isRemovingItem}
100+
isDisabled={isDisabled || isRemovingItem}
110101
isLoading={isLoading}
111102
callback={() => {
112-
if (!removeItem) return;
113103
setIsRemovingItem(true);
114-
wrapWithToast(
115-
async () =>
116-
await removeItem().then((response) => {
117-
return response.hash;
118-
}),
119-
publicClient
120-
)
121-
.then((res) => {
122-
console.log({ res });
123-
refetch();
124-
toggleModal();
104+
105+
const evidenceFile = new File([JSON.stringify(evidence)], "evidence.json", {
106+
type: "application/json",
107+
});
108+
109+
uploadFileToIPFS(evidenceFile)
110+
.then(async (res) => {
111+
if (res.status === 200 && walletClient) {
112+
const response = await res.json();
113+
const fileURI = response["cids"][0];
114+
115+
const { request } = await prepareWriteCurateV2({
116+
//@ts-ignore
117+
address: registryAddress,
118+
functionName: "removeItem",
119+
args: [itemId as `0x${string}`, fileURI],
120+
value: depositRequired,
121+
});
122+
123+
wrapWithToast(async () => await walletClient.writeContract(request), publicClient).then((res) => {
124+
console.log({ res });
125+
refetch();
126+
toggleModal();
127+
});
128+
}
125129
})
126-
.catch(() => {})
130+
.catch((err) => console.log(err))
127131
.finally(() => setIsRemovingItem(false));
128132
}}
129133
/>

0 commit comments

Comments
 (0)