Skip to content

Commit

Permalink
refactor code around recent matches & open orders (called active orde…
Browse files Browse the repository at this point in the history
…rs previuosly); add completed orders views (and extend order filtering API to support the use-case of fetching trully relevant orders); format quantities on open/completed views to lot size; finally, minor adjustments to relevant html/css have been made
  • Loading branch information
norwnd committed Dec 30, 2024
1 parent e2a7e8d commit 62dad15
Show file tree
Hide file tree
Showing 25 changed files with 558 additions and 197 deletions.
14 changes: 8 additions & 6 deletions client/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -4933,12 +4933,14 @@ func (c *Core) Orders(filter *OrderFilter) ([]*Order, error) {
}

ords, err := c.db.Orders(&db.OrderFilter{
N: filter.N,
Offset: oid,
Hosts: filter.Hosts,
Assets: filter.Assets,
Market: mkt,
Statuses: filter.Statuses,
N: filter.N,
Offset: oid,
Hosts: filter.Hosts,
Assets: filter.Assets,
Market: mkt,
Statuses: filter.Statuses,
FilledOnly: filter.FilledOnly,
FresherThanUnixMs: filter.FresherThanUnixMs,
})
if err != nil {
return nil, fmt.Errorf("UserOrders error: %w", err)
Expand Down
14 changes: 8 additions & 6 deletions client/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1121,12 +1121,14 @@ type PostBondResult struct {
// OrderFilter is almost the same as db.OrderFilter, except the Offset order ID
// is a dex.Bytes instead of a order.OrderID.
type OrderFilter struct {
N int `json:"n"`
Offset dex.Bytes `json:"offset"`
Hosts []string `json:"hosts"`
Assets []uint32 `json:"assets"`
Statuses []order.OrderStatus `json:"statuses"`
Market *struct {
N int `json:"n"`
Offset dex.Bytes `json:"offset"`
FresherThanUnixMs uint64 `json:"fresherThanUnixMs"`
Hosts []string `json:"hosts"`
Assets []uint32 `json:"assets"`
Statuses []order.OrderStatus `json:"statuses"`
FilledOnly bool `json:"filledOnly"`
Market *struct {
Base uint32 `json:"baseID"`
Quote uint32 `json:"quoteID"`
} `json:"market"`
Expand Down
78 changes: 77 additions & 1 deletion client/db/bolt/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,29 @@ func (db *BoltDB) Orders(orderFilter *dexdb.OrderFilter) (ords []*dexdb.MetaOrde
}
}

if orderFilter.FilledOnly {
filledOrders, err := db.filledOrders()
if err != nil {
return nil, fmt.Errorf("filledOrders: %w", err)
}
filters = append(filters, func(oidB []byte, oBkt *bbolt.Bucket) bool {
oid, err := order.IDFromBytes(oidB)
if err != nil {
db.log.Error("couldn't parse order ID bytes: %x", oidB)
return false
}
_, ok := filledOrders[oid]
return ok
})
}

if orderFilter.FresherThanUnixMs > 0 {
filters = append(filters, func(oidB []byte, oBkt *bbolt.Bucket) bool {
stamp := intCoder.Uint64(oBkt.Get(updateTimeKey))
return stamp >= orderFilter.FresherThanUnixMs
})
}

if orderFilter.Market != nil {
filters = append(filters, func(_ []byte, oBkt *bbolt.Bucket) bool {
baseID, quoteID := intCoder.Uint32(oBkt.Get(baseKey)), intCoder.Uint32(oBkt.Get(quoteKey))
Expand Down Expand Up @@ -1541,7 +1564,6 @@ func (db *BoltDB) DEXOrdersWithActiveMatches(dex string) ([]order.OrderID, error
ids = append(ids, id)
}
return ids, nil

}

// MatchesForOrder retrieves the matches for the specified order ID.
Expand All @@ -1553,6 +1575,28 @@ func (db *BoltDB) MatchesForOrder(oid order.OrderID, excludeCancels bool) ([]*de
}, excludeCancels, true) // include archived matches
}

// filledOrders returns a set of fully filled or partially filled orders. Order is partially
// filled if it has at least 1 non-cancel match with non-zero quantity.
func (db *BoltDB) filledOrders() (map[order.OrderID]struct{}, error) {
matches, err := db.filteredMatchesDecoded(func(match *dexdb.MetaMatch) bool {
// cancel order matches have an empty Address field, we don't want to count these
// hence skip
if match.Address == "" {
return false
}
return match.Quantity > 0 // means corresponding order is filled at least partially
}, false, true) // include archived matches
if err != nil {
return nil, fmt.Errorf("filteredMatchesDecoded: %w", err)
}

result := make(map[order.OrderID]struct{}, len(matches))
for _, m := range matches {
result[m.OrderID] = struct{}{}
}
return result, nil
}

// filteredMatches gets all matches that pass the provided filter function. Each
// match's bucket is provided to the filter, and a boolean true return value
// indicates the match should be decoded and returned. Matches with cancel
Expand Down Expand Up @@ -1591,6 +1635,38 @@ func (db *BoltDB) filteredMatches(filter func(*bbolt.Bucket) bool, excludeCancel
})
}

// filteredMatchesDecoded is same as filteredMatches but applies filter to decoded match.
func (db *BoltDB) filteredMatchesDecoded(filter func(*dexdb.MetaMatch) bool, excludeCancels, includeArchived bool) ([]*dexdb.MetaMatch, error) {
var matches []*dexdb.MetaMatch
return matches, db.matchesView(func(mb, archivedMB *bbolt.Bucket) error {
buckets := []*bbolt.Bucket{mb}
if includeArchived {
buckets = append(buckets, archivedMB)
}
for _, master := range buckets {
err := master.ForEach(func(k, _ []byte) error {
mBkt := master.Bucket(k)
if mBkt == nil {
return fmt.Errorf("match %x bucket is not a bucket", k)
}
match, err := loadMatchBucket(mBkt, excludeCancels)
if err != nil {
return fmt.Errorf("loading match %x bucket: %w", k, err)
}
if match == nil || !filter(match) {
return nil
}
matches = append(matches, match)
return nil
})
if err != nil {
return err
}
}
return nil
})
}

func loadMatchBucket(mBkt *bbolt.Bucket, excludeCancels bool) (*dexdb.MetaMatch, error) {
var proof *dexdb.MatchProof
matchB := getCopy(mBkt, matchKey)
Expand Down
6 changes: 6 additions & 0 deletions client/db/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,12 @@ type OrderFilter struct {
// Statuses is a list of acceptable statuses. A zero-length Statuses means
// all statuses are accepted.
Statuses []order.OrderStatus
// FilledOnly is a flag that when specified limits results to only those orders
// that have been fully filled or partially filled.
FilledOnly bool
// FresherThanUnixMs is a unix millisecond timestamp used to filter out orders that are
// older than its value.
FresherThanUnixMs uint64
}

// noteKeySize must be <= 32.
Expand Down
22 changes: 22 additions & 0 deletions client/webserver/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,27 @@ func (s *WebServer) handleExportOrders(w http.ResponseWriter, r *http.Request) {
return
}

nStr := r.Form.Get("n")
if nStr != "" {
n, err := strconv.ParseInt(nStr, 10, 32)
if err != nil {
log.Errorf("error parsing N: %v", err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
filter.N = int(n)
}
fresherThanUnixMsStr := r.Form.Get("fresherThanUnixMs")
if fresherThanUnixMsStr != "" {
fresherThanUnixMs, err := strconv.ParseUint(fresherThanUnixMsStr, 10, 64)
if err != nil {
log.Errorf("error parsing fresherThanUnixMs: %v", err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
filter.FresherThanUnixMs = fresherThanUnixMs
}

filter.Hosts = r.Form["hosts"]
assets := r.Form["assets"]
filter.Assets = make([]uint32, len(assets))
Expand All @@ -383,6 +404,7 @@ func (s *WebServer) handleExportOrders(w http.ResponseWriter, r *http.Request) {
}
filter.Statuses[k] = order.OrderStatus(statusNumID)
}
filter.FilledOnly = r.Form.Get("filledOnly") == "true"

ords, err := s.core.Orders(filter)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion client/webserver/locales/en-us.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ var EnUS = map[string]*intl.Translation{
"immature": {T: "immature"},
"fee balance": {T: "fee balance"},
"Sell Orders": {T: "Sell Orders"},
"Your Orders": {T: "Your Orders"},
"Open Orders": {T: "Open Orders"},
"Completed Orders": {T: "Completed Orders"},
"sweep_orders": {T: "Hide fully executed orders"},
"sweep_order": {T: "Hide this fully executed order"},
"Recent Matches": {T: "Recent Matches"},
Expand Down
4 changes: 4 additions & 0 deletions client/webserver/site/src/css/colors.scss
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ body.dark {
background-color: var(--section-bg);
}

.section-bg-strong {
background-color: var(--section-bg-strong);
}

.text-good {
color: var(--indicator-good);
}
4 changes: 0 additions & 4 deletions client/webserver/site/src/css/forms_dark.scss
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ body.dark {
background-color: #263846;
}
}

.bordertop {
border-top: solid 1px #999;
}
}

.selectable.selected {
Expand Down
34 changes: 28 additions & 6 deletions client/webserver/site/src/css/market.scss
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ div[data-handler=markets] {
button {
opacity: 0.85;
padding: 5px 25px;
border-radius: 3px;
border-radius: 6px;
background-color: var(--section-bg);
color: var(--market-btn-selected-color);

Expand Down Expand Up @@ -312,7 +312,7 @@ div[data-handler=markets] {
& > section {
&:first-child { // leftmost section
order: 1;
flex-basis: 22%; /* width/height - depending on flex-direction */
flex-basis: 18%; /* width/height - depending on flex-direction */
}

&:nth-child(2) { // middle section
Expand Down Expand Up @@ -346,7 +346,7 @@ div[data-handler=markets] {

&:last-child { // user orders, recent matches
order: 3;
flex-basis: 22%; /* width/height - depending on flex-direction */
flex-basis: 26%; /* width/height - depending on flex-direction */
}
}

Expand All @@ -361,7 +361,7 @@ div[data-handler=markets] {
}
}

#durBttnBox {
#candleDurBttnBox {
position: absolute;
left: 65px;
top: 5px;
Expand All @@ -370,13 +370,14 @@ div[data-handler=markets] {

.candle-dur-bttn {
background-color: var(--section-bg);
border: 1px solid var(--btn-border-color);
padding: 2px 4px;
font-size: 14px;
line-height: 1;
margin: 0 2px;

&:hover {
background-color: #7777;
background-color: var(--section-bg-strong);
}

&:hover,
Expand All @@ -388,7 +389,7 @@ div[data-handler=markets] {
}

#loaderMsg {
color: #777;
color: var(--text-grey);
}

#bondCreationPending {
Expand Down Expand Up @@ -515,6 +516,27 @@ div[data-handler=markets] {
}
}

#completedOrderHistoryDurBttnBox {
background-color: var(--section-bg);
z-index: 1;

.completed-order-dur-bttn {
background-color: var(--section-bg);
border: 1px solid var(--btn-border-color);
line-height: 1;

&:hover {
background-color: var(--section-bg-strong);
}

&:hover,
&.selected {
border-color: var(--text-warning);
color: var(--text-warning);
}
}
}

@include media-breakpoint-up(xl) {
#marketStats {
display: none;
Expand Down
8 changes: 1 addition & 7 deletions client/webserver/site/src/css/mm.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ div[data-handler=mmsettings],
div[data-handler=mmarchives],
div[data-handler=mmlogs],
div[data-handler=mm] {
#overview {
section {
background-color: var(--section-bg-strong);
}
}

#gapStrategySelect {
width: 300px;
}
Expand Down Expand Up @@ -35,7 +29,7 @@ div[data-handler=mm] {

.bot-type-selector {
@include border;
@extend .rounded3;
@extend .border-rounded3;

display: flex;
flex-direction: column;
Expand Down
2 changes: 1 addition & 1 deletion client/webserver/site/src/css/utilities.scss
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
color: var(--text-grey);
}

.rounded3 {
.border-rounded3 {
border-radius: 3px;
}

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 @@ -150,7 +150,7 @@
<span id="actionDialogCount">4</span>
</span>
</div>
<div id="actionDialog" class="mw-375 rounded3 m-3 p-3 d-hide">
<div id="actionDialog" class="mw-375 border-rounded3 m-3 p-3 d-hide">
<div class="d-flex justify-content-between">
<div class="fs22">
<span class="ico-info text-warning me-2"></span>
Expand Down
2 changes: 1 addition & 1 deletion client/webserver/site/src/html/forms.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
</div>
<div data-tmpl="regAssetErr" class="fs14 text-danger flex-center p-3"></div>
<div data-tmpl="bondAssets" class="mt-3 border-top">
<div data-tmpl="bondAssetTmpl" class="border rounded3 d-flex align-items-stretch p-2 hoverbg pointer mt-3">
<div data-tmpl="bondAssetTmpl" class="border d-flex align-items-stretch p-2 hoverbg pointer mt-3">
<div class="flex-center pe-4">
<div class="flex-center">
<img class="small-icon" data-tmpl="logo">
Expand Down
Loading

0 comments on commit 62dad15

Please sign in to comment.