Skip to content

Commit 89891ee

Browse files
authored
Merge pull request #1399 from lightninglabs/fix-mock-price-oracle-satsperasset
rfq+rfqmath: fix sats-per-asset conversion in mock price oracle
2 parents bc61c0e + e9d4dd9 commit 89891ee

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

rfq/mock.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,8 @@ func NewMockPriceOracleSatPerAsset(expiryDelay uint64,
4545
satsPerAsset uint64) *MockPriceOracle {
4646

4747
return &MockPriceOracle{
48-
expiryDelay: expiryDelay,
49-
50-
// TODO(ffranr): This is incorrect, we should convert
51-
// satoshis per asset to assets per BTC.
52-
assetToBtcRate: rfqmath.NewBigIntFixedPoint(
53-
satsPerAsset, 0,
54-
),
48+
expiryDelay: expiryDelay,
49+
assetToBtcRate: rfqmath.SatsPerAssetToAssetRate(satsPerAsset),
5550
}
5651
}
5752

rfqmath/convert.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,16 @@ func MinTransportableMSat(dustLimit lnwire.MilliSatoshi,
172172
oneAssetUnit := NewBigIntFixedPoint(1, 0)
173173
return dustLimit + UnitsToMilliSatoshi(oneAssetUnit, rate)
174174
}
175+
176+
// SatsPerAssetToAssetRate converts a satoshis per asset rate to an asset to
177+
// BTC rate.
178+
func SatsPerAssetToAssetRate(satsPerAsset uint64) BigIntFixedPoint {
179+
if satsPerAsset == 0 {
180+
return NewBigIntFixedPoint(0, 0)
181+
}
182+
183+
satsPerAssetFp := NewBigIntFixedPoint(satsPerAsset, 0)
184+
satsPerBTC := NewBigIntFixedPoint(100_000_000, 0)
185+
186+
return satsPerBTC.Div(satsPerAssetFp)
187+
}

rfqmath/convert_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,3 +828,59 @@ func TestConversionMsat(t *testing.T) {
828828
rapid.MakeCheck(testRoundTripConversion[BigInt]),
829829
)
830830
}
831+
832+
// TestConversionSatsPerAsset tests the conversion of satoshis per asset to an
833+
// asset per BTC rate.
834+
func TestConversionSatsPerAsset(t *testing.T) {
835+
t.Parallel()
836+
837+
testCases := []struct {
838+
satsPerAsset uint64
839+
expectedValue uint64
840+
}{
841+
{
842+
satsPerAsset: 5,
843+
expectedValue: 20_000_000,
844+
},
845+
{
846+
satsPerAsset: 10,
847+
expectedValue: 10_000_000,
848+
},
849+
{
850+
satsPerAsset: 20,
851+
expectedValue: 5_000_000,
852+
},
853+
{
854+
satsPerAsset: 1,
855+
expectedValue: 100_000_000,
856+
},
857+
{
858+
satsPerAsset: 50,
859+
expectedValue: 2_000_000,
860+
},
861+
{
862+
satsPerAsset: 100,
863+
expectedValue: 1_000_000,
864+
},
865+
{
866+
satsPerAsset: 0,
867+
expectedValue: 0,
868+
},
869+
}
870+
871+
for idx := range testCases {
872+
testCase := testCases[idx]
873+
874+
t.Run(fmt.Sprintf("SatsPerAsset=%d", testCase.satsPerAsset),
875+
func(t *testing.T) {
876+
actual := SatsPerAssetToAssetRate(
877+
testCase.satsPerAsset,
878+
)
879+
expected := NewBigIntFixedPoint(
880+
testCase.expectedValue, 0,
881+
)
882+
require.Equal(t, expected, actual)
883+
},
884+
)
885+
}
886+
}

0 commit comments

Comments
 (0)