109 lines
3.7 KiB
JavaScript
109 lines
3.7 KiB
JavaScript
const marketId = document.querySelector("#market-id").value
|
|
const yesShareId = document.querySelector("#yes-share").value
|
|
const noShareId = document.querySelector("#no-share").value
|
|
|
|
const yesOrderBtn = document.querySelector("#yes-order")
|
|
const yesForm = document.querySelector("#yes-form")
|
|
const yesBuyBtn = document.querySelector("#yes-buy")
|
|
const yesSellBtn = document.querySelector("#yes-sell")
|
|
const yesSideInput = document.querySelector("#yes-side")
|
|
const yesQuantityInput = document.querySelector("#yes-quantity")
|
|
const yesCostDisplay = document.querySelector("#yes-cost")
|
|
const yesCostLabel = document.querySelector("#yes-cost-label")
|
|
const yesSubmitLabel = document.querySelector("#yes-submit-label")
|
|
|
|
const noOrderBtn = document.querySelector("#no-order")
|
|
const noForm = document.querySelector("#no-form")
|
|
const noBuyBtn = document.querySelector("#no-buy")
|
|
const noSellBtn = document.querySelector("#no-sell")
|
|
const noSideInput = document.querySelector("#no-side")
|
|
const noQuantityInput = document.querySelector("#no-quantity")
|
|
const noCostDisplay = document.querySelector("#no-cost")
|
|
const noCostLabel = document.querySelector("#no-cost-label")
|
|
const noSubmitLabel = document.querySelector("#no-submit-label")
|
|
|
|
function resetInputs() {
|
|
yesQuantityInput.value = undefined
|
|
yesCostDisplay.value = undefined
|
|
noQuantityInput.value = undefined
|
|
noCostDisplay.value = undefined
|
|
}
|
|
|
|
function showYesForm() {
|
|
resetInputs()
|
|
yesOrderBtn.classList.add("selected")
|
|
yesForm.style.display = "grid"
|
|
noOrderBtn.classList.remove("selected")
|
|
noForm.style.display = "none"
|
|
}
|
|
yesOrderBtn.onclick = showYesForm
|
|
|
|
function showNoForm() {
|
|
resetInputs()
|
|
noOrderBtn.classList.add("selected")
|
|
noForm.style.display = "grid"
|
|
yesOrderBtn.classList.remove("selected")
|
|
yesForm.style.display = "none"
|
|
}
|
|
noOrderBtn.onclick = showNoForm
|
|
|
|
function showBuyForm() {
|
|
resetInputs()
|
|
yesBuyBtn.classList.add("selected")
|
|
yesSellBtn.classList.remove("selected")
|
|
yesCostLabel.textContent = 'cost [sats]'
|
|
yesSubmitLabel.textContent = 'BUY YES shares'
|
|
yesSideInput.value = "BUY"
|
|
|
|
noBuyBtn.classList.add("selected")
|
|
noSellBtn.classList.remove("selected")
|
|
noCostLabel.textContent = 'cost [sats]'
|
|
noSubmitLabel.textContent = 'BUY YES shares'
|
|
noSideInput.value = "BUY"
|
|
}
|
|
function showSellForm() {
|
|
resetInputs()
|
|
yesBuyBtn.classList.remove("selected")
|
|
yesSellBtn.classList.add("selected")
|
|
yesCostLabel.textContent = 'payout [sats]'
|
|
yesSubmitLabel.textContent = 'SELL NO shares'
|
|
yesSideInput.value = "SELL"
|
|
|
|
noBuyBtn.classList.remove("selected")
|
|
noSellBtn.classList.add("selected")
|
|
noCostLabel.textContent = 'payout [sats]'
|
|
noSubmitLabel.textContent = 'SELL YES shares'
|
|
noSideInput.value = "SELL"
|
|
}
|
|
yesBuyBtn.onclick = showBuyForm
|
|
yesSellBtn.onclick = showSellForm
|
|
noBuyBtn.onclick = showBuyForm
|
|
noSellBtn.onclick = showSellForm
|
|
|
|
function updatePrice(marketId, shareId) {
|
|
return async function (e) {
|
|
const quantity = parseInt(e.target.value, 10)
|
|
const body = {
|
|
share_id: shareId,
|
|
quantity,
|
|
side: yesSideInput.value
|
|
}
|
|
const rBody = await fetch(`/api/market/${marketId}/cost`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-type": "application/json"
|
|
},
|
|
body: JSON.stringify(body)
|
|
})
|
|
.then(r => r.json())
|
|
.catch((err) => {
|
|
console.error(err);
|
|
return null
|
|
})
|
|
if (!rBody) return null;
|
|
yesCostDisplay.value = parseFloat(Math.abs(rBody.cost)).toFixed(3)
|
|
}
|
|
}
|
|
yesQuantityInput.onchange = updatePrice(marketId, yesShareId)
|
|
noQuantityInput.onchange = updatePrice(marketId, noShareId)
|