mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 23:16:49 +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
363 lines
11 KiB
Go
363 lines
11 KiB
Go
package bitflyer
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
|
|
)
|
|
|
|
const (
|
|
// Bitflyer chain analysis endpoints
|
|
// APIURL
|
|
chainAnalysis = "https://chainflyer.bitflyer.jp/v1/"
|
|
|
|
// Public endpoints for chain analysis
|
|
latestBlock = "block/latest"
|
|
blockByBlockHash = "block/"
|
|
blockByBlockHeight = "block/height/"
|
|
transaction = "tx/"
|
|
address = "address/"
|
|
|
|
// APIURL
|
|
japanURL = "https://api.bitflyer.jp/v1"
|
|
usURL = "https://api.bitflyer.com/v1"
|
|
europeURL = "https://api.bitflyer.com/v1"
|
|
|
|
// Public Endpoints
|
|
pubGetMarkets = "/getmarkets/"
|
|
pubGetBoard = "/getboard"
|
|
pubGetTicker = "/getticker"
|
|
pubGetExecutionHistory = "/getexecutions"
|
|
pubGetHealth = "/gethealth"
|
|
pubGetChats = "/getchats"
|
|
|
|
// Autheticated Endpoints
|
|
privGetPermissions = "/me/getpermissions"
|
|
privGetBalance = "/me/getbalance"
|
|
privMarginStatus = "/me/getcollateral"
|
|
privGetCollateralAcc = "/me/getcollateralaccounts"
|
|
privGetDepositAddress = "/me/getaddresses"
|
|
privDepositHistory = "/me/getcoinins"
|
|
privTransactionHistory = "/me/getcoinouts"
|
|
privBankAccSummary = "/me/getbankaccounts"
|
|
privGetDeposits = "/me/getdeposits"
|
|
privWithdraw = "/me/withdraw"
|
|
privDepositCancellationHistory = "/me/getwithdrawals"
|
|
privSendOrder = "/me/sendchildorder"
|
|
privCancelOrder = "/me/cancelchildorder"
|
|
privParentOrder = "/me/sendparentorder"
|
|
privCancelParentOrder = "/me/cancelparentorder"
|
|
privCancelOrders = "/me/cancelallchildorders"
|
|
privListOrders = "/me/getchildorders"
|
|
privListParentOrders = "/me/getparentorders"
|
|
privParentOrderDetails = "/me/getparentorder"
|
|
privExecutions = "/me/getexecutions"
|
|
privOpenInterest = "/me/getpositions"
|
|
privMarginChange = "/me/getcollateralhistory"
|
|
privTradingCommission = "/me/gettradingcommission"
|
|
|
|
orders request.EndpointLimit = iota
|
|
lowVolume
|
|
)
|
|
|
|
// Bitflyer is the overarching type across this package
|
|
type Bitflyer struct {
|
|
exchange.Base
|
|
}
|
|
|
|
// GetLatestBlockCA returns the latest block information from bitflyer chain
|
|
// analysis system
|
|
func (b *Bitflyer) GetLatestBlockCA() (ChainAnalysisBlock, error) {
|
|
var resp ChainAnalysisBlock
|
|
return resp, b.SendHTTPRequest(exchange.ChainAnalysis, latestBlock, &resp)
|
|
}
|
|
|
|
// GetBlockCA returns block information by blockhash from bitflyer chain
|
|
// analysis system
|
|
func (b *Bitflyer) GetBlockCA(blockhash string) (ChainAnalysisBlock, error) {
|
|
var resp ChainAnalysisBlock
|
|
return resp, b.SendHTTPRequest(exchange.ChainAnalysis, blockByBlockHash+blockhash, &resp)
|
|
}
|
|
|
|
// GetBlockbyHeightCA returns the block information by height from bitflyer chain
|
|
// analysis system
|
|
func (b *Bitflyer) GetBlockbyHeightCA(height int64) (ChainAnalysisBlock, error) {
|
|
var resp ChainAnalysisBlock
|
|
return resp, b.SendHTTPRequest(exchange.ChainAnalysis, blockByBlockHeight+strconv.FormatInt(height, 10), &resp)
|
|
}
|
|
|
|
// GetTransactionByHashCA returns transaction information by txHash from
|
|
// bitflyer chain analysis system
|
|
func (b *Bitflyer) GetTransactionByHashCA(txHash string) (ChainAnalysisTransaction, error) {
|
|
var resp ChainAnalysisTransaction
|
|
return resp, b.SendHTTPRequest(exchange.ChainAnalysis, transaction+txHash, &resp)
|
|
}
|
|
|
|
// GetAddressInfoCA returns balance information for address by addressln string
|
|
// from bitflyer chain analysis system
|
|
func (b *Bitflyer) GetAddressInfoCA(addressln string) (ChainAnalysisAddress, error) {
|
|
var resp ChainAnalysisAddress
|
|
return resp, b.SendHTTPRequest(exchange.ChainAnalysis, address+addressln, &resp)
|
|
}
|
|
|
|
// GetMarkets returns market information
|
|
func (b *Bitflyer) GetMarkets() ([]MarketInfo, error) {
|
|
var resp []MarketInfo
|
|
return resp, b.SendHTTPRequest(exchange.RestSpot, pubGetMarkets, &resp)
|
|
}
|
|
|
|
// GetOrderBook returns market orderbook depth
|
|
func (b *Bitflyer) GetOrderBook(symbol string) (Orderbook, error) {
|
|
var resp Orderbook
|
|
v := url.Values{}
|
|
v.Set("product_code", symbol)
|
|
|
|
return resp, b.SendHTTPRequest(exchange.RestSpot, pubGetBoard+"?"+v.Encode(), &resp)
|
|
}
|
|
|
|
// GetTicker returns ticker information
|
|
func (b *Bitflyer) GetTicker(symbol string) (Ticker, error) {
|
|
var resp Ticker
|
|
v := url.Values{}
|
|
v.Set("product_code", symbol)
|
|
return resp, b.SendHTTPRequest(exchange.RestSpot, pubGetTicker+"?"+v.Encode(), &resp)
|
|
}
|
|
|
|
// GetExecutionHistory returns past trades that were executed on the market
|
|
func (b *Bitflyer) GetExecutionHistory(symbol string) ([]ExecutedTrade, error) {
|
|
var resp []ExecutedTrade
|
|
v := url.Values{}
|
|
v.Set("product_code", symbol)
|
|
|
|
return resp, b.SendHTTPRequest(exchange.RestSpot, pubGetExecutionHistory+"?"+v.Encode(), &resp)
|
|
}
|
|
|
|
// GetExchangeStatus returns exchange status information
|
|
func (b *Bitflyer) GetExchangeStatus() (string, error) {
|
|
resp := make(map[string]string)
|
|
err := b.SendHTTPRequest(exchange.RestSpot, pubGetHealth, &resp)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
switch resp["status"] {
|
|
case "BUSY":
|
|
return "the exchange is experiencing high traffic", nil
|
|
case "VERY BUSY":
|
|
return "the exchange is experiencing heavy traffic", nil
|
|
case "SUPER BUSY":
|
|
return "the exchange is experiencing extremely heavy traffic. There is a possibility that orders will fail or be processed after a delay.", nil
|
|
case "STOP":
|
|
return "STOP", errors.New("the exchange has been stopped. Orders will not be accepted")
|
|
}
|
|
|
|
return "NORMAL", nil
|
|
}
|
|
|
|
// GetChats returns trollbox chat log
|
|
// Note: returns vary from instant to infinty
|
|
func (b *Bitflyer) GetChats(fromDate string) ([]ChatLog, error) {
|
|
var resp []ChatLog
|
|
v := url.Values{}
|
|
v.Set("from_date", fromDate)
|
|
return resp, b.SendHTTPRequest(exchange.RestSpot, pubGetChats+"?"+v.Encode(), &resp)
|
|
}
|
|
|
|
// GetPermissions returns current permissions for associated with your API
|
|
// keys
|
|
func (b *Bitflyer) GetPermissions() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// GetAccountBalance returnsthe full list of account funds
|
|
func (b *Bitflyer) GetAccountBalance() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// GetMarginStatus returns current margin status
|
|
func (b *Bitflyer) GetMarginStatus() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// GetCollateralAccounts returns a full list of collateralised accounts
|
|
func (b *Bitflyer) GetCollateralAccounts() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// GetCryptoDepositAddress returns an address for cryptocurrency deposits
|
|
func (b *Bitflyer) GetCryptoDepositAddress() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// GetDepositHistory returns a full history of deposits
|
|
func (b *Bitflyer) GetDepositHistory() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// GetTransactionHistory returns a full history of transactions
|
|
func (b *Bitflyer) GetTransactionHistory() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// GetBankAccSummary returns a full list of bank accounts assoc. with your keys
|
|
func (b *Bitflyer) GetBankAccSummary() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// GetCashDeposits returns a full list of cash deposits to the exchange
|
|
func (b *Bitflyer) GetCashDeposits() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// WithdrawFunds withdraws funds to a certain bank
|
|
func (b *Bitflyer) WithdrawFunds() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// GetDepositCancellationHistory returns the cancellation history of deposits
|
|
func (b *Bitflyer) GetDepositCancellationHistory() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// SendOrder creates new order
|
|
func (b *Bitflyer) SendOrder() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// CancelExistingOrder cancels an order
|
|
func (b *Bitflyer) CancelExistingOrder() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// SendParentOrder sends a special order
|
|
func (b *Bitflyer) SendParentOrder() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// CancelParentOrder cancels a special order
|
|
func (b *Bitflyer) CancelParentOrder() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// CancelAllExistingOrders cancels all orders on the exchange
|
|
func (b *Bitflyer) CancelAllExistingOrders() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// GetAllOrders returns a list of all orders
|
|
func (b *Bitflyer) GetAllOrders() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// GetParentOrders returns a list of all parent orders
|
|
func (b *Bitflyer) GetParentOrders() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// GetParentOrderDetails returns a detailing of a parent order
|
|
func (b *Bitflyer) GetParentOrderDetails() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// GetExecutions returns execution details
|
|
func (b *Bitflyer) GetExecutions() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// GetOpenInterest returns a summary of open interest
|
|
func (b *Bitflyer) GetOpenInterest() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// GetMarginChange returns collateral history
|
|
func (b *Bitflyer) GetMarginChange() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// GetTradingCommission returns trading commission
|
|
func (b *Bitflyer) GetTradingCommission() {
|
|
// Needs to be updated
|
|
}
|
|
|
|
// SendHTTPRequest sends an unauthenticated request
|
|
func (b *Bitflyer) SendHTTPRequest(ep exchange.URL, path string, result interface{}) error {
|
|
endpoint, err := b.API.Endpoints.GetURL(ep)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return b.SendPayload(context.Background(), &request.Item{
|
|
Method: http.MethodGet,
|
|
Path: endpoint + path,
|
|
Result: result,
|
|
Verbose: b.Verbose,
|
|
HTTPDebugging: b.HTTPDebugging,
|
|
HTTPRecording: b.HTTPRecording,
|
|
})
|
|
}
|
|
|
|
// SendAuthHTTPRequest sends an authenticated HTTP request
|
|
// Note: HTTP not done due to incorrect account privileges, please open a PR
|
|
// if you have access and update the authenticated requests
|
|
// TODO: Fill out this function once API access is obtained
|
|
func (b *Bitflyer) SendAuthHTTPRequest() {
|
|
// nolint:gocritic // code example
|
|
// headers := make(map[string]string)
|
|
// headers["ACCESS-KEY"] = b.API.Credentials.Key
|
|
// headers["ACCESS-TIMESTAMP"] = strconv.FormatInt(time.Now().UnixNano(), 10)
|
|
}
|
|
|
|
// GetFee returns an estimate of fee based on type of transaction
|
|
// TODO: Figure out the weird fee structure. Do we use Bitcoin Easy Exchange,Lightning Spot,Bitcoin Market,Lightning FX/Futures ???
|
|
func (b *Bitflyer) GetFee(feeBuilder *exchange.FeeBuilder) (float64, error) {
|
|
var fee float64
|
|
|
|
switch feeBuilder.FeeType {
|
|
case exchange.CryptocurrencyTradeFee:
|
|
fee = calculateTradingFee(feeBuilder.PurchasePrice, feeBuilder.Amount)
|
|
case exchange.InternationalBankDepositFee:
|
|
fee = getDepositFee(feeBuilder.BankTransactionType, feeBuilder.FiatCurrency)
|
|
case exchange.InternationalBankWithdrawalFee:
|
|
fee = getWithdrawalFee(feeBuilder.BankTransactionType, feeBuilder.FiatCurrency, feeBuilder.Amount)
|
|
case exchange.OfflineTradeFee:
|
|
fee = calculateTradingFee(feeBuilder.PurchasePrice, feeBuilder.Amount)
|
|
}
|
|
if fee < 0 {
|
|
fee = 0
|
|
}
|
|
return fee, nil
|
|
}
|
|
|
|
// calculateTradingFee returns fee when performing a trade
|
|
func calculateTradingFee(price, amount float64) float64 {
|
|
// bitflyer has fee tiers, but does not disclose them via API, so the largest has to be assumed
|
|
return 0.0012 * price * amount
|
|
}
|
|
|
|
func getDepositFee(bankTransactionType exchange.InternationalBankTransactionType, c currency.Code) (fee float64) {
|
|
if bankTransactionType == exchange.WireTransfer {
|
|
if c.Item == currency.JPY.Item {
|
|
fee = 324
|
|
}
|
|
}
|
|
return fee
|
|
}
|
|
|
|
func getWithdrawalFee(bankTransactionType exchange.InternationalBankTransactionType, c currency.Code, amount float64) (fee float64) {
|
|
if bankTransactionType == exchange.WireTransfer {
|
|
if c.Item == currency.JPY.Item {
|
|
if amount < 30000 {
|
|
fee = 540
|
|
} else {
|
|
fee = 756
|
|
}
|
|
}
|
|
}
|
|
return fee
|
|
}
|