Skip to content

Commit

Permalink
multi: revise rfq.SellOrder fields
Browse files Browse the repository at this point in the history
This commit removes the `max_asset_amount` and `min_ask` fields from the
`SellOrder` type and introduces a new `payment_max_amt` field.

The removed fields were not used correctly. The new `payment_max_amt`
field allows us to leverage the `MaxInAmount` from the RFQ request wire
message.
  • Loading branch information
ffranr committed Nov 4, 2024
1 parent 73b5fd1 commit a70f2b7
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 253 deletions.
6 changes: 2 additions & 4 deletions itest/rfq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ func testRfqAssetSellHtlcIntercept(t *harnessTest) {

// Alice sends a sell order to Bob for some amount of the newly minted
// asset.
purchaseAssetAmt := uint64(200)
askAmt := uint64(42000)
sellOrderExpiry := uint64(time.Now().Add(24 * time.Hour).Unix())

Expand All @@ -275,9 +274,8 @@ func testRfqAssetSellHtlcIntercept(t *harnessTest) {
AssetId: mintedAssetIdBytes,
},
},
MaxAssetAmount: purchaseAssetAmt,
MinAsk: askAmt,
Expiry: sellOrderExpiry,
PaymentMaxAmt: askAmt,
Expiry: sellOrderExpiry,

// Here we explicitly specify Bob as the destination
// peer for the sell order. This will prompt Alice's
Expand Down
13 changes: 7 additions & 6 deletions rfq/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,14 +739,15 @@ type SellOrder struct {
// AssetGroupKey is the public key of the asset group to sell.
AssetGroupKey *btcec.PublicKey

// MaxAssetAmount is the maximum amount of the asset that can be sold as
// part of the order.
MaxAssetAmount uint64

// MinAsk is the minimum ask price that the seller is willing to accept.
MinAsk lnwire.MilliSatoshi
// PaymentMaxAmt is the maximum msat amount that the responding peer
// must agree to pay.
PaymentMaxAmt lnwire.MilliSatoshi

// Expiry is the unix timestamp at which the order expires.
//
// TODO(ffranr): This is the invoice expiry unix timestamp in seconds.
// We should make use of this field to ensure quotes are valid for the
// duration of the invoice.
Expiry uint64

// Peer is the peer that the buy order is intended for. This field is
Expand Down
10 changes: 2 additions & 8 deletions rfq/negotiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,13 +489,10 @@ func (n *Negotiator) HandleOutgoingSellOrder(order SellOrder) {

if n.cfg.PriceOracle != nil && assetSpecifier.IsSome() {
// Query the price oracle for an asking price.
//
// TODO(ffranr): Add paymentMaxAmt to SellOrder and use
// as arg here.
assetRate, err := n.queryAskFromPriceOracle(
order.Peer, assetSpecifier,
fn.None[uint64](),
fn.None[lnwire.MilliSatoshi](),
fn.Some(order.PaymentMaxAmt),
fn.None[rfqmsg.AssetRate](),
)
if err != nil {
Expand All @@ -508,12 +505,9 @@ func (n *Negotiator) HandleOutgoingSellOrder(order SellOrder) {
assetRateHint = fn.Some[rfqmsg.AssetRate](*assetRate)
}

// TODO(ffranr): Add paymentMaxAmt to SellOrder and use as arg
// here.
request, err := rfqmsg.NewSellRequest(
*order.Peer, order.AssetID, order.AssetGroupKey,
lnwire.MilliSatoshi(order.MaxAssetAmount),
assetRateHint,
order.PaymentMaxAmt, assetRateHint,
)
if err != nil {
err := fmt.Errorf("unable to create sell request "+
Expand Down
52 changes: 37 additions & 15 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6405,12 +6405,10 @@ func unmarshalAssetSellOrder(
}

return &rfq.SellOrder{
AssetID: assetId,
AssetGroupKey: assetGroupKey,
MaxAssetAmount: req.MaxAssetAmount,
MinAsk: lnwire.MilliSatoshi(req.MinAsk),
Expiry: req.Expiry,
Peer: peer,
AssetID: assetId,
AssetGroupKey: assetGroupKey,
PaymentMaxAmt: lnwire.MilliSatoshi(req.PaymentMaxAmt),
Peer: peer,
}, nil
}

Expand Down Expand Up @@ -6922,11 +6920,36 @@ func (r *rpcServer) SendPayment(req *tchrpc.SendPaymentRequest,
// set.
peerPubKey = &assetChan.channelInfo.PubKeyBytes

// TODO(guggero): This should actually be the max BTC amount
// (invoice amount plus fee limit) in milli-satoshi, not the
// asset amount. Need to change the whole RFQ API to do that
// though.
maxAssetAmount := assetChan.assetInfo.LocalBalance
// paymentMaxAmt is the maximum amount that the counterparty is
// expected to pay. This is the amount that the invoice is
// asking for plus the fee limit in milli-satoshis.
var paymentMaxAmt lnwire.MilliSatoshi
if invoice.MilliSat == nil {
amt, err := lnrpc.UnmarshallAmt(pReq.Amt, pReq.AmtMsat)
if err != nil {
return fmt.Errorf("error unmarshalling "+
"amount: %w", err)
}
if amt == 0 {
return errors.New("amount must be specified " +
"when paying a zero amount invoice")
}

paymentMaxAmt = amt
} else {
paymentMaxAmt = *invoice.MilliSat
}

// Calculate the fee limit that should be used for this payment.
feeLimit, err := lnrpc.UnmarshallAmt(
pReq.FeeLimitSat, pReq.FeeLimitMsat,
)
if err != nil {
return fmt.Errorf("error unmarshalling fee limit: %w",
err)
}

paymentMaxAmt += feeLimit

expiryTimestamp := invoice.Timestamp.Add(invoice.Expiry())
resp, err := r.AddAssetSellOrder(
Expand All @@ -6936,10 +6959,9 @@ func (r *rpcServer) SendPayment(req *tchrpc.SendPaymentRequest,
AssetId: assetID[:],
},
},
MaxAssetAmount: maxAssetAmount,
MinAsk: uint64(*invoice.MilliSat),
Expiry: uint64(expiryTimestamp.Unix()),
PeerPubKey: peerPubKey[:],
PaymentMaxAmt: uint64(paymentMaxAmt),
Expiry: uint64(expiryTimestamp.Unix()),
PeerPubKey: peerPubKey[:],
TimeoutSeconds: uint32(
rfq.DefaultTimeout.Seconds(),
),
Expand Down
Loading

0 comments on commit a70f2b7

Please sign in to comment.