diff --git a/public/app.js b/public/app.js index a2b9a37..7b7ba4c 100644 --- a/public/app.js +++ b/public/app.js @@ -43,11 +43,9 @@ app.get('/question_feed', async function(request, response) { }); app.get('/question_hash', async function(request, response) { - console.log("/GET question_hash " + request); - let q_summary = request.query.summary; - let q_hash = sha256(q_summary); + console.log("/GET question_hash summary: " + request.query.summary); let data = { - q_hash: q_hash + qHash: sha256(request.query.summary) }; response.send(JSON.stringify(data)); }); @@ -69,7 +67,7 @@ app.post('/submit_question', async function(request, response) { let bounty = Number(request.body.bounty); let asker_addr = request.body.asker_addr; - let returnData = await model.createQuestion(bounty, request.body.time_exp_days, request.body.time_exp_hours, request.body.time_exp_minutes, request.body.title, request.body.details, asker_addr); + let returnData = await model.createQuestion(bounty, request.body.min_execution_date, request.body.title, request.body.details, asker_addr); response.set('Content-type', 'application/json'); response.status(STATUS_OK); @@ -118,7 +116,7 @@ async function clearDB() { async function initDB() { let askerAddr = await model.createUser("mchang4", "0x66FDDd026Dbf64D6F907154365113ae124eB2DD6"); let answererAddr = await model.createUser("peterlu6", "0xd08923976D510F8f834E1B8BC4E1c03599F2644F"); - let returnData = await model.createQuestion(50, 10, 1, 23, "how do i make friends", "i have no friends", askerAddr); + let returnData = await model.createQuestion(50, Date.now() + 100000, "how do i make friends", "i have no friends", askerAddr); let answerObject = await model.createAnswer(answererAddr, returnData.questionId, "plastic surgery"); // await model.markQuestionClosed(returnData.questionId); } diff --git a/public/app/js/metamask.js b/public/app/js/metamask.js index 2cb8874..82a7200 100644 --- a/public/app/js/metamask.js +++ b/public/app/js/metamask.js @@ -47,12 +47,12 @@ function app() { }).catch(console.error); function refreshBalance() { - console.log("refreshed"); + console.log("refreshed"); } - window.collectBounty = function (qHash, bounty, callback) { - console.log("collected bounty with qHash: " + qHash + " and bounty: " + bounty); - contract.methods.collectBounty(userAccount, qHash, bounty).send({from: userAccount, to: contractAddress, value: bounty}) + window.collectBounty = function (qHash, bounty, minExecutionDate, callback) { + console.log("collected bounty with qHash: " + qHash + ", bounty: " + bounty + ", and expTime: " + minExecutionDate); + contract.methods.collectBounty(userAccount, qHash, bounty, minExecutionDate).send({from: userAccount, to: contractAddress, value: bounty}) .then(callback) .catch(console.error); }; diff --git a/public/app/js/model.js b/public/app/js/model.js index cc22eaf..826c7c1 100644 --- a/public/app/js/model.js +++ b/public/app/js/model.js @@ -67,11 +67,9 @@ async function markQuestionClosed(questionId) { console.log("marked question closed"); } -var createQuestion = async function(bounty, timeExpDays, timeExpHours, timeExpMinutes, title, body, askerAddr) { +var createQuestion = async function(bounty, timeExp, title, body, askerAddr) { console.log("askerAddr: " + askerAddr); let questionHash = sha256(bounty + title + body + askerAddr); - let timeToClose = timeExpDays * 24 * 3600 * 1000 + timeExpHours * 3600 * 1000 + timeExpMinutes * 60 * 1000 - let timeExp = Date.now() + timeToClose let newQuestion = new schema.Question ({ answers: [], bounty: bounty, @@ -84,6 +82,9 @@ var createQuestion = async function(bounty, timeExpDays, timeExpHours, timeExpMi askerAddr: askerAddr, state: OPEN_STATE }); + console.log(timeExp); + let timeToClose = timeExp - Date.now(); + console.log("timeToClose: " + timeToClose); try { let savedQuestion = await newQuestion.save(); await schema.User.findOneAndUpdate({address: askerAddr}, {$push: {questions: savedQuestion.id}}, {upsert: true}); diff --git a/public/app/js/newsfeed-view.js b/public/app/js/newsfeed-view.js index 777deba..a68346f 100755 --- a/public/app/js/newsfeed-view.js +++ b/public/app/js/newsfeed-view.js @@ -47,6 +47,8 @@ function submitQuestion() { let q_time_exp_days = $("#time_exp_days").val() let q_time_exp_hours = $("#time_exp_hours").val() let q_time_exp_minutes = $("#time_exp_minutes").val() + let timeToClose = q_time_exp_days * 24 * 3600 * 1000 + q_time_exp_hours * 3600 * 1000 + q_time_exp_minutes * 60 * 1000 + let q_min_execution_date = Date.now() + timeToClose; if (q_time_exp_days > 20 || q_time_exp_hours > 23 || q_time_exp_minutes > 59) { $("#submit_question_error").html("Must submit a valid time less than 20 days."); @@ -57,6 +59,8 @@ function submitQuestion() { return; } + let asker_addr = localStorage.getItem("userAccount"); + $("#question_title").val(""); $("textarea").val(""); $("#bounty_amount").val(null) @@ -64,21 +68,20 @@ function submitQuestion() { let question_data = { title: q_title, details: q_details, - asker_addr: localStorage.getItem("userAccount"), + asker_addr: asker_addr, bounty: q_bounty, - time_exp_days: q_time_exp_days, - time_exp_hours: q_time_exp_hours, - time_exp_minutes: q_time_exp_minutes + min_execution_date: q_min_execution_date } - let q_summary_string = q_bounty + q_title + q_details + localStorage.getItem("userAccount"); + let q_summary_string = q_bounty + q_title + q_details + asker_addr; let xmlHashRequest = new XMLHttpRequest(); xmlHashRequest.addEventListener('load', function() { if (xmlHashRequest.status === 200) { - var hashData = JSON.parse(xmlHashRequest.responseText) - collectBounty(hashData.q_hash, question_data.bounty, function() { + console.log(JSON.stringify(xmlHashRequest.responseText)) + let hashData = JSON.parse(xmlHashRequest.responseText); + collectBounty(hashData.qHash, q_bounty, q_min_execution_date, function() { console.log("adding data"); PostModel.add(question_data); });