forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pair_methods.go
117 lines (99 loc) · 3.14 KB
/
pair_methods.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package currency
import (
"encoding/json"
"strings"
)
// String returns a currency pair string
func (p Pair) String() string {
return p.Base.String() + p.Delimiter + p.Quote.String()
}
// Lower converts the pair object to lowercase
func (p Pair) Lower() Pair {
return Pair{
Delimiter: p.Delimiter,
Base: p.Base.Lower(),
Quote: p.Quote.Lower(),
}
}
// Upper converts the pair object to uppercase
func (p Pair) Upper() Pair {
return Pair{
Delimiter: p.Delimiter,
Base: p.Base.Upper(),
Quote: p.Quote.Upper(),
}
}
// UnmarshalJSON comforms type to the umarshaler interface
func (p *Pair) UnmarshalJSON(d []byte) error {
var pair string
err := json.Unmarshal(d, &pair)
if err != nil {
return err
}
newPair, err := NewPairFromString(pair)
if err != nil {
return err
}
p.Base = newPair.Base
p.Quote = newPair.Quote
p.Delimiter = newPair.Delimiter
return nil
}
// MarshalJSON conforms type to the marshaler interface
func (p Pair) MarshalJSON() ([]byte, error) {
return json.Marshal(p.String())
}
// Format changes the currency based on user preferences overriding the default
// String() display
func (p Pair) Format(delimiter string, uppercase bool) Pair {
newP := Pair{Base: p.Base, Quote: p.Quote, Delimiter: delimiter}
if uppercase {
return newP.Upper()
}
return newP.Lower()
}
// Equal compares two currency pairs and returns whether or not they are equal
func (p Pair) Equal(cPair Pair) bool {
return strings.EqualFold(p.Base.String(), cPair.Base.String()) &&
strings.EqualFold(p.Quote.String(), cPair.Quote.String())
}
// EqualIncludeReciprocal compares two currency pairs and returns whether or not
// they are the same including reciprocal currencies.
func (p Pair) EqualIncludeReciprocal(cPair Pair) bool {
if (p.Base.Item == cPair.Base.Item && p.Quote.Item == cPair.Quote.Item) ||
(p.Base.Item == cPair.Quote.Item && p.Quote.Item == cPair.Base.Item) {
return true
}
return false
}
// IsCryptoPair checks to see if the pair is a crypto pair e.g. BTCLTC
func (p Pair) IsCryptoPair() bool {
return storage.IsCryptocurrency(p.Base) &&
storage.IsCryptocurrency(p.Quote)
}
// IsCryptoFiatPair checks to see if the pair is a crypto fiat pair e.g. BTCUSD
func (p Pair) IsCryptoFiatPair() bool {
return (storage.IsCryptocurrency(p.Base) && storage.IsFiatCurrency(p.Quote)) ||
(storage.IsFiatCurrency(p.Base) && storage.IsCryptocurrency(p.Quote))
}
// IsFiatPair checks to see if the pair is a fiat pair e.g. EURUSD
func (p Pair) IsFiatPair() bool {
return storage.IsFiatCurrency(p.Base) && storage.IsFiatCurrency(p.Quote)
}
// IsInvalid checks invalid pair if base and quote are the same
func (p Pair) IsInvalid() bool {
return p.Base.Item == p.Quote.Item
}
// Swap turns the currency pair into its reciprocal
func (p Pair) Swap() Pair {
return Pair{Base: p.Quote, Quote: p.Base}
}
// IsEmpty returns whether or not the pair is empty or is missing a currency
// code
func (p Pair) IsEmpty() bool {
return p.Base.IsEmpty() && p.Quote.IsEmpty()
}
// ContainsCurrency checks to see if a pair contains a specific currency
func (p Pair) ContainsCurrency(c Code) bool {
return p.Base.Item == c.Item || p.Quote.Item == c.Item
}