Compare commits

...

2 Commits

Author SHA1 Message Date
ekzyis fe8c55815d Limit y to 0..1 2024-10-03 09:23:35 -05:00
ekzyis 1e9c33bc89 Colorize potential earn 2024-10-02 16:40:58 -05:00
2 changed files with 27 additions and 11 deletions

View File

@ -20,7 +20,7 @@ const element = document.getElementById('chart')
function transformPoint({ X, Y }) { function transformPoint({ X, Y }) {
return { x: new Date(X), y: Y * 100 } return { x: new Date(X), y: Y }
} }
const no = JSON.parse($("#chart-data").getAttribute("chart-data-p0")).map(transformPoint) const no = JSON.parse($("#chart-data").getAttribute("chart-data-p0")).map(transformPoint)
@ -55,8 +55,10 @@ const config = {
time: { unit: 'month' } time: { unit: 'month' }
}, },
y: { y: {
display: true,
text: 'price',
min: 0, min: 0,
max: 100 max: 1
} }
} }
} }

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"git.ekzyis.com/ekzyis/delphi.market/types" "git.ekzyis.com/ekzyis/delphi.market/types"
"strconv" "strconv"
"math"
) )
templ MarketForm(m types.Market, outcome int, q types.MarketQuote, uQ int) { templ MarketForm(m types.Market, outcome int, q types.MarketQuote, uQ int) {
@ -18,14 +19,16 @@ templ MarketForm(m types.Market, outcome int, q types.MarketQuote, uQ int) {
> >
<input type="hidden" name="o" value={ fmt.Sprint(outcome) }/> <input type="hidden" name="o" value={ fmt.Sprint(outcome) }/>
<div class="none col-span-2 htmx-request"></div> <div class="none col-span-2 htmx-request"></div>
<label for="p">avg price per share:</label> <label for="p">price:</label>
<div id="p">{ formatPrice(q.AvgPrice) }</div> <div id="p">{ formatPrice(q.AvgPrice, 3) }</div>
<label for="q">how many?</label> <label for="q">shares:</label>
<input <input
id={ inputId(outcome) } id={ inputId(outcome) }
name="q" name="q"
class="text-black px-1" class="text-black px-1"
type="number" type="number"
step="100"
min="0"
autofocus autofocus
hx-get={ fmt.Sprintf("/market/%d", m.Id) } hx-get={ fmt.Sprintf("/market/%d", m.Id) }
hx-replace-url="true" hx-replace-url="true"
@ -37,10 +40,10 @@ templ MarketForm(m types.Market, outcome int, q types.MarketQuote, uQ int) {
hx-disabled-elt="next button" hx-disabled-elt="next button"
hx-indicator={ hxIndicator(outcome) } hx-indicator={ hxIndicator(outcome) }
/> />
<label for="total">you pay:</label> <label for="total">total:</label>
<div id="total">{ formatPrice(q.TotalPrice) }</div> <div id="total">{ formatPrice(q.TotalPrice, 3) }</div>
<label for="reward">{ "if you win:" }</label> <label for="reward">{ "potential earn:" }</label>
<div id="reward">+{ formatPrice(q.Reward) }</div> <span id="reward" class={ colorize(q.Reward) }>+{ formatPrice(q.Reward, 3) }</span>
<label for="uQ">you have:</label> <label for="uQ">you have:</label>
<div id="uQ">{ fmt.Sprint(uQ) }</div> <div id="uQ">{ fmt.Sprint(uQ) }</div>
<button type="submit" class="col-span-2">submit</button> <button type="submit" class="col-span-2">submit</button>
@ -61,6 +64,17 @@ func hxIndicator(outcome int) string {
formId(outcome), formId(outcome), formId(outcome)) formId(outcome), formId(outcome), formId(outcome))
} }
func formatPrice(p float64) string { func formatPrice(p float64, n int) string {
return fmt.Sprintf("%v sats", strconv.FormatFloat(p, 'f', 3, 64)) if p == 0 || math.IsNaN(p) {
return "0 sats"
}
return fmt.Sprintf("%v sats", strconv.FormatFloat(p, 'f', n, 64))
} }
func colorize(f float64) string {
eps := 1e-5
if f-eps > 0 {
return "text-success"
}
return "text-reset"
}