Skip to content

Commit 88613f7

Browse files
committed
priceoraclerpc: add MarshalAssetRates RPC marshal function
Implements a function to marshal rfqmsg.AssetRate into priceoraclerpc.AssetRates, with the assumption that the payment asset is BTC.
1 parent 7a5f028 commit 88613f7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

taprpc/priceoraclerpc/marshal.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"math/big"
88

99
"github.com/lightninglabs/taproot-assets/rfqmath"
10+
"github.com/lightninglabs/taproot-assets/rfqmsg"
11+
lfn "github.com/lightningnetwork/lnd/fn"
1012
)
1113

1214
// IsAssetBtc is a helper function that returns true if the given asset
@@ -44,6 +46,33 @@ func IsAssetBtc(assetSpecifier *AssetSpecifier) bool {
4446
return isAssetIdZero && !groupKeySet
4547
}
4648

49+
// MarshalAssetRates converts an asset rate to an RPC AssetRates.
50+
// The OK result has a pointer type so that it is nil if there is an error.
51+
// NOTE: The payment asset is assumed to be BTC.
52+
func MarshalAssetRates(assetRate rfqmsg.AssetRate) lfn.Result[*AssetRates] {
53+
// Marshal the subject asset rate.
54+
subjectAssetRate, err := MarshalBigIntFixedPoint(assetRate.Rate)
55+
if err != nil {
56+
return lfn.Err[*AssetRates](err)
57+
}
58+
59+
// Marshal the payment asset rate. For now, we only support BTC as the
60+
// payment asset.
61+
paymentAssetRate, err := MarshalBigIntFixedPoint(rfqmsg.MilliSatPerBtc)
62+
if err != nil {
63+
return lfn.Err[*AssetRates](err)
64+
}
65+
66+
// Compute an expiry unix timestamp from the given asset rate expiry.
67+
expiryTimestamp := uint64(assetRate.Expiry.Unix())
68+
69+
return lfn.Ok[*AssetRates](&AssetRates{
70+
SubjectAssetRate: subjectAssetRate,
71+
PaymentAssetRate: paymentAssetRate,
72+
ExpiryTimestamp: expiryTimestamp,
73+
})
74+
}
75+
4776
// MarshalBigIntFixedPoint converts a BigIntFixedPoint to an RPC FixedPoint.
4877
func MarshalBigIntFixedPoint(fp rfqmath.BigIntFixedPoint) (*FixedPoint, error) {
4978
return &FixedPoint{

0 commit comments

Comments
 (0)