Skip to content

Commit

Permalink
cmd: self_chan_amt cli validation
Browse files Browse the repository at this point in the history
  • Loading branch information
positiveblue committed Nov 6, 2022
1 parent 9fd6d26 commit bdf41e5
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions cmd/pool/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,41 @@ func validateMinChanAmount(ctx *cli.Context, orderAmt uint64,
return nil
}

// validateOrderSelfBalance checks that the self balance amount is valid for
// the given market.
func validateOrderSelfBalance(ctx *cli.Context, bid *poolrpc.Bid) error {

bidAmt := btcutil.Amount(bid.Details.Amt)
bidUnits := order.NewSupplyFromSats(bidAmt)

switch {
case isInboundLiquidityOrder(ctx):
// Make sure the self channel balance is within reasonable
// limits (currently max the same of the total order amount)
// and that the min units matched is also set to 100% of the
// order amount.
if bid.Details.MinUnitsMatch != uint32(bidUnits) {
return fmt.Errorf("when using " +
"self_chan_balance the min_chan_amt must be " +
"set to the same value as amt")
}

case isOutboundLiquidityOrder(ctx):
if bid.SelfChanBalance < uint64(order.BaseSupplyUnit) {
return fmt.Errorf("the self balance amount must be at "+
"least the base supply amount (%v sats)",
order.BaseSupplyUnit)
}
}

// Default checks.
return order.CheckOfferParams(
order.AuctionType(bid.Details.AuctionType), bidAmt,
btcutil.Amount(bid.SelfChanBalance),
order.BaseSupplyUnit,
)
}

// parseCommonParams tries to read the common order parameters from the command
// line positional arguments and/or flags and parses them based on their
// destination data type. No formal in-depth validation is performed as the
Expand Down Expand Up @@ -806,6 +841,21 @@ func parseBaseBid(ctx *cli.Context) (*poolrpc.Bid, *sidecar.Ticket, error) {

bid.Details = params

hasSelfChanBalance := ctx.IsSet("self_chan_balance")
switch {
case !hasSelfChanBalance && isOutboundLiquidityOrder(ctx):
return nil, nil, fmt.Errorf("the `self_chan_balance` "+
"parameter must be set to at least %v sats",
order.BaseSupplyUnit)

case hasSelfChanBalance:
bid.SelfChanBalance = ctx.Uint64("self_chan_balance")
if err = validateOrderSelfBalance(ctx, bid); err != nil {
return nil, nil, fmt.Errorf("invalid self balance "+
"amount: %v", err)
}
}

return bid, ticket, nil
}

Expand Down

0 comments on commit bdf41e5

Please sign in to comment.