Skip to content

Commit

Permalink
(Exchanges) Introduce validation method and small updates (thrasher-c…
Browse files Browse the repository at this point in the history
…orp#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
  • Loading branch information
shazbert authored Oct 2, 2020
1 parent ecbc685 commit 4e828a8
Show file tree
Hide file tree
Showing 93 changed files with 3,061 additions and 1,667 deletions.
5 changes: 3 additions & 2 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ MadCozBadd | https://github.com/MadCozBadd
140am | https://github.com/140am
marcofranssen | https://github.com/marcofranssen
dackroyd | https://github.com/dackroyd
Rots | https://github.com/Rots
vazha | https://github.com/vazha
cranktakular | https://github.com/cranktakular
woshidama323 | https://github.com/woshidama323
vazha | https://github.com/vazha
crackcomm | https://github.com/crackcomm
azhang | https://github.com/azhang
andreygrehov | https://github.com/andreygrehov
bretep | https://github.com/bretep
Christian-Achilli | https://github.com/Christian-Achilli
Expand All @@ -24,7 +26,6 @@ if1live | https://github.com/if1live
lozdog245 | https://github.com/lozdog245
soxipy | https://github.com/soxipy
herenow | https://github.com/herenow
azhang | https://github.com/azhang
blombard | https://github.com/blombard
CodeLingoBot | https://github.com/CodeLingoBot
CodeLingoTeam | https://github.com/CodeLingoTeam
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,23 @@ Binaries will be published once the codebase reaches a stable condition.

|User|Contribution Amount|
|--|--|
| [thrasher-](https://github.com/thrasher-) | 641 |
| [shazbert](https://github.com/shazbert) | 191 |
| [gloriousCode](https://github.com/gloriousCode) | 169 |
| [dependabot-preview[bot]](https://github.com/apps/dependabot-preview) | 51 |
| [xtda](https://github.com/xtda) | 45 |
| [thrasher-](https://github.com/thrasher-) | 643 |
| [shazbert](https://github.com/shazbert) | 195 |
| [gloriousCode](https://github.com/gloriousCode) | 170 |
| [dependabot-preview[bot]](https://github.com/apps/dependabot-preview) | 68 |
| [xtda](https://github.com/xtda) | 47 |
| [ermalguni](https://github.com/ermalguni) | 14 |
| [vadimzhukck](https://github.com/vadimzhukck) | 10 |
| [MadCozBadd](https://github.com/MadCozBadd) | 9 |
| [140am](https://github.com/140am) | 8 |
| [marcofranssen](https://github.com/marcofranssen) | 8 |
| [dackroyd](https://github.com/dackroyd) | 5 |
| [Rots](https://github.com/Rots) | 5 |
| [vazha](https://github.com/vazha) | 5 |
| [cranktakular](https://github.com/cranktakular) | 5 |
| [woshidama323](https://github.com/woshidama323) | 3 |
| [vazha](https://github.com/vazha) | 3 |
| [crackcomm](https://github.com/crackcomm) | 3 |
| [azhang](https://github.com/azhang) | 2 |
| [andreygrehov](https://github.com/andreygrehov) | 2 |
| [bretep](https://github.com/bretep) | 2 |
| [Christian-Achilli](https://github.com/Christian-Achilli) | 2 |
Expand All @@ -165,7 +167,6 @@ Binaries will be published once the codebase reaches a stable condition.
| [lozdog245](https://github.com/lozdog245) | 2 |
| [soxipy](https://github.com/soxipy) | 2 |
| [herenow](https://github.com/herenow) | 2 |
| [azhang](https://github.com/azhang) | 1 |
| [blombard](https://github.com/blombard) | 1 |
| [CodeLingoBot](https://github.com/CodeLingoBot) | 1 |
| [CodeLingoTeam](https://github.com/CodeLingoTeam) | 1 |
Expand Down
9 changes: 7 additions & 2 deletions cmd/documentation/documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,11 @@ func main() {

// Github API missing contributors
contributors = append(contributors, []Contributor{
// idoall's contributors were forked and merged, so his contributions
// aren't automatically retrievable
{
Login: "daniel-cohen",
URL: "https://github.com/daniel-cohen",
Contributions: 1,
},
{
Login: "DirectX",
URL: "https://github.com/DirectX",
Expand All @@ -181,6 +184,8 @@ func main() {
URL: "https://github.com/frankzougc",
Contributions: 1,
},
// idoall's contributors were forked and merged, so his contributions
// aren't automatically retrievable
{
Login: "idoall",
URL: "https://github.com/idoall",
Expand Down
78 changes: 78 additions & 0 deletions cmd/documentation/exchanges_templates/validate.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{{define "exchanges validate" -}}
{{template "header" .}}
## Current Features for {{.Name}}

+ This package allows for validation options to occur exchange side e.g.
- Checking for ID in an order cancellation struct.
- Determining the correct withdrawal bank details for a specific exchange.

+ Example Usage below:

```go
// import package
"github.com/thrasher-corp/exchanges/validate"

// define your data structure across potential exchanges
type Critical struct {
ID string
Person string
Banks string
MoneysUSD float64
}

// define validation and add a variadic param
func (supercritcalinfo *Critical) Validate(opt ...validate.Checker) error {
// define base level validation
if supercritcalinfo != nil {
// oh no this is nil, could panic program!
}

// range over potential checks coming from individual packages
var errs common.Errors
for _, o := range opt {
err := o.Check()
if err != nil {
errs = append(errs, err)
}
}

if errs != nil {
return errs
}
return nil
}

// define an exchange or package level check that returns a validate.Checker
// interface
func (supercritcalinfo *Critical) PleaseDontSendMoneyToParents() validate.Checker {
return validate.Check(func() error {
if supercritcalinfo.Person == "Mother Dearest" ||
supercritcalinfo.Person == "Father Dearest" {
return errors.New("nope")
}
return nil
})
}


// Now in the package all you have to do is add in your options or not...
d := Critical{Person: "Mother Dearest", MoneysUSD: 1337.30}

// This should not error
err := d.Validate()
if err != nil {
return err
}

// This should error
err := d.Validate(d.PleaseDontSendMoneyToParents())
if err != nil {
return err
}

```

### Please click GoDocs chevron above to view current GoDoc information for this package
{{template "contributions"}}
{{template "donations" .}}
{{end}}
26 changes: 25 additions & 1 deletion cmd/exchange_template/wrapper_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -340,16 +340,25 @@ func ({{.Variable}} *{{.CapitalName}}) SubmitOrder(s *order.Submit) (order.Submi
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func ({{.Variable}} *{{.CapitalName}}) ModifyOrder(action *order.Modify) (string, error) {
// if err := action.Validate(); err != nil {
// return "", err
// }
return "", common.ErrNotYetImplemented
}

// CancelOrder cancels an order by its corresponding ID number
func ({{.Variable}} *{{.CapitalName}}) CancelOrder(order *order.Cancel) error {
func ({{.Variable}} *{{.CapitalName}}) CancelOrder(ord *order.Cancel) error {
// if err := ord.Validate(ord.StandardCancel()); err != nil {
// return err
// }
return common.ErrNotYetImplemented
}

// CancelAllOrders cancels all orders associated with a currency pair
func ({{.Variable}} *{{.CapitalName}}) CancelAllOrders(orderCancellation *order.Cancel) (order.CancelAllResponse, error) {
// if err := orderCancellation.Validate(); err != nil {
// return err
// }
return order.CancelAllResponse{}, common.ErrNotYetImplemented
}

Expand All @@ -366,29 +375,44 @@ func ({{.Variable}} *{{.CapitalName}}) GetDepositAddress(cryptocurrency currency
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
// submitted
func ({{.Variable}} *{{.CapitalName}}) WithdrawCryptocurrencyFunds(withdrawRequest *withdraw.Request) (*withdraw.ExchangeResponse, error) {
// if err := withdrawRequest.Validate(); err != nil {
// return nil, err
// }
return nil, common.ErrNotYetImplemented
}

// WithdrawFiatFunds returns a withdrawal ID when a withdrawal is
// submitted
func ({{.Variable}} *{{.CapitalName}}) WithdrawFiatFunds(withdrawRequest *withdraw.Request) (*withdraw.ExchangeResponse, error) {
// if err := withdrawRequest.Validate(); err != nil {
// return nil, err
// }
return nil, common.ErrNotYetImplemented
}

// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a withdrawal is
// submitted
func ({{.Variable}} *{{.CapitalName}}) WithdrawFiatFundsToInternationalBank(withdrawRequest *withdraw.Request) (*withdraw.ExchangeResponse, error) {
// if err := withdrawRequest.Validate(); err != nil {
// return nil, err
// }
return nil, common.ErrNotYetImplemented
}

// GetActiveOrders retrieves any orders that are active/open
func ({{.Variable}} *{{.CapitalName}}) GetActiveOrders(getOrdersRequest *order.GetOrdersRequest) ([]order.Detail, error) {
// if err := getOrdersRequest.Validate(); err != nil {
// return nil, err
// }
return nil, common.ErrNotYetImplemented
}

// GetOrderHistory retrieves account order information
// Can Limit response to specific order status
func ({{.Variable}} *{{.CapitalName}}) GetOrderHistory(getOrdersRequest *order.GetOrdersRequest) ([]order.Detail, error) {
// if err := getOrdersRequest.Validate(); err != nil {
// return nil, err
// }
return nil, common.ErrNotYetImplemented
}

Expand Down
43 changes: 17 additions & 26 deletions cmd/exchange_wrapper_coverage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/thrasher-corp/gocryptotrader/engine"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
)

const (
Expand All @@ -33,10 +31,11 @@ func main() {
log.Printf("Loading exchanges..")
var wg sync.WaitGroup
for x := range exchange.Exchanges {
name := exchange.Exchanges[x]
err := engine.Bot.LoadExchange(name, true, &wg)
err := engine.Bot.LoadExchange(exchange.Exchanges[x], true, &wg)
if err != nil {
log.Printf("Failed to load exchange %s. Err: %s", name, err)
log.Printf("Failed to load exchange %s. Err: %s",
exchange.Exchanges[x],
err)
continue
}
}
Expand All @@ -48,12 +47,12 @@ func main() {
wg = sync.WaitGroup{}
exchanges := engine.Bot.GetExchanges()
for x := range exchanges {
exch := exchanges[x]
wg.Add(1)
go func(num int) {
name := exchanges[num].GetName()
results[name] = testWrappers(exchanges[num])
go func(e exchange.IBotExchange) {
results[e.GetName()] = testWrappers(e)
wg.Done()
}(x)
}(exch)
}
wg.Wait()
log.Println("Done.")
Expand Down Expand Up @@ -127,30 +126,22 @@ func testWrappers(e exchange.IBotExchange) []string {
funcs = append(funcs, "GetFundingHistory")
}

s := &order.Submit{
Pair: p,
Side: order.Buy,
Type: order.Limit,
Amount: 1000000,
Price: 10000000000,
ClientID: "meow",
}
_, err = e.SubmitOrder(s)
_, err = e.SubmitOrder(nil)
if err == common.ErrNotYetImplemented {
funcs = append(funcs, "SubmitOrder")
}

_, err = e.ModifyOrder(&order.Modify{})
_, err = e.ModifyOrder(nil)
if err == common.ErrNotYetImplemented {
funcs = append(funcs, "ModifyOrder")
}

err = e.CancelOrder(&order.Cancel{})
err = e.CancelOrder(nil)
if err == common.ErrNotYetImplemented {
funcs = append(funcs, "CancelOrder")
}

_, err = e.CancelAllOrders(&order.Cancel{})
_, err = e.CancelAllOrders(nil)
if err == common.ErrNotYetImplemented {
funcs = append(funcs, "CancelAllOrders")
}
Expand All @@ -160,12 +151,12 @@ func testWrappers(e exchange.IBotExchange) []string {
funcs = append(funcs, "GetOrderInfo")
}

_, err = e.GetOrderHistory(&order.GetOrdersRequest{})
_, err = e.GetOrderHistory(nil)
if err == common.ErrNotYetImplemented {
funcs = append(funcs, "GetOrderHistory")
}

_, err = e.GetActiveOrders(&order.GetOrdersRequest{})
_, err = e.GetActiveOrders(nil)
if err == common.ErrNotYetImplemented {
funcs = append(funcs, "GetActiveOrders")
}
Expand All @@ -175,16 +166,16 @@ func testWrappers(e exchange.IBotExchange) []string {
funcs = append(funcs, "GetDepositAddress")
}

_, err = e.WithdrawCryptocurrencyFunds(&withdraw.Request{})
_, err = e.WithdrawCryptocurrencyFunds(nil)
if err == common.ErrNotYetImplemented {
funcs = append(funcs, "WithdrawCryptocurrencyFunds")
}

_, err = e.WithdrawFiatFunds(&withdraw.Request{})
_, err = e.WithdrawFiatFunds(nil)
if err == common.ErrNotYetImplemented {
funcs = append(funcs, "WithdrawFiatFunds")
}
_, err = e.WithdrawFiatFundsToInternationalBank(&withdraw.Request{})
_, err = e.WithdrawFiatFundsToInternationalBank(nil)
if err == common.ErrNotYetImplemented {
funcs = append(funcs, "WithdrawFiatFundsToInternationalBank")
}
Expand Down
19 changes: 10 additions & 9 deletions cmd/exchange_wrapper_issues/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,13 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)
})

s := &order.Submit{
Pair: p,
Side: testOrderSide,
Type: testOrderType,
Amount: config.OrderSubmission.Amount,
Price: config.OrderSubmission.Price,
ClientID: config.OrderSubmission.OrderID,
Pair: p,
Side: testOrderSide,
Type: testOrderType,
Amount: config.OrderSubmission.Amount,
Price: config.OrderSubmission.Price,
ClientID: config.OrderSubmission.OrderID,
AssetType: assetTypes[i],
}
var r11 order.SubmitResponse
r11, err = e.SubmitOrder(s)
Expand Down Expand Up @@ -635,7 +636,7 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)

withdrawRequest := withdraw.Request{
Currency: p.Quote,
Crypto: &withdraw.CryptoRequest{
Crypto: withdraw.CryptoRequest{
Address: withdrawAddressOverride,
},
Amount: config.OrderSubmission.Amount,
Expand Down Expand Up @@ -679,8 +680,8 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config)
withdrawRequestFiat := withdraw.Request{
Currency: p.Quote,
Amount: config.OrderSubmission.Amount,
Fiat: &withdraw.FiatRequest{
Bank: &banking.Account{
Fiat: withdraw.FiatRequest{
Bank: banking.Account{
AccountName: config.BankDetails.BankAccountName,
AccountNumber: config.BankDetails.BankAccountNumber,
SWIFTCode: config.BankDetails.SwiftCode,
Expand Down
Loading

0 comments on commit 4e828a8

Please sign in to comment.