From 619a125938f2db9a84ee174c3641b4515a39ea90 Mon Sep 17 00:00:00 2001 From: Philemon Ukane Date: Wed, 23 Oct 2024 02:49:22 +0100 Subject: [PATCH] dex: use midGap as default order rate for limit orders Signed-off-by: Philemon Ukane --- ui/page/dcrdex/market.go | 38 +++++++++++++++++++++++++++++++------ ui/values/localizable/en.go | 1 + ui/values/strings.go | 1 + 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/ui/page/dcrdex/market.go b/ui/page/dcrdex/market.go index d238a6c51..68ef5dd7f 100644 --- a/ui/page/dcrdex/market.go +++ b/ui/page/dcrdex/market.go @@ -1420,6 +1420,11 @@ func (pg *DEXMarketPage) orderFormEditorSubtext() (totalSubText, lotsSubText str } func (pg *DEXMarketPage) handleEditorEvents(gtx C) { + isMktOrder := pg.isMarketOrder() + if pg.priceEditor.Editor.Text() == "" && !isMktOrder && !pg.priceEditor.IsFocused() { + pg.refreshPriceField() + } + if pg.toggleBuyAndSellBtn.Changed() { pg.refreshOrderForm() pg.setMaxBuyAndMaxSell() @@ -1429,11 +1434,10 @@ func (pg *DEXMarketPage) handleEditorEvents(gtx C) { pg.refreshPriceField() } - isMktOrder := pg.isMarketOrder() mkt := pg.selectedMarketInfo() var reEstimateFee bool - for pg.priceEditor.Changed() { + for pg.priceEditor.Changed() && pg.priceEditor.IsFocused() { pg.priceEditor.SetError("") priceStr := pg.priceEditor.Editor.Text() if isMktOrder || priceStr == "" { @@ -1446,7 +1450,13 @@ func (pg *DEXMarketPage) handleEditorEvents(gtx C) { continue } - formattedPrice := price - mkt.MsgRateToConventional(mkt.ConventionalRateToMsg(price)%mkt.RateStep) + mktRate := mkt.ConventionalRateToMsg(price) + if mktRate < mkt.MinimumRate { + pg.priceEditor.SetError(values.StringF(values.StrInvalidRateFmt, trimmedConventionalAmtString(price), trimmedConventionalAmtString(mkt.MsgRateToConventional(mkt.MinimumRate)))) + continue + } + + formattedPrice := price - mkt.MsgRateToConventional(mktRate%mkt.RateStep) if formattedPrice != price { pg.priceEditor.Editor.SetText(trimmedConventionalAmtString(formattedPrice)) } @@ -1484,9 +1494,17 @@ func (pg *DEXMarketPage) refreshPriceField() { pg.priceEditor.Editor.ReadOnly = isMktOrder if isMktOrder { pg.priceEditor.Editor.SetText(values.String(values.StrMarket)) - } else if mkt != nil && mkt.SpotPrice != nil { - price := mkt.MsgRateToConventional(mkt.SpotPrice.Rate) - pg.priceEditor.Editor.SetText(trimmedConventionalAmtString(price)) + } else if mkt != nil { + orderPrice := pg.orderPrice(mkt) + if orderPrice == 0 { + pg.priceEditor.Editor.SetText("") + } else { + pg.priceEditor.Editor.SetText(trimmedConventionalAmtString(orderPrice)) + formattedPrice := orderPrice - mkt.MsgRateToConventional(mkt.ConventionalRateToMsg(orderPrice)%mkt.RateStep) + if formattedPrice != orderPrice { + pg.priceEditor.Editor.SetText(trimmedConventionalAmtString(formattedPrice)) + } + } } else { pg.priceEditor.Editor.SetText("") } @@ -1903,6 +1921,14 @@ func (pg *DEXMarketPage) orderPrice(mkt *core.Market) (price float64) { limitOrdPriceStr := pg.priceEditor.Editor.Text() if !pg.isMarketOrder() && limitOrdPriceStr != "" { price, _ = strconv.ParseFloat(limitOrdPriceStr, 64) + } else if mkt != nil && !pg.isSellOrder() { + var midGap uint64 + if pg.selectedMarketOrderBook.book != nil { + midGap, _ = pg.selectedMarketOrderBook.book.MidGap() + } + if midGap != 0 { + price = mkt.MsgRateToConventional(midGap) + } } else if mkt != nil && mkt.SpotPrice != nil { price = mkt.MsgRateToConventional(mkt.SpotPrice.Rate) } diff --git a/ui/values/localizable/en.go b/ui/values/localizable/en.go index 87f819e7a..77b43e7fa 100644 --- a/ui/values/localizable/en.go +++ b/ui/values/localizable/en.go @@ -297,6 +297,7 @@ const EN = ` "insufficientFund" = "Insufficient funds" "invalidAddress" = "Invalid address" "invalidAmount" = "Invalid amount" +"invalidRateFmt" = "rate is lower than the market's minimum rate. %v < %v" "invalidHex" = "Invalid hex" "invalidPassphrase" = "Password entered was not valid." "invalidSeedPhrase" = "Invalid seed phrase" diff --git a/ui/values/strings.go b/ui/values/strings.go index ffe2f863c..ccea4fed6 100644 --- a/ui/values/strings.go +++ b/ui/values/strings.go @@ -406,6 +406,7 @@ const ( StrInsufficientFund = "insufficientFund" StrInvalidAddress = "invalidAddress" StrInvalidAmount = "invalidAmount" + StrInvalidRateFmt = "invalidRateFmt" StrInvalidHex = "invalidHex" StrInvalidPassphrase = "invalidPassphrase" StrInvalidSeedPhrase = "invalidSeedPhrase"