|
1 | | -import { useState } from "react"; |
| 1 | +import { useState, useEffect } from "react"; |
2 | 2 | import SolutionPopUp from "../components/SolutionPopUp"; |
3 | 3 | import { FilledButton } from "../components/Buttons"; |
4 | 4 | import { useAlert } from "../context/AlertContext"; |
| 5 | +import { useWallet } from "../context/WalletContext"; |
| 6 | +import computeCommit from "../utils/computeCommit"; |
| 7 | +import TaskManagerAbi from "../../../abi/TaskManagerAbi.json"; |
| 8 | +import * as snarkjs from "snarkjs"; |
5 | 9 |
|
6 | 10 |
|
7 | 11 | function SolutionPopUpContainer() { |
8 | 12 | const [isOpen, setIsOpen] = useState(false); |
| 13 | + const [contractAddress, setContractAddress] = useState<string>(""); |
| 14 | + //const { signer, account } = useWallet(); |
9 | 15 | const { showAlert } = useAlert(); |
10 | 16 |
|
| 17 | + // Fetch contract address once when popup opens |
| 18 | + useEffect(() => { |
| 19 | + if (isOpen) { |
| 20 | + (async () => { |
| 21 | + try { |
| 22 | + const res = await fetch("/api/contract-address"); |
| 23 | + const json = await res.json(); |
| 24 | + setContractAddress(json.address); |
| 25 | + } catch (e) { |
| 26 | + showAlert("error", "Failed to fetch contract address", 4000); |
| 27 | + } |
| 28 | + })(); |
| 29 | + } |
| 30 | + }, [isOpen]); |
11 | 31 |
|
12 | | - const handleSolutionSubmit = (solution: string) => { |
| 32 | + const handleSolutionSubmit = async (solution: number) => { |
| 33 | + /* |
| 34 | + // Call TaskManager smart contract |
| 35 | + try { |
| 36 | + if (!signer) throw new Error("Wallet not connected"); |
| 37 | + const contract = new (window as any).ethers.Contract( |
| 38 | + contractAddress, |
| 39 | + TaskManagerAbi, |
| 40 | + signer |
| 41 | + ); |
13 | 42 |
|
| 43 | + // TODO: Missing taskId and salt. Need to fetch them from backend with other task data! |
| 44 | + // TODO: Fix input |
| 45 | + // Prepare input for the circuit |
| 46 | + const commit = await computeCommit(BigInt(solution), salt); |
| 47 | + const input = { |
| 48 | + solution: BigInt(solution), |
| 49 | + taskId: BigInt(taskId), |
| 50 | + taskSalt: salt, |
| 51 | + commit: commit, |
| 52 | + recipient: account |
| 53 | + }; |
| 54 | + |
| 55 | + const { proof, publicSignals } = await snarkjs.groth16.fullProve(input, "/TaskProof.wasm", "/TaskProof_final.zkey"); |
| 56 | + // build calldata |
| 57 | + const calldata = await snarkjs.groth16.exportSolidityCallData(proof, publicSignals); |
| 58 | + const argv = calldata.replace(/["[\]\s]/g, "").split(",").map(x => BigInt(x).toString()); |
| 59 | +
|
| 60 | + const a = [argv[0], argv[1]]; |
| 61 | + const b = [[argv[2], argv[3]], [argv[4], argv[5]]]; |
| 62 | + const c = [argv[6], argv[7]]; |
| 63 | + const publicInputs = argv.slice(8); // should be [commit, taskId, taskSalt, recipient] |
| 64 | +
|
| 65 | + const tx = await contract.solveTask( |
| 66 | + taskId, |
| 67 | + account, // My wallet address |
| 68 | + a, |
| 69 | + b, |
| 70 | + c, |
| 71 | + publicInputs |
| 72 | + ); |
| 73 | + await tx.wait(); |
| 74 | + showAlert("success", `Task solved`, 4000); |
| 75 | + } catch (e) { |
| 76 | + console.log(e); |
| 77 | + showAlert("error", "Failed to call smart contract", 4000); |
| 78 | + return; |
| 79 | + } |
| 80 | + setIsOpen(false); |
14 | 81 | showAlert("info", `Task solution: "${solution}" subbmited!`, 4000); |
| 82 | + */ |
15 | 83 | } |
16 | 84 |
|
17 | 85 |
|
|
0 commit comments