Skip to content

Commit

Permalink
dex: use midGap as default order rate for limit orders
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 Oct 23, 2024
1 parent 9702d21 commit 619a125
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
38 changes: 32 additions & 6 deletions ui/page/dcrdex/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 == "" {
Expand All @@ -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))
}
Expand Down Expand Up @@ -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("")
}
Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions ui/values/localizable/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions ui/values/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ const (
StrInsufficientFund = "insufficientFund"
StrInvalidAddress = "invalidAddress"
StrInvalidAmount = "invalidAmount"
StrInvalidRateFmt = "invalidRateFmt"
StrInvalidHex = "invalidHex"
StrInvalidPassphrase = "invalidPassphrase"
StrInvalidSeedPhrase = "invalidSeedPhrase"
Expand Down

0 comments on commit 619a125

Please sign in to comment.