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

client/{btc,dcr,zec}: Use binary search for maxOrder loop #2634

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 21 additions & 11 deletions client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1782,21 +1782,31 @@ func (btc *baseWallet) maxOrder(lotSize, feeSuggestion, maxFeeRate uint64) (utxo
if err != nil {
return nil, nil, fmt.Errorf("error parsing unspent outputs: %w", err)
}

// Start by attempting max lots with a basic fee.
basicFee := btc.initTxSize * maxFeeRate
lots := avail / (lotSize + basicFee)
for lots > 0 {
est, _, _, err := btc.estimateSwap(lots, lotSize, feeSuggestion, maxFeeRate,
utxos, true, 1.0)
// The only failure mode of estimateSwap -> btc.fund is when there is
// not enough funds, so if an error is encountered, count down the lots
// and repeat until we have enough.
if err != nil {
lots--
continue

// Find the max lots we can fund.
maxLots := avail / (lotSize + basicFee)
var minLots uint64
for minLots <= maxLots {
midLots := (minLots + maxLots) / 2
if _, _, _, err = btc.estimateSwap(midLots, lotSize, feeSuggestion, maxFeeRate, utxos, true, 1.0); err != nil {
// The only failure mode of estimateSwap -> btc.fund is when there
// is not enough funds, so if an error is encountered, reduce
// maxLots and try again.
maxLots = midLots - 1
} else {
// Okay, we got an estimation, let's increase minLots and try again.
minLots = midLots + 1
}
return utxos, est, nil
}

if maxLots > 0 {
est, _, _, err = btc.estimateSwap(maxLots, lotSize, feeSuggestion, maxFeeRate, utxos, true, 1.0)
return utxos, est, err
}

return utxos, &asset.SwapEstimate{}, nil
}

Expand Down
30 changes: 20 additions & 10 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1271,21 +1271,31 @@ func (dcr *ExchangeWallet) maxOrder(lotSize, feeSuggestion, maxFeeRate uint64) (

// Start by attempting max lots with a basic fee.
basicFee := dexdcr.InitTxSize * maxFeeRate
lots := avail / (lotSize + basicFee)
// NOTE: Split tx is an order-time option. The max order is generally
// attainable when split is used, regardless of whether they choose it on
// the order form. Allow the split for max order purposes.
trySplitTx := true
for lots > 0 {
est, _, _, err := dcr.estimateSwap(lots, lotSize, feeSuggestion, maxFeeRate, utxos, trySplitTx, 1.0)
// The only failure mode of estimateSwap -> dcr.fund is when there is
// not enough funds, so if an error is encountered, count down the lots
// and repeat until we have enough.
if err != nil {
lots--
continue

// Find the max lots we can fund.
maxLots := avail / (lotSize + basicFee)
var minLots uint64
for minLots <= maxLots {
midLots := (minLots + maxLots) / 2
if _, _, _, err = dcr.estimateSwap(midLots, lotSize, feeSuggestion, maxFeeRate, utxos, trySplitTx, 1.0); err != nil {
// The only failure mode of estimateSwap -> dcr.fund is when there
// is
// not enough funds, so if an error is encountered, reduce
// maxLots and try again.
maxLots = midLots - 1
} else {
// Okay, we got an estimation, let's increase minLots and try again.
minLots = midLots + 1
}
return utxos, est, nil
}

if maxLots > 0 {
est, _, _, err = dcr.estimateSwap(maxLots, lotSize, feeSuggestion, maxFeeRate, utxos, trySplitTx, 1.0)
return utxos, est, err
}

return nil, &asset.SwapEstimate{}, nil
Expand Down
27 changes: 16 additions & 11 deletions client/asset/zec/zec.go
Original file line number Diff line number Diff line change
Expand Up @@ -943,18 +943,23 @@ func (w *zecWallet) maxOrder(lotSize, feeSuggestion, maxFeeRate uint64) (utxos [

avail += bals.orchard.avail

// Start by attempting max lots with a basic fee.
lots := avail / lotSize
for lots > 0 {
est, _, _, err := w.estimateSwap(lots, lotSize, feeSuggestion, maxFeeRate, utxos, bals.orchard, true)
// The only failure mode of estimateSwap -> zec.fund is when there is
// not enough funds, so if an error is encountered, count down the lots
// and repeat until we have enough.
if err != nil {
lots--
continue
// Find the max lots we can fund.
maxLots := avail / lotSize
var minLots uint64
for minLots <= maxLots {
midLots := (minLots + maxLots) / 2
if _, _, _, err = w.estimateSwap(midLots, lotSize, feeSuggestion, maxFeeRate, utxos, bals.orchard, true); err != nil {
// The only failure mode of estimateSwap -> zec.fund is when there is
// not enough funds.
maxLots = midLots - 1
} else {
minLots = midLots + 1
}
return utxos, bals, est, nil
}

if maxLots > 0 {
est, _, _, err = w.estimateSwap(maxLots, lotSize, feeSuggestion, maxFeeRate, utxos, bals.orchard, true)
return utxos, bals, est, err
}

return utxos, bals, &asset.SwapEstimate{}, nil
Expand Down