diff --git a/currency/pair_methods.go b/currency/pair_methods.go index 69ae5d52..504a3be5 100644 --- a/currency/pair_methods.go +++ b/currency/pair_methods.go @@ -9,7 +9,9 @@ import ( // EMPTYFORMAT defines an empty pair format var EMPTYFORMAT = PairFormat{} -var errCurrencyNotAssociatedWithPair = errors.New("currency not associated with pair") +// ErrCurrencyNotAssociatedWithPair defines an error where a currency is not +// associated with a pair. +var ErrCurrencyNotAssociatedWithPair = errors.New("currency not associated with pair") // String returns a currency pair string func (p Pair) String() string { @@ -198,7 +200,6 @@ func (p Pair) getOrderParameters(c Code, selling, market bool) (*OrderParameters if selling { params.SellingCurrency = p.Base params.PurchasingCurrency = p.Quote - params.IsBuySide = false params.IsAskLiquidity = !market } else { params.SellingCurrency = p.Quote @@ -215,11 +216,16 @@ func (p Pair) getOrderParameters(c Code, selling, market bool) (*OrderParameters } else { params.SellingCurrency = p.Base params.PurchasingCurrency = p.Quote - params.IsBuySide = false params.IsAskLiquidity = !market } default: - return nil, fmt.Errorf("%w %v: %v", errCurrencyNotAssociatedWithPair, c, p) + return nil, fmt.Errorf("%w %v: %v", ErrCurrencyNotAssociatedWithPair, c, p) } + params.Pair = p return ¶ms, nil } + +// IsAssociated checks to see if the pair is associated with another pair +func (p Pair) IsAssociated(a Pair) bool { + return p.Base.Equal(a.Base) || p.Quote.Equal(a.Base) || p.Base.Equal(a.Quote) || p.Quote.Equal(a.Quote) +} diff --git a/currency/pair_test.go b/currency/pair_test.go index bfabfaec..06d62fa3 100644 --- a/currency/pair_test.go +++ b/currency/pair_test.go @@ -977,17 +977,17 @@ func TestGetOrderParameters(t *testing.T) { }{ {expectedError: ErrCurrencyPairEmpty}, {Pair: p, expectedError: ErrCurrencyCodeEmpty}, - {Pair: p, currency: XRP, selling: true, market: true, expectedError: errCurrencyNotAssociatedWithPair}, + {Pair: p, currency: XRP, selling: true, market: true, expectedError: ErrCurrencyNotAssociatedWithPair}, - {Pair: p, currency: BTC, selling: true, market: true, expectedParams: &OrderParameters{SellingCurrency: BTC, PurchasingCurrency: USDT, IsBuySide: false, IsAskLiquidity: false}}, - {Pair: p, currency: BTC, selling: false, market: true, expectedParams: &OrderParameters{SellingCurrency: USDT, PurchasingCurrency: BTC, IsBuySide: true, IsAskLiquidity: true}}, - {Pair: p, currency: BTC, selling: true, market: false, expectedParams: &OrderParameters{SellingCurrency: BTC, PurchasingCurrency: USDT, IsBuySide: false, IsAskLiquidity: true}}, - {Pair: p, currency: BTC, selling: false, market: false, expectedParams: &OrderParameters{SellingCurrency: USDT, PurchasingCurrency: BTC, IsBuySide: true, IsAskLiquidity: false}}, + {Pair: p, currency: BTC, selling: true, market: true, expectedParams: &OrderParameters{SellingCurrency: BTC, PurchasingCurrency: USDT, IsBuySide: false, IsAskLiquidity: false, Pair: p}}, + {Pair: p, currency: BTC, selling: false, market: true, expectedParams: &OrderParameters{SellingCurrency: USDT, PurchasingCurrency: BTC, IsBuySide: true, IsAskLiquidity: true, Pair: p}}, + {Pair: p, currency: BTC, selling: true, market: false, expectedParams: &OrderParameters{SellingCurrency: BTC, PurchasingCurrency: USDT, IsBuySide: false, IsAskLiquidity: true, Pair: p}}, + {Pair: p, currency: BTC, selling: false, market: false, expectedParams: &OrderParameters{SellingCurrency: USDT, PurchasingCurrency: BTC, IsBuySide: true, IsAskLiquidity: false, Pair: p}}, - {Pair: p, currency: USDT, selling: true, market: true, expectedParams: &OrderParameters{SellingCurrency: USDT, PurchasingCurrency: BTC, IsBuySide: true, IsAskLiquidity: true}}, - {Pair: p, currency: USDT, selling: false, market: true, expectedParams: &OrderParameters{SellingCurrency: BTC, PurchasingCurrency: USDT, IsBuySide: false, IsAskLiquidity: false}}, - {Pair: p, currency: USDT, selling: true, market: false, expectedParams: &OrderParameters{SellingCurrency: USDT, PurchasingCurrency: BTC, IsBuySide: true, IsAskLiquidity: false}}, - {Pair: p, currency: USDT, selling: false, market: false, expectedParams: &OrderParameters{SellingCurrency: BTC, PurchasingCurrency: USDT, IsBuySide: false, IsAskLiquidity: true}}, + {Pair: p, currency: USDT, selling: true, market: true, expectedParams: &OrderParameters{SellingCurrency: USDT, PurchasingCurrency: BTC, IsBuySide: true, IsAskLiquidity: true, Pair: p}}, + {Pair: p, currency: USDT, selling: false, market: true, expectedParams: &OrderParameters{SellingCurrency: BTC, PurchasingCurrency: USDT, IsBuySide: false, IsAskLiquidity: false, Pair: p}}, + {Pair: p, currency: USDT, selling: true, market: false, expectedParams: &OrderParameters{SellingCurrency: USDT, PurchasingCurrency: BTC, IsBuySide: true, IsAskLiquidity: false, Pair: p}}, + {Pair: p, currency: USDT, selling: false, market: false, expectedParams: &OrderParameters{SellingCurrency: BTC, PurchasingCurrency: USDT, IsBuySide: false, IsAskLiquidity: true, Pair: p}}, } for i, tc := range testCases { @@ -1033,6 +1033,37 @@ func TestGetOrderParameters(t *testing.T) { if resp.IsAskLiquidity != tc.expectedParams.IsAskLiquidity { t.Fatalf("AskLiquidity received %v, expected %v", resp.IsAskLiquidity, tc.expectedParams.IsAskLiquidity) } + + if resp.Pair != tc.expectedParams.Pair { + t.Fatalf("Pair received %v, expected %v", resp.Pair, tc.expectedParams.Pair) + } + }) + } +} + +func TestIsAssociated(t *testing.T) { + t.Parallel() + + testCases := []struct { + Pair Pair + associate Pair + expectedResult bool + }{ + {Pair: NewPair(BTC, USDT), associate: NewPair(BTC, USDT), expectedResult: true}, + {Pair: NewPair(USDT, BTC), associate: NewPair(BTC, USDT), expectedResult: true}, + {Pair: NewPair(BTC, USDT), associate: NewPair(USDT, BTC), expectedResult: true}, + {Pair: NewPair(BTC, USDT), associate: NewPair(XRP, USDT), expectedResult: true}, + {Pair: NewPair(BTC, LTC), associate: NewPair(XRP, USDT), expectedResult: false}, + {Pair: NewPair(MA, LTC), associate: NewPair(LTC, USDT), expectedResult: true}, + } + + for x := range testCases { + x := x + t.Run(fmt.Sprintf("%d", x), func(t *testing.T) { + t.Parallel() + if testCases[x].Pair.IsAssociated(testCases[x].associate) != testCases[x].expectedResult { + t.Fatalf("Test %d failed. Expected %v, received %v", x, testCases[x].expectedResult, testCases[x].Pair.IsAssociated(testCases[x].associate)) + } }) } } diff --git a/currency/pair_types.go b/currency/pair_types.go index ec2c096d..2db8832f 100644 --- a/currency/pair_types.go +++ b/currency/pair_types.go @@ -31,4 +31,6 @@ type OrderParameters struct { // IsAskLiquidity is the side of the orderbook that will be used, false for // bid liquidity. IsAskLiquidity bool + // Pair is the currency pair that the order parameters are derived from. + Pair Pair }