Currency package update (#247)

* Initial currency overhaul before service system implementation

* Remove redundant currency string in orderbook.Base
Unexport lastupdated field in orderbook.Base as it was being instantiated multiple times
Add error handling for process orderbook

*  Remove redundant currency string in ticker.Price
 Unexport lastupdated field in ticker.Price
 Add error handling for process ticker function and fix tests

* Phase Two Update

* Update translations to use map type - thankyou to kempeng for spotting this

* Change pair method name from Display -> Format for better readability

* Fixes misspelling and tests

* Implement requested changes from GloriousCode

* Remove reduntant function and streamlined return in currency_translation.go

* Revert pair method naming conventions

* Change currency naming conventions

* Changed code type to exported Item type with underlying string to reduce complexity

* Added interim orderbook process method to orderbook.Base type

* Changed feebuilder struct field to currency.Pair

* Adds fall over system for backup fx providers

* deprecate function and children and fix linter issue with btcmarkets

* Fixed requested changes

* Fix bug and move mtx for rates

* Fixed after rebase oopsies

* Fix linter issues

* Fixes race conditions in testing functions

* Final phase coinmarketcap update

* fix linter issues

* Implement requested changes

* Adds configuration variables to increase/decrease time durations between updating currency file and fetching new currency rates

* Add a collection of tests to improve codecov

* After rebase oopsy fixes for btse

* Fix requested changes

* fix after rebase oopsies and add more efficient comparison checks within currency pair

* Fix linter issues
This commit is contained in:
Ryan O'Hara-Reid
2019-03-19 11:49:05 +11:00
committed by Adrian Gallagher
parent ed760e184e
commit 0990f9d118
189 changed files with 11982 additions and 8055 deletions

View File

@@ -19,7 +19,7 @@ import (
"github.com/gorilla/websocket"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/config"
"github.com/thrasher-/gocryptotrader/currency/symbol"
"github.com/thrasher-/gocryptotrader/currency"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/request"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
@@ -113,9 +113,9 @@ func (h *HUOBI) Setup(exch config.ExchangeConfig) {
h.RESTPollingDelay = exch.RESTPollingDelay
h.Verbose = exch.Verbose
h.Websocket.SetWsStatusAndConnection(exch.Websocket)
h.BaseCurrencies = common.SplitStrings(exch.BaseCurrencies, ",")
h.AvailablePairs = common.SplitStrings(exch.AvailablePairs, ",")
h.EnabledPairs = common.SplitStrings(exch.EnabledPairs, ",")
h.BaseCurrencies = exch.BaseCurrencies
h.AvailablePairs = exch.AvailablePairs
h.EnabledPairs = exch.EnabledPairs
err := h.SetCurrencyPairFormat()
if err != nil {
log.Fatal(err)
@@ -774,7 +774,7 @@ func (h *HUOBI) GetMarginAccountBalance(symbol string) ([]MarginAccountBalance,
}
// Withdraw withdraws the desired amount and currency
func (h *HUOBI) Withdraw(address, currency, addrTag string, amount, fee float64) (int64, error) {
func (h *HUOBI) Withdraw(c currency.Code, address, addrTag string, amount, fee float64) (int64, error) {
type response struct {
Response
WithdrawID int64 `json:"data"`
@@ -788,7 +788,7 @@ func (h *HUOBI) Withdraw(address, currency, addrTag string, amount, fee float64)
AddrTag string `json:"addr-tag,omitempty"`
}{
Address: address,
Currency: currency,
Currency: c.Lower().String(),
Amount: strconv.FormatFloat(amount, 'f', -1, 64),
}
@@ -796,7 +796,7 @@ func (h *HUOBI) Withdraw(address, currency, addrTag string, amount, fee float64)
data.Fee = strconv.FormatFloat(fee, 'f', -1, 64)
}
if currency == symbol.XRP {
if c == currency.XRP {
data.AddrTag = addrTag
}

View File

@@ -12,8 +12,7 @@ import (
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/config"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/currency/symbol"
"github.com/thrasher-/gocryptotrader/currency"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
)
@@ -40,9 +39,9 @@ func getDefaultConfig() config.ExchangeConfig {
APIKey: "",
APISecret: "",
ClientID: "",
AvailablePairs: "BTC-USDT,BCH-USDT",
EnabledPairs: "BTC-USDT",
BaseCurrencies: "USD",
AvailablePairs: currency.NewPairsFromStrings([]string{"BTC-USDT", "BCH-USDT"}),
EnabledPairs: currency.NewPairsFromStrings([]string{"BTC-USDT"}),
BaseCurrencies: currency.NewCurrenciesFromStringArray([]string{"USD"}),
AssetTypes: "SPOT",
SupportsAutoPairUpdates: false,
ConfigCurrencyPairFormat: &config.CurrencyPairFormatConfig{
@@ -295,14 +294,13 @@ func TestPEMLoadAndSign(t *testing.T) {
func setFeeBuilder() exchange.FeeBuilder {
return exchange.FeeBuilder{
Amount: 1,
Delimiter: "_",
FeeType: exchange.CryptocurrencyTradeFee,
FirstCurrency: symbol.BTC,
SecondCurrency: symbol.LTC,
IsMaker: false,
Amount: 1,
FeeType: exchange.CryptocurrencyTradeFee,
Pair: currency.NewPairWithDelimiter(currency.BTC.String(),
currency.LTC.String(),
"_"),
PurchasePrice: 1,
CurrencyItem: symbol.USD,
FiatCurrency: currency.USD,
BankTransactionType: exchange.WireTransfer,
}
}
@@ -350,7 +348,7 @@ func TestGetFee(t *testing.T) {
// CryptocurrencyWithdrawalFee Invalid currency
feeBuilder = setFeeBuilder()
feeBuilder.FirstCurrency = "hello"
feeBuilder.Pair.Base = currency.NewCode("hello")
feeBuilder.FeeType = exchange.CryptocurrencyWithdrawalFee
if resp, err := h.GetFee(feeBuilder); resp != float64(0) || err != nil {
t.Errorf("Test Failed - GetFee() error. Expected: %f, Received: %f", float64(0), resp)
@@ -376,7 +374,7 @@ func TestGetFee(t *testing.T) {
// InternationalBankWithdrawalFee Basic
feeBuilder = setFeeBuilder()
feeBuilder.FeeType = exchange.InternationalBankWithdrawalFee
feeBuilder.CurrencyItem = symbol.USD
feeBuilder.FiatCurrency = currency.USD
if resp, err := h.GetFee(feeBuilder); resp != float64(0) || err != nil {
t.Errorf("Test Failed - GetFee() error. Expected: %f, Received: %f", float64(0), resp)
t.Error(err)
@@ -400,7 +398,7 @@ func TestGetActiveOrders(t *testing.T) {
var getOrdersRequest = exchange.GetOrdersRequest{
OrderType: exchange.AnyOrderType,
Currencies: []pair.CurrencyPair{pair.NewCurrencyPair(symbol.BTC, symbol.USDT)},
Currencies: []currency.Pair{currency.NewPair(currency.BTC, currency.USDT)},
}
_, err := h.GetActiveOrders(getOrdersRequest)
@@ -417,7 +415,7 @@ func TestGetOrderHistory(t *testing.T) {
var getOrdersRequest = exchange.GetOrdersRequest{
OrderType: exchange.AnyOrderType,
Currencies: []pair.CurrencyPair{pair.NewCurrencyPair(symbol.BTC, symbol.USDT)},
Currencies: []currency.Pair{currency.NewPair(currency.BTC, currency.USDT)},
}
_, err := h.GetOrderHistory(getOrdersRequest)
@@ -451,10 +449,10 @@ func TestSubmitOrder(t *testing.T) {
t.Skip()
}
var p = pair.CurrencyPair{
Delimiter: "",
FirstCurrency: symbol.BTC,
SecondCurrency: symbol.USDT,
var p = currency.Pair{
Delimiter: "",
Base: currency.BTC,
Quote: currency.USDT,
}
accounts, err := h.GetAccounts()
@@ -478,7 +476,7 @@ func TestCancelExchangeOrder(t *testing.T) {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
currencyPair := pair.NewCurrencyPair(symbol.LTC, symbol.BTC)
currencyPair := currency.NewPair(currency.LTC, currency.BTC)
var orderCancellation = exchange.OrderCancellation{
OrderID: "1",
@@ -504,7 +502,7 @@ func TestCancelAllExchangeOrders(t *testing.T) {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
currencyPair := pair.NewCurrencyPair(symbol.LTC, symbol.BTC)
currencyPair := currency.NewPair(currency.LTC, currency.BTC)
var orderCancellation = exchange.OrderCancellation{
OrderID: "1",
WalletAddress: "1F5zVDgNjorJ51oGebSvNCrSAHpwGkUdDB",
@@ -552,7 +550,7 @@ func TestWithdraw(t *testing.T) {
TestSetup(t)
var withdrawCryptoRequest = exchange.WithdrawRequest{
Amount: 100,
Currency: symbol.BTC,
Currency: currency.BTC,
Address: "1F5zVDgNjorJ51oGebSvNCrSAHpwGkUdDB",
Description: "WITHDRAW IT ALL",
}
@@ -603,7 +601,7 @@ func TestWithdrawInternationalBank(t *testing.T) {
}
func TestGetDepositAddress(t *testing.T) {
_, err := h.GetDepositAddress(symbol.BTC, "")
_, err := h.GetDepositAddress(currency.BTC, "")
if err == nil {
t.Error("Test Failed - GetDepositAddress() error cannot be nil")
}

View File

@@ -13,7 +13,7 @@ import (
"github.com/gorilla/websocket"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/currency"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
log "github.com/thrasher-/gocryptotrader/logger"
@@ -156,7 +156,7 @@ func (h *HUOBI) WsHandleData() {
Timestamp: time.Unix(0, kline.Timestamp),
Exchange: h.GetName(),
AssetType: "SPOT",
Pair: pair.NewCurrencyPairFromString(data[1]),
Pair: currency.NewPairFromString(data[1]),
OpenPrice: kline.Tick.Open,
ClosePrice: kline.Tick.Close,
HighPrice: kline.Tick.High,
@@ -177,7 +177,7 @@ func (h *HUOBI) WsHandleData() {
h.Websocket.DataHandler <- exchange.TradeData{
Exchange: h.GetName(),
AssetType: "SPOT",
CurrencyPair: pair.NewCurrencyPairFromString(data[1]),
CurrencyPair: currency.NewPairFromString(data[1]),
Timestamp: time.Unix(0, trade.Tick.Timestamp),
}
}
@@ -201,13 +201,11 @@ func (h *HUOBI) WsProcessOrderbook(ob WsDepth, symbol string) error {
Amount: askLevel[0].(float64)})
}
p := pair.NewCurrencyPairFromString(symbol)
p := currency.NewPairFromString(symbol)
var newOrderbook orderbook.Base
newOrderbook.Asks = asks
newOrderbook.Bids = bids
newOrderbook.CurrencyPair = symbol
newOrderbook.LastUpdated = time.Now()
newOrderbook.Pair = p
err := h.Websocket.Orderbook.LoadSnapshot(newOrderbook, h.GetName(), false)

View File

@@ -10,7 +10,7 @@ import (
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/config"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/currency"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
@@ -39,19 +39,20 @@ func (h *HUOBI) Run() {
log.Errorf("%s Failed to get available symbols.\n", h.GetName())
} else {
forceUpgrade := false
if common.StringDataContains(h.EnabledPairs, "CNY") || common.StringDataContains(h.AvailablePairs, "CNY") {
if common.StringDataContains(h.EnabledPairs.Strings(), "CNY") ||
common.StringDataContains(h.AvailablePairs.Strings(), "CNY") {
forceUpgrade = true
}
if common.StringDataContains(h.BaseCurrencies, "CNY") {
if common.StringDataContains(h.BaseCurrencies.Strings(), "CNY") {
cfg := config.GetConfig()
exchCfg, errCNY := cfg.GetExchangeConfig(h.Name)
if err != nil {
log.Errorf("%s failed to get exchange config. %s\n", h.Name, errCNY)
return
}
exchCfg.BaseCurrencies = "USD"
h.BaseCurrencies = []string{"USD"}
exchCfg.BaseCurrencies = currency.Currencies{currency.USD}
h.BaseCurrencies = currency.Currencies{currency.USD}
errCNY = cfg.UpdateExchangeConfig(&exchCfg)
if errCNY != nil {
@@ -67,7 +68,12 @@ func (h *HUOBI) Run() {
}
if forceUpgrade {
enabledPairs := []string{"btc-usdt"}
enabledPairs := currency.Pairs{currency.Pair{
Base: currency.BTC.Lower(),
Quote: currency.USDT.Lower(),
Delimiter: "-",
},
}
log.Warn("Available and enabled pairs for Huobi reset due to config upgrade, please enable the ones you would like again")
err = h.UpdateCurrencies(enabledPairs, true, true)
@@ -75,7 +81,14 @@ func (h *HUOBI) Run() {
log.Errorf("%s Failed to update enabled currencies.\n", h.GetName())
}
}
err = h.UpdateCurrencies(currencies, false, forceUpgrade)
var newCurrencies currency.Pairs
for _, p := range currencies {
newCurrencies = append(newCurrencies,
currency.NewPairFromString(p))
}
err = h.UpdateCurrencies(newCurrencies, false, forceUpgrade)
if err != nil {
log.Errorf("%s Failed to update available currencies.\n", h.GetName())
}
@@ -83,7 +96,7 @@ func (h *HUOBI) Run() {
}
// UpdateTicker updates and returns the ticker for a currency pair
func (h *HUOBI) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
func (h *HUOBI) UpdateTicker(p currency.Pair, assetType string) (ticker.Price, error) {
var tickerPrice ticker.Price
tick, err := h.GetMarketDetailMerged(exchange.FormatExchangeCurrency(h.Name, p).String())
if err != nil {
@@ -104,12 +117,16 @@ func (h *HUOBI) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Pric
tickerPrice.Bid = tick.Bid[0]
}
ticker.ProcessTicker(h.GetName(), p, tickerPrice, assetType)
err = ticker.ProcessTicker(h.GetName(), tickerPrice, assetType)
if err != nil {
return tickerPrice, err
}
return ticker.GetTicker(h.Name, p, assetType)
}
// GetTickerPrice returns the ticker for a currency pair
func (h *HUOBI) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
func (h *HUOBI) GetTickerPrice(p currency.Pair, assetType string) (ticker.Price, error) {
tickerNew, err := ticker.GetTicker(h.GetName(), p, assetType)
if err != nil {
return h.UpdateTicker(p, assetType)
@@ -118,8 +135,8 @@ func (h *HUOBI) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Pr
}
// GetOrderbookEx returns orderbook base on the currency pair
func (h *HUOBI) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(h.GetName(), p, assetType)
func (h *HUOBI) GetOrderbookEx(p currency.Pair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.Get(h.GetName(), p, assetType)
if err != nil {
return h.UpdateOrderbook(p, assetType)
}
@@ -127,7 +144,7 @@ func (h *HUOBI) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (h *HUOBI) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
func (h *HUOBI) UpdateOrderbook(p currency.Pair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := h.GetDepth(OrderBookDataRequestParams{
Symbol: exchange.FormatExchangeCurrency(h.Name, p).String(),
@@ -147,8 +164,16 @@ func (h *HUOBI) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderboo
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: data[1], Price: data[0]})
}
orderbook.ProcessOrderbook(h.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(h.Name, p, assetType)
orderBook.Pair = p
orderBook.ExchangeName = h.GetName()
orderBook.AssetType = assetType
err = orderBook.Process()
if err != nil {
return orderBook, err
}
return orderbook.Get(h.Name, p, assetType)
}
// GetAccountID returns the account ID for trades
@@ -195,7 +220,7 @@ func (h *HUOBI) GetAccountInfo() (exchange.AccountInfo, error) {
var updated bool
for i := range currencyDetails {
if currencyDetails[i].CurrencyName == balance.Currency {
if currencyDetails[i].CurrencyName == currency.NewCode(balance.Currency) {
if frozen {
currencyDetails[i].Hold = balance.Balance
} else {
@@ -212,13 +237,13 @@ func (h *HUOBI) GetAccountInfo() (exchange.AccountInfo, error) {
if frozen {
currencyDetails = append(currencyDetails,
exchange.AccountCurrencyInfo{
CurrencyName: balance.Currency,
CurrencyName: currency.NewCode(balance.Currency),
Hold: balance.Balance,
})
} else {
currencyDetails = append(currencyDetails,
exchange.AccountCurrencyInfo{
CurrencyName: balance.Currency,
CurrencyName: currency.NewCode(balance.Currency),
TotalValue: balance.Balance,
})
}
@@ -239,14 +264,14 @@ func (h *HUOBI) GetFundingHistory() ([]exchange.FundHistory, error) {
}
// GetExchangeHistory returns historic trade data since exchange opening.
func (h *HUOBI) GetExchangeHistory(p pair.CurrencyPair, assetType string) ([]exchange.TradeHistory, error) {
func (h *HUOBI) GetExchangeHistory(p currency.Pair, assetType string) ([]exchange.TradeHistory, error) {
var resp []exchange.TradeHistory
return resp, common.ErrNotYetImplemented
}
// SubmitOrder submits a new order
func (h *HUOBI) SubmitOrder(p pair.CurrencyPair, side exchange.OrderSide, orderType exchange.OrderType, amount, price float64, clientID string) (exchange.SubmitOrderResponse, error) {
func (h *HUOBI) SubmitOrder(p currency.Pair, side exchange.OrderSide, orderType exchange.OrderType, amount, price float64, clientID string) (exchange.SubmitOrderResponse, error) {
var submitOrderResponse exchange.SubmitOrderResponse
accountID, err := strconv.ParseInt(clientID, 10, 64)
if err != nil {
@@ -257,7 +282,7 @@ func (h *HUOBI) SubmitOrder(p pair.CurrencyPair, side exchange.OrderSide, orderT
var params = SpotNewOrderRequestParams{
Amount: amount,
Source: "api",
Symbol: common.StringToLower(p.Pair().String()),
Symbol: common.StringToLower(p.String()),
AccountID: int(accountID),
}
@@ -338,14 +363,14 @@ func (h *HUOBI) GetOrderInfo(orderID string) (exchange.OrderDetail, error) {
}
// GetDepositAddress returns a deposit address for a specified currency
func (h *HUOBI) GetDepositAddress(cryptocurrency pair.CurrencyItem, accountID string) (string, error) {
func (h *HUOBI) GetDepositAddress(cryptocurrency currency.Code, accountID string) (string, error) {
return "", common.ErrFunctionNotSupported
}
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
// submitted
func (h *HUOBI) WithdrawCryptocurrencyFunds(withdrawRequest exchange.WithdrawRequest) (string, error) {
resp, err := h.Withdraw(withdrawRequest.Address, withdrawRequest.Currency.Lower().String(), withdrawRequest.AddressTag, withdrawRequest.Amount, withdrawRequest.FeeAmount)
resp, err := h.Withdraw(withdrawRequest.Currency, withdrawRequest.Address, withdrawRequest.AddressTag, withdrawRequest.Amount, withdrawRequest.FeeAmount)
return fmt.Sprintf("%v", resp), err
}
@@ -385,8 +410,8 @@ func (h *HUOBI) GetActiveOrders(getOrdersRequest exchange.GetOrdersRequest) ([]e
}
var allOrders []OrderInfo
for _, currency := range getOrdersRequest.Currencies {
resp, err := h.GetOpenOrders(h.ClientID, currency.Pair().Lower().String(), side, 500)
for _, c := range getOrdersRequest.Currencies {
resp, err := h.GetOpenOrders(h.ClientID, c.Lower().String(), side, 500)
if err != nil {
return nil, err
}
@@ -395,7 +420,8 @@ func (h *HUOBI) GetActiveOrders(getOrdersRequest exchange.GetOrdersRequest) ([]e
var orders []exchange.OrderDetail
for _, order := range allOrders {
symbol := pair.NewCurrencyPairDelimiter(order.Symbol, h.ConfigCurrencyPairFormat.Delimiter)
symbol := currency.NewPairDelimiter(order.Symbol,
h.ConfigCurrencyPairFormat.Delimiter)
orderDate := time.Unix(order.CreatedAt, 0)
orders = append(orders, exchange.OrderDetail{
@@ -410,7 +436,8 @@ func (h *HUOBI) GetActiveOrders(getOrdersRequest exchange.GetOrdersRequest) ([]e
})
}
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks, getOrdersRequest.EndTicks)
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks,
getOrdersRequest.EndTicks)
return orders, nil
}
@@ -425,7 +452,14 @@ func (h *HUOBI) GetOrderHistory(getOrdersRequest exchange.GetOrdersRequest) ([]e
states := "partial-canceled,filled,canceled"
var allOrders []OrderInfo
for _, currency := range getOrdersRequest.Currencies {
resp, err := h.GetOrders(currency.Pair().Lower().String(), "", "", "", states, "", "", "")
resp, err := h.GetOrders(currency.Lower().String(),
"",
"",
"",
states,
"",
"",
"")
if err != nil {
return nil, err
}
@@ -434,7 +468,8 @@ func (h *HUOBI) GetOrderHistory(getOrdersRequest exchange.GetOrdersRequest) ([]e
var orders []exchange.OrderDetail
for _, order := range allOrders {
symbol := pair.NewCurrencyPairDelimiter(order.Symbol, h.ConfigCurrencyPairFormat.Delimiter)
symbol := currency.NewPairDelimiter(order.Symbol,
h.ConfigCurrencyPairFormat.Delimiter)
orderDate := time.Unix(order.CreatedAt, 0)
orders = append(orders, exchange.OrderDetail{
@@ -447,7 +482,8 @@ func (h *HUOBI) GetOrderHistory(getOrdersRequest exchange.GetOrdersRequest) ([]e
})
}
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks, getOrdersRequest.EndTicks)
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks,
getOrdersRequest.EndTicks)
return orders, nil
}