(Exchanges) Introduce validation method and small updates (#565)

* Remove pointer reference

* Fix portfolio withdraw tests

* Add nil protection in validator method to reduce prospective panics and for future outbound checking

* Updated tests

* ch order var to not ref package

* rm comparison

* Add order ID validation check

* Add exchange name validation check

* Add in test details

* fix tests

* fix linter issues

* linter issues strikes again

* linter rabbit hole

* Addr nitterinos

* Add validation variadic interface to define sets of functionality check POC

* didn't want to add an amount other than 0, didn't want to add address to exchange withdraw, didn't want to whitlist, can change if need be

* add coverage

* Add validation method options for exchange wrappers and abstracted validation into its own package

* Add validation code for structs in exchange template generation

* remove extra validation call as this is done in wrapper

* fix niterinos for examplerinos

* Add template to documentation tool and regenerated documentation

* Addr niticles

* Fix tests due to validation update

* Add more validation checks for modify/submit orders

* update tests

* fix more tests

* Add asset type to submit variable in tests and rpc call. Regen funcs.

* Add field to modify struct in tests

* applied field asset to cancel struct across project

* fix woopsy
This commit is contained in:
Ryan O'Hara-Reid
2020-10-02 13:36:01 +10:00
committed by GitHub
parent ecbc68561f
commit 4e828a8124
93 changed files with 3070 additions and 1676 deletions

View File

@@ -2,7 +2,7 @@ fmt := import("fmt")
exch := import("exchange")
load := func() {
info := exch.ordersubmit("BTC Markets","BTC-AUD","-","LIMIT","SELL",1000000, 1,"")
info := exch.ordersubmit("BTC Markets","BTC-AUD","-","LIMIT","SELL",1000000, 1,"", SPOT)
fmt.println(info)
}

View File

@@ -318,7 +318,7 @@ func ExchangeOrderCancel(args ...objects.Object) (objects.Object, error) {
// ExchangeOrderSubmit submit order on exchange
func ExchangeOrderSubmit(args ...objects.Object) (objects.Object, error) {
if len(args) != 8 {
if len(args) != 9 {
return nil, objects.ErrWrongNumArguments
}
@@ -334,6 +334,10 @@ func ExchangeOrderSubmit(args ...objects.Object) (objects.Object, error) {
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, delimiter)
}
pair, err := currency.NewPairDelimiter(currencyPair, delimiter)
if err != nil {
return nil, err
}
orderType, ok := objects.ToString(args[3])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, orderType)
@@ -354,23 +358,23 @@ func ExchangeOrderSubmit(args ...objects.Object) (objects.Object, error) {
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, orderClientID)
}
pair, err := currency.NewPairDelimiter(currencyPair, delimiter)
if err != nil {
return nil, err
assetType, ok := objects.ToString(args[8])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, orderClientID)
}
a := asset.Item(assetType)
if !asset.IsValid(a) {
return nil, fmt.Errorf("asset type: %s is invalid", a)
}
tempSubmit := &order.Submit{
Pair: pair,
Type: order.Type(orderType),
Side: order.Side(orderSide),
Price: orderPrice,
Amount: orderAmount,
ClientID: orderClientID,
}
err = tempSubmit.Validate()
if err != nil {
return nil, err
Pair: pair,
Type: order.Type(orderType),
Side: order.Side(orderSide),
Price: orderPrice,
Amount: orderAmount,
ClientID: orderClientID,
AssetType: a,
}
rtn, err := wrappers.GetWrapper().SubmitOrder(tempSubmit)
@@ -452,7 +456,8 @@ func ExchangeWithdrawCrypto(args ...objects.Object) (objects.Object, error) {
}
withdrawRequest := &withdraw.Request{
Crypto: &withdraw.CryptoRequest{
Exchange: exchangeName,
Crypto: withdraw.CryptoRequest{
Address: address,
AddressTag: addressTag,
FeeAmount: feeAmount,
@@ -462,7 +467,7 @@ func ExchangeWithdrawCrypto(args ...objects.Object) (objects.Object, error) {
Amount: amount,
}
rtn, err := wrappers.GetWrapper().WithdrawalCryptoFunds(exchangeName, withdrawRequest)
rtn, err := wrappers.GetWrapper().WithdrawalCryptoFunds(withdrawRequest)
if err != nil {
return nil, err
}
@@ -498,12 +503,13 @@ func ExchangeWithdrawFiat(args ...objects.Object) (objects.Object, error) {
}
withdrawRequest := &withdraw.Request{
Exchange: exchangeName,
Currency: currency.NewCode(cur),
Description: description,
Amount: amount,
}
rtn, err := wrappers.GetWrapper().WithdrawalFiatFunds(exchangeName, bankAccountID, withdrawRequest)
rtn, err := wrappers.GetWrapper().WithdrawalFiatFunds(bankAccountID, withdrawRequest)
if err != nil {
return nil, err
}

View File

@@ -8,6 +8,7 @@ import (
"time"
objects "github.com/d5/tengo/v2"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/gctscript/modules"
"github.com/thrasher-corp/gocryptotrader/gctscript/wrappers/validator"
)
@@ -191,21 +192,22 @@ func TestExchangeOrderSubmit(t *testing.T) {
orderType := &objects.String{Value: "LIMIT"}
orderPrice := &objects.Float{Value: 1}
orderAmount := &objects.Float{Value: 1}
orderAsset := &objects.String{Value: asset.Spot.String()}
_, err = ExchangeOrderSubmit(exch, currencyPair, delimiter,
orderType, orderSide, orderPrice, orderAmount, orderID)
orderType, orderSide, orderPrice, orderAmount, orderID, orderAsset)
if err != nil && !errors.Is(err, errTestFailed) {
t.Fatal(err)
}
_, err = ExchangeOrderSubmit(exch, currencyPair, delimiter,
orderType, orderSide, orderPrice, orderAmount, orderID)
orderType, orderSide, orderPrice, orderAmount, orderID, orderAsset)
if err != nil {
t.Fatal(err)
}
_, err = ExchangeOrderSubmit(objects.TrueValue, currencyPair, delimiter,
orderType, orderSide, orderPrice, orderAmount, orderID)
orderType, orderSide, orderPrice, orderAmount, orderID, orderAsset)
if err != nil {
t.Fatal(err)
}

View File

@@ -40,8 +40,8 @@ type Exchange interface {
CancelOrder(exch, orderid string) (bool, error)
AccountInformation(exch string) (account.Holdings, error)
DepositAddress(exch string, currencyCode currency.Code) (string, error)
WithdrawalFiatFunds(exch, bankAccountID string, request *withdraw.Request) (out string, err error)
WithdrawalCryptoFunds(exch string, request *withdraw.Request) (out string, err error)
WithdrawalFiatFunds(bankAccountID string, request *withdraw.Request) (out string, err error)
WithdrawalCryptoFunds(request *withdraw.Request) (out string, err error)
OHLCV(exch string, pair currency.Pair, item asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error)
}

View File

@@ -118,6 +118,7 @@ func (e Exchange) CancelOrder(exch, orderID string) (bool, error) {
ID: orderDetails.ID,
Pair: orderDetails.Pair,
Side: orderDetails.Side,
AssetType: orderDetails.AssetType,
}
err = engine.Bot.OrderManager.Cancel(cancel)
@@ -152,8 +153,8 @@ func (e Exchange) DepositAddress(exch string, currencyCode currency.Code) (out s
}
// WithdrawalFiatFunds withdraw funds from exchange to requested fiat source
func (e Exchange) WithdrawalFiatFunds(exch, bankAccountID string, request *withdraw.Request) (string, error) {
ex, err := e.GetExchange(exch)
func (e Exchange) WithdrawalFiatFunds(bankAccountID string, request *withdraw.Request) (string, error) {
ex, err := e.GetExchange(request.Exchange)
if err != nil {
return "", err
}
@@ -166,7 +167,7 @@ func (e Exchange) WithdrawalFiatFunds(exch, bankAccountID string, request *withd
}
}
otp, err := engine.Bot.GetExchangeoOTPByName(exch)
otp, err := engine.Bot.GetExchangeoOTPByName(request.Exchange)
if err == nil {
otpValue, errParse := strconv.ParseInt(otp, 10, 64)
if errParse != nil {
@@ -174,7 +175,6 @@ func (e Exchange) WithdrawalFiatFunds(exch, bankAccountID string, request *withd
}
request.OneTimePassword = otpValue
}
request.Exchange = exch
request.Fiat.Bank.AccountName = v.AccountName
request.Fiat.Bank.AccountNumber = v.AccountNumber
request.Fiat.Bank.BankName = v.BankName
@@ -186,7 +186,7 @@ func (e Exchange) WithdrawalFiatFunds(exch, bankAccountID string, request *withd
request.Fiat.Bank.SWIFTCode = v.SWIFTCode
request.Fiat.Bank.IBAN = v.IBAN
resp, err := engine.SubmitWithdrawal(ex.GetName(), request)
resp, err := engine.SubmitWithdrawal(request)
if err != nil {
return "", err
}
@@ -194,13 +194,13 @@ func (e Exchange) WithdrawalFiatFunds(exch, bankAccountID string, request *withd
}
// WithdrawalCryptoFunds withdraw funds from exchange to requested Crypto source
func (e Exchange) WithdrawalCryptoFunds(exch string, request *withdraw.Request) (out string, err error) {
ex, err := e.GetExchange(exch)
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(exch)
otp, err := engine.Bot.GetExchangeoOTPByName(request.Exchange)
if err == nil {
v, errParse := strconv.ParseInt(otp, 10, 64)
if errParse != nil {
@@ -209,7 +209,7 @@ func (e Exchange) WithdrawalCryptoFunds(exch string, request *withdraw.Request)
request.OneTimePassword = v
}
resp, err := engine.SubmitWithdrawal(ex.GetName(), request)
resp, err := engine.SubmitWithdrawal(request)
if err != nil {
return "", err
}

View File

@@ -162,6 +162,7 @@ func TestExchange_SubmitOrder(t *testing.T) {
Amount: orderAmount,
ClientID: orderClientID,
Exchange: exchName,
AssetType: asset.Spot,
}
_, err = exchangeTest.SubmitOrder(tempOrder)
if err != nil {

View File

@@ -215,18 +215,18 @@ func (w Wrapper) DepositAddress(exch string, _ currency.Code) (string, error) {
}
// WithdrawalCryptoFunds validator for test execution/scripts
func (w Wrapper) WithdrawalCryptoFunds(exch string, _ *withdraw.Request) (out string, err error) {
if exch == exchError.String() {
return exch, errTestFailed
func (w Wrapper) WithdrawalCryptoFunds(r *withdraw.Request) (out string, err error) {
if r.Exchange == exchError.String() {
return r.Exchange, errTestFailed
}
return "", nil
}
// WithdrawalFiatFunds validator for test execution/scripts
func (w Wrapper) WithdrawalFiatFunds(exch, _ string, _ *withdraw.Request) (out string, err error) {
if exch == exchError.String() {
return exch, errTestFailed
func (w Wrapper) WithdrawalFiatFunds(_ string, r *withdraw.Request) (out string, err error) {
if r.Exchange == exchError.String() {
return r.Exchange, errTestFailed
}
return "123", nil

View File

@@ -8,6 +8,7 @@ import (
"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/portfolio/withdraw"
)
const (
@@ -163,6 +164,7 @@ func TestWrapper_SubmitOrder(t *testing.T) {
Amount: orderAmount,
ClientID: orderClientID,
Exchange: "true",
AssetType: asset.Spot,
}
_, err = testWrapper.SubmitOrder(tempOrder)
if err != nil {
@@ -193,24 +195,24 @@ func TestWrapper_Ticker(t *testing.T) {
}
func TestWrapper_WithdrawalCryptoFunds(t *testing.T) {
_, err := testWrapper.WithdrawalCryptoFunds(exchError.String(), nil)
_, err := testWrapper.WithdrawalCryptoFunds(&withdraw.Request{Exchange: exchError.String()})
if err == nil {
t.Fatal("expected WithdrawalCryptoFunds to return error with invalid name")
}
_, err = testWrapper.WithdrawalCryptoFunds(exchName, nil)
_, err = testWrapper.WithdrawalCryptoFunds(&withdraw.Request{Exchange: exchName})
if err != nil {
t.Fatal("expected WithdrawalCryptoFunds to return error with invalid name")
}
}
func TestWrapper_WithdrawalFiatFunds(t *testing.T) {
_, err := testWrapper.WithdrawalFiatFunds(exchError.String(), "", nil)
_, err := testWrapper.WithdrawalFiatFunds("", &withdraw.Request{Exchange: exchError.String()})
if err == nil {
t.Fatal("expected WithdrawalFiatFunds to return error with invalid name")
}
_, err = testWrapper.WithdrawalFiatFunds(exchName, "", nil)
_, err = testWrapper.WithdrawalFiatFunds("", &withdraw.Request{Exchange: exchName})
if err != nil {
t.Fatal("expected WithdrawalCryptoFunds to return error with invalid name")
}