Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi: use average rate for market order rate display #2463

Merged
merged 9 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions client/core/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,26 @@ func (ord *OrderReader) StatusString() string {

// SimpleRateString is the formatted match rate.
func (ord *OrderReader) SimpleRateString() string {
if ord.Type == order.MarketOrderType {
return ord.AverageRateString()
}
ukane-philemon marked this conversation as resolved.
Show resolved Hide resolved
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"
nMatches := len(ord.Matches)
if nMatches == 0 {
return "market" // "market" is better than 0 BTC/ETH ?
}
rateStr = ord.AverageRateString()
if len(ord.Matches) > 1 {
rateStr = "~ " + rateStr // "~" only makes sense if the order has more than one match.
}
}
return fmt.Sprintf("%s %s/%s", ord.formatRate(ord.Rate), ord.QuoteUnitInfo.Conventional.Unit, ord.BaseUnitInfo.Conventional.Unit)
return fmt.Sprintf("%s %s/%s", rateStr, ord.QuoteUnitInfo.Conventional.Unit, ord.BaseUnitInfo.Conventional.Unit)
}

// AverageRateString returns a formatting string containing the average rate of
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 @@ -103,7 +103,7 @@
{{end}}

{{define "bottom"}}
<script src="/js/entry.js?v=e3c6714c|0bc54d99"></script>
<script src="/js/entry.js?v=0a518229|5c5461e5"></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 @@ -846,7 +846,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
21 changes: 20 additions & 1 deletion client/webserver/site/src/js/markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1552,7 +1552,10 @@ 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 = mord.header.qty.textContent = Doc.formatCoinValue(ord.qty, market.baseUnitInfo)
details.rate.textContent = mord.header.rate.textContent = Doc.formatRateFullPrecision(ord.rate, market.baseUnitInfo, market.quoteUnitInfo, cfg.ratestep)
let rateStr: string
if (ord.type === OrderUtil.Market) rateStr = this.marketOrderRateString(ord, market)
else rateStr = Doc.formatRateFullPrecision(ord.rate, market.baseUnitInfo, market.quoteUnitInfo, cfg.ratestep)
details.rate.textContent = mord.header.rate.textContent = rateStr
header.baseSymbol.textContent = market.baseUnitInfo.conventional.unit
details.type.textContent = market.quoteUnitInfo.conventional.unit
this.updateMetaOrder(mord)
Expand Down Expand Up @@ -1658,6 +1661,17 @@ export default class MarketsPage extends BasePage {
this.setDepthMarkers()
}

/*
marketOrderRateString uses the market config rate step to format the average
market order rate.
*/
marketOrderRateString (ord: Order, mkt: CurrentMarket) :string {
if (!ord.matches?.length) return intl.prep(intl.ID_MARKET_ORDER)
let rateStr = Doc.formatRateFullPrecision(OrderUtil.averageRate(ord), mkt.baseUnitInfo, mkt.quoteUnitInfo, mkt.cfg.ratestep)
if (ord.matches.length > 1) rateStr = '~ ' + rateStr // ~ only makes sense if the order has more than one match
return rateStr
}

/*
* updateMetaOrder sets the td contents of the user's order table row.
*/
Expand Down Expand Up @@ -2361,6 +2375,11 @@ export default class MarketsPage extends BasePage {
handleMatchNote (note: MatchNote) {
const mord = this.metaOrders[note.orderID]
if (!mord) return this.refreshActiveOrders()
else if (mord.ord.type === OrderUtil.Market && note.match.status === OrderUtil.NewlyMatched) { // Update the average market rate display.
// Fetch and use the updated order.
const ord = app().order(note.orderID)
if (ord) mord.details.rate.textContent = mord.header.rate.textContent = this.marketOrderRateString(ord, this.market)
}
if (app().canAccelerateOrder(mord.ord)) Doc.show(mord.details.accelerateBttn)
else Doc.hide(mord.details.accelerateBttn)
}
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 @@ -198,7 +198,9 @@ export default class OrdersPage extends BasePage {
tmpl.toLogo.src = Doc.logoPath(toSymbol)
tmpl.toSymbol.textContent = toUnit
tmpl.type.textContent = `${OrderUtil.typeString(ord)} ${OrderUtil.sellString(ord)}`
tmpl.rate.textContent = Doc.formatCoinValue(app().conventionalRate(ord.baseID, ord.quoteID, ord.rate, xc))
let rate = Doc.formatCoinValue(app().conventionalRate(ord.baseID, ord.quoteID, ord.rate, xc))
if (ord.type === OrderUtil.Market) rate = OrderUtil.averageMarketOrderRateString(ord)
tmpl.rate.textContent = rate
tmpl.status.textContent = OrderUtil.statusString(ord)
tmpl.filled.textContent = `${(OrderUtil.filled(ord) / ord.qty * 100).toFixed(1)}%`
tmpl.settled.textContent = `${(OrderUtil.settled(ord) / ord.qty * 100).toFixed(1)}%`
Expand Down
23 changes: 23 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,28 @@ export function settled (order: Order) {
}, 0)
}

/* averageRateString returns a formatting string containing the average rate of
the matches that have been filled for a market order. */
export function averageMarketOrderRateString (ord: Order): string {
if (!ord.matches?.length) return intl.prep(intl.ID_MARKET_ORDER)
let rateStr = Doc.formatCoinValue(app().conventionalRate(ord.baseID, ord.quoteID, averageRate(ord)))
if (ord.matches.length > 1) rateStr = '~ ' + rateStr // "~" only makes sense if the order has more than one match.
return rateStr
}

/* 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) return 0
let rateProduct = 0
let baseQty = 0
for (const m of ord.matches) {
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