|
| 1 | +package rfq |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "slices" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/lightninglabs/taproot-assets/asset" |
| 9 | + "github.com/lightninglabs/taproot-assets/fn" |
| 10 | + "github.com/lightninglabs/taproot-assets/rfqmath" |
| 11 | + "github.com/lightninglabs/taproot-assets/rfqmsg" |
| 12 | + "github.com/lightningnetwork/lnd/lnwire" |
| 13 | + "github.com/stretchr/testify/mock" |
| 14 | +) |
| 15 | + |
| 16 | +// MockPriceOracle is a mock implementation of the PriceOracle interface. |
| 17 | +// It returns the suggested rate as the exchange rate. |
| 18 | +type MockPriceOracle struct { |
| 19 | + // Mock is the underlying mock object used to track method invocations |
| 20 | + // in tests. |
| 21 | + mock.Mock |
| 22 | + |
| 23 | + // expiryDelay is the lifetime of a quote in seconds. |
| 24 | + expiryDelay uint64 |
| 25 | + |
| 26 | + // assetToBtcRate is the default asset to BTC exchange rate. |
| 27 | + assetToBtcRate rfqmath.BigIntFixedPoint |
| 28 | +} |
| 29 | + |
| 30 | +// NewMockPriceOracle creates a new mock price oracle. |
| 31 | +func NewMockPriceOracle(expiryDelay, |
| 32 | + assetRateCoefficient uint64) *MockPriceOracle { |
| 33 | + |
| 34 | + return &MockPriceOracle{ |
| 35 | + expiryDelay: expiryDelay, |
| 36 | + assetToBtcRate: rfqmath.NewBigIntFixedPoint( |
| 37 | + assetRateCoefficient, 0, |
| 38 | + ), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// NewMockPriceOracleSatPerAsset creates a new mock price oracle with a |
| 43 | +// specified satoshis per asset rate. |
| 44 | +func NewMockPriceOracleSatPerAsset(expiryDelay uint64, |
| 45 | + satsPerAsset uint64) *MockPriceOracle { |
| 46 | + |
| 47 | + 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 | + ), |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// QueryAskPrice returns the ask price for the given asset amount. |
| 59 | +func (m *MockPriceOracle) QueryAskPrice(ctx context.Context, |
| 60 | + assetSpecifier asset.Specifier, |
| 61 | + assetMaxAmt fn.Option[uint64], |
| 62 | + paymentMaxAmt fn.Option[lnwire.MilliSatoshi], |
| 63 | + assetRateHint fn.Option[rfqmsg.AssetRate]) (*OracleResponse, error) { |
| 64 | + |
| 65 | + // Return early with default value if no expected calls are predefined |
| 66 | + // for this method. |
| 67 | + if !hasExpectedCall(m.ExpectedCalls, "QueryAskPrice") { |
| 68 | + // Calculate the rate expiry timestamp. |
| 69 | + lifetime := time.Duration(m.expiryDelay) * time.Second |
| 70 | + expiry := time.Now().Add(lifetime).UTC() |
| 71 | + |
| 72 | + return &OracleResponse{ |
| 73 | + AssetRate: rfqmsg.NewAssetRate( |
| 74 | + m.assetToBtcRate, expiry, |
| 75 | + ), |
| 76 | + }, nil |
| 77 | + } |
| 78 | + |
| 79 | + // If an expected call exist, call normally. |
| 80 | + args := m.Called( |
| 81 | + ctx, assetSpecifier, assetMaxAmt, paymentMaxAmt, assetRateHint, |
| 82 | + ) |
| 83 | + resp, _ := args.Get(0).(*OracleResponse) |
| 84 | + return resp, args.Error(1) |
| 85 | +} |
| 86 | + |
| 87 | +// QueryBidPrice returns a bid price for the given asset amount. |
| 88 | +func (m *MockPriceOracle) QueryBidPrice(ctx context.Context, |
| 89 | + assetSpecifier asset.Specifier, |
| 90 | + assetMaxAmt fn.Option[uint64], |
| 91 | + paymentMaxAmt fn.Option[lnwire.MilliSatoshi], |
| 92 | + assetRateHint fn.Option[rfqmsg.AssetRate]) (*OracleResponse, error) { |
| 93 | + |
| 94 | + // Return early with default value if no expected calls are predefined |
| 95 | + // for this method. |
| 96 | + if !hasExpectedCall(m.ExpectedCalls, "QueryBidPrice") { |
| 97 | + // Calculate the rate expiry timestamp. |
| 98 | + lifetime := time.Duration(m.expiryDelay) * time.Second |
| 99 | + expiry := time.Now().Add(lifetime).UTC() |
| 100 | + |
| 101 | + return &OracleResponse{ |
| 102 | + AssetRate: rfqmsg.NewAssetRate( |
| 103 | + m.assetToBtcRate, expiry, |
| 104 | + ), |
| 105 | + }, nil |
| 106 | + } |
| 107 | + |
| 108 | + // If an expected call exist, call normally. |
| 109 | + args := m.Called( |
| 110 | + ctx, assetSpecifier, assetMaxAmt, paymentMaxAmt, assetRateHint, |
| 111 | + ) |
| 112 | + resp, _ := args.Get(0).(*OracleResponse) |
| 113 | + return resp, args.Error(1) |
| 114 | +} |
| 115 | + |
| 116 | +// Ensure that MockPriceOracle implements the PriceOracle interface. |
| 117 | +var _ PriceOracle = (*MockPriceOracle)(nil) |
| 118 | + |
| 119 | +// hasExpectedCall checks if the method call has been registered as an expected |
| 120 | +// call with the mock object. |
| 121 | +func hasExpectedCall(expectedCalls []*mock.Call, method string) bool { |
| 122 | + return slices.ContainsFunc(expectedCalls, func(call *mock.Call) bool { |
| 123 | + return call.Method == method |
| 124 | + }) |
| 125 | +} |
0 commit comments