mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-18 07:26:50 +00:00
* currency: Add method to derive pair * currency: Add method to lower entire charset but used the slice copy and returned that. This will change the original, just gotta see if this is an issue, but the slice usually goes out of scope anyway. * currency/pairs: add filter method * currency: add function to derive select currencies from currency pairs * currency/engine: slight adjustments * currency: fix linter issue also shift burden of proof to caller instead of repair, more performant. * currency: more linter * pairs: optimize; reduce allocs/op and B/op * currency: Add in function 'NewPairsFromString' for testing purposes * currency: don't suppress error * currency: stop panic on empty currency code * currency: Add helper method to match currencies between exchanges * currency: fixed my bad spelling * currency: Implement stable coin checks, refactored base code methods, optimized upper and lower case strings for currency code/pairs * currency: add pairs method to derive stable coins from internal list. * Currency: Cleanup, fix tests. * engine/exchanges/currency: fix whoops * Currency: force govet no copy on Item datatype * Currency: fix naughty linter issues * exchange: revert change * currency/config: fix config upgrade mistake * currency: re-implement currency sub-systems * *RetrieveConfigCurrencyPairs removed *CheckCurrencyConfigValues to only provide warnings, add additional support when, disable when support is lost or not available and set default values. *Drop Cryptocurrencies from configuration as this is not needed. *Drop REST Poll delay field as this was unused. *Update default values for currencyFileUpdateDuration & foreignExchangeUpdateDuration. *Allow Role to be marshalled for file type. *Refactor RunUpdater to verify and check config values and set default running foreign exchange provider. * currency: cleanup * currency: change match -> equal for comparison which is more of a standard and little easier to find * currency: address nits * currency: fix whoops * currency: Add some more pairs methods * currency: linter issues * currency: RM unused field * currency: rm verbose * currency: fix word * currency: gocritic * currency: fix another whoopsie * example_config: default to show log system name * Currency: Force all support packages to use Equal method for comparison as there is a small comparison bug when checking upper and lower casing, this has a more of a pronounced impact between exchanges and client instances of currency generation * currency: fix log name * ordermanager: fix potential panic * currency: small optim. * engine: display correct bool and force shutdown * currency: add function and fix regression * Change ConvertCurrency -> ConvertFiat to be more precise * ADD GetForeignExchangeRate to get specific exchange rate for fiat pair * Fix currency display and formatting regression and tied in with config.Currency fields * engine: fix tests * currency: return the amount when no conversion needs to take place * currency: reduce method name * currency: Address nits glorious nits * currency: fix linter * currency: addr nits * currency: check underlying role in test * gct: change to EMPTYCODE and EMPTYPAIR across codebase * currency: fix nits * currency: this fixes test race but this issue has not been resolved. Please see: https://trello.com/c/54eizOIo/143-currency-package-upgrades * currency: Add temp dir for testing * Update engine/engine.go Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io> * documentation: update and regen * currency: Address niterinos * currency: Add test case for config upgrade when falling over to exchange rate host as default from exchangeRates provider * currency: addr nits * currency: fix whoops Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io> Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
140 lines
4.1 KiB
Go
140 lines
4.1 KiB
Go
package currency
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
var errCannotCreatePair = errors.New("cannot create currency pair")
|
|
|
|
// NewPairDelimiter splits the desired currency string at delimeter, the returns
|
|
// a Pair struct
|
|
func NewPairDelimiter(currencyPair, delimiter string) (Pair, error) {
|
|
if !strings.Contains(currencyPair, delimiter) {
|
|
return EMPTYPAIR,
|
|
fmt.Errorf("delimiter: [%s] not found in currencypair string", delimiter)
|
|
}
|
|
result := strings.Split(currencyPair, delimiter)
|
|
if len(result) < 2 {
|
|
return EMPTYPAIR,
|
|
fmt.Errorf("supplied pair: [%s] cannot be split with %s",
|
|
currencyPair,
|
|
delimiter)
|
|
}
|
|
if len(result) > 2 {
|
|
result[1] = strings.Join(result[1:], delimiter)
|
|
}
|
|
return Pair{
|
|
Delimiter: delimiter,
|
|
Base: NewCode(result[0]),
|
|
Quote: NewCode(result[1]),
|
|
}, nil
|
|
}
|
|
|
|
// NewPairFromStrings returns a CurrencyPair without a delimiter
|
|
func NewPairFromStrings(base, quote string) (Pair, error) {
|
|
if strings.Contains(base, " ") {
|
|
return EMPTYPAIR,
|
|
fmt.Errorf("cannot create pair, invalid base currency string [%s]",
|
|
base)
|
|
}
|
|
|
|
if strings.Contains(quote, " ") {
|
|
return EMPTYPAIR,
|
|
fmt.Errorf("cannot create pair, invalid quote currency string [%s]",
|
|
quote)
|
|
}
|
|
|
|
return Pair{Base: NewCode(base), Quote: NewCode(quote)}, nil
|
|
}
|
|
|
|
// NewPair returns a currency pair from currency codes
|
|
func NewPair(baseCurrency, quoteCurrency Code) Pair {
|
|
return Pair{
|
|
Base: baseCurrency,
|
|
Quote: quoteCurrency,
|
|
}
|
|
}
|
|
|
|
// NewPairWithDelimiter returns a CurrencyPair with a delimiter
|
|
func NewPairWithDelimiter(base, quote, delimiter string) Pair {
|
|
return Pair{
|
|
Base: NewCode(base),
|
|
Quote: NewCode(quote),
|
|
Delimiter: delimiter,
|
|
}
|
|
}
|
|
|
|
// NewPairFromIndex returns a CurrencyPair via a currency string and specific
|
|
// index
|
|
func NewPairFromIndex(currencyPair, index string) (Pair, error) {
|
|
i := strings.Index(currencyPair, index)
|
|
if i == -1 {
|
|
return EMPTYPAIR,
|
|
fmt.Errorf("index %s not found in currency pair string", index)
|
|
}
|
|
if i == 0 {
|
|
return NewPairFromStrings(currencyPair[0:len(index)],
|
|
currencyPair[len(index):])
|
|
}
|
|
return NewPairFromStrings(currencyPair[0:i], currencyPair[i:])
|
|
}
|
|
|
|
// NewPairFromString converts currency string into a new CurrencyPair
|
|
// with or without delimeter
|
|
func NewPairFromString(currencyPair string) (Pair, error) {
|
|
for x := range delimiters {
|
|
if strings.Contains(currencyPair, delimiters[x]) {
|
|
return NewPairDelimiter(currencyPair, delimiters[x])
|
|
}
|
|
}
|
|
if len(currencyPair) < 3 {
|
|
return EMPTYPAIR,
|
|
fmt.Errorf("%w from %s string too short to be a current pair",
|
|
errCannotCreatePair,
|
|
currencyPair)
|
|
}
|
|
return NewPairFromStrings(currencyPair[0:3], currencyPair[3:])
|
|
}
|
|
|
|
// NewPairFromFormattedPairs matches a supplied currency pair to a list of pairs
|
|
// with a specific format. This is helpful for exchanges which
|
|
// provide currency pairs with no delimiter so we can match it with a list and
|
|
// apply the same format
|
|
func NewPairFromFormattedPairs(currencyPair string, pairs Pairs, pairFmt PairFormat) (Pair, error) {
|
|
for x := range pairs {
|
|
if strings.EqualFold(pairFmt.Format(pairs[x]), currencyPair) {
|
|
return pairs[x], nil
|
|
}
|
|
}
|
|
return NewPairFromString(currencyPair)
|
|
}
|
|
|
|
// Format formats the given pair as a string
|
|
func (f *PairFormat) Format(pair Pair) string {
|
|
return pair.Format(f.Delimiter, f.Uppercase).String()
|
|
}
|
|
|
|
// MatchPairsWithNoDelimiter will move along a predictable index on the provided currencyPair
|
|
// it will then split on that index and verify whether that currencypair exists in the
|
|
// supplied pairs
|
|
// this allows for us to match strange currencies with no delimiter where it is difficult to
|
|
// infer where the delimiter is located eg BETHERETH is BETHER ETH
|
|
func MatchPairsWithNoDelimiter(currencyPair string, pairs Pairs, pairFmt PairFormat) (Pair, error) {
|
|
for i := range pairs {
|
|
fPair := pairs[i].Format(pairFmt.Delimiter, pairFmt.Uppercase)
|
|
maxLen := 6
|
|
if len(currencyPair) < maxLen {
|
|
maxLen = len(currencyPair)
|
|
}
|
|
for j := 1; j <= maxLen; j++ {
|
|
if fPair.Base.String() == currencyPair[0:j] &&
|
|
fPair.Quote.String() == currencyPair[j:] {
|
|
return fPair, nil
|
|
}
|
|
}
|
|
}
|
|
return EMPTYPAIR, fmt.Errorf("currency %v not found in supplied pairs", currencyPair)
|
|
}
|