mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-20 23:16:49 +00:00
* implemented binance and bitfinex GetHistoricCandles wrapper methods) * coinbene supported added * after and before clean up * gateio wrapper completed * merged upstream/master * Added bsaic KlineIntervalSupported() method * Converted binance fixed test * WIP * new KlineConvertToExchangeStandardString method added * end of day WIP * WIP * end of day WIP started migration of trade history * added kline support to hitbtc huobi lbank * added exchangehistory to all supported exchanges started work on coinbase 300 candles/request method * end of day WIP * removed unused ta and misc changes to flag ready for review * yobit cleanup * revert coinbase changES * general code clean up and added zb support * poloniex support added * renamed method to FormatExchangeKlineInterval other misc fixes * linter fixes * linter fixes * removed verbose * fixed poloniex test coverage * revert poloniex mock data * regenerated poloniex mock data * a very verbose clean up * binance mock clean up * removed unneeded t.Log() * setting verbose to true to debug CI issue * first pass changes addressed * common.ErrNotYetImplemented implemented :D * comments added * WIP-addressed exchange requests and reverted previous GetExchangeHistory changes * WIP-addressed exchange requests and reverted previous GetExchangeHistory changes * increased test coverage added kraken support * OKGroup support completed started work on address GetExchangeHistory feedback and migrating to own PR under https://github.com/xtda/gocryptotrader/tree/exchange_history * convert zb ratelimits * gofmt run on okcoin * increased delay on rate limit * gofmt package * fixed panic with coinbene and bithumb if conversion fails * very broken end of day WIP * added support for GetHistoricCandlesEx to coinbase and binance * gofmt package * coinbase, btcmarkets, zb ex wrapper function added * added all exchange support for ex regenerated mock data * update bithumb to return wrapper method * gofmt package * end of day started work on changes * reworked test coverage added okgroup support general fixes/change requests addressed * Added OneMonth * limit checks on supportedexchanges * reverted getexchangehistory * reworked binance tesT * added workaround for kraken panic * renamed command to extended removed interval check on non-implemented commands * added wrapperconfig back * increased test coverage for FormatExchangeKlineInterval * WIP * increased test coverage for FormatExchangeKlineInterval bitfinex/gateio/huobi * linter fixes * zb kraken lbank coinbene btcmarkets support added * removed verbose * OK group support for other asset types added * swapped margin to use spot endpoint * index support added test coverage added for asset types * added asset type to okcoin test * gofmt * add asset to extended method * removed verbose * add support for coinbene swap increase test coverage * removed verbose * small clean up of okgroup wrapper functions * verbose to troubleshoot CI issues * removed verbose * added error check reverted coinbasechanges * readme updated * removed unused start/finish started work on decoupling api requests from kline package * restructured coinbene, bithumb methods, added bitstamp support * kraken time fix * BTCMarkets restructure * typo fix * removed test for futures due to contact changing * added start/end date to extended method over range * converted to assettranslator * removed verbose * removed invalid char * reverted incorrectly removed return * added import * further template updates * macos hates my keyboard :D * misc canges * x -> i * removed verbose * updated fixCasing to allocate var before checks * removed time conversion * sort all outgoing kline candles * fixCasing fix * after/before checks added * added parallel to test * logic check on BTCmarkets * removed unused param, used correct iterator * converted HitBTC to use time.Time * add iszero false check to candle times * updated resultlimit to 5000 * new line added * added comment to exported const * use configured ratelimit * fixed pair for test * panic fixed WIP on fixCasing * fixCasing rework, started work on readme docs * enable rate limiter for wrapper issues tool * docs updated * removed err from return and formatted currency * updated Yobit supported status * Updated HitBTC to use onehour candles due to test exeuction times * added further details to gctcli output * added link to docs * added link to tempalte * disable FTX websocket in config_example * fix poloneix * regenerated poloniex mock data * removed recording flag
429 lines
12 KiB
Go
429 lines
12 KiB
Go
package yobit
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/common/crypto"
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
|
|
"github.com/thrasher-corp/gocryptotrader/log"
|
|
)
|
|
|
|
const (
|
|
apiPublicURL = "https://yobit.net/api"
|
|
apiPrivateURL = "https://yobit.net/tapi"
|
|
apiPublicVersion = "3"
|
|
publicInfo = "info"
|
|
publicTicker = "ticker"
|
|
publicDepth = "depth"
|
|
publicTrades = "trades"
|
|
privateAccountInfo = "getInfo"
|
|
privateTrade = "Trade"
|
|
privateActiveOrders = "ActiveOrders"
|
|
privateOrderInfo = "OrderInfo"
|
|
privateCancelOrder = "CancelOrder"
|
|
privateTradeHistory = "TradeHistory"
|
|
privateGetDepositAddress = "GetDepositAddress"
|
|
privateWithdrawCoinsToAddress = "WithdrawCoinsToAddress"
|
|
privateCreateCoupon = "CreateYobicode"
|
|
privateRedeemCoupon = "RedeemYobicode"
|
|
|
|
yobitAuthRate = 0
|
|
yobitUnauthRate = 0
|
|
)
|
|
|
|
// Yobit is the overarching type across the Yobit package
|
|
type Yobit struct {
|
|
exchange.Base
|
|
}
|
|
|
|
// GetInfo returns the Yobit info
|
|
func (y *Yobit) GetInfo() (Info, error) {
|
|
resp := Info{}
|
|
path := fmt.Sprintf("%s/%s/%s/", y.API.Endpoints.URL, apiPublicVersion, publicInfo)
|
|
|
|
return resp, y.SendHTTPRequest(path, &resp)
|
|
}
|
|
|
|
// GetTicker returns a ticker for a specific currency
|
|
func (y *Yobit) GetTicker(symbol string) (map[string]Ticker, error) {
|
|
type Response struct {
|
|
Data map[string]Ticker
|
|
}
|
|
|
|
response := Response{}
|
|
path := fmt.Sprintf("%s/%s/%s/%s", y.API.Endpoints.URL, apiPublicVersion, publicTicker, symbol)
|
|
|
|
return response.Data, y.SendHTTPRequest(path, &response.Data)
|
|
}
|
|
|
|
// GetDepth returns the depth for a specific currency
|
|
func (y *Yobit) GetDepth(symbol string) (Orderbook, error) {
|
|
type Response struct {
|
|
Data map[string]Orderbook
|
|
}
|
|
|
|
response := Response{}
|
|
path := fmt.Sprintf("%s/%s/%s/%s", y.API.Endpoints.URL, apiPublicVersion, publicDepth, symbol)
|
|
|
|
return response.Data[symbol],
|
|
y.SendHTTPRequest(path, &response.Data)
|
|
}
|
|
|
|
// GetTrades returns the trades for a specific currency
|
|
func (y *Yobit) GetTrades(symbol string, start int64, sortAsc bool) ([]Trades, error) {
|
|
type Response struct {
|
|
Data map[string][]Trades
|
|
}
|
|
|
|
v := url.Values{}
|
|
if sortAsc {
|
|
v.Set("order", "ASC")
|
|
} else {
|
|
v.Set("order", "DESC")
|
|
}
|
|
if start != 0 {
|
|
v.Set("since", strconv.FormatInt(start, 10))
|
|
}
|
|
|
|
var response Response
|
|
path := y.API.Endpoints.URL + "/" + apiPublicVersion + "/" + publicTrades + "/" + symbol + "?" + v.Encode()
|
|
return response.Data[symbol], y.SendHTTPRequest(path, &response.Data)
|
|
}
|
|
|
|
// GetAccountInformation returns a users account info
|
|
func (y *Yobit) GetAccountInformation() (AccountInfo, error) {
|
|
result := AccountInfo{}
|
|
|
|
err := y.SendAuthenticatedHTTPRequest(privateAccountInfo, url.Values{}, &result)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
if result.Error != "" {
|
|
return result, errors.New(result.Error)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// Trade places an order and returns the order ID if successful or an error
|
|
func (y *Yobit) Trade(pair, orderType string, amount, price float64) (int64, error) {
|
|
req := url.Values{}
|
|
req.Add("pair", pair)
|
|
req.Add("type", strings.ToLower(orderType))
|
|
req.Add("amount", strconv.FormatFloat(amount, 'f', -1, 64))
|
|
req.Add("rate", strconv.FormatFloat(price, 'f', -1, 64))
|
|
|
|
result := Trade{}
|
|
|
|
err := y.SendAuthenticatedHTTPRequest(privateTrade, req, &result)
|
|
if err != nil {
|
|
return int64(result.OrderID), err
|
|
}
|
|
if result.Error != "" {
|
|
return int64(result.OrderID), errors.New(result.Error)
|
|
}
|
|
return int64(result.OrderID), nil
|
|
}
|
|
|
|
// GetOpenOrders returns the active orders for a specific currency
|
|
func (y *Yobit) GetOpenOrders(pair string) (map[string]ActiveOrders, error) {
|
|
req := url.Values{}
|
|
req.Add("pair", pair)
|
|
|
|
result := map[string]ActiveOrders{}
|
|
|
|
return result, y.SendAuthenticatedHTTPRequest(privateActiveOrders, req, &result)
|
|
}
|
|
|
|
// GetOrderInformation returns the order info for a specific order ID
|
|
func (y *Yobit) GetOrderInformation(orderID int64) (map[string]OrderInfo, error) {
|
|
req := url.Values{}
|
|
req.Add("order_id", strconv.FormatInt(orderID, 10))
|
|
|
|
result := map[string]OrderInfo{}
|
|
|
|
return result, y.SendAuthenticatedHTTPRequest(privateOrderInfo, req, &result)
|
|
}
|
|
|
|
// CancelExistingOrder cancels an order for a specific order ID
|
|
func (y *Yobit) CancelExistingOrder(orderID int64) error {
|
|
req := url.Values{}
|
|
req.Add("order_id", strconv.FormatInt(orderID, 10))
|
|
|
|
result := CancelOrder{}
|
|
|
|
err := y.SendAuthenticatedHTTPRequest(privateCancelOrder, req, &result)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if result.Error != "" {
|
|
return errors.New(result.Error)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetTradeHistory returns the trade history
|
|
func (y *Yobit) GetTradeHistory(tidFrom, count, tidEnd, since, end int64, order, pair string) (map[string]TradeHistory, error) {
|
|
req := url.Values{}
|
|
req.Add("from", strconv.FormatInt(tidFrom, 10))
|
|
req.Add("count", strconv.FormatInt(count, 10))
|
|
req.Add("from_id", strconv.FormatInt(tidFrom, 10))
|
|
req.Add("end_id", strconv.FormatInt(tidEnd, 10))
|
|
req.Add("order", order)
|
|
req.Add("since", strconv.FormatInt(since, 10))
|
|
req.Add("end", strconv.FormatInt(end, 10))
|
|
req.Add("pair", pair)
|
|
|
|
result := TradeHistoryResponse{}
|
|
|
|
err := y.SendAuthenticatedHTTPRequest(privateTradeHistory, req, &result)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if result.Success == 0 {
|
|
return nil, errors.New(result.Error)
|
|
}
|
|
|
|
return result.Data, nil
|
|
}
|
|
|
|
// GetCryptoDepositAddress returns the deposit address for a specific currency
|
|
func (y *Yobit) GetCryptoDepositAddress(coin string) (DepositAddress, error) {
|
|
req := url.Values{}
|
|
req.Add("coinName", coin)
|
|
|
|
result := DepositAddress{}
|
|
|
|
err := y.SendAuthenticatedHTTPRequest(privateGetDepositAddress, req, &result)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
if result.Success != 1 {
|
|
return result, fmt.Errorf("%s", result.Error)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// WithdrawCoinsToAddress initiates a withdrawal to a specified address
|
|
func (y *Yobit) WithdrawCoinsToAddress(coin string, amount float64, address string) (WithdrawCoinsToAddress, error) {
|
|
req := url.Values{}
|
|
req.Add("coinName", coin)
|
|
req.Add("amount", strconv.FormatFloat(amount, 'f', -1, 64))
|
|
req.Add("address", address)
|
|
|
|
result := WithdrawCoinsToAddress{}
|
|
|
|
err := y.SendAuthenticatedHTTPRequest(privateWithdrawCoinsToAddress, req, &result)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
if result.Error != "" {
|
|
return result, errors.New(result.Error)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// CreateCoupon creates an exchange coupon for a sepcific currency
|
|
func (y *Yobit) CreateCoupon(currency string, amount float64) (CreateCoupon, error) {
|
|
req := url.Values{}
|
|
req.Add("currency", currency)
|
|
req.Add("amount", strconv.FormatFloat(amount, 'f', -1, 64))
|
|
|
|
var result CreateCoupon
|
|
|
|
err := y.SendAuthenticatedHTTPRequest(privateCreateCoupon, req, &result)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
if result.Error != "" {
|
|
return result, errors.New(result.Error)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// RedeemCoupon redeems an exchange coupon
|
|
func (y *Yobit) RedeemCoupon(coupon string) (RedeemCoupon, error) {
|
|
req := url.Values{}
|
|
req.Add("coupon", coupon)
|
|
|
|
result := RedeemCoupon{}
|
|
|
|
err := y.SendAuthenticatedHTTPRequest(privateRedeemCoupon, req, &result)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
if result.Error != "" {
|
|
return result, errors.New(result.Error)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// SendHTTPRequest sends an unauthenticated HTTP request
|
|
func (y *Yobit) SendHTTPRequest(path string, result interface{}) error {
|
|
return y.SendPayload(context.Background(), &request.Item{
|
|
Method: http.MethodGet,
|
|
Path: path,
|
|
Result: result,
|
|
Verbose: y.Verbose,
|
|
HTTPDebugging: y.HTTPDebugging,
|
|
HTTPRecording: y.HTTPRecording,
|
|
})
|
|
}
|
|
|
|
// SendAuthenticatedHTTPRequest sends an authenticated HTTP request to Yobit
|
|
func (y *Yobit) SendAuthenticatedHTTPRequest(path string, params url.Values, result interface{}) (err error) {
|
|
if !y.AllowAuthenticatedRequest() {
|
|
return fmt.Errorf(exchange.WarningAuthenticatedRequestWithoutCredentialsSet, y.Name)
|
|
}
|
|
|
|
if params == nil {
|
|
params = url.Values{}
|
|
}
|
|
|
|
n := y.Requester.GetNonce(false).String()
|
|
|
|
params.Set("nonce", n)
|
|
params.Set("method", path)
|
|
|
|
encoded := params.Encode()
|
|
hmac := crypto.GetHMAC(crypto.HashSHA512, []byte(encoded), []byte(y.API.Credentials.Secret))
|
|
|
|
if y.Verbose {
|
|
log.Debugf(log.ExchangeSys, "Sending POST request to %s calling path %s with params %s\n",
|
|
apiPrivateURL,
|
|
path,
|
|
encoded)
|
|
}
|
|
|
|
headers := make(map[string]string)
|
|
headers["Key"] = y.API.Credentials.Key
|
|
headers["Sign"] = crypto.HexEncodeToString(hmac)
|
|
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
|
|
|
return y.SendPayload(context.Background(), &request.Item{
|
|
Method: http.MethodPost,
|
|
Path: apiPrivateURL,
|
|
Headers: headers,
|
|
Body: strings.NewReader(encoded),
|
|
Result: result,
|
|
AuthRequest: true,
|
|
NonceEnabled: true,
|
|
Verbose: y.Verbose,
|
|
HTTPDebugging: y.HTTPDebugging,
|
|
HTTPRecording: y.HTTPRecording,
|
|
})
|
|
}
|
|
|
|
// GetFee returns an estimate of fee based on type of transaction
|
|
func (y *Yobit) GetFee(feeBuilder *exchange.FeeBuilder) (float64, error) {
|
|
var fee float64
|
|
switch feeBuilder.FeeType {
|
|
case exchange.CryptocurrencyTradeFee:
|
|
fee = calculateTradingFee(feeBuilder.PurchasePrice, feeBuilder.Amount)
|
|
case exchange.CryptocurrencyWithdrawalFee:
|
|
fee = getWithdrawalFee(feeBuilder.Pair.Base)
|
|
case exchange.InternationalBankDepositFee:
|
|
fee = getInternationalBankDepositFee(feeBuilder.FiatCurrency,
|
|
feeBuilder.BankTransactionType)
|
|
case exchange.InternationalBankWithdrawalFee:
|
|
fee = getInternationalBankWithdrawalFee(feeBuilder.FiatCurrency,
|
|
feeBuilder.Amount,
|
|
feeBuilder.BankTransactionType)
|
|
case exchange.OfflineTradeFee:
|
|
fee = calculateTradingFee(feeBuilder.PurchasePrice, feeBuilder.Amount)
|
|
}
|
|
if fee < 0 {
|
|
fee = 0
|
|
}
|
|
|
|
return fee, nil
|
|
}
|
|
|
|
func calculateTradingFee(price, amount float64) (fee float64) {
|
|
return 0.002 * price * amount
|
|
}
|
|
|
|
func getWithdrawalFee(c currency.Code) float64 {
|
|
return WithdrawalFees[c]
|
|
}
|
|
|
|
func getInternationalBankWithdrawalFee(c currency.Code, amount float64, bankTransactionType exchange.InternationalBankTransactionType) float64 {
|
|
var fee float64
|
|
|
|
switch bankTransactionType {
|
|
case exchange.PerfectMoney:
|
|
if c == currency.USD {
|
|
fee = 0.02 * amount
|
|
}
|
|
case exchange.Payeer:
|
|
switch c {
|
|
case currency.USD:
|
|
fee = 0.03 * amount
|
|
case currency.RUR:
|
|
fee = 0.006 * amount
|
|
}
|
|
case exchange.AdvCash:
|
|
switch c {
|
|
case currency.USD:
|
|
fee = 0.04 * amount
|
|
case currency.RUR:
|
|
fee = 0.03 * amount
|
|
}
|
|
case exchange.Qiwi:
|
|
if c == currency.RUR {
|
|
fee = 0.04 * amount
|
|
}
|
|
case exchange.Capitalist:
|
|
if c == currency.USD {
|
|
fee = 0.06 * amount
|
|
}
|
|
}
|
|
|
|
return fee
|
|
}
|
|
|
|
// getInternationalBankDepositFee; No real fees for yobit deposits, but want to be explicit on what each payment type supports
|
|
func getInternationalBankDepositFee(c currency.Code, bankTransactionType exchange.InternationalBankTransactionType) float64 {
|
|
var fee float64
|
|
switch bankTransactionType {
|
|
case exchange.PerfectMoney:
|
|
if c == currency.USD {
|
|
fee = 0
|
|
}
|
|
case exchange.Payeer:
|
|
switch c {
|
|
case currency.USD:
|
|
fee = 0
|
|
case currency.RUR:
|
|
fee = 0
|
|
}
|
|
case exchange.AdvCash:
|
|
switch c {
|
|
case currency.USD:
|
|
fee = 0
|
|
case currency.RUR:
|
|
fee = 0
|
|
}
|
|
case exchange.Qiwi:
|
|
if c == currency.RUR {
|
|
fee = 0
|
|
}
|
|
case exchange.Capitalist:
|
|
switch c {
|
|
case currency.USD:
|
|
fee = 0
|
|
case currency.RUR:
|
|
fee = 0
|
|
}
|
|
}
|
|
|
|
return fee
|
|
}
|