Search…
Task Function
In this example we break up the task into the main task function plus a helper function. The helper function getQuoteOfTheDay call the quote of the day API and returns the JSON response data.
// Get quote of the day JSON
async function getQuoteOfTheDay() {
axios({
method: 'get',
url: 'https://favqs.com/api/qotd'
}).then(async (response) => {
if (response.data) {
return response.data;
}
}).catch(err => {
console.log("Failed to get Quote of the day", err.message);
})
​
}
The task function gets the quote data from the helper function then signs and writes that data to IPFS using Web3.Storage. It then takes the CID of the data returned and submits it to K2.
async function task() {
const quoteOfTheDay = getQuoteOfTheDay();
const signedJSON = await namespace.signData(qodJSON);
fs.writeFileSync("qod.json", signedJSON);
if (storageClient) {
// Storing on IPFS through web3 storage as example
const file = await getFilesFromPath('./qod.json');
const cid = await storageClient.put(file);
console.log("CID of Uploaded Data: ", cid);
await namespace.redisSet('cid', cid);
await namespace.checkSubmissionAndUpdateRound(cid);
} else {
console.error("No web3 storage API key provided");
}
}
Copy link