Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 57 additions & 47 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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" });
}
}

Expand All @@ -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')))
Expand Down
15 changes: 9 additions & 6 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
},
Expand All @@ -185,7 +185,7 @@ function Web3ContextProvider(props: any) {

useEffect(() => {
if (web3Modal.cachedProvider) {
connectWallet().then();
connectWallet().catch((err) => console.error("Error connecting wallet", err));
}
}, []);

Expand Down Expand Up @@ -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 (
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/components/Requesting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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]);
Expand Down
18 changes: 16 additions & 2 deletions frontend/src/components/SubmitSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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);
});
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/components/WinnerSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
};

Expand Down
5 changes: 4 additions & 1 deletion frontend/src/contexts/SubmissionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ function SubmissionProvider({ children, submissionsContract }: { children: any,
}
})
)
);
)
.catch((err: any) => {
console.error("Error fetching submissions", err);
});
}
if (chainId) {
if (web3 && connected) {
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/helpers/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ export function callMakeTask(
(err: any, data: any) => {
if (err) {
reject(err)
} else {
resolve(data)
}
resolve(data)
}
)
} catch (err) {
Expand Down Expand Up @@ -148,8 +149,9 @@ export function callSubmit(
(err: any, data: any) => {
if (err) {
reject(err)
} else {
resolve(data)
}
resolve(data)
}
)
} catch (err) {
Expand Down Expand Up @@ -181,8 +183,9 @@ export function callAssignWinner(
(err: any, data: any) => {
if (err) {
reject(err)
} else {
resolve(data)
}
resolve(data)
}
)
} catch (err) {
Expand Down
Loading