mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-04 23:16:54 +00:00
* ALMOST THERE * more api wips * more api thingz * testing n more api wipz * more apiz * more wips * what is goin on * more wips * whip n testing * testing * testing no keys * remove log * kraken is broken ugh * still broken * fixing auth funcs + usdtm api docs * wip * api stuffs * whip * more wips * whip * more wip * api wip n testing * wip * wip * unsaved * wip n testing * wip * wip * wip * wip * wip * wip * wip * wip * wip * whip * wrapper authenticated functions * adding asset type and fixing dependencies * wip * binance auth wrapper start * wrapper functionality * wip * wip * wip * wrapper cancel functions * order submission for wrappers * wip * more error fixing and nits * websocket beginning n error fix * wip * WOW * glorious n shazzy nits * useless nits * wip * fixing things * merge stuffs * crapveyor * crapveyor rebuild * probably broke more things than he fixed * rm lns n other thangs * hope * please * stop it * done * ofcourse * rm vb * fix lbank * appveyor please * float lev * DONT ASK RYAN FOR HELP EVER * wip * wip * endpoint upgrades continued * path upgrade * NeeeNeeeNeeeNeeeNING * fix stuffs * fixing time issue * fixing broken funcs * glorious nits * shaz changes * fixing errors for fundmon * more error fixing for fundmon * test running past 30s * basic changes * THX AGAIN SHAZBERT * path system upgrade * config upgrade * unsaved stuffs * broken wip config upgrade * path system upgrade contd. * path system upgrade contd * path upgrade ready for review * testing verbose removed * linter stuffs * appveyor stuffs * appveyor stuff * fixed? * bugfix * wip * broken stuff * fix test * wierd hack fix * appveyor pls stop * error found * more useless nits * bitmex err * broken wip * broken wip path upgrade change to uint32 * changed url lookups to uint * WOW * ready4review * config fixed HOPEFULLY * config fix and glorious changes * efficient way of getting orders and open orders * binance wrapper logic fixing * testing, adding tests and fixing lot of errrrrs * merge master * appveyor stuffs * appveyor stuffs * fmt * test * octalLiteral issue fix? * octalLiteral fix? * rm vb * prnt ln to restart * adding testz * test fixzzz * READY FOR REVIEW * Actually ready now * FORMATTING * addressing shazzy n glorious nits * crapveyor * rm vb * small change * fixing err * shazbert nits * review changes * requested changes * more requested changes * noo * last nit fixes * restart appveyor * improving test cov * Update .golangci.yml * shazbert changes * moving pair formatting * format pair update wip * path upgrade complete * error fix * appveyor linters * more linters * remove testexch * more formatting changes * changes * shazbert changes * checking older requested changes to ensure completion * wip * fixing broken code * error fix * all fixed * additional changes * more changes * remove commented code * ftx margin api * appveyor fixes * more appveyor issues + test addition * more appveyor issues + test addition * remove unnecessary * testing * testing, fixing okex api, error fix * git merge fix * go sum * glorious changes and error fix * rm vb * more glorious changes and go mod tidy * fixed now * okex testing upgrade * old config migration and batch fetching fix * added test * glorious requested changes WIP * tested and fixed * go fmted * go fmt and test fix * additional funcs and tests for fundingRates * OKEX tested and fixed * appveyor fixes * ineff assign * 1 glorious change * error fix * typo * shazbert changes * glorious code changes and path fixing huobi WIP * adding assetType to accountinfo functions * fixing panic * panic fix and updating account info wrappers WIP * updateaccountinfo updated * testing WIP binance USDT n Coin Margined and Kraken Futures * auth functions tested and fixed * added test * config reverted * shazbert and glorious changes * shazbert and glorious changes * latest changes and portfolio update * go fmt change: * remove commented codes * improved error checking * index out of range fix * rm ln * critical nit * glorious latest changes * appveyor changes * shazbert change * easier readability * latest glorious changes * shadow dec * assetstore updated * last change * another last change * merge changes * go mod tidy * thrasher requested changes wip * improving struct layouts * appveyor go fmt * remove unnecessary code * shazbert changes * small change * oopsie * tidy * configtest reverted * error fix * oopsie * for what * test patch fix * insecurities * fixing tests * fix config
234 lines
6.8 KiB
Go
234 lines
6.8 KiB
Go
package exchange
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"sort"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
"github.com/thrasher-corp/gocryptotrader/engine"
|
|
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
|
|
"github.com/thrasher-corp/gocryptotrader/portfolio/banking"
|
|
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
|
|
)
|
|
|
|
// Exchange implements all required methods for Wrapper
|
|
type Exchange struct{}
|
|
|
|
// Exchanges returns slice of all current exchanges
|
|
func (e Exchange) Exchanges(enabledOnly bool) []string {
|
|
return engine.Bot.GetExchangeNames(enabledOnly)
|
|
}
|
|
|
|
// GetExchange returns IBotExchange for exchange or error if exchange is not found
|
|
func (e Exchange) GetExchange(exch string) (exchange.IBotExchange, error) {
|
|
ex := engine.Bot.GetExchangeByName(exch)
|
|
if ex == nil {
|
|
return nil, fmt.Errorf("%v exchange not found", exch)
|
|
}
|
|
|
|
return ex, nil
|
|
}
|
|
|
|
// IsEnabled returns if requested exchange is enabled or disabled
|
|
func (e Exchange) IsEnabled(exch string) bool {
|
|
ex, err := e.GetExchange(exch)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
return ex.IsEnabled()
|
|
}
|
|
|
|
// Orderbook returns current orderbook requested exchange, pair and asset
|
|
func (e Exchange) Orderbook(exch string, pair currency.Pair, item asset.Item) (*orderbook.Base, error) {
|
|
return engine.Bot.GetSpecificOrderbook(pair, exch, item)
|
|
}
|
|
|
|
// Ticker returns ticker for provided currency pair & asset type
|
|
func (e Exchange) Ticker(exch string, pair currency.Pair, item asset.Item) (*ticker.Price, error) {
|
|
ex, err := e.GetExchange(exch)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return ex.FetchTicker(pair, item)
|
|
}
|
|
|
|
// Pairs returns either all or enabled currency pairs
|
|
func (e Exchange) Pairs(exch string, enabledOnly bool, item asset.Item) (*currency.Pairs, error) {
|
|
x, err := engine.Bot.Config.GetExchangeConfig(exch)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ps, err := x.CurrencyPairs.Get(item)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if enabledOnly {
|
|
return &ps.Enabled, nil
|
|
}
|
|
return &ps.Available, nil
|
|
}
|
|
|
|
// QueryOrder returns details of a valid exchange order
|
|
func (e Exchange) QueryOrder(exch, orderID string, pair currency.Pair, assetType asset.Item) (*order.Detail, error) {
|
|
o, err := engine.Bot.OrderManager.GetOrderInfo(exch, orderID, pair, assetType)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &o, nil
|
|
}
|
|
|
|
// SubmitOrder submit new order on exchange
|
|
func (e Exchange) SubmitOrder(submit *order.Submit) (*order.SubmitResponse, error) {
|
|
r, err := engine.Bot.OrderManager.Submit(submit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &r.SubmitResponse, nil
|
|
}
|
|
|
|
// CancelOrder wrapper to cancel order on exchange
|
|
func (e Exchange) CancelOrder(exch, orderID string, cp currency.Pair, a asset.Item) (bool, error) {
|
|
orderDetails, err := e.QueryOrder(exch, orderID, cp, a)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
cancel := &order.Cancel{
|
|
AccountID: orderDetails.AccountID,
|
|
ID: orderDetails.ID,
|
|
Pair: orderDetails.Pair,
|
|
Side: orderDetails.Side,
|
|
AssetType: orderDetails.AssetType,
|
|
Exchange: exch,
|
|
}
|
|
|
|
err = engine.Bot.OrderManager.Cancel(cancel)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// AccountInformation returns account information (balance etc) for requested exchange
|
|
func (e Exchange) AccountInformation(exch string, assetType asset.Item) (account.Holdings, error) {
|
|
ex, err := e.GetExchange(exch)
|
|
if err != nil {
|
|
return account.Holdings{}, err
|
|
}
|
|
|
|
accountInfo, err := ex.FetchAccountInfo(assetType)
|
|
if err != nil {
|
|
return account.Holdings{}, err
|
|
}
|
|
|
|
return accountInfo, nil
|
|
}
|
|
|
|
// DepositAddress gets the address required to deposit funds for currency type
|
|
func (e Exchange) DepositAddress(exch string, currencyCode currency.Code) (out string, err error) {
|
|
if currencyCode.IsEmpty() {
|
|
err = errors.New("currency code is empty")
|
|
return
|
|
}
|
|
return engine.Bot.DepositAddressManager.GetDepositAddressByExchange(exch, currencyCode)
|
|
}
|
|
|
|
// WithdrawalFiatFunds withdraw funds from exchange to requested fiat source
|
|
func (e Exchange) WithdrawalFiatFunds(bankAccountID string, request *withdraw.Request) (string, error) {
|
|
ex, err := e.GetExchange(request.Exchange)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
var v *banking.Account
|
|
v, err = banking.GetBankAccountByID(bankAccountID)
|
|
if err != nil {
|
|
v, err = ex.GetBase().GetExchangeBankAccounts(bankAccountID, request.Currency.String())
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
otp, err := engine.Bot.GetExchangeoOTPByName(request.Exchange)
|
|
if err == nil {
|
|
otpValue, errParse := strconv.ParseInt(otp, 10, 64)
|
|
if errParse != nil {
|
|
return "", errors.New("failed to generate OTP unable to continue")
|
|
}
|
|
request.OneTimePassword = otpValue
|
|
}
|
|
request.Fiat.Bank.AccountName = v.AccountName
|
|
request.Fiat.Bank.AccountNumber = v.AccountNumber
|
|
request.Fiat.Bank.BankName = v.BankName
|
|
request.Fiat.Bank.BankAddress = v.BankAddress
|
|
request.Fiat.Bank.BankPostalCity = v.BankPostalCity
|
|
request.Fiat.Bank.BankCountry = v.BankCountry
|
|
request.Fiat.Bank.BankPostalCode = v.BankPostalCode
|
|
request.Fiat.Bank.BSBNumber = v.BSBNumber
|
|
request.Fiat.Bank.SWIFTCode = v.SWIFTCode
|
|
request.Fiat.Bank.IBAN = v.IBAN
|
|
|
|
resp, err := engine.SubmitWithdrawal(request)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return resp.Exchange.ID, nil
|
|
}
|
|
|
|
// WithdrawalCryptoFunds withdraw funds from exchange to requested Crypto source
|
|
func (e Exchange) WithdrawalCryptoFunds(request *withdraw.Request) (string, error) {
|
|
// Checks if exchange is enabled or not so we don't call OTP generation
|
|
_, err := e.GetExchange(request.Exchange)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
otp, err := engine.Bot.GetExchangeoOTPByName(request.Exchange)
|
|
if err == nil {
|
|
v, errParse := strconv.ParseInt(otp, 10, 64)
|
|
if errParse != nil {
|
|
return "", errors.New("failed to generate OTP unable to continue")
|
|
}
|
|
request.OneTimePassword = v
|
|
}
|
|
|
|
resp, err := engine.SubmitWithdrawal(request)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return resp.Exchange.ID, nil
|
|
}
|
|
|
|
// OHLCV returns open high low close volume candles for requested exchange/pair/asset/start & end time
|
|
func (e Exchange) OHLCV(exch string, pair currency.Pair, item asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
|
ex, err := e.GetExchange(exch)
|
|
if err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
ret, err := ex.GetHistoricCandlesExtended(pair, item, start, end, interval)
|
|
if err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
|
|
sort.Slice(ret.Candles, func(i, j int) bool {
|
|
return ret.Candles[i].Time.Before(ret.Candles[j].Time)
|
|
})
|
|
|
|
ret.FormatDates()
|
|
|
|
return ret, nil
|
|
}
|