Skip to content

Commit 52be50c

Browse files
committed
rfq: add EstimateAssetUnits helper
1 parent bad508d commit 52be50c

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

rfq/manager.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/lightninglabs/taproot-assets/address"
1616
"github.com/lightninglabs/taproot-assets/asset"
1717
"github.com/lightninglabs/taproot-assets/fn"
18+
"github.com/lightninglabs/taproot-assets/rfqmath"
1819
"github.com/lightninglabs/taproot-assets/rfqmsg"
1920
lfn "github.com/lightningnetwork/lnd/fn/v2"
2021
"github.com/lightningnetwork/lnd/lnutils"
@@ -1062,6 +1063,33 @@ func (m *Manager) publishSubscriberEvent(event fn.Event) {
10621063
)
10631064
}
10641065

1066+
// EstimateAssetUnits is a helper function that queries our price oracle to find
1067+
// out how many units of an asset are needed to evaluate to the provided amount
1068+
// in msats.
1069+
func EstimateAssetUnits(ctx context.Context, oracle PriceOracle,
1070+
specifier asset.Specifier,
1071+
amtMsat lnwire.MilliSatoshi) (uint64, error) {
1072+
1073+
oracleRes, err := oracle.QueryBidPrice(
1074+
ctx, specifier, fn.None[uint64](), fn.Some(amtMsat),
1075+
fn.None[rfqmsg.AssetRate](),
1076+
)
1077+
if err != nil {
1078+
return 0, err
1079+
}
1080+
1081+
if oracleRes.Err != nil {
1082+
return 0, fmt.Errorf("cannot query oracle: %v",
1083+
oracleRes.Err.Error())
1084+
}
1085+
1086+
assetUnits := rfqmath.MilliSatoshiToUnits(
1087+
amtMsat, oracleRes.AssetRate.Rate,
1088+
)
1089+
1090+
return assetUnits.ScaleTo(0).ToUint64(), nil
1091+
}
1092+
10651093
// PeerAcceptedBuyQuoteEvent is an event that is broadcast when the RFQ manager
10661094
// receives an accept quote message from a peer. This is a quote which was
10671095
// requested by our node and has been accepted by a peer.

rpcserver.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7938,25 +7938,13 @@ func calculateAssetMaxAmount(ctx context.Context, priceOracle rfq.PriceOracle,
79387938
// query our oracle first to get an estimation on the asset rate. This
79397939
// will help us establish a quote with the correct amount of asset
79407940
// units.
7941-
oracleRes, err := priceOracle.QueryBidPrice(
7942-
ctx, specifier, fn.None[uint64](), fn.Some(amtMsat),
7943-
fn.None[rfqmsg.AssetRate](),
7941+
maxUnits, err := rfq.EstimateAssetUnits(
7942+
ctx, priceOracle, specifier, amtMsat,
79447943
)
79457944
if err != nil {
79467945
return 0, err
79477946
}
79487947

7949-
if oracleRes.Err != nil {
7950-
return 0, fmt.Errorf("cannot query oracle: %v",
7951-
oracleRes.Err.Error())
7952-
}
7953-
7954-
assetUnits := rfqmath.MilliSatoshiToUnits(
7955-
amtMsat, oracleRes.AssetRate.Rate,
7956-
)
7957-
7958-
maxUnits := assetUnits.ToUint64()
7959-
79607948
maxMathUnits := rfqmath.NewBigIntFromUint64(maxUnits)
79617949

79627950
// Since we used a different oracle price query above calculate the max

0 commit comments

Comments
 (0)