mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-30 07:26:46 +00:00
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:
committed by
Adrian Gallagher
parent
ed760e184e
commit
0990f9d118
@@ -116,9 +116,9 @@ func (o *OKGroup) Setup(exch config.ExchangeConfig) {
|
||||
o.RESTPollingDelay = exch.RESTPollingDelay
|
||||
o.Verbose = exch.Verbose
|
||||
o.Websocket.SetWsStatusAndConnection(exch.Websocket)
|
||||
o.BaseCurrencies = common.SplitStrings(exch.BaseCurrencies, ",")
|
||||
o.AvailablePairs = common.SplitStrings(exch.AvailablePairs, ",")
|
||||
o.EnabledPairs = common.SplitStrings(exch.EnabledPairs, ",")
|
||||
o.BaseCurrencies = exch.BaseCurrencies
|
||||
o.AvailablePairs = exch.AvailablePairs
|
||||
o.EnabledPairs = exch.EnabledPairs
|
||||
err := o.SetCurrencyPairFormat()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -670,12 +670,12 @@ func (o *OKGroup) GetFee(feeBuilder exchange.FeeBuilder) (fee float64, _ error)
|
||||
case exchange.CryptocurrencyTradeFee:
|
||||
fee = calculateTradingFee(feeBuilder.PurchasePrice, feeBuilder.Amount, feeBuilder.IsMaker)
|
||||
case exchange.CryptocurrencyWithdrawalFee:
|
||||
withdrawFees, err := o.GetAccountWithdrawalFee(feeBuilder.CurrencyItem)
|
||||
withdrawFees, err := o.GetAccountWithdrawalFee(feeBuilder.FiatCurrency.String())
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
for _, withdrawFee := range withdrawFees {
|
||||
if withdrawFee.Currency == feeBuilder.CurrencyItem {
|
||||
if withdrawFee.Currency == feeBuilder.FiatCurrency.String() {
|
||||
fee = withdrawFee.MinFee
|
||||
break
|
||||
}
|
||||
|
||||
@@ -15,9 +15,10 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/currency/pair"
|
||||
exchange "github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
|
||||
log "github.com/thrasher-/gocryptotrader/logger"
|
||||
@@ -204,7 +205,7 @@ func (o *OKGroup) WsConnect() error {
|
||||
func (o *OKGroup) WsSubscribeToDefaults() (err error) {
|
||||
channelsToSubscribe := []string{okGroupWsSpotDepth, okGroupWsSpotCandle300s, okGroupWsSpotTicker, okGroupWsSpotTrade}
|
||||
for _, pair := range o.EnabledPairs {
|
||||
formattedPair := strings.ToUpper(strings.Replace(pair, "_", "-", 1))
|
||||
formattedPair := strings.ToUpper(strings.Replace(pair.String(), "_", "-", 1))
|
||||
for _, channel := range channelsToSubscribe {
|
||||
err = o.WsSubscribeToChannel(fmt.Sprintf("%v:%s", channel, formattedPair))
|
||||
if err != nil {
|
||||
@@ -501,7 +502,7 @@ func logDataResponse(response *WebsocketDataResponse) {
|
||||
// wsProcessTickers converts ticker data and sends it to the datahandler
|
||||
func (o *OKGroup) wsProcessTickers(response *WebsocketDataResponse) {
|
||||
for _, tickerData := range response.Data {
|
||||
instrument := pair.NewCurrencyPairDelimiter(tickerData.InstrumentID, "-")
|
||||
instrument := currency.NewPairDelimiter(tickerData.InstrumentID, "-")
|
||||
o.Websocket.DataHandler <- exchange.TickerData{
|
||||
Timestamp: tickerData.Timestamp,
|
||||
Exchange: o.GetName(),
|
||||
@@ -517,7 +518,7 @@ func (o *OKGroup) wsProcessTickers(response *WebsocketDataResponse) {
|
||||
// wsProcessTrades converts trade data and sends it to the datahandler
|
||||
func (o *OKGroup) wsProcessTrades(response *WebsocketDataResponse) {
|
||||
for _, trade := range response.Data {
|
||||
instrument := pair.NewCurrencyPairDelimiter(trade.InstrumentID, "-")
|
||||
instrument := currency.NewPairDelimiter(trade.InstrumentID, "-")
|
||||
o.Websocket.DataHandler <- exchange.TradeData{
|
||||
Amount: trade.Qty,
|
||||
AssetType: o.GetAssetTypeFromTableName(response.Table),
|
||||
@@ -534,7 +535,7 @@ func (o *OKGroup) wsProcessTrades(response *WebsocketDataResponse) {
|
||||
// wsProcessCandles converts candle data and sends it to the data handler
|
||||
func (o *OKGroup) wsProcessCandles(response *WebsocketDataResponse) {
|
||||
for _, candle := range response.Data {
|
||||
instrument := pair.NewCurrencyPairDelimiter(candle.InstrumentID, "-")
|
||||
instrument := currency.NewPairDelimiter(candle.InstrumentID, "-")
|
||||
timeData, err := time.Parse(time.RFC3339Nano, candle.WebsocketCandleResponse.Candle[0])
|
||||
if err != nil {
|
||||
log.Warnf("%v Time data could not be parsed: %v", o.GetName(), candle.Candle[0])
|
||||
@@ -567,7 +568,7 @@ func (o *OKGroup) wsProcessCandles(response *WebsocketDataResponse) {
|
||||
// WsProcessOrderBook Validates the checksum and updates internal orderbook values
|
||||
func (o *OKGroup) WsProcessOrderBook(response *WebsocketDataResponse) (err error) {
|
||||
for i := range response.Data {
|
||||
instrument := pair.NewCurrencyPairDelimiter(response.Data[i].InstrumentID, "-")
|
||||
instrument := currency.NewPairDelimiter(response.Data[i].InstrumentID, "-")
|
||||
if response.Action == okGroupWsOrderbookPartial {
|
||||
err = o.WsProcessPartialOrderBook(&response.Data[i], instrument, response.Table)
|
||||
} else if response.Action == okGroupWsOrderbookUpdate {
|
||||
@@ -592,7 +593,7 @@ func (o *OKGroup) AppendWsOrderbookItems(entries [][]interface{}) (orderbookItem
|
||||
|
||||
// WsProcessPartialOrderBook takes websocket orderbook data and creates an orderbook
|
||||
// Calculates checksum to ensure it is valid
|
||||
func (o *OKGroup) WsProcessPartialOrderBook(wsEventData *WebsocketDataWrapper, instrument pair.CurrencyPair, tableName string) error {
|
||||
func (o *OKGroup) WsProcessPartialOrderBook(wsEventData *WebsocketDataWrapper, instrument currency.Pair, tableName string) error {
|
||||
signedChecksum := o.CalculatePartialOrderbookChecksum(wsEventData)
|
||||
if signedChecksum != wsEventData.Checksum {
|
||||
return fmt.Errorf("channel: %v. Orderbook partial for %v checksum invalid", tableName, instrument)
|
||||
@@ -606,9 +607,9 @@ func (o *OKGroup) WsProcessPartialOrderBook(wsEventData *WebsocketDataWrapper, i
|
||||
Asks: asks,
|
||||
Bids: bids,
|
||||
AssetType: o.GetAssetTypeFromTableName(tableName),
|
||||
CurrencyPair: wsEventData.InstrumentID,
|
||||
LastUpdated: wsEventData.Timestamp,
|
||||
Pair: instrument,
|
||||
ExchangeName: o.GetName(),
|
||||
}
|
||||
|
||||
err := o.Websocket.Orderbook.LoadSnapshot(newOrderBook, o.GetName(), true)
|
||||
@@ -625,7 +626,7 @@ func (o *OKGroup) WsProcessPartialOrderBook(wsEventData *WebsocketDataWrapper, i
|
||||
|
||||
// WsProcessUpdateOrderbook updates an existing orderbook using websocket data
|
||||
// After merging WS data, it will sort, validate and finally update the existing orderbook
|
||||
func (o *OKGroup) WsProcessUpdateOrderbook(wsEventData *WebsocketDataWrapper, instrument pair.CurrencyPair, tableName string) error {
|
||||
func (o *OKGroup) WsProcessUpdateOrderbook(wsEventData *WebsocketDataWrapper, instrument currency.Pair, tableName string) error {
|
||||
internalOrderbook, err := o.GetOrderbookEx(instrument, o.GetAssetTypeFromTableName(tableName))
|
||||
if err != nil {
|
||||
return errors.New("orderbook nil, could not load existing orderbook")
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"sync"
|
||||
|
||||
"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"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
@@ -41,9 +41,9 @@ func (o *OKGroup) Run() {
|
||||
return
|
||||
}
|
||||
|
||||
var pairs []string
|
||||
var pairs currency.Pairs
|
||||
for x := range prods {
|
||||
pairs = append(pairs, prods[x].BaseCurrency+"_"+prods[x].QuoteCurrency)
|
||||
pairs = append(pairs, currency.NewPairFromString(prods[x].BaseCurrency+"_"+prods[x].QuoteCurrency))
|
||||
}
|
||||
|
||||
err = o.UpdateCurrencies(pairs, false, false)
|
||||
@@ -54,29 +54,28 @@ func (o *OKGroup) Run() {
|
||||
}
|
||||
|
||||
// UpdateTicker updates and returns the ticker for a currency pair
|
||||
func (o *OKGroup) UpdateTicker(p pair.CurrencyPair, assetType string) (tickerData ticker.Price, err error) {
|
||||
func (o *OKGroup) UpdateTicker(p currency.Pair, assetType string) (tickerData ticker.Price, err error) {
|
||||
resp, err := o.GetSpotAllTokenPairsInformationForCurrency(exchange.FormatExchangeCurrency(o.Name, p).String())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
tickerData = ticker.Price{
|
||||
Ask: resp.BestAsk,
|
||||
Bid: resp.BestBid,
|
||||
CurrencyPair: exchange.FormatExchangeCurrency(o.Name, p).String(),
|
||||
High: resp.High24h,
|
||||
Last: resp.Last,
|
||||
LastUpdated: resp.Timestamp,
|
||||
Low: resp.Low24h,
|
||||
Pair: p,
|
||||
Volume: resp.BaseVolume24h,
|
||||
Ask: resp.BestAsk,
|
||||
Bid: resp.BestBid,
|
||||
High: resp.High24h,
|
||||
Last: resp.Last,
|
||||
LastUpdated: resp.Timestamp,
|
||||
Low: resp.Low24h,
|
||||
Pair: exchange.FormatExchangeCurrency(o.Name, p),
|
||||
Volume: resp.BaseVolume24h,
|
||||
}
|
||||
|
||||
ticker.ProcessTicker(o.Name, p, tickerData, assetType)
|
||||
err = ticker.ProcessTicker(o.Name, tickerData, assetType)
|
||||
return
|
||||
}
|
||||
|
||||
// GetTickerPrice returns the ticker for a currency pair
|
||||
func (o *OKGroup) GetTickerPrice(p pair.CurrencyPair, assetType string) (tickerData ticker.Price, err error) {
|
||||
func (o *OKGroup) GetTickerPrice(p currency.Pair, assetType string) (tickerData ticker.Price, err error) {
|
||||
tickerData, err = ticker.GetTicker(o.GetName(), p, assetType)
|
||||
if err != nil {
|
||||
return o.UpdateTicker(p, assetType)
|
||||
@@ -85,8 +84,8 @@ func (o *OKGroup) GetTickerPrice(p pair.CurrencyPair, assetType string) (tickerD
|
||||
}
|
||||
|
||||
// GetOrderbookEx returns orderbook base on the currency pair
|
||||
func (o *OKGroup) GetOrderbookEx(p pair.CurrencyPair, assetType string) (resp orderbook.Base, err error) {
|
||||
ob, err := orderbook.GetOrderbook(o.GetName(), p, assetType)
|
||||
func (o *OKGroup) GetOrderbookEx(p currency.Pair, assetType string) (resp orderbook.Base, err error) {
|
||||
ob, err := orderbook.Get(o.GetName(), p, assetType)
|
||||
if err != nil {
|
||||
return o.UpdateOrderbook(p, assetType)
|
||||
}
|
||||
@@ -94,7 +93,7 @@ func (o *OKGroup) GetOrderbookEx(p pair.CurrencyPair, assetType string) (resp or
|
||||
}
|
||||
|
||||
// UpdateOrderbook updates and returns the orderbook for a currency pair
|
||||
func (o *OKGroup) UpdateOrderbook(p pair.CurrencyPair, assetType string) (resp orderbook.Base, err error) {
|
||||
func (o *OKGroup) UpdateOrderbook(p currency.Pair, assetType string) (resp orderbook.Base, err error) {
|
||||
orderbookNew, err := o.GetSpotOrderBook(GetSpotOrderBookRequest{
|
||||
InstrumentID: exchange.FormatExchangeCurrency(o.Name, p).String(),
|
||||
})
|
||||
@@ -103,12 +102,12 @@ func (o *OKGroup) UpdateOrderbook(p pair.CurrencyPair, assetType string) (resp o
|
||||
}
|
||||
|
||||
for x := range orderbookNew.Bids {
|
||||
amount, err := strconv.ParseFloat(orderbookNew.Bids[x][1], 64)
|
||||
if err != nil {
|
||||
amount, convErr := strconv.ParseFloat(orderbookNew.Bids[x][1], 64)
|
||||
if convErr != nil {
|
||||
log.Errorf("Could not convert %v to float64", orderbookNew.Bids[x][1])
|
||||
}
|
||||
price, err := strconv.ParseFloat(orderbookNew.Bids[x][0], 64)
|
||||
if err != nil {
|
||||
price, convErr := strconv.ParseFloat(orderbookNew.Bids[x][0], 64)
|
||||
if convErr != nil {
|
||||
log.Errorf("Could not convert %v to float64", orderbookNew.Bids[x][0])
|
||||
}
|
||||
resp.Bids = append(resp.Bids, orderbook.Item{
|
||||
@@ -118,12 +117,12 @@ func (o *OKGroup) UpdateOrderbook(p pair.CurrencyPair, assetType string) (resp o
|
||||
}
|
||||
|
||||
for x := range orderbookNew.Asks {
|
||||
amount, err := strconv.ParseFloat(orderbookNew.Asks[x][1], 64)
|
||||
if err != nil {
|
||||
amount, convErr := strconv.ParseFloat(orderbookNew.Asks[x][1], 64)
|
||||
if convErr != nil {
|
||||
log.Errorf("Could not convert %v to float64", orderbookNew.Asks[x][1])
|
||||
}
|
||||
price, err := strconv.ParseFloat(orderbookNew.Asks[x][0], 64)
|
||||
if err != nil {
|
||||
price, convErr := strconv.ParseFloat(orderbookNew.Asks[x][0], 64)
|
||||
if convErr != nil {
|
||||
log.Errorf("Could not convert %v to float64", orderbookNew.Asks[x][0])
|
||||
}
|
||||
resp.Asks = append(resp.Asks, orderbook.Item{
|
||||
@@ -132,8 +131,16 @@ func (o *OKGroup) UpdateOrderbook(p pair.CurrencyPair, assetType string) (resp o
|
||||
})
|
||||
}
|
||||
|
||||
orderbook.ProcessOrderbook(o.GetName(), p, resp, assetType)
|
||||
return orderbook.GetOrderbook(o.Name, p, assetType)
|
||||
resp.Pair = p
|
||||
resp.AssetType = assetType
|
||||
resp.ExchangeName = o.Name
|
||||
|
||||
err = resp.Process()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return orderbook.Get(o.Name, p, assetType)
|
||||
}
|
||||
|
||||
// GetAccountInfo retrieves balances for all enabled currencies
|
||||
@@ -152,7 +159,7 @@ func (o *OKGroup) GetAccountInfo() (resp exchange.AccountInfo, err error) {
|
||||
log.Errorf("Could not convert %v to float64", curr.Balance)
|
||||
}
|
||||
currencyAccount.Currencies = append(currencyAccount.Currencies, exchange.AccountCurrencyInfo{
|
||||
CurrencyName: curr.ID,
|
||||
CurrencyName: currency.NewCode(curr.ID),
|
||||
Hold: hold,
|
||||
TotalValue: totalValue,
|
||||
})
|
||||
@@ -206,12 +213,12 @@ func (o *OKGroup) GetFundingHistory() (resp []exchange.FundHistory, err error) {
|
||||
}
|
||||
|
||||
// GetExchangeHistory returns historic trade data since exchange opening.
|
||||
func (o *OKGroup) GetExchangeHistory(p pair.CurrencyPair, assetType string) ([]exchange.TradeHistory, error) {
|
||||
func (o *OKGroup) GetExchangeHistory(p currency.Pair, assetType string) ([]exchange.TradeHistory, error) {
|
||||
return nil, common.ErrNotYetImplemented
|
||||
}
|
||||
|
||||
// SubmitOrder submits a new order
|
||||
func (o *OKGroup) SubmitOrder(p pair.CurrencyPair, side exchange.OrderSide, orderType exchange.OrderType, amount, price float64, clientID string) (resp exchange.SubmitOrderResponse, err error) {
|
||||
func (o *OKGroup) SubmitOrder(p currency.Pair, side exchange.OrderSide, orderType exchange.OrderType, amount, price float64, clientID string) (resp exchange.SubmitOrderResponse, err error) {
|
||||
request := PlaceSpotOrderRequest{
|
||||
ClientOID: clientID,
|
||||
InstrumentID: exchange.FormatExchangeCurrency(o.Name, p).String(),
|
||||
@@ -292,8 +299,9 @@ func (o *OKGroup) GetOrderInfo(orderID string) (resp exchange.OrderDetail, err e
|
||||
return
|
||||
}
|
||||
resp = exchange.OrderDetail{
|
||||
Amount: order.Size,
|
||||
CurrencyPair: pair.NewCurrencyPairDelimiter(order.InstrumentID, o.ConfigCurrencyPairFormat.Delimiter),
|
||||
Amount: order.Size,
|
||||
CurrencyPair: currency.NewPairDelimiter(order.InstrumentID,
|
||||
o.ConfigCurrencyPairFormat.Delimiter),
|
||||
Exchange: o.Name,
|
||||
OrderDate: order.Timestamp,
|
||||
ExecutedAmount: order.FilledSize,
|
||||
@@ -304,7 +312,7 @@ func (o *OKGroup) GetOrderInfo(orderID string) (resp exchange.OrderDetail, err e
|
||||
}
|
||||
|
||||
// GetDepositAddress returns a deposit address for a specified currency
|
||||
func (o *OKGroup) GetDepositAddress(p pair.CurrencyItem, accountID string) (_ string, err error) {
|
||||
func (o *OKGroup) GetDepositAddress(p currency.Code, accountID string) (_ string, err error) {
|
||||
wallet, err := o.GetAccountDepositAddressForCurrency(p.Lower().String())
|
||||
if err != nil {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user