Skip to content

Commit 55a8de5

Browse files
committed
rfq: add AssetRateHint arg to queryBidFromPriceOracle and QueryBidPrice
This update adds an asset rate hint argument to the `queryBidFromPriceOracle` negotiator method and the `PriceOracle` interface's `QueryBidPrice` method.
1 parent 19817d9 commit 55a8de5

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

rfq/negotiator.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ func NewNegotiator(cfg NegotiatorCfg) (*Negotiator, error) {
106106
// queryBidFromPriceOracle queries the price oracle for a bid price. It returns
107107
// an appropriate outgoing response message which should be sent to the peer.
108108
func (n *Negotiator) queryBidFromPriceOracle(peer route.Vertex,
109-
assetId *asset.ID, assetGroupKey *btcec.PublicKey,
110-
assetAmount uint64) (*rfqmath.BigIntFixedPoint, uint64, error) {
109+
assetId *asset.ID, assetGroupKey *btcec.PublicKey, assetAmount uint64,
110+
assetRateHint fn.Option[rfqmsg.AssetRate]) (*rfqmath.BigIntFixedPoint,
111+
uint64, error) {
111112

112113
// TODO(ffranr): Optionally accept a peer's proposed ask price as an
113114
// arg to this func and pass it to the price oracle. The price oracle
@@ -120,7 +121,7 @@ func (n *Negotiator) queryBidFromPriceOracle(peer route.Vertex,
120121
defer cancel()
121122

122123
oracleResponse, err := n.cfg.PriceOracle.QueryBidPrice(
123-
ctx, assetId, assetGroupKey, assetAmount,
124+
ctx, assetId, assetGroupKey, assetAmount, assetRateHint,
124125
)
125126
if err != nil {
126127
return nil, 0, fmt.Errorf("failed to query price oracle for "+
@@ -168,6 +169,7 @@ func (n *Negotiator) HandleOutgoingBuyOrder(buyOrder BuyOrder) error {
168169
rate, _, err := n.queryBidFromPriceOracle(
169170
*buyOrder.Peer, buyOrder.AssetID,
170171
buyOrder.AssetGroupKey, buyOrder.MinAssetAmount,
172+
fn.None[rfqmsg.AssetRate](),
171173
)
172174
if err != nil {
173175
// If we fail to query the price oracle for a
@@ -407,7 +409,7 @@ func (n *Negotiator) HandleIncomingSellRequest(
407409
// sell to us.
408410
assetRate, rateExpiry, err := n.queryBidFromPriceOracle(
409411
request.Peer, request.AssetID, request.AssetGroupKey,
410-
request.AssetAmount,
412+
request.AssetAmount, request.AssetRateHint,
411413
)
412414
if err != nil {
413415
// Send a reject message to the peer.
@@ -692,7 +694,7 @@ func (n *Negotiator) HandleIncomingSellAccept(msg rfqmsg.SellAccept,
692694
// by the price oracle with the bid price provided by the peer.
693695
assetRate, _, err := n.queryBidFromPriceOracle(
694696
msg.Peer, msg.Request.AssetID, nil,
695-
msg.Request.AssetAmount,
697+
msg.Request.AssetAmount, msg.Request.AssetRateHint,
696698
)
697699
if err != nil {
698700
// The price oracle returned an error. We will return

rfq/oracle.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ type PriceOracle interface {
106106
// The bid price is the amount the oracle suggests a peer should pay
107107
// to another peer to receive the specified asset amount.
108108
QueryBidPrice(ctx context.Context, assetId *asset.ID,
109-
assetGroupKey *btcec.PublicKey,
110-
assetAmount uint64) (*OracleResponse, error)
109+
assetGroupKey *btcec.PublicKey, assetAmount uint64,
110+
assetRateHint fn.Option[rfqmsg.AssetRate]) (
111+
*OracleResponse, error)
111112
}
112113

113114
// RpcPriceOracle is a price oracle that uses an external RPC server to get
@@ -279,8 +280,8 @@ func (r *RpcPriceOracle) QueryAskPrice(ctx context.Context,
279280

280281
// QueryBidPrice returns a bid price for the given asset amount.
281282
func (r *RpcPriceOracle) QueryBidPrice(ctx context.Context, assetId *asset.ID,
282-
assetGroupKey *btcec.PublicKey,
283-
maxAssetAmount uint64) (*OracleResponse, error) {
283+
assetGroupKey *btcec.PublicKey, maxAssetAmount uint64,
284+
assetRateHint fn.Option[rfqmsg.AssetRate]) (*OracleResponse, error) {
284285

285286
// For now, we only support querying the ask price with an asset ID.
286287
if assetId == nil {
@@ -296,6 +297,14 @@ func (r *RpcPriceOracle) QueryBidPrice(ctx context.Context, assetId *asset.ID,
296297
// set the subject asset ID.
297298
copy(subjectAssetId, assetId[:])
298299

300+
// Construct the RPC asset rates hint.
301+
rpcAssetRatesHint, err := fn.MapOptionZ(
302+
assetRateHint, oraclerpc.MarshalAssetRates,
303+
).Unpack()
304+
if err != nil {
305+
return nil, err
306+
}
307+
299308
req := &oraclerpc.QueryAssetRatesRequest{
300309
TransactionType: oraclerpc.TransactionType_PURCHASE,
301310
SubjectAsset: &oraclerpc.AssetSpecifier{
@@ -309,7 +318,7 @@ func (r *RpcPriceOracle) QueryBidPrice(ctx context.Context, assetId *asset.ID,
309318
AssetId: paymentAssetId,
310319
},
311320
},
312-
AssetRatesHint: nil,
321+
AssetRatesHint: rpcAssetRatesHint,
313322
}
314323

315324
// Perform query.
@@ -409,7 +418,8 @@ func (m *MockPriceOracle) QueryAskPrice(_ context.Context,
409418

410419
// QueryBidPrice returns a bid price for the given asset amount.
411420
func (m *MockPriceOracle) QueryBidPrice(_ context.Context, _ *asset.ID,
412-
_ *btcec.PublicKey, _ uint64) (*OracleResponse, error) {
421+
_ *btcec.PublicKey, _ uint64,
422+
_ fn.Option[rfqmsg.AssetRate]) (*OracleResponse, error) {
413423

414424
// Calculate the rate expiryDelay lifetime.
415425
expiry := uint64(time.Now().Unix()) + m.expiryDelay

rfq/oracle_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ func runQueryBidPriceTest(t *testing.T, tc *testCaseQueryBidPrice) {
262262

263263
resp, err := client.QueryBidPrice(
264264
ctx, tc.assetId, tc.assetGroupKey, assetAmount,
265+
fn.None[rfqmsg.AssetRate](),
265266
)
266267

267268
// If we expect an error, ensure that it is returned.

0 commit comments

Comments
 (0)