|
7 | 7 | "math/big" |
8 | 8 |
|
9 | 9 | "github.com/lightninglabs/taproot-assets/rfqmath" |
| 10 | + "github.com/lightninglabs/taproot-assets/rfqmsg" |
| 11 | + lfn "github.com/lightningnetwork/lnd/fn" |
10 | 12 | ) |
11 | 13 |
|
12 | 14 | // IsAssetBtc is a helper function that returns true if the given asset |
@@ -44,6 +46,33 @@ func IsAssetBtc(assetSpecifier *AssetSpecifier) bool { |
44 | 46 | return isAssetIdZero && !groupKeySet |
45 | 47 | } |
46 | 48 |
|
| 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 | + |
47 | 76 | // MarshalBigIntFixedPoint converts a BigIntFixedPoint to an RPC FixedPoint. |
48 | 77 | func MarshalBigIntFixedPoint(fp rfqmath.BigIntFixedPoint) (*FixedPoint, error) { |
49 | 78 | return &FixedPoint{ |
|
0 commit comments