Use checkbox to toggle between buy and sell

This commit is contained in:
ekzyis 2023-12-02 02:17:00 +01:00
parent 6bdd4e19b2
commit b8898451ea
5 changed files with 174 additions and 291 deletions

View File

@ -1,135 +0,0 @@
<template>
<div>
<button type="button" :class="yesClass" class="label success font-mono mx-1 my-3"
@click.prevent="toggleYes">YES</button>
<button type="button" :class="noClass" class="label error font-mono mx-1 my-3" @click.prevent="toggleNo">NO</button>
<form v-show="showForm" @submit.prevent="submitForm">
<label for="stake">how much?</label>
<input name="stake" v-model="stake" type="number" min="0" placeholder="sats" required />
<label for="certainty">how sure?</label>
<input name="certainty" v-model="certainty" type="number" min="0.01" max="0.99" step="0.01" required />
<label>you receive:</label>
<label>{{ format(shares) }} {{ selected }} shares @ {{ format(price) }} sats</label>
<label>you pay:</label>
<label>{{ format(cost) }} sats</label>
<label>if you win:</label>
<label>+{{ format(profit) }} sats</label>
<button class="col-span-2" type="submit">submit buy order</button>
</form>
<div v-if="err" class="red text-center">{{ err }}</div>
</div>
</template>
<script setup>
import { useSession } from '@/stores/session'
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
const session = useSession()
const router = useRouter()
const route = useRoute()
const marketId = route.params.id
const selected = ref(route.query.share || null)
const showForm = computed(() => selected.value !== null)
const err = ref(null)
// how much wants the user bet?
const stake = ref(route.query.stake || 100)
// how sure is the user he will win?
const certainty = ref(route.query.certainty || 0.5)
// price per share: more risk, lower price, higher reward
const price = computed(() => certainty.value * 100)
// how many (full) shares can be bought?
const shares = computed(() => {
const val = price.value > 0 ? stake.value / price.value : null
// only full shares can be bought
return Math.round(val)
})
// how much does this order cost?
const cost = computed(() => {
return shares.value * price.value
})
// how high is the potential reward?
const profit = computed(() => {
// shares expire at 10 or 0 sats
const val = (100 * shares.value) - cost.value
return isNaN(val) ? 0 : val
})
const format = (x, i = 3) => x === null ? null : x >= 1 ? Math.round(x) : x === 0 ? x : x.toFixed(i)
const market = ref(null)
const url = '/api/market/' + marketId
await fetch(url)
.then(r => r.json())
.then(body => {
market.value = body
})
.catch(console.error)
// Currently, we only support binary markets.
// (only events which can be answered with YES and NO)
const yesShareId = computed(() => {
return market?.value.Shares.find(s => s.Description === 'YES').Id
})
const noShareId = computed(() => {
return market?.value.Shares.find(s => s.Description === 'NO').Id
})
const shareId = computed(() => {
return selected.value === 'YES' ? yesShareId.value : noShareId.value
})
const submitForm = async () => {
if (!session.isAuthenticated) return router.push('/login')
// TODO validate form
const url = window.origin + '/api/order'
const body = JSON.stringify({
sid: shareId.value,
quantity: shares.value,
price: price.value,
// TODO support selling
side: 'BUY'
})
const res = await fetch(url, { method: 'POST', headers: { 'Content-type': 'application/json' }, body })
const resBody = await res.json()
if (res.status !== 402) {
err.value = `error: server responded with HTTP ${resBody.status}`
return
}
const invoiceId = resBody.id
router.push('/invoice/' + invoiceId)
}
const toggleYes = () => {
selected.value = selected.value === 'YES' ? null : 'YES'
}
const toggleNo = () => {
selected.value = selected.value === 'NO' ? null : 'NO'
}
const yesClass = computed(() => selected.value === 'YES' ? ['active'] : [])
const noClass = computed(() => selected.value === 'NO' ? ['active'] : [])
</script>
<style scoped>
.success.active {
background-color: #35df8d;
color: white;
}
.error.active {
background-color: #ff7386;
color: white;
}
form {
margin: 0 auto;
display: grid;
grid-template-columns: auto auto;
}
form>* {
margin: 0.5em 1em;
}
</style>

View File

@ -1,7 +1,7 @@
<template>
<ul>
<li class="my-3" v-for="market in markets" :key="market.id">
<router-link :to="'/market/' + market.id + '/form/buy'">{{ market.description }}</router-link>
<router-link :to="'/market/' + market.id + '/form'">{{ market.description }}</router-link>
</li>
</ul>
<button v-if="!showForm" @click.prevent="toggleForm">+ create market</button>

View File

@ -1,31 +1,189 @@
<template>
<div>
<header class="flex flex-row text-center justify-center pt-1">
<nav>
<StyledLink :to="'/market/' + marketId + '/form/buy'">buy</StyledLink>
<StyledLink :to="'/market/' + marketId + '/form/sell'">sell</StyledLink>
</nav>
</header>
<router-view />
<div>
<button type="button" :class="yesClass" class="label success font-mono mx-1 my-3"
@click.prevent="toggleYes">YES</button>
<button type="button" :class="noClass" class="label error font-mono mx-1 my-3" @click.prevent="toggleNo">NO</button>
</div>
<div v-if="userShares > 0 && showForm" class="flex justify-center items-center">
<label for="side" class="m-1"><i>sell?</i></label>
<input v-model="side" true-value="SELL" false-value="BUY" type="checkbox" name="side" class="m-1"/>
</div>
<form v-if="side === 'BUY'" v-show="showForm" @submit.prevent="submitBuyForm">
<label for="stake">how much?</label>
<input name="stake" v-model="stake" type="number" min="0" placeholder="sats" required />
<label for="certainty">how sure?</label>
<input name="certainty" v-model="certainty" type="number" min="0.01" max="0.99" step="0.01" required />
<label>you receive:</label>
<label>{{ format(shares) }} {{ selected }} shares @ {{ format(price) }} sats</label>
<label>you pay:</label>
<label>{{ format(cost) }} sats</label>
<label>if you win:</label>
<label>+{{ format(profit) }} sats</label>
<button class="col-span-2" type="submit">submit buy order</button>
</form>
<form v-else v-show="showForm" @submit.prevent="submitSellForm">
<label for="inv">you have:</label>
<label name="inv">{{ userShares }} shares</label>
<label for="shares">how many?</label>
<input name="shares" v-model="shares" type="number" min="1" :max="userShares" placeholder="shares" required />
<label for="price">price?</label>
<input name="price" v-model="price" type="number" min="1" max="99" step="1" required />
<label>you sell:</label>
<label>{{ shares }} {{ selected }} shares @ {{ price }} sats</label>
<label>you make:</label>
<label>{{ format(profit) }} sats</label>
<button class="col-span-2" type="submit" :disabled="disabled">submit sell order</button>
</form>
<div v-if="err" class="red text-center">{{ err }}</div>
</div>
</template>
<script setup>
import { useRoute } from 'vue-router'
import StyledLink from '@/components/StyledLink'
import { useSession } from '@/stores/session'
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
const session = useSession()
const router = useRouter()
const route = useRoute()
const marketId = route.params.id
// YES NO button logic
// -- which button was pressed?
const selected = ref(route.query.share || null)
// -- button css
const yesClass = computed(() => selected.value === 'YES' ? ['active'] : [])
const noClass = computed(() => selected.value === 'NO' ? ['active'] : [])
// -- show form if any button was pressed
const showForm = computed(() => selected.value !== null)
const toggleYes = () => {
selected.value = selected.value === 'YES' ? null : 'YES'
}
const toggleNo = () => {
selected.value = selected.value === 'NO' ? null : 'NO'
}
// show errors below form
const err = ref(null)
// BUY or SELL?
const side = ref(route.query.side || 'BUY')
// -- BUY params
// how much wants the user bet?
const stake = ref(route.query.stake || 100)
// how sure is the user he will win?
const certainty = ref(route.query.certainty || 0.5)
// price per share: more risk, lower price, higher reward
const price = computed(() => certainty.value * 100)
// how many (full) shares can be bought?
const shares = computed(() => {
const val = price.value > 0 ? stake.value / price.value : null
// only full shares can be bought
return Math.round(val)
})
// how much does this order cost?
const cost = computed(() => {
return shares.value * price.value
})
// how high is the potential reward?
const profit = computed(() => {
// shares expire at 10 or 0 sats
const val = (100 * shares.value) - cost.value
return isNaN(val) ? 0 : val
})
// -- SELL params
// how many shares does the user own?
const userShares = computed(() => (((selected.value === 'YES' ? market.value.user?.YES : market.value.user?.NO) || 0) - sold.value))
// how many share did the user sell since we refreshed our data?
const sold = ref(0)
const disabled = computed(() => userShares.value === 0)
const format = (x, i = 3) => x === null ? null : x >= 1 ? Math.round(x) : x === 0 ? x : x.toFixed(i)
// Fetch market data
const market = ref(null)
const url = '/api/market/' + marketId
await fetch(url)
.then(r => r.json())
.then(body => {
market.value = body
})
.catch(console.error)
// Currently, we only support binary markets.
// (only events which can be answered with YES and NO)
const yesShareId = computed(() => {
return market?.value.Shares.find(s => s.Description === 'YES').Id
})
const noShareId = computed(() => {
return market?.value.Shares.find(s => s.Description === 'NO').Id
})
const shareId = computed(() => {
return selected.value === 'YES' ? yesShareId.value : noShareId.value
})
const submitBuyForm = async () => {
if (!session.isAuthenticated) return router.push('/login')
// TODO validate form
const url = window.origin + '/api/order'
const body = JSON.stringify({
sid: shareId.value,
quantity: shares.value,
price: price.value,
side: 'BUY'
})
const res = await fetch(url, { method: 'POST', headers: { 'Content-type': 'application/json' }, body })
const resBody = await res.json()
if (res.status !== 402) {
err.value = `error: server responded with HTTP ${resBody.status}`
return
}
const invoiceId = resBody.id
router.push('/invoice/' + invoiceId)
}
const submitSellForm = async () => {
if (!session.isAuthenticated) return router.push('/login')
// TODO validate form
const url = window.origin + '/api/order'
const body = JSON.stringify({
sid: shareId.value,
quantity: shares.value,
price: price.value,
side: 'SELL'
})
const res = await fetch(url, { method: 'POST', headers: { 'Content-type': 'application/json' }, body })
const resBody = await res.json()
if (res.status !== 402) {
err.value = `error: server responded with HTTP ${resBody.status}`
return
}
const invoiceId = resBody.id
router.push('/invoice/' + invoiceId)
}
</script>
<style scoped>
nav {
display: flex;
justify-content: center;
.success.active {
background-color: #35df8d;
color: white;
}
nav>a {
margin: 0 3px;
.error.active {
background-color: #ff7386;
color: white;
}
form {
margin: 0 auto;
display: grid;
grid-template-columns: auto auto;
}
form>* {
margin: 0.5em 1em;
}
</style>

View File

@ -1,125 +0,0 @@
<template>
<div>
<button type="button" :class="yesClass" class="label success font-mono mx-1 my-3"
@click.prevent="toggleYes">YES</button>
<button type="button" :class="noClass" class="label error font-mono mx-1 my-3" @click.prevent="toggleNo">NO</button>
<form v-show="showForm" @submit.prevent="submitForm">
<label for="inv">you have:</label>
<label name="inv">{{ userShares }} shares</label>
<label for="shares">how many?</label>
<input name="shares" v-model="shares" type="number" min="1" :max="userShares" placeholder="shares" required />
<label for="price">price?</label>
<input name="price" v-model="price" type="number" min="1" max="99" step="1" required />
<label>you sell:</label>
<label>{{ shares }} {{ selected }} shares @ {{ price }} sats</label>
<label>you make:</label>
<label>{{ format(profit) }} sats</label>
<button class="col-span-2" type="submit" :disabled="disabled">submit sell order</button>
</form>
<div v-if="err" class="red text-center">{{ err }}</div>
</div>
</template>
<script setup>
import { useSession } from '@/stores/session'
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
const session = useSession()
const router = useRouter()
const route = useRoute()
const marketId = route.params.id
const selected = ref(route.query.share || null)
const showForm = computed(() => selected.value !== null)
const err = ref(null)
// how many shares wants the user sell?
const shares = ref(route.query.shares || 0)
// at which price?
const price = ref(route.query.price || 50)
// how high is the potential reward?
const profit = computed(() => {
const val = shares.value * price.value
return isNaN(val) ? 0 : val
})
const format = (x, i = 3) => x === null ? null : x >= 1 ? Math.round(x) : x === 0 ? x : x.toFixed(i)
const market = ref(null)
const url = '/api/market/' + marketId
await fetch(url)
.then(r => r.json())
.then(body => {
market.value = body
})
.catch(console.error)
// Currently, we only support binary markets.
// (only events which can be answered with YES and NO)
const yesShareId = computed(() => market?.value.Shares.find(s => s.Description === 'YES').Id)
const noShareId = computed(() => market?.value.Shares.find(s => s.Description === 'NO').Id)
const shareId = computed(() => selected.value === 'YES' ? yesShareId.value : noShareId.value)
const sold = ref(0)
const userShares = computed(() => (((selected.value === 'YES' ? market.value.user?.YES : market.value.user?.NO) || 0) - sold.value))
const disabled = computed(() => userShares.value === 0)
const submitForm = async () => {
if (!session.isAuthenticated) return router.push('/login')
// TODO validate form
const url = window.origin + '/api/order'
const body = JSON.stringify({
sid: shareId.value,
quantity: shares.value,
price: price.value,
// TODO support selling
side: 'SELL'
})
const res = await fetch(url, { method: 'POST', headers: { 'Content-type': 'application/json' }, body })
const resBody = await res.json()
if (res.status === 201) {
sold.value += shares.value
return
}
if (res.status !== 402) {
err.value = `error: server responded with HTTP ${resBody.status}`
return
}
const invoiceId = resBody.id
router.push('/invoice/' + invoiceId)
}
const toggleYes = () => {
selected.value = selected.value === 'YES' ? null : 'YES'
}
const toggleNo = () => {
selected.value = selected.value === 'NO' ? null : 'NO'
}
const yesClass = computed(() => selected.value === 'YES' ? ['active'] : [])
const noClass = computed(() => selected.value === 'NO' ? ['active'] : [])
</script>
<style scoped>
.success.active {
background-color: #35df8d;
color: white;
}
.error.active {
background-color: #ff7386;
color: white;
}
form {
margin: 0 auto;
display: grid;
grid-template-columns: auto auto;
}
form>* {
margin: 0.5em 1em;
}
</style>

View File

@ -16,9 +16,6 @@ import UserOrders from '@/components/UserOrders'
import OrderForm from '@/components/OrderForm'
import MarketOrders from '@/components/MarketOrders'
import MarketStats from '@/components/MarketStats'
import BuyOrderForm from '@/components/BuyOrderForm'
import SellOrderForm from '@/components/SellOrderForm'
const routes = [
{
path: '/', component: HomeView
@ -39,19 +36,7 @@ const routes = [
path: '/market/:id',
component: MarketView,
children: [
{
path: 'form',
name: 'form',
component: OrderForm,
children: [
{
path: 'buy', name: 'form-buy', component: BuyOrderForm
},
{
path: 'sell', name: 'form-sell', component: SellOrderForm
}
]
},
{ path: 'form', name: 'form', component: OrderForm },
{ path: 'orders', name: 'market-orders', component: MarketOrders },
{ path: 'stats', name: 'market-stats', component: MarketStats }
]