From 7d5c826f41ab5180858cd5cd6ba1b8e306fc3642 Mon Sep 17 00:00:00 2001 From: Philemon Ukane Date: Tue, 22 Aug 2023 15:28:02 +0100 Subject: [PATCH] site: simplify empty order match check and set the correct header rate HTMLElement Signed-off-by: Philemon Ukane --- client/webserver/site/src/html/bodybuilder.tmpl | 2 +- client/webserver/site/src/js/markets.ts | 10 +++------- client/webserver/site/src/js/orderutil.ts | 11 +++++------ 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/client/webserver/site/src/html/bodybuilder.tmpl b/client/webserver/site/src/html/bodybuilder.tmpl index 1de7529a04..669f8f569b 100644 --- a/client/webserver/site/src/html/bodybuilder.tmpl +++ b/client/webserver/site/src/html/bodybuilder.tmpl @@ -103,7 +103,7 @@ {{end}} {{define "bottom"}} - + {{end}} diff --git a/client/webserver/site/src/js/markets.ts b/client/webserver/site/src/js/markets.ts index c3ef51da97..20318851ff 100644 --- a/client/webserver/site/src/js/markets.ts +++ b/client/webserver/site/src/js/markets.ts @@ -1568,10 +1568,9 @@ export default class MarketsPage extends BasePage { market order rate. */ marketOrderRateString (ord: Order, mkt: CurrentMarket) :string { - const nMatch = ord.matches?.length - if (nMatch === 0) return intl.prep(intl.ID_MARKET_ORDER) + if (!ord.matches) return intl.prep(intl.ID_MARKET_ORDER) let rateStr = Doc.formatRateFullPrecision(OrderUtil.averageRate(ord), mkt.baseUnitInfo, mkt.quoteUnitInfo, mkt.cfg.ratestep) - if (nMatch > 1) rateStr = '~ ' + rateStr // ~ only makes sense if the order has more than one match + if (ord.matches.length > 1) rateStr = '~ ' + rateStr // ~ only makes sense if the order has more than one match return rateStr } @@ -2280,10 +2279,7 @@ export default class MarketsPage extends BasePage { 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) { - const rateStr = this.marketOrderRateString(ord, this.market) - mord.details.rate.textContent = mord.details.header.textContent = rateStr - } + 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) diff --git a/client/webserver/site/src/js/orderutil.ts b/client/webserver/site/src/js/orderutil.ts index 73b5804c0a..2641551e83 100644 --- a/client/webserver/site/src/js/orderutil.ts +++ b/client/webserver/site/src/js/orderutil.ts @@ -123,23 +123,22 @@ export function settled (order: Order) { /* 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 { - const nMatch = ord.matches?.length - if (nMatch === 0) return intl.prep(intl.ID_MARKET_ORDER) + if (!ord.matches) return intl.prep(intl.ID_MARKET_ORDER) let rateStr = Doc.formatCoinValue(app().conventionalRate(ord.baseID, ord.quoteID, averageRate(ord))) - if (nMatch > 1) rateStr = '~ ' + rateStr // "~" only makes sense if the order has more than one match. + 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 === 0) return 0 + if (!ord.matches) return 0 let rateProduct = 0 let baseQty = 0 - ord.matches.forEach((m) => { + for (const m of ord.matches) { baseQty += m.qty rateProduct += (m.rate * m.qty) // order ~ 1e16 - }) + } return rateProduct / baseQty }