mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
currency: Add Pair method IsAssociated (#1230)
* currency: pair method add IsAssociated() * Add pair into Order params, export error * glorious: nits --------- Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user