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

rfqmsg: request fields blip align #1157

Merged
merged 24 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
640ea7b
rfqmsg: reorder requestWireMsgData fields for clarity
ffranr Oct 22, 2024
a907cad
rfqmsg: re-name request msg field SuggestedAssetRate to InAssetRateHint
ffranr Oct 22, 2024
c12991c
rfqmsg: add request msg field OutAssetRateHint and use in SellRequest
ffranr Oct 22, 2024
8253285
rfqmsg: rename request field AssetMaxAmount to MaxInAsset
ffranr Oct 22, 2024
e6ba958
asset: introduce NewSpecifier constructor for asset.Specifier
ffranr Oct 23, 2024
2e78ef0
asset: add String method to asset.Specifier
ffranr Oct 23, 2024
d701056
asset: add Specifier.UnwrapToPtr method
ffranr Oct 23, 2024
49ff032
rfq+rfqmsg: add AssetSpecifier field to SellRequest
ffranr Oct 23, 2024
39d4024
rfqmath: add NewBigIntFromUint64 constructor
ffranr Oct 23, 2024
1e20488
rfqmsg: refactor unit test for easier extensibility
ffranr Oct 23, 2024
54200c1
rfqmsg: add Htlc.SumAssetBalance method
ffranr Oct 23, 2024
163a0b0
rfqmath: add FixedPoint.SetIntValue method
ffranr Oct 25, 2024
5440228
rfq: validate in-flight HTLC asset amounts against HTLC out msat
ffranr Oct 23, 2024
4ac78fb
rfq: improve AssetSalePolicy.CheckHtlcCompliance comments
ffranr Oct 25, 2024
26aa3bf
rfq: refactor AssetSalePolicy field names for clarity
ffranr Oct 25, 2024
a33a027
multi: rename field BuyRequest.AssetAmount to AssetMaxAmt
ffranr Oct 25, 2024
8f5fb5f
rfq+rfqmsg: add AssetSpecifier field to BuyRequest
ffranr Oct 26, 2024
f922c40
rfqmsg: add request fields MinInAsset and MinOutAsset
ffranr Oct 26, 2024
d755237
rfq+asset: carry asset specifier through to price oracle query
ffranr Oct 30, 2024
51f2389
rfq+priceoraclerpc: add payment_asset_max_amount field to oracle request
ffranr Oct 30, 2024
ecc8450
rfq: reformulate oracle `assetAmount` to `assetMaxAmt` with Option type
ffranr Oct 30, 2024
eae8f82
rfq: add field SellRequest.PaymentMaxAmt
ffranr Oct 31, 2024
9e8c663
multi: revise rfq.SellOrder fields
ffranr Oct 31, 2024
c5d356e
rfq: add PaymentMaxAmt check to AssetPurchasePolicy
ffranr Oct 31, 2024
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
21 changes: 16 additions & 5 deletions rfq/negotiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/lightninglabs/taproot-assets/rfqmath"
"github.com/lightninglabs/taproot-assets/rfqmsg"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing/route"
)

Expand Down Expand Up @@ -106,6 +107,7 @@ func NewNegotiator(cfg NegotiatorCfg) (*Negotiator, error) {
// an appropriate outgoing response message which should be sent to the peer.
func (n *Negotiator) queryBidFromPriceOracle(peer route.Vertex,
assetSpecifier asset.Specifier, assetAmount uint64,
paymentMaxAmt fn.Option[lnwire.MilliSatoshi],
assetRateHint fn.Option[rfqmsg.AssetRate]) (*rfqmsg.AssetRate, error) {

// TODO(ffranr): Optionally accept a peer's proposed ask price as an
Expand All @@ -119,7 +121,7 @@ func (n *Negotiator) queryBidFromPriceOracle(peer route.Vertex,
defer cancel()

oracleResponse, err := n.cfg.PriceOracle.QueryBidPrice(
ctx, assetSpecifier, assetAmount, assetRateHint,
ctx, assetSpecifier, assetAmount, paymentMaxAmt, assetRateHint,
)
if err != nil {
return nil, fmt.Errorf("failed to query price oracle for "+
Expand Down Expand Up @@ -179,6 +181,7 @@ func (n *Negotiator) HandleOutgoingBuyOrder(buyOrder BuyOrder) error {
assetRate, err := n.queryBidFromPriceOracle(
*buyOrder.Peer, assetSpecifier,
buyOrder.MinAssetAmount,
fn.None[lnwire.MilliSatoshi](),
fn.None[rfqmsg.AssetRate](),
)
if err != nil {
Expand Down Expand Up @@ -227,14 +230,16 @@ func (n *Negotiator) HandleOutgoingBuyOrder(buyOrder BuyOrder) error {
// peer.
func (n *Negotiator) queryAskFromPriceOracle(peer *route.Vertex,
assetSpecifier asset.Specifier, assetAmount uint64,
paymentMaxAmt fn.Option[lnwire.MilliSatoshi],
assetRateHint fn.Option[rfqmsg.AssetRate]) (*rfqmsg.AssetRate, error) {

// Query the price oracle for an asking price.
ctx, cancel := n.WithCtxQuitNoTimeout()
defer cancel()

oracleResponse, err := n.cfg.PriceOracle.QueryAskPrice(
ctx, assetSpecifier, assetAmount, assetRateHint,
ctx, assetSpecifier, assetAmount, paymentMaxAmt,
assetRateHint,
)
if err != nil {
return nil, fmt.Errorf("failed to query price oracle for "+
Expand Down Expand Up @@ -323,6 +328,7 @@ func (n *Negotiator) HandleIncomingBuyRequest(
// Query the price oracle for an asking price.
assetRate, err := n.queryAskFromPriceOracle(
nil, request.AssetSpecifier, request.AssetMaxAmt,
fn.None[lnwire.MilliSatoshi](),
request.AssetRateHint,
)
if err != nil {
Expand Down Expand Up @@ -419,7 +425,8 @@ func (n *Negotiator) HandleIncomingSellRequest(
// sell to us.
assetRate, err := n.queryBidFromPriceOracle(
request.Peer, request.AssetSpecifier,
request.AssetAmount, request.AssetRateHint,
request.AssetAmount, fn.None[lnwire.MilliSatoshi](),
request.AssetRateHint,
)
if err != nil {
// Send a reject message to the peer.
Expand Down Expand Up @@ -479,6 +486,7 @@ func (n *Negotiator) HandleOutgoingSellOrder(order SellOrder) {
assetRate, err := n.queryAskFromPriceOracle(
order.Peer, assetSpecifier,
order.MaxAssetAmount,
fn.None[lnwire.MilliSatoshi](),
fn.None[rfqmsg.AssetRate](),
)
if err != nil {
Expand Down Expand Up @@ -592,7 +600,9 @@ func (n *Negotiator) HandleIncomingBuyAccept(msg rfqmsg.BuyAccept,
// by the price oracle with the ask price provided by the peer.
assetRate, err := n.queryAskFromPriceOracle(
&msg.Peer, msg.Request.AssetSpecifier,
msg.Request.AssetMaxAmt, fn.None[rfqmsg.AssetRate](),
msg.Request.AssetMaxAmt,
fn.None[lnwire.MilliSatoshi](),
fn.None[rfqmsg.AssetRate](),
)
if err != nil {
// The price oracle returned an error. We will return
Expand Down Expand Up @@ -716,7 +726,8 @@ func (n *Negotiator) HandleIncomingSellAccept(msg rfqmsg.SellAccept,
// by the price oracle with the bid price provided by the peer.
assetRate, err := n.queryBidFromPriceOracle(
msg.Peer, msg.Request.AssetSpecifier,
msg.Request.AssetAmount, msg.Request.AssetRateHint,
msg.Request.AssetAmount, fn.None[lnwire.MilliSatoshi](),
msg.Request.AssetRateHint,
)
if err != nil {
// The price oracle returned an error. We will return
Expand Down
34 changes: 26 additions & 8 deletions rfq/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"net/url"
"time"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightninglabs/taproot-assets/rfqmath"
"github.com/lightninglabs/taproot-assets/rfqmsg"
oraclerpc "github.com/lightninglabs/taproot-assets/taprpc/priceoraclerpc"
"github.com/lightningnetwork/lnd/lnwire"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
Expand Down Expand Up @@ -96,14 +96,17 @@ type PriceOracle interface {
// from another peer to provide the specified asset amount.
QueryAskPrice(ctx context.Context, assetSpecifier asset.Specifier,
assetAmount uint64,
paymentMaxAmt fn.Option[lnwire.MilliSatoshi],
assetRateHint fn.Option[rfqmsg.AssetRate]) (
*OracleResponse, error)

// QueryBidPrice returns the bid price for a given asset amount.
// The bid price is the amount the oracle suggests a peer should pay
// to another peer to receive the specified asset amount.
QueryBidPrice(ctx context.Context, assetSpecifier asset.Specifier,
assetAmount uint64, assetRateHint fn.Option[rfqmsg.AssetRate]) (
assetAmount uint64,
paymentMaxAmt fn.Option[lnwire.MilliSatoshi],
assetRateHint fn.Option[rfqmsg.AssetRate]) (
*OracleResponse, error)
}

Expand Down Expand Up @@ -189,6 +192,7 @@ func NewRpcPriceOracle(addrStr string, dialInsecure bool) (*RpcPriceOracle,
// QueryAskPrice returns the ask price for the given asset amount.
func (r *RpcPriceOracle) QueryAskPrice(ctx context.Context,
assetSpecifier asset.Specifier, assetAmount uint64,
paymentMaxAmt fn.Option[lnwire.MilliSatoshi],
assetRateHint fn.Option[rfqmsg.AssetRate]) (*OracleResponse,
error) {

Expand Down Expand Up @@ -216,6 +220,11 @@ func (r *RpcPriceOracle) QueryAskPrice(ctx context.Context,
return nil, err
}

// Marshal payment asset max amount.
paymentAssetMaxAmount := uint64(
paymentMaxAmt.UnwrapOr(lnwire.MilliSatoshi(0)),
)

req := &oraclerpc.QueryAssetRatesRequest{
TransactionType: oraclerpc.TransactionType_SALE,
SubjectAsset: &oraclerpc.AssetSpecifier{
Expand All @@ -229,7 +238,8 @@ func (r *RpcPriceOracle) QueryAskPrice(ctx context.Context,
AssetId: paymentAssetId,
},
},
AssetRatesHint: rpcAssetRatesHint,
PaymentAssetMaxAmount: paymentAssetMaxAmount,
AssetRatesHint: rpcAssetRatesHint,
}

// Perform query.
Expand Down Expand Up @@ -287,7 +297,9 @@ func (r *RpcPriceOracle) QueryAskPrice(ctx context.Context,
// QueryBidPrice returns a bid price for the given asset amount.
func (r *RpcPriceOracle) QueryBidPrice(ctx context.Context,
assetSpecifier asset.Specifier, maxAssetAmount uint64,
assetRateHint fn.Option[rfqmsg.AssetRate]) (*OracleResponse, error) {
paymentMaxAmt fn.Option[lnwire.MilliSatoshi],
assetRateHint fn.Option[rfqmsg.AssetRate]) (*OracleResponse,
error) {

// For now, we only support querying the ask price with an asset ID.
if !assetSpecifier.HasId() {
Expand All @@ -305,6 +317,11 @@ func (r *RpcPriceOracle) QueryBidPrice(ctx context.Context,
copy(subjectAssetId, assetId[:])
})

// Marshal payment asset max amount.
paymentAssetMaxAmount := uint64(
paymentMaxAmt.UnwrapOr(lnwire.MilliSatoshi(0)),
)

// Construct the RPC asset rates hint.
rpcAssetRatesHint, err := fn.MapOptionZ(
assetRateHint, oraclerpc.MarshalAssetRates,
Expand All @@ -326,7 +343,8 @@ func (r *RpcPriceOracle) QueryBidPrice(ctx context.Context,
AssetId: paymentAssetId,
},
},
AssetRatesHint: rpcAssetRatesHint,
PaymentAssetMaxAmount: paymentAssetMaxAmount,
AssetRatesHint: rpcAssetRatesHint,
}

// Perform query.
Expand Down Expand Up @@ -421,7 +439,7 @@ func NewMockPriceOracleSatPerAsset(expiryDelay uint64,

// QueryAskPrice returns the ask price for the given asset amount.
func (m *MockPriceOracle) QueryAskPrice(_ context.Context,
_ asset.Specifier, _ uint64,
_ asset.Specifier, _ uint64, _ fn.Option[lnwire.MilliSatoshi],
_ fn.Option[rfqmsg.AssetRate]) (*OracleResponse, error) {

// Calculate the rate expiry timestamp.
Expand All @@ -434,8 +452,8 @@ func (m *MockPriceOracle) QueryAskPrice(_ context.Context,
}

// QueryBidPrice returns a bid price for the given asset amount.
func (m *MockPriceOracle) QueryBidPrice(_ context.Context,
_ asset.Specifier, _ uint64,
func (m *MockPriceOracle) QueryBidPrice(_ context.Context, _ asset.Specifier,
_ uint64, _ fn.Option[lnwire.MilliSatoshi],
_ fn.Option[rfqmsg.AssetRate]) (*OracleResponse, error) {

// Calculate the rate expiry timestamp.
Expand Down
4 changes: 3 additions & 1 deletion rfq/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ func runQueryAskPriceTest(t *testing.T, tc *testCaseQueryAskPrice) {
require.NoError(t, err)

resp, err := client.QueryAskPrice(
ctx, assetSpecifier, assetAmount, fn.Some(assetRateHint),
ctx, assetSpecifier, assetAmount,
fn.None[lnwire.MilliSatoshi](), fn.Some(assetRateHint),
)

// If we expect an error, ensure that it is returned.
Expand Down Expand Up @@ -276,6 +277,7 @@ func runQueryBidPriceTest(t *testing.T, tc *testCaseQueryBidPrice) {

resp, err := client.QueryBidPrice(
ctx, assetSpecifier, assetAmount,
fn.None[lnwire.MilliSatoshi](),
fn.None[rfqmsg.AssetRate](),
)

Expand Down
100 changes: 57 additions & 43 deletions taprpc/priceoraclerpc/price_oracle.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading