Skip to content

Commit

Permalink
multi: use average rate for market order display
Browse files Browse the repository at this point in the history
Signed-off-by: Philemon Ukane <ukanephilemon@gmail.com>
  • Loading branch information
ukane-philemon committed Aug 8, 2023
1 parent 79fa969 commit 95fdaf9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
10 changes: 7 additions & 3 deletions client/core/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,19 @@ func (ord *OrderReader) StatusString() string {

// SimpleRateString is the formatted match rate.
func (ord *OrderReader) SimpleRateString() string {
if ord.Type == order.MarketOrderType {
return ord.AverageRateString()
}
return ord.formatRate(ord.Rate)
}

// RateString is a formatted rate with units.
func (ord *OrderReader) RateString() string {
rateStr := ord.formatRate(ord.Rate)
if ord.Type == order.MarketOrderType {
return "market"
rateStr = ord.AverageRateString()
}
return fmt.Sprintf("%s %s/%s", ord.formatRate(ord.Rate), ord.QuoteSymbol, ord.BaseSymbol)
return fmt.Sprintf("%s %s/%s", rateStr, ord.QuoteSymbol, ord.BaseSymbol)
}

// AverageRateString returns a formatting string containing the average rate of
Expand All @@ -332,7 +336,7 @@ func (ord *OrderReader) AverageRateString() string {
baseQty += match.Qty
rateProduct += match.Rate * match.Qty // order ~ 1e16
}
return ord.formatRate(rateProduct / baseQty)
return "~ " + ord.formatRate(rateProduct/baseQty)
}

// SwapFeesString is a formatted string of the paid swap fees.
Expand Down
2 changes: 1 addition & 1 deletion client/webserver/site/src/html/bodybuilder.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
{{end}}

{{define "bottom"}}
<script src="/js/entry.js?v=fd4f6cab|6e85ed8b"></script>
<script src="/js/entry.js?v=e9f3d67b|cfdf763f"></script>
</body>
</html>
{{end}}
2 changes: 1 addition & 1 deletion client/webserver/site/src/js/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ export default class Application {
const wallet = this.walletMap[fromAssetID]
if (!wallet || !(wallet.traits & walletTraitAccelerator)) return false
if (order.matches) {
for (let i = 0; i < order.matches.length; i++) {
for (let i = 0; i < order.matches?.length; i++) {
const match = order.matches[i]
if (match.swap && match.swap.confs && match.swap.confs.count === 0 && !match.revoked) {
return true
Expand Down
8 changes: 7 additions & 1 deletion client/webserver/site/src/js/markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,13 @@ export default class MarketsPage extends BasePage {
details.side.classList.add(ord.sell ? 'sellcolor' : 'buycolor')
header.side.classList.add(ord.sell ? 'sellcolor' : 'buycolor')
details.qty.textContent = header.qty.textContent = Doc.formatCoinValue(ord.qty, market.baseUnitInfo)
details.rate.textContent = header.rate.textContent = Doc.formatRateFullPrecision(ord.rate, market.baseUnitInfo, market.quoteUnitInfo, cfg.ratestep)
let rate = ord.rate
let ratePrefix = ''
if (ord.type === OrderUtil.Market && ord.matches?.length > 0) {
ratePrefix = '~ '
rate = OrderUtil.averageRate(ord)
}
details.rate.textContent = header.rate.textContent = ratePrefix + Doc.formatRateFullPrecision(rate, market.baseUnitInfo, market.quoteUnitInfo, cfg.ratestep)
header.baseSymbol.textContent = ord.baseSymbol.toUpperCase()
details.type.textContent = OrderUtil.typeString(ord)
this.updateMetaOrder(mord)
Expand Down
4 changes: 3 additions & 1 deletion client/webserver/site/src/js/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ export default class OrdersPage extends BasePage {
Doc.tmplElement(tr, 'toLogo').src = Doc.logoPath(to)
set('toSymbol', to)
set('type', `${OrderUtil.typeString(ord)} ${OrderUtil.sellString(ord)}`)
set('rate', Doc.formatCoinValue(app().conventionalRate(ord.baseID, ord.quoteID, ord.rate)))
let rate = Doc.formatCoinValue(app().conventionalRate(ord.baseID, ord.quoteID, ord.rate))
if (ord.type === OrderUtil.Market) rate = OrderUtil.averageRateString(ord)
set('rate', rate)
set('status', OrderUtil.statusString(ord))
set('filled', `${(OrderUtil.filled(ord) / ord.qty * 100).toFixed(1)}%`)
set('settled', `${(OrderUtil.settled(ord) / ord.qty * 100).toFixed(1)}%`)
Expand Down
21 changes: 21 additions & 0 deletions client/webserver/site/src/js/orderutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Match
} from './registry'
import { BooleanOption, XYRangeOption } from './opts'
import Doc from './doc'

export const Limit = 1
export const Market = 2
Expand Down Expand Up @@ -119,6 +120,26 @@ export function settled (order: Order) {
}, 0)
}

/* averageRateString returns a formatting string containing the average rate of
the matches that have been filled in an order. */
export function averageRateString (ord: Order): string {
if (ord.matches?.length === 0) return '0'
return '~ ' + Doc.formatCoinValue(app().conventionalRate(ord.baseID, ord.quoteID, averageRate(ord)))
}

/* averageRate returns a the average rate of the matches that have been filled
in an order. */
export function averageRate (ord: Order): number {
if (ord.matches?.length === 0) return 0
let rateProduct = 0
let baseQty = 0
ord.matches.forEach((m) => {
baseQty += m.qty
rateProduct += (m.rate * m.qty) // order ~ 1e16
})
return rateProduct / baseQty
}

/* baseToQuote returns the quantity of the quote asset. */
export function baseToQuote (rate: number, base: number) : number {
return rate * base / RateEncodingFactor
Expand Down

0 comments on commit 95fdaf9

Please sign in to comment.