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

@@ -1,6 +1,7 @@
package exmo
import (
"errors"
"fmt"
"log"
"net/url"
@@ -171,7 +172,9 @@ func (e *EXMO) GetUserInfo() (UserInfo, error) {
// Type can be buy, sell, market_buy, market_sell, market_buy_total and market_sell_total
func (e *EXMO) CreateOrder(pair, orderType string, price, amount float64) (int64, error) {
type response struct {
OrderID int64 `json:"order_id"`
OrderID int64 `json:"order_id"`
Result bool `json:"result"`
Error string `json:"error"`
}
v := url.Values{}
@@ -180,17 +183,28 @@ func (e *EXMO) CreateOrder(pair, orderType string, price, amount float64) (int64
v.Set("price", strconv.FormatFloat(price, 'f', -1, 64))
v.Set("quantity", strconv.FormatFloat(amount, 'f', -1, 64))
var result response
err := e.SendAuthenticatedHTTPRequest("POST", exmoOrderCreate, v, &result)
return result.OrderID, err
var resp response
err := e.SendAuthenticatedHTTPRequest("POST", exmoOrderCreate, v, &resp)
if !resp.Result {
return -1, errors.New(resp.Error)
}
return resp.OrderID, err
}
// CancelExistingOrder cancels an order by the orderID
func (e *EXMO) CancelExistingOrder(orderID int64) error {
v := url.Values{}
v.Set("order_id", strconv.FormatInt(orderID, 10))
var result interface{}
return e.SendAuthenticatedHTTPRequest("POST", exmoOrderCancel, v, &result)
type response struct {
Result bool `json:"result"`
Error string `json:"error"`
}
var resp response
err := e.SendAuthenticatedHTTPRequest("POST", exmoOrderCancel, v, &resp)
if !resp.Result {
return errors.New(resp.Error)
}
return err
}
// GetOpenOrders returns the users open orders
@@ -268,7 +282,10 @@ func (e *EXMO) GetCryptoDepositAddress() (map[string]string, error) {
// NOTE: This API function is available only after request to their tech support team
func (e *EXMO) WithdrawCryptocurrency(currency, address, invoice string, amount float64) (int64, error) {
type response struct {
TaskID int64 `json:"task_id,string"`
TaskID int64 `json:"task_id,string"`
Result bool `json:"result"`
Error string `json:"error"`
Success int64 `json:"success"`
}
v := url.Values{}
@@ -280,9 +297,15 @@ func (e *EXMO) WithdrawCryptocurrency(currency, address, invoice string, amount
}
v.Set("amount", strconv.FormatFloat(amount, 'f', -1, 64))
var result response
err := e.SendAuthenticatedHTTPRequest("POST", exmoWithdrawCrypt, v, &result)
return result.TaskID, err
var resp response
err := e.SendAuthenticatedHTTPRequest("POST", exmoWithdrawCrypt, v, &resp)
if err != nil {
return -1, err
}
if resp.Success == 0 || !resp.Result {
return -1, errors.New(resp.Error)
}
return resp.TaskID, err
}
// GetWithdrawTXID gets the result of a withdrawal request

View File

@@ -250,21 +250,19 @@ 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 e.APIKey == "" || e.APISecret == "" ||
e.APIKey == "Key" || e.APISecret == "Secret" ||
!canManipulateRealOrders {
return false
func areTestAPIKeysSet() bool {
if e.APIKey != "" && e.APIKey != "Key" &&
e.APISecret != "" && e.APISecret != "Secret" {
return true
}
return true
return false
}
func TestSubmitOrder(t *testing.T) {
e.SetDefaults()
TestSetup(t)
if !isRealOrderTestEnabled() {
t.Skip()
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
var p = pair.CurrencyPair{
@@ -273,8 +271,10 @@ func TestSubmitOrder(t *testing.T) {
SecondCurrency: symbol.USD,
}
response, err := e.SubmitOrder(p, exchange.Buy, exchange.Market, 1, 10, "1234234")
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")
}
}
@@ -282,9 +282,8 @@ func TestCancelExchangeOrder(t *testing.T) {
// Arrange
e.SetDefaults()
TestSetup(t)
if !isRealOrderTestEnabled() {
t.Skip()
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
currencyPair := pair.NewCurrencyPair(symbol.LTC, symbol.BTC)
@@ -298,10 +297,12 @@ func TestCancelExchangeOrder(t *testing.T) {
// Act
err := e.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)
}
}
@@ -310,12 +311,11 @@ func TestCancelAllExchangeOrders(t *testing.T) {
e.SetDefaults()
TestSetup(t)
if !isRealOrderTestEnabled() {
t.Skip()
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
currencyPair := pair.NewCurrencyPair(symbol.LTC, symbol.BTC)
var orderCancellation = exchange.OrderCancellation{
OrderID: "1",
WalletAddress: "1F5zVDgNjorJ51oGebSvNCrSAHpwGkUdDB",
@@ -325,10 +325,12 @@ func TestCancelAllExchangeOrders(t *testing.T) {
// Act
resp, err := e.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 orders: %v", err)
}
if len(resp.OrderStatus) > 0 {
@@ -342,3 +344,26 @@ func TestModifyOrder(t *testing.T) {
t.Error("Test failed - ModifyOrder() error")
}
}
func TestWithdraw(t *testing.T) {
e.SetDefaults()
TestSetup(t)
var withdrawCryptoRequest = exchange.WithdrawRequest{
Amount: 100,
Currency: symbol.LTC,
Address: "1F5zVDgNjorJ51oGebSvNCrSAHpwGkUdDB",
Description: "WITHDRAW IT ALL",
}
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
_, err := e.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

@@ -255,19 +255,21 @@ func (e *EXMO) GetDepositAddress(cryptocurrency pair.CurrencyItem) (string, erro
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
// submitted
func (e *EXMO) WithdrawCryptocurrencyFunds(address string, cryptocurrency pair.CurrencyItem, amount float64) (string, error) {
return "", common.ErrNotYetImplemented
func (e *EXMO) WithdrawCryptocurrencyFunds(withdrawRequest exchange.WithdrawRequest) (string, error) {
resp, err := e.WithdrawCryptocurrency(withdrawRequest.Currency.String(), withdrawRequest.Address, withdrawRequest.AddressTag, withdrawRequest.Amount)
return fmt.Sprintf("%v", resp), err
}
// WithdrawFiatFunds returns a withdrawal ID when a
// withdrawal is submitted
func (e *EXMO) WithdrawFiatFunds(currency pair.CurrencyItem, amount float64) (string, error) {
func (e *EXMO) WithdrawFiatFunds(withdrawRequest exchange.WithdrawRequest) (string, error) {
return "", common.ErrNotYetImplemented
}
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a
// withdrawal is submitted
func (e *EXMO) WithdrawFiatFundsToInternationalBank(currency pair.CurrencyItem, amount float64) (string, error) {
func (e *EXMO) WithdrawFiatFundsToInternationalBank(withdrawRequest exchange.WithdrawRequest) (string, error) {
return "", common.ErrNotYetImplemented
}