@@ -12,6 +12,7 @@ import (
1212 "github.com/lightninglabs/lndclient"
1313 "github.com/lightninglabs/taproot-assets/asset"
1414 "github.com/lightninglabs/taproot-assets/fn"
15+ "github.com/lightninglabs/taproot-assets/rfqmath"
1516 "github.com/lightninglabs/taproot-assets/rfqmsg"
1617 lfn "github.com/lightningnetwork/lnd/fn/v2"
1718 "github.com/lightningnetwork/lnd/lnutils"
@@ -1044,6 +1045,33 @@ func (m *Manager) publishSubscriberEvent(event fn.Event) {
10441045 )
10451046}
10461047
1048+ // EstimateAssetUnits is a helper function that queries our price oracle to find
1049+ // out how many units of an asset are needed to evaluate to the provided amount
1050+ // in msats.
1051+ func EstimateAssetUnits (ctx context.Context , oracle PriceOracle ,
1052+ specifier asset.Specifier ,
1053+ amtMsat lnwire.MilliSatoshi ) (uint64 , error ) {
1054+
1055+ oracleRes , err := oracle .QueryBidPrice (
1056+ ctx , specifier , fn .None [uint64 ](), fn .Some (amtMsat ),
1057+ fn .None [rfqmsg.AssetRate ](),
1058+ )
1059+ if err != nil {
1060+ return 0 , err
1061+ }
1062+
1063+ if oracleRes .Err != nil {
1064+ return 0 , fmt .Errorf ("cannot query oracle: %v" ,
1065+ oracleRes .Err .Error ())
1066+ }
1067+
1068+ assetUnits := rfqmath .MilliSatoshiToUnits (
1069+ amtMsat , oracleRes .AssetRate .Rate ,
1070+ )
1071+
1072+ return assetUnits .ToUint64 (), nil
1073+ }
1074+
10471075// PeerAcceptedBuyQuoteEvent is an event that is broadcast when the RFQ manager
10481076// receives an accept quote message from a peer. This is a quote which was
10491077// requested by our node and has been accepted by a peer.
0 commit comments