diff --git a/backend/index.js b/backend/index.js index 118043e..6808a6a 100644 --- a/backend/index.js +++ b/backend/index.js @@ -102,30 +102,35 @@ const getUrlsStability = async (prompt) => { const urls_from_prompt = async (prompt) => { //const generations = (await dalle.generate(prompt)).data; //console.log("generations:", generations); - try { - const urlsStability = await getUrlsStability(prompt); - //image_url = response.data.data[0].url; - // download all images to local folder - const ids = await Promise.all(urlsStability.map(url => { - const id = uuidv4(); - // download image from url - return downloadImage(url, "public/" + id + ".jpg").then(() => id); - })); - console.log("ids:", ids); - // return list of ddalle urls - const urls = ids.map(id => `${DOMAIN}/${id}.jpg`); - return urls; - } catch (e) { - console.log("Error generating images for prompt", e.response.data); - } + const urlsStability = await getUrlsStability(prompt); + //image_url = response.data.data[0].url; + // download all images to local folder + const ids = await Promise.all(urlsStability.map(url => { + const id = uuidv4(); + // download image from url + return downloadImage(url, "public/" + id + ".jpg").then(() => id); + })); + console.log("ids:", ids); + // return list of ddalle urls + const urls = ids.map(id => `${DOMAIN}/${id}.jpg`); + return urls; } const wsOnConnect = (ws) => { ws.on('message', async (data) => { console.log('received: %s', data); - const prompt = JSON.parse(data).prompt; - const res_data = { type: "result", success: true, urls: await urls_from_prompt(prompt) }; - ws.send(JSON.stringify(res_data)); + try { + const prompt = JSON.parse(data).prompt; + const urls = await urls_from_prompt(prompt); + ws.send(JSON.stringify({ type: "result", success: true, urls })); + } catch (e) { + console.error("Error handling prompt message", e); + ws.send(JSON.stringify({ + type: "result", + success: false, + error: "Failed to generate images" + })); + } }); // keep heroku ws alive @@ -152,22 +157,19 @@ const submit = async (req, res) => { const address = DDALLE_DEPLOYMENT.address[chainId]; if (!address) return res.status(500).send({ success: false, error: "chainId not supported" }); - const signer = await getSigner(chainId); - // console.log("Made signer"); - - const contract = new ethers.Contract(submissionsContract, DDALLE_DEPLOYMENT.submissions_abi, signer); - // console.log("Made contract"); - - const url = signer.provider.connection.url; - const response = await axios.post(url, { - 'jsonrpc': '2.0', - 'id': 0, - 'method': 'klay_gasPrice', - }) - const { result: gasPrice } = response.data; - const estimation = await contract.estimateGas.submit(uri, prompt); - try { + const signer = await getSigner(chainId); + const contract = new ethers.Contract(submissionsContract, DDALLE_DEPLOYMENT.submissions_abi, signer); + + const url = signer.provider.connection.url; + const response = await axios.post(url, { + 'jsonrpc': '2.0', + 'id': 0, + 'method': 'klay_gasPrice', + }) + const { result: gasPrice } = response.data; + const estimation = await contract.estimateGas.submit(uri, prompt); + const txn = await contract.submit(uri, prompt, { gasLimit: estimation, gasPrice: gasPrice, @@ -177,8 +179,8 @@ const submit = async (req, res) => { txn }); } catch (e) { - console.log("Error submitting", e); - return res.status(500).send({ success: false, error: e }); + console.error("Error submitting", e); + return res.status(500).send({ success: false, error: e.message || "Failed to submit" }); } } @@ -192,21 +194,29 @@ const submissions = async (req, res) => { const address = DDALLE_DEPLOYMENT.address[chainId]; if (!address) return res.status(500).send({ success: false, error: "chainId not supported" }); - const signer = await getSigner(chainId); - const contract = new ethers.Contract(submissionsContract, DDALLE_DEPLOYMENT.submissions_abi, signer); - - return res.status(200).send({ - "description": await contract.getPrompt(submissionId), - "external_url": `https://ddalle.xyz/propose/${submissionsContract}`, - "image": await contract.getImageURL(submissionId), - "name": `${await contract.name()} #${submissionId}` - }) + try { + const signer = await getSigner(chainId); + const contract = new ethers.Contract(submissionsContract, DDALLE_DEPLOYMENT.submissions_abi, signer); + + return res.status(200).send({ + "description": await contract.getPrompt(submissionId), + "external_url": `https://ddalle.xyz/propose/${submissionsContract}`, + "image": await contract.getImageURL(submissionId), + "name": `${await contract.name()} #${submissionId}` + }) + } catch (e) { + console.error("Error fetching submission metadata", e); + return res.status(500).send({ success: false, error: e.message || "Failed to fetch submission" }); + } } setup().then(() => { init(); - downloadImage("https://storage.googleapis.com/decentralized-dall-e.appspot.com/generation-q5lkwJFPwIcMvcWK0PRbh1U0.jpg", "public/test.jpg"); - urls_from_prompt("A dog").then(console.log); + downloadImage("https://storage.googleapis.com/decentralized-dall-e.appspot.com/generation-q5lkwJFPwIcMvcWK0PRbh1U0.jpg", "public/test.jpg") + .catch((e) => console.error("Error downloading test image", e)); + urls_from_prompt("A dog") + .then(console.log) + .catch((e) => console.error("Error generating startup images", e)); const server = express() .use(express.static(path.join(__dirname, 'public'))) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index ad0ddd4..1feb86f 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -172,11 +172,11 @@ function Web3ContextProvider(props: any) { ], }); } catch (addError) { - // handle "add" error + console.error("Error adding chain to wallet", addError); } } } - connectWallet().then(); + connectWallet().catch((err) => console.error("Error reconnecting wallet", err)); switchingChains = false; } }, @@ -185,7 +185,7 @@ function Web3ContextProvider(props: any) { useEffect(() => { if (web3Modal.cachedProvider) { - connectWallet().then(); + connectWallet().catch((err) => console.error("Error connecting wallet", err)); } }, []); @@ -255,24 +255,27 @@ function GlobalDataProvider(props: any) { if (currBountyCnt === bountyRefreshCnt) { setBountyData(newBountyData); } + }) + .catch((err: any) => { + console.error("Error refreshing bounty data", err); }); } async function refreshBountyData() { if (chainId) { if (connected && web3) { - refreshBountyDataWithWeb3(web3, chainId).then(); + await refreshBountyDataWithWeb3(web3, chainId); } else { const web3 = new Web3( getChainData(chainId).rpc_url ); - refreshBountyDataWithWeb3(web3, chainId).then() + await refreshBountyDataWithWeb3(web3, chainId); } } } useEffect(() => { - refreshBountyData().then(); + refreshBountyData().catch((err) => console.error("Error refreshing bounty data", err)); }, [web3, provider, chainId, connected, networkId]) return ( diff --git a/frontend/src/components/Requesting.tsx b/frontend/src/components/Requesting.tsx index e86cc92..d17d4de 100644 --- a/frontend/src/components/Requesting.tsx +++ b/frontend/src/components/Requesting.tsx @@ -36,6 +36,9 @@ function Requesting() { web3 ).then((res) => { console.log("Result: ", res); + }).catch((e) => { + console.error("Error creating bounty", e); + alert("Error creating bounty: " + (e?.message || e)); }); setDescription(""); setPriceValue("0"); @@ -47,6 +50,8 @@ function Requesting() { web3.eth.getBalance(address).then((weiBalance) => { console.log("weiBalance", weiBalance); setWeiBalance(weiBalance); + }).catch((e: any) => { + console.error("Error fetching balance", e); }); } }, [web3, address, connected, chainId, networkId]); diff --git a/frontend/src/components/SubmitSection.tsx b/frontend/src/components/SubmitSection.tsx index c281a8d..2ef273b 100644 --- a/frontend/src/components/SubmitSection.tsx +++ b/frontend/src/components/SubmitSection.tsx @@ -23,10 +23,20 @@ function SubmitSection({ data }: { data: BountyT }) { useEffect(() => { if (lastMessage !== null) { - const resp = JSON.parse(lastMessage.data); + let resp; + try { + resp = JSON.parse(lastMessage.data); + } catch (e) { + console.error("Failed to parse websocket message", e); + return; + } console.log("Response: ", resp); if (resp.type === "result") { - if (!resp.success) alert("Error: " + resp); + if (!resp.success) { + alert("Error: " + (resp.error || "Failed to generate images")); + setLoading(false); + return; + } setResults(resp.urls); setLoading(false); @@ -65,6 +75,8 @@ function SubmitSection({ data }: { data: BountyT }) { const receipt = await txn.wait(); console.log("Receipt: ", receipt); }).catch((e) => { + console.error("Error proposing submission", e); + alert("Error proposing submission: " + (e?.message || e)); }).then(() => { setProposing(false); }); @@ -93,6 +105,8 @@ function SubmitSection({ data }: { data: BountyT }) { alert("Error: " + res.error); } }).catch((e) => { + console.error("Error proposing submission", e); + alert("Error proposing submission: " + (e?.message || e)); }).then(() => { setProposing(false); }); diff --git a/frontend/src/components/WinnerSection.tsx b/frontend/src/components/WinnerSection.tsx index 8c0988b..ab5b8cc 100644 --- a/frontend/src/components/WinnerSection.tsx +++ b/frontend/src/components/WinnerSection.tsx @@ -21,8 +21,9 @@ function WinnerSection(props: { submission: SubmissionT | null, bounty: BountyT, ).then((res) => { console.log("Result: ", res); chooseWinner(); - }).catch().then(() => { - + }).catch((e) => { + console.error("Error assigning winner", e); + alert("Error selecting winner: " + (e?.message || e)); }); }; diff --git a/frontend/src/contexts/SubmissionProvider.tsx b/frontend/src/contexts/SubmissionProvider.tsx index 1a88218..66c3dd8 100644 --- a/frontend/src/contexts/SubmissionProvider.tsx +++ b/frontend/src/contexts/SubmissionProvider.tsx @@ -32,7 +32,10 @@ function SubmissionProvider({ children, submissionsContract }: { children: any, } }) ) - ); + ) + .catch((err: any) => { + console.error("Error fetching submissions", err); + }); } if (chainId) { if (web3 && connected) { diff --git a/frontend/src/helpers/web3.ts b/frontend/src/helpers/web3.ts index 5b87c89..9421a24 100644 --- a/frontend/src/helpers/web3.ts +++ b/frontend/src/helpers/web3.ts @@ -51,8 +51,9 @@ export function callMakeTask( (err: any, data: any) => { if (err) { reject(err) + } else { + resolve(data) } - resolve(data) } ) } catch (err) { @@ -148,8 +149,9 @@ export function callSubmit( (err: any, data: any) => { if (err) { reject(err) + } else { + resolve(data) } - resolve(data) } ) } catch (err) { @@ -181,8 +183,9 @@ export function callAssignWinner( (err: any, data: any) => { if (err) { reject(err) + } else { + resolve(data) } - resolve(data) } ) } catch (err) {