mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-01 23:16:51 +00:00
Engine: Scripting support (#383)
* WIP * updated appveyor and increased deadline 5 seconds due to increased linters being added * revert files to upstream/engine * WIP * WIP * mod file changes * added script manager * Added manager/and cli interfaces to scripting * Added script task handler * WIP - Added timer/repeat support and fleshed out wrapper further * autoload support added + WIP * WIP commit * added account balance info * btc markets temp work around * WIP - merged with upstream for new order package BTC Markets responses broken * Cancel order wrapper WIP * order wrapper update * Added test coverage for VM * moved to map for VM List shutdown of all VM now handled added gctcli commands for list and stop of running scripts * added override to load/execute for path * fixed incorrect channel shutdown added further test coverage and restructured gctcli commands into sub commands * increased test coverage for packages * Added docs cleaned up tests and example scripts * Test coverage increased for module/gct/exchange package * windows fixes * merged upstream/engine * WIP * logger fixes - removed pointer to bool check removed duplicate test check for logger * remove unused mutex * added inital upload support * fix linter issues for go-fmt * added zip support for uploading and added base for fund withdrawing * changed error return types and also log errors, fix zip path issue * improved error outputs and code flow * pairs response fix added protobuf defs for stop all and list all * added stop all running scripts general clean up and moved across to OrderManager * linter fixes (gofmt) * added list all command * rewrote zip handler to be cleaner also fixed file overwrite on upload * added query command reworked tests * added further error checking to compileandrun corrected use of pointers for accountinfo * bumped tengo version * Removed named returns reworded log messages removed unused falseptr * WIP * Added virutal machine limit improved config options * added model for script event added upload validation * script_event table has been completed, tests for wrapper functions implemented * README updates * reverted changes opened new PR to move withdraw struct outs * intial work on adding withdraw support after merger of withdraw package * started work on examples * Added crypto withdraw support * fix switch case assignment and gofmt project * Reworking Fiat withdraw request pending #402 * removed double pointer call * added withdraw support for fiat currencies * added tests for withdraw methods increased readme * removed local tengo require and also fix linter issues * Added default log size const added basic test for invalid script execution * First pass at moving wrapper to validator package to allow proper validation of uploaded scripts * Added script details to README added config test added test for no file extension * moved tests to const and fixed incorrect pathing * added test coverage to withdraw package * corrected file close handling * point to included configtest.json * extended validator support when a script is uploaded * Bug fix on bool logic * Added mutex * Don't create autit events on test execution * reverted common to master * moved file rename to unix timestamp format * converted logger enabled back to pointer as i need nilness check also moved scriptid to text over blob * started work on autoload add/remove support * First round of PR fixes (mostly commented exports) * Moved GCTScript load to last, removed unneeded error from cleanup() * Comment clairty for AuitEventID * added autoload add/remove command to cli * added tests for autoload * Test updates for Exchanges * linter fixes (gofmt) * Removed double check of engine pointer * remove possible nil pointer on GetSpecificTicker * Fixed not closing file handler on write that causes archive removal to fail * file handler Close clean ups * corrected spelling on error return and return invalid name n autoload * moved strings to cosnt moved bool pointer creation to convert package * new zip extractor added * Validation has been added to archive uploads * removed shadow var on err * added ok check to conversion * converted condition check * basic test for zip extract added * new zip handler * reverted back to old atomic loading system * removed shadow err * lets add a new line * added space to error return * command line toggle for script now works properly * readme updated * set configLoaded to true * check for configLoaded condition * added mutex to allow for multiple access on virtual machine increased test coverage disable script manager if scripting is disabled * linked up to enable/disablesubsystem commands * added start/stop example to readme * reworked logic on test as check should be done on Load() * updated to tengo v2 * linters * lower time on ntp client to stop slippage * remove all fails if any fail validtion from an archive * remove vm from list if timer is invalid * removed shadow on err * remove config creation from NTPCheck test * WIP testing DB changes * add unique constraint * WIP: created has many model * linters run * basic sqlite3 support added for new database format * linters run * Added test coverage for script repo * removed unused print * updated env vars for CI instances * updated env vars for CI instances * Updated test packages * Test updates for postgresql * removed invalid tests from postgres * remove duplication of struct and improved code flow * general cleanup * wording changes on log output * use databasemgr logger and add support for autoload without file extension * corrected test naming * return correct error * return correct error again version 82 * store scriptdata on creation * Hello * Errorln -> Errorf * Removed unused vars * Read me updates * testing without parallel * comment on exported type * added nil check against VM for test * add debugging information * gofmt * remove verbose and data sent to channel * Added debug information * linter fixes (gofmt) * remove unused CompileAndRun() call * test sleep to see if issue is timing related * semi-concurrent map fixes * one day i will run gofmt or setup precommit hooks * new line :D * increased test coverage * added correct sleep time * Moved over to sync map * linter fixes (gofmt) * goimports * moved VM related methods to vm.go * new line at end of file * trying increased timeout on golangci-lint for appveyor * add debugging information * removed timeout * reworked timeout logic * linter fixes (gofmt) * increased test coverage * increased test coverage * one day i will run gofmt or setup precommit hooks * removed unused exchange test * increased golangci-lint timeout * Added nil check on shutdown and test coverage for it lowered timeout back to 1:30 * reworked ID system * removed script hash as it was unused * added comments on exported methods and read me update * reorder code * removed to atomic.value for test execution flag * increased test coverage * move add further up execution * point to correct script file
This commit is contained in:
216
gctscript/wrappers/gct/exchange/exchange.go
Normal file
216
gctscript/wrappers/gct/exchange/exchange.go
Normal file
@@ -0,0 +1,216 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"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/exchanges/orderbook"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/withdraw"
|
||||
"github.com/thrasher-corp/gocryptotrader/gctscript/modules"
|
||||
)
|
||||
|
||||
// Exchange implements all required methods for Wrapper
|
||||
type Exchange struct{}
|
||||
|
||||
// Exchanges returns slice of all current exchanges
|
||||
func (e Exchange) Exchanges(enabledOnly bool) []string {
|
||||
return engine.GetExchanges(enabledOnly)
|
||||
}
|
||||
|
||||
// GetExchange returns IBotExchange for exchange or error if exchange is not found
|
||||
func (e Exchange) GetExchange(exch string) (exchange.IBotExchange, error) {
|
||||
ex := engine.GetExchangeByName(exch)
|
||||
if ex == nil {
|
||||
return nil, fmt.Errorf("%v exchange not found", exch)
|
||||
}
|
||||
|
||||
return ex, nil
|
||||
}
|
||||
|
||||
// IsEnabled returns if requested exchange is enabled or disabled
|
||||
func (e Exchange) IsEnabled(exch string) bool {
|
||||
ex, err := e.GetExchange(exch)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return ex.IsEnabled()
|
||||
}
|
||||
|
||||
// Orderbook returns current orderbook requested exchange, pair and asset
|
||||
func (e Exchange) Orderbook(exch string, pair currency.Pair, item asset.Item) (*orderbook.Base, error) {
|
||||
return engine.GetSpecificOrderbook(pair, exch, item)
|
||||
}
|
||||
|
||||
// Ticker returns ticker for provided currency pair & asset type
|
||||
func (e Exchange) Ticker(exch string, pair currency.Pair, item asset.Item) (*ticker.Price, error) {
|
||||
ex, err := e.GetExchange(exch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ex.FetchTicker(pair, item)
|
||||
}
|
||||
|
||||
// Pairs returns either all or enabled currency pairs
|
||||
func (e Exchange) Pairs(exch string, enabledOnly bool, item asset.Item) (*currency.Pairs, error) {
|
||||
x, err := engine.Bot.Config.GetExchangeConfig(exch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if enabledOnly {
|
||||
return &x.CurrencyPairs.Get(item).Enabled, nil
|
||||
}
|
||||
return &x.CurrencyPairs.Get(item).Available, nil
|
||||
}
|
||||
|
||||
// QueryOrder returns details of a valid exchange order
|
||||
func (e Exchange) QueryOrder(exch, orderID string) (*order.Detail, error) {
|
||||
ex, err := e.GetExchange(exch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r, err := ex.GetOrderInfo(orderID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
// SubmitOrder submit new order on exchange
|
||||
func (e Exchange) SubmitOrder(exch string, submit *order.Submit) (*order.SubmitResponse, error) {
|
||||
r, err := engine.Bot.OrderManager.Submit(exch, submit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &r.SubmitResponse, nil
|
||||
}
|
||||
|
||||
// CancelOrder wrapper to cancel order on exchange
|
||||
func (e Exchange) CancelOrder(exch, orderID string) (bool, error) {
|
||||
orderDetails, err := e.QueryOrder(exch, orderID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
cancel := &order.Cancel{
|
||||
AccountID: orderDetails.AccountID,
|
||||
OrderID: orderDetails.ID,
|
||||
CurrencyPair: orderDetails.CurrencyPair,
|
||||
Side: orderDetails.OrderSide,
|
||||
}
|
||||
|
||||
err = engine.Bot.OrderManager.Cancel(exch, cancel)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// AccountInformation returns account information (balance etc) for requested exchange
|
||||
func (e Exchange) AccountInformation(exch string) (*modules.AccountInfo, error) {
|
||||
ex, err := e.GetExchange(exch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r, err := ex.GetAccountInfo()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
temp, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
accountInfo := modules.AccountInfo{}
|
||||
err = json.Unmarshal(temp, &accountInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &accountInfo, nil
|
||||
}
|
||||
|
||||
// DepositAddress gets the address required to deposit funds for currency type
|
||||
func (e Exchange) DepositAddress(exch string, currencyCode currency.Code) (out string, err error) {
|
||||
if currencyCode.IsEmpty() {
|
||||
err = errors.New("currency code is empty")
|
||||
return
|
||||
}
|
||||
return engine.Bot.DepositAddressManager.GetDepositAddressByExchange(exch, currencyCode)
|
||||
}
|
||||
|
||||
// WithdrawalFiatFunds withdraw funds from exchange to requested fiat source
|
||||
func (e Exchange) WithdrawalFiatFunds(exch, bankaccountid string, request *withdraw.FiatRequest) (string, error) {
|
||||
ex, err := e.GetExchange(exch)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
v, err := engine.Bot.Config.GetBankAccountByID(bankaccountid)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
otp, err := engine.GetExchangeoOTPByName(exch)
|
||||
if err == nil {
|
||||
otpValue, errParse := strconv.ParseInt(otp, 10, 64)
|
||||
if errParse != nil {
|
||||
return "", errors.New("failed to generate OTP unable to continue")
|
||||
}
|
||||
request.GenericInfo.OneTimePassword = otpValue
|
||||
}
|
||||
request.BankAccountName = v.AccountName
|
||||
request.BankAccountNumber = v.AccountNumber
|
||||
request.BankName = v.BankName
|
||||
request.BankAddress = v.BankAddress
|
||||
request.BankCity = v.BankPostalCity
|
||||
request.BankCountry = v.BankCountry
|
||||
request.BankPostalCode = v.BankPostalCode
|
||||
request.BSB = v.BSBNumber
|
||||
request.SwiftCode = v.SWIFTCode
|
||||
request.IBAN = v.IBAN
|
||||
|
||||
err = withdraw.Valid(request)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return ex.WithdrawFiatFunds(request)
|
||||
}
|
||||
|
||||
// WithdrawalCryptoFunds withdraw funds from exchange to requested Crypto source
|
||||
func (e Exchange) WithdrawalCryptoFunds(exch string, request *withdraw.CryptoRequest) (out string, err error) {
|
||||
ex, err := e.GetExchange(exch)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
otp, err := engine.GetExchangeoOTPByName(exch)
|
||||
if err == nil {
|
||||
v, errParse := strconv.ParseInt(otp, 10, 64)
|
||||
if errParse != nil {
|
||||
return "", errors.New("failed to generate OTP unable to continue")
|
||||
}
|
||||
request.GenericInfo.OneTimePassword = v
|
||||
}
|
||||
|
||||
err = withdraw.Valid(request)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return ex.WithdrawCryptocurrencyFunds(request)
|
||||
}
|
||||
190
gctscript/wrappers/gct/exchange/exchange_test.go
Normal file
190
gctscript/wrappers/gct/exchange/exchange_test.go
Normal file
@@ -0,0 +1,190 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/engine"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
// change these if you wish to test another exchange and/or currency pair
|
||||
const (
|
||||
exchName = "BTC Markets" // change to test on another exchange
|
||||
exchAPIKEY = ""
|
||||
exchAPISECRET = ""
|
||||
exchClientID = ""
|
||||
pairs = "BTC-AUD" // change to test another currency pair
|
||||
delimiter = "-"
|
||||
assetType = asset.Spot
|
||||
orderID = "1234"
|
||||
orderType = order.Limit
|
||||
orderSide = order.Buy
|
||||
orderClientID = ""
|
||||
orderPrice = 1
|
||||
orderAmount = 1
|
||||
)
|
||||
|
||||
var (
|
||||
settings = engine.Settings{
|
||||
ConfigFile: filepath.Join("..", "..", "..", "..", "testdata", "configtest.json"),
|
||||
EnableDryRun: true,
|
||||
DataDir: filepath.Join("..", "..", "..", "..", "testdata", "gocryptotrader"),
|
||||
Verbose: false,
|
||||
EnableGRPC: false,
|
||||
EnableDeprecatedRPC: false,
|
||||
EnableWebsocketRPC: false,
|
||||
}
|
||||
exchangeTest = Exchange{}
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
var t int
|
||||
err := setupEngine()
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to configure exchange test cannot continue: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
t = m.Run()
|
||||
cleanup()
|
||||
os.Exit(t)
|
||||
}
|
||||
|
||||
func TestExchange_Exchanges(t *testing.T) {
|
||||
t.Parallel()
|
||||
x := exchangeTest.Exchanges(false)
|
||||
y := len(x)
|
||||
if y != 27 {
|
||||
t.Fatalf("expected 27 received %v", y)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExchange_GetExchange(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := exchangeTest.GetExchange(exchName)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = exchangeTest.GetExchange("hello world")
|
||||
if err == nil {
|
||||
t.Fatal("unexpected error message received nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExchange_IsEnabled(t *testing.T) {
|
||||
t.Parallel()
|
||||
x := exchangeTest.IsEnabled(exchName)
|
||||
if !x {
|
||||
t.Fatal("expected return to be true")
|
||||
}
|
||||
x = exchangeTest.IsEnabled("fake_exchange")
|
||||
if x {
|
||||
t.Fatal("expected return to be false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExchange_Ticker(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := currency.NewPairDelimiter(pairs, delimiter)
|
||||
_, err := exchangeTest.Ticker(exchName, c, assetType)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExchange_Orderbook(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := currency.NewPairDelimiter(pairs, delimiter)
|
||||
_, err := exchangeTest.Orderbook(exchName, c, assetType)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExchange_Pairs(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := exchangeTest.Pairs(exchName, false, assetType)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = exchangeTest.Pairs(exchName, true, assetType)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExchange_AccountInformation(t *testing.T) {
|
||||
if !configureExchangeKeys() {
|
||||
t.Skip("no exchange configured test skipped")
|
||||
}
|
||||
_, err := exchangeTest.AccountInformation(exchName)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExchange_QueryOrder(t *testing.T) {
|
||||
if !configureExchangeKeys() {
|
||||
t.Skip("no exchange configured test skipped")
|
||||
}
|
||||
_, err := exchangeTest.QueryOrder(exchName, orderID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExchange_SubmitOrder(t *testing.T) {
|
||||
if !configureExchangeKeys() {
|
||||
t.Skip("no exchange configured test skipped")
|
||||
}
|
||||
tempOrder := &order.Submit{
|
||||
Pair: currency.NewPairDelimiter(pairs, delimiter),
|
||||
OrderType: orderType,
|
||||
OrderSide: orderSide,
|
||||
TriggerPrice: 0,
|
||||
TargetAmount: 0,
|
||||
Price: orderPrice,
|
||||
Amount: orderAmount,
|
||||
ClientID: orderClientID,
|
||||
}
|
||||
_, err := exchangeTest.SubmitOrder(exchName, tempOrder)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExchange_CancelOrder(t *testing.T) {
|
||||
if !configureExchangeKeys() {
|
||||
t.Skip("no exchange configured test skipped")
|
||||
}
|
||||
_, err := exchangeTest.CancelOrder(exchName, orderID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func setupEngine() (err error) {
|
||||
engine.Bot, err = engine.NewFromSettings(&settings)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return engine.Bot.Start()
|
||||
}
|
||||
|
||||
func cleanup() {
|
||||
err := os.RemoveAll(settings.DataDir)
|
||||
if err != nil {
|
||||
fmt.Printf("Clean up failed to remove file: %v manual removal may be required", err)
|
||||
}
|
||||
}
|
||||
|
||||
func configureExchangeKeys() bool {
|
||||
ex := engine.GetExchangeByName(exchName).GetBase()
|
||||
ex.SetAPIKeys(exchAPIKEY, exchAPISECRET, exchClientID)
|
||||
ex.SkipAuthCheck = true
|
||||
return ex.ValidateAPICredentials()
|
||||
}
|
||||
10
gctscript/wrappers/gct/gctwrapper.go
Normal file
10
gctscript/wrappers/gct/gctwrapper.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package gct
|
||||
|
||||
import "github.com/thrasher-corp/gocryptotrader/gctscript/wrappers/gct/exchange"
|
||||
|
||||
// Setup returns a Wrapper
|
||||
func Setup() *Wrapper {
|
||||
return &Wrapper{
|
||||
&exchange.Exchange{},
|
||||
}
|
||||
}
|
||||
14
gctscript/wrappers/gct/gctwrapper_test.go
Normal file
14
gctscript/wrappers/gct/gctwrapper_test.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package gct
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSetup(t *testing.T) {
|
||||
x := Setup()
|
||||
xType := reflect.TypeOf(x).String()
|
||||
if xType != "*gct.Wrapper" {
|
||||
t.Fatalf("Setup() should return pointer to Wrapper instead received: %v", x)
|
||||
}
|
||||
}
|
||||
8
gctscript/wrappers/gct/gctwrapper_type.go
Normal file
8
gctscript/wrappers/gct/gctwrapper_type.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package gct
|
||||
|
||||
import "github.com/thrasher-corp/gocryptotrader/gctscript/wrappers/gct/exchange"
|
||||
|
||||
// Wrapper struct
|
||||
type Wrapper struct {
|
||||
*exchange.Exchange
|
||||
}
|
||||
211
gctscript/wrappers/validator/validator.go
Normal file
211
gctscript/wrappers/validator/validator.go
Normal file
@@ -0,0 +1,211 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/withdraw"
|
||||
"github.com/thrasher-corp/gocryptotrader/gctscript/modules"
|
||||
)
|
||||
|
||||
// Exchanges validator for test execution/scripts
|
||||
func (w Wrapper) Exchanges(enabledOnly bool) []string {
|
||||
if enabledOnly {
|
||||
return []string{
|
||||
"hello world",
|
||||
}
|
||||
}
|
||||
return []string{
|
||||
"nope",
|
||||
}
|
||||
}
|
||||
|
||||
// IsEnabled returns if requested exchange is enabled or disabled
|
||||
func (w Wrapper) IsEnabled(exch string) (v bool) {
|
||||
if exch == exchError.String() {
|
||||
return
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Orderbook validator for test execution/scripts
|
||||
func (w Wrapper) Orderbook(exch string, pair currency.Pair, item asset.Item) (*orderbook.Base, error) {
|
||||
if exch == exchError.String() {
|
||||
return nil, errTestFailed
|
||||
}
|
||||
|
||||
return &orderbook.Base{
|
||||
ExchangeName: exch,
|
||||
AssetType: item,
|
||||
Pair: pair,
|
||||
Bids: []orderbook.Item{
|
||||
{
|
||||
Amount: 1,
|
||||
Price: 1,
|
||||
},
|
||||
},
|
||||
Asks: []orderbook.Item{
|
||||
{
|
||||
Amount: 1,
|
||||
Price: 1,
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Ticker validator for test execution/scripts
|
||||
func (w Wrapper) Ticker(exch string, pair currency.Pair, item asset.Item) (*ticker.Price, error) {
|
||||
if exch == exchError.String() {
|
||||
return nil, errTestFailed
|
||||
}
|
||||
return &ticker.Price{
|
||||
Last: 1,
|
||||
High: 2,
|
||||
Low: 3,
|
||||
Bid: 4,
|
||||
Ask: 5,
|
||||
Volume: 6,
|
||||
QuoteVolume: 7,
|
||||
PriceATH: 8,
|
||||
Open: 9,
|
||||
Close: 10,
|
||||
Pair: pair,
|
||||
ExchangeName: exch,
|
||||
AssetType: item,
|
||||
LastUpdated: time.Now(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Pairs validator for test execution/scripts
|
||||
func (w Wrapper) Pairs(exch string, _ bool, _ asset.Item) (*currency.Pairs, error) {
|
||||
if exch == exchError.String() {
|
||||
return nil, errTestFailed
|
||||
}
|
||||
|
||||
pairs := currency.NewPairsFromStrings([]string{"btc_usd", "btc_aud", "btc_ltc"})
|
||||
return &pairs, nil
|
||||
}
|
||||
|
||||
// QueryOrder validator for test execution/scripts
|
||||
func (w Wrapper) QueryOrder(exch, _ string) (*order.Detail, error) {
|
||||
if exch == exchError.String() {
|
||||
return nil, errTestFailed
|
||||
}
|
||||
return &order.Detail{
|
||||
Exchange: exch,
|
||||
AccountID: "hello",
|
||||
ID: "1",
|
||||
CurrencyPair: currency.NewPairFromString("BTCAUD"),
|
||||
OrderSide: "ask",
|
||||
OrderType: "limit",
|
||||
OrderDate: time.Now(),
|
||||
Status: "cancelled",
|
||||
Price: 1,
|
||||
Amount: 2,
|
||||
ExecutedAmount: 1,
|
||||
RemainingAmount: 0,
|
||||
Fee: 0,
|
||||
Trades: []order.TradeHistory{
|
||||
{
|
||||
Timestamp: time.Now(),
|
||||
TID: "",
|
||||
Price: 1,
|
||||
Amount: 2,
|
||||
Exchange: exch,
|
||||
Type: "limit",
|
||||
Side: "ask",
|
||||
Fee: 0,
|
||||
Description: "",
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SubmitOrder validator for test execution/scripts
|
||||
func (w Wrapper) SubmitOrder(exch string, _ *order.Submit) (*order.SubmitResponse, error) {
|
||||
if exch == exchError.String() {
|
||||
return nil, errTestFailed
|
||||
}
|
||||
|
||||
tempOrder := &order.SubmitResponse{
|
||||
IsOrderPlaced: false,
|
||||
OrderID: exch,
|
||||
}
|
||||
|
||||
if exch == "true" {
|
||||
tempOrder.IsOrderPlaced = true
|
||||
}
|
||||
|
||||
return tempOrder, nil
|
||||
}
|
||||
|
||||
// CancelOrder validator for test execution/scripts
|
||||
func (w Wrapper) CancelOrder(exch, orderid string) (bool, error) {
|
||||
if exch == exchError.String() {
|
||||
return false, errTestFailed
|
||||
}
|
||||
return orderid != "false", nil
|
||||
}
|
||||
|
||||
// AccountInformation validator for test execution/scripts
|
||||
func (w Wrapper) AccountInformation(exch string) (*modules.AccountInfo, error) {
|
||||
if exch == exchError.String() {
|
||||
return &modules.AccountInfo{}, errTestFailed
|
||||
}
|
||||
|
||||
return &modules.AccountInfo{
|
||||
Exchange: exch,
|
||||
Accounts: []modules.Account{
|
||||
{
|
||||
ID: exch,
|
||||
Currencies: []modules.AccountCurrencyInfo{
|
||||
{
|
||||
CurrencyName: currency.Code{
|
||||
Item: ¤cy.Item{
|
||||
ID: 0,
|
||||
FullName: "Bitcoin",
|
||||
Symbol: "BTC",
|
||||
Role: 1,
|
||||
AssocChain: "",
|
||||
AssocExchange: nil,
|
||||
},
|
||||
},
|
||||
TotalValue: 100,
|
||||
Hold: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// DepositAddress validator for test execution/scripts
|
||||
func (w Wrapper) DepositAddress(exch string, _ currency.Code) (string, error) {
|
||||
if exch == exchError.String() {
|
||||
return exch, errTestFailed
|
||||
}
|
||||
|
||||
return exch, nil
|
||||
}
|
||||
|
||||
// WithdrawalCryptoFunds validator for test execution/scripts
|
||||
func (w Wrapper) WithdrawalCryptoFunds(exch string, _ *withdraw.CryptoRequest) (out string, err error) {
|
||||
if exch == exchError.String() {
|
||||
return exch, errTestFailed
|
||||
}
|
||||
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// WithdrawalFiatFunds validator for test execution/scripts
|
||||
func (w Wrapper) WithdrawalFiatFunds(exch, _ string, _ *withdraw.FiatRequest) (out string, err error) {
|
||||
if exch == exchError.String() {
|
||||
return exch, errTestFailed
|
||||
}
|
||||
|
||||
return "123", nil
|
||||
}
|
||||
205
gctscript/wrappers/validator/validator_test.go
Normal file
205
gctscript/wrappers/validator/validator_test.go
Normal file
@@ -0,0 +1,205 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
const (
|
||||
exchName = "BTC Markets" // change to test on another exchange
|
||||
exchAPIKEY = ""
|
||||
exchAPISECRET = ""
|
||||
exchClientID = ""
|
||||
pairs = "BTC-AUD" // change to test another currency pair
|
||||
delimiter = "-"
|
||||
assetType = asset.Spot
|
||||
orderID = "1234"
|
||||
orderType = order.Limit
|
||||
orderSide = order.Buy
|
||||
orderClientID = ""
|
||||
orderPrice = 1
|
||||
orderAmount = 1
|
||||
)
|
||||
|
||||
var (
|
||||
currencyPair = currency.NewPairFromString("BTCAUD")
|
||||
testWrapper = Wrapper{}
|
||||
)
|
||||
|
||||
func TestWrapper_Exchanges(t *testing.T) {
|
||||
t.Parallel()
|
||||
x := testWrapper.Exchanges(false)
|
||||
y := len(x)
|
||||
if y != 1 {
|
||||
t.Fatalf("expected 1 received %v", y)
|
||||
}
|
||||
|
||||
x = testWrapper.Exchanges(true)
|
||||
y = len(x)
|
||||
if y != 1 {
|
||||
t.Fatalf("expected 1 received %v", y)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapper_IsEnabled(t *testing.T) {
|
||||
t.Parallel()
|
||||
f := testWrapper.IsEnabled("hello")
|
||||
if !f {
|
||||
t.Fatal("expected IsEnabled to return true for enabled exchange")
|
||||
}
|
||||
|
||||
f = testWrapper.IsEnabled(exchError.String())
|
||||
if f {
|
||||
t.Fatal("expected IsEnabled to return false for disabled exchange")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapper_AccountInformation(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := testWrapper.AccountInformation(exchName)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = testWrapper.AccountInformation(exchError.String())
|
||||
if err == nil {
|
||||
t.Fatal("expected AccountInformation to return error on invalid name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapper_CancelOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := testWrapper.CancelOrder(exchName, orderID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = testWrapper.CancelOrder(exchError.String(), "")
|
||||
if err == nil {
|
||||
t.Fatal("expected CancelOrder to return error on invalid name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapper_DepositAddress(t *testing.T) {
|
||||
_, err := testWrapper.DepositAddress(exchError.String(), currency.NewCode("BTC"))
|
||||
if err == nil {
|
||||
t.Fatal("expected DepositAddress to return error on invalid name")
|
||||
}
|
||||
|
||||
_, err = testWrapper.DepositAddress(exchName, currency.NewCode("BTC"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapper_Orderbook(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := currency.NewPairDelimiter(pairs, delimiter)
|
||||
_, err := testWrapper.Orderbook(exchName, c, assetType)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = testWrapper.Orderbook(exchError.String(), currencyPair, asset.Spot)
|
||||
if err == nil {
|
||||
t.Fatal("expected Orderbook to return error with invalid name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapper_Pairs(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := testWrapper.Pairs(exchName, false, assetType)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = testWrapper.Pairs(exchName, true, assetType)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = testWrapper.Pairs(exchError.String(), false, asset.Spot)
|
||||
if err == nil {
|
||||
t.Fatal("expected Pairs to return error on invalid name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapper_QueryOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := testWrapper.QueryOrder(exchName, orderID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = testWrapper.QueryOrder(exchError.String(), "")
|
||||
if err == nil {
|
||||
t.Fatal("expected QueryOrder to return error on invalid name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapper_SubmitOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tempOrder := &order.Submit{
|
||||
Pair: currency.NewPairDelimiter(pairs, delimiter),
|
||||
OrderType: orderType,
|
||||
OrderSide: orderSide,
|
||||
TriggerPrice: 0,
|
||||
TargetAmount: 0,
|
||||
Price: orderPrice,
|
||||
Amount: orderAmount,
|
||||
ClientID: orderClientID,
|
||||
}
|
||||
_, err := testWrapper.SubmitOrder("true", tempOrder)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = testWrapper.SubmitOrder(exchError.String(), nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected SubmitOrder to return error with invalid name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapper_Ticker(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := currency.NewPairDelimiter(pairs, delimiter)
|
||||
_, err := testWrapper.Ticker(exchName, c, assetType)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = testWrapper.Ticker(exchError.String(), currencyPair, asset.Spot)
|
||||
if err == nil {
|
||||
t.Fatal("expected Ticker to return error with invalid name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapper_WithdrawalCryptoFunds(t *testing.T) {
|
||||
_, err := testWrapper.WithdrawalCryptoFunds(exchError.String(), nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected WithdrawalCryptoFunds to return error with invalid name")
|
||||
}
|
||||
|
||||
_, err = testWrapper.WithdrawalCryptoFunds(exchName, nil)
|
||||
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)
|
||||
if err == nil {
|
||||
t.Fatal("expected WithdrawalFiatFunds to return error with invalid name")
|
||||
}
|
||||
|
||||
_, err = testWrapper.WithdrawalFiatFunds(exchName, "", nil)
|
||||
if err != nil {
|
||||
t.Fatal("expected WithdrawalCryptoFunds to return error with invalid name")
|
||||
}
|
||||
}
|
||||
21
gctscript/wrappers/validator/validator_types.go
Normal file
21
gctscript/wrappers/validator/validator_types.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
|
||||
objects "github.com/d5/tengo/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
// IsTestExecution if test is executed under test conditions
|
||||
IsTestExecution atomic.Value
|
||||
|
||||
exchError = &objects.String{
|
||||
Value: "error",
|
||||
}
|
||||
errTestFailed = errors.New("test failed")
|
||||
)
|
||||
|
||||
// Wrapper for validator interface
|
||||
type Wrapper struct{}
|
||||
14
gctscript/wrappers/wrappers.go
Normal file
14
gctscript/wrappers/wrappers.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package wrappers
|
||||
|
||||
import (
|
||||
"github.com/thrasher-corp/gocryptotrader/gctscript/modules"
|
||||
"github.com/thrasher-corp/gocryptotrader/gctscript/wrappers/validator"
|
||||
)
|
||||
|
||||
// GetWrapper returns the instance of each wrapper to use
|
||||
func GetWrapper() modules.GCT {
|
||||
if validator.IsTestExecution.Load() == true {
|
||||
return validator.Wrapper{}
|
||||
}
|
||||
return modules.Wrapper
|
||||
}
|
||||
Reference in New Issue
Block a user