Skip to content

Commit 387d50f

Browse files
GeorgeTsagkguggero
authored andcommitted
rfq: add EstimateAssetUnits helper
1 parent 4a6b5b6 commit 387d50f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
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 milli satoshi.
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.

0 commit comments

Comments
 (0)