Withdraw Crypto wrapper mapping (#226)

* Initial commit

* Updates signature for all withdrawal methods to use new withdrawRequest struct type

* Implements crypto withdraw features & tests for Alphapoint, ANX, Binance, Bitfinex, Bitflyer, Bithumb, Bitmex, Bitstamp, Bittrex, BTCC, BTCmarkets, CoinbasePro, Coinut. Updates WithdrawRequest type with more members. Breaking change to update real order testing for increased code coverage

* Updates all realOrder tests to run when no API key is present. Updates exchange functions to handle errors better

* Implements crypto withdrawals for Exmo, GateIO, Gemini, HitBTC, Huobi, HuobiHadax, Kraken, LakeBTC, Liqui, Localbitcoins, OKCoin, OKEX, Poloniex, Wex, Yobit and ZB. Updates real order test formatting for all real order tests

* Update alphapoint. Fixes anx typos. Adds function WithdrawFiatFundsToInternationalBank to exchange wrapper interface. Adds WithdrawFiatFundsToInternationalBank to alphapoint, bitmex, coinbasepro. Updates Kraken to use TradePassword property

* Reverts alphapoint to use ErrNotYetImplemented

* Fixes line spacing and removes unnecessary line
This commit is contained in:
Scott
2019-01-03 13:15:07 +11:00
committed by Adrian Gallagher
parent 9ebcb1a462
commit b1e6534e7c
88 changed files with 2050 additions and 802 deletions

View File

@@ -51,6 +51,17 @@ const (
openOrders = "/api/v3/openOrders"
allOrders = "/api/v3/allOrders"
// Withdraw API endpoints
withdraw = "/wapi/v3/withdraw.html"
depositHistory = "/wapi/v3/depositHistory.html"
withdrawalHistory = "/wapi/v3/withdrawHistory.html"
depositAddress = "/wapi/v3/depositAddress.html"
accountStatus = "/wapi/v3/accountStatus.html"
systemStatus = "/wapi/v3/systemStatus.html"
dustLog = "/wapi/v3/userAssetDribbletLog.html"
tradeFee = "/wapi/v3/tradeFee.html"
assetDetail = "/wapi/v3/assetDetail.html"
// binance authenticated and unauthenticated limit rates
// to-do
binanceAuthRate = 0
@@ -702,3 +713,45 @@ func calculateTradingFee(purchasePrice, amount, multiplier float64) float64 {
func getCryptocurrencyWithdrawalFee(currency string, purchasePrice, amount float64) float64 {
return WithdrawalFees[currency]
}
// WithdrawCrypto sends cryptocurrency to the address of your choosing
func (b *Binance) WithdrawCrypto(asset, address, addressTag, name, amount string) (int64, error) {
var resp WithdrawResponse
path := fmt.Sprintf("%s%s", b.APIUrl, withdraw)
params := url.Values{}
params.Set("asset", asset)
params.Set("address", string(address))
params.Set("amount", string(amount))
if len(name) > 0 {
params.Set("name", string(name))
}
if len(addressTag) > 0 {
params.Set("addressTag", string(addressTag))
}
if err := b.SendAuthHTTPRequest("POST", path, params, &resp); err != nil {
return -1, err
}
if !resp.Success {
return resp.ID, errors.New(resp.Msg)
}
return resp.ID, nil
}
//GetDepositAddressForCurrency retrieves the wallet address for a given currency
func (b *Binance) GetDepositAddressForCurrency(currency string) error {
path := fmt.Sprintf("%s%s", b.APIUrl, depositAddress)
var resp interface{}
params := url.Values{}
params.Set("asset", currency)
if err := b.SendAuthHTTPRequest("GET", path, params, &resp); err != nil {
return err
}
return nil
}

View File

@@ -334,31 +334,28 @@ func TestFormatWithdrawPermissions(t *testing.T) {
// Any tests below this line have the ability to impact your orders on the exchange. Enable canManipulateRealOrders to run them
// -----------------------------------------------------------------------------------------------------------------------------
func isRealOrderTestEnabled() bool {
if b.APIKey == "" || b.APISecret == "" ||
b.APIKey == "Key" || b.APISecret == "Secret" ||
!canManipulateRealOrders {
return false
func areTestAPIKeysSet() bool {
if b.APIKey != "" && b.APIKey != "Key" &&
b.APISecret != "" && b.APISecret != "Secret" {
return true
}
return true
return false
}
func TestSubmitOrder(t *testing.T) {
b.SetDefaults()
TestSetup(t)
if !isRealOrderTestEnabled() {
t.Skip()
}
var p = pair.CurrencyPair{
Delimiter: "",
FirstCurrency: symbol.LTC,
SecondCurrency: symbol.BTC,
}
response, err := b.SubmitOrder(p, exchange.Buy, exchange.Market, 1, 1, "clientId")
if err != nil || !response.IsOrderPlaced {
if areTestAPIKeysSet() && (err != nil || !response.IsOrderPlaced) {
t.Errorf("Order failed to be placed: %v", err)
} else if !areTestAPIKeysSet() && err == nil {
t.Error("Expecting an error when no keys are set")
}
}
@@ -367,10 +364,6 @@ func TestCancelExchangeOrder(t *testing.T) {
b.SetDefaults()
TestSetup(t)
if !isRealOrderTestEnabled() {
t.Skip()
}
currencyPair := pair.NewCurrencyPair(symbol.LTC, symbol.BTC)
var orderCancellation = exchange.OrderCancellation{
@@ -384,8 +377,11 @@ func TestCancelExchangeOrder(t *testing.T) {
err := b.CancelOrder(orderCancellation)
// Assert
if err != nil {
t.Errorf("Could not cancel order: %s", err)
if !areTestAPIKeysSet() && err == nil {
t.Errorf("Expecting an error when no keys are set: %v", err)
}
if areTestAPIKeysSet() && err != nil {
t.Errorf("Could not cancel orders: %v", err)
}
}
@@ -394,10 +390,6 @@ func TestCancelAllExchangeOrders(t *testing.T) {
b.SetDefaults()
TestSetup(t)
if !isRealOrderTestEnabled() {
t.Skip()
}
currencyPair := pair.NewCurrencyPair(symbol.LTC, symbol.BTC)
var orderCancellation = exchange.OrderCancellation{
@@ -411,8 +403,11 @@ func TestCancelAllExchangeOrders(t *testing.T) {
resp, err := b.CancelAllOrders(orderCancellation)
// Assert
if err != nil {
t.Errorf("Could not cancel order: %s", err)
if !areTestAPIKeysSet() && err == nil {
t.Errorf("Expecting an error when no keys are set: %v", err)
}
if areTestAPIKeysSet() && err != nil {
t.Errorf("Could not cancel order: %v", err)
}
if len(resp.OrderStatus) > 0 {
@@ -421,6 +416,8 @@ func TestCancelAllExchangeOrders(t *testing.T) {
}
func TestGetAccountInfo(t *testing.T) {
b.SetDefaults()
TestSetup(t)
if testAPIKey == "" || testAPISecret == "" {
t.Skip()
}
@@ -437,3 +434,27 @@ func TestModifyOrder(t *testing.T) {
t.Error("Test failed - ModifyOrder() error")
}
}
func TestWithdraw(t *testing.T) {
b.SetDefaults()
TestSetup(t)
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
var withdrawCryptoRequest = exchange.WithdrawRequest{
Amount: 100,
Currency: symbol.BTC,
Address: "1F5zVDgNjorJ51oGebSvNCrSAHpwGkUdDB",
Description: "WITHDRAW IT ALL",
}
_, err := b.WithdrawCryptocurrencyFunds(withdrawCryptoRequest)
if !areTestAPIKeysSet() && err == nil {
t.Errorf("Expecting an error when no keys are set: %v", err)
}
if areTestAPIKeysSet() && err != nil {
t.Errorf("Withdraw failed to be placed: %v", err)
}
}

View File

@@ -616,3 +616,10 @@ var WithdrawalFees = map[string]float64{
symbol.APPC: 12.4,
symbol.PIVX: 0.02,
}
// WithdrawResponse contains status of withdrawal request
type WithdrawResponse struct {
Success bool `json:"success"`
Msg string `json:"msg"`
ID int64 `json:"id"`
}

View File

@@ -270,19 +270,22 @@ func (b *Binance) GetDepositAddress(cryptocurrency pair.CurrencyItem) (string, e
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
// submitted
func (b *Binance) WithdrawCryptocurrencyFunds(address string, cryptocurrency pair.CurrencyItem, amount float64) (string, error) {
return "", common.ErrNotYetImplemented
func (b *Binance) WithdrawCryptocurrencyFunds(withdrawRequest exchange.WithdrawRequest) (string, error) {
amountStr := strconv.FormatFloat(withdrawRequest.Amount, 'f', -1, 64)
id, err := b.WithdrawCrypto(withdrawRequest.Currency.String(), withdrawRequest.Address, withdrawRequest.AddressTag, withdrawRequest.Description, amountStr)
return strconv.FormatInt(id, 10), err
}
// WithdrawFiatFunds returns a withdrawal ID when a
// withdrawal is submitted
func (b *Binance) WithdrawFiatFunds(currency pair.CurrencyItem, amount float64) (string, error) {
func (b *Binance) WithdrawFiatFunds(withdrawRequest exchange.WithdrawRequest) (string, error) {
return "", common.ErrNotYetImplemented
}
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a
// withdrawal is submitted
func (b *Binance) WithdrawFiatFundsToInternationalBank(currency pair.CurrencyItem, amount float64) (string, error) {
func (b *Binance) WithdrawFiatFundsToInternationalBank(withdrawRequest exchange.WithdrawRequest) (string, error) {
return "", common.ErrNotYetImplemented
}