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:
Andrew
2020-01-23 13:54:04 +11:00
committed by Adrian Gallagher
parent 4c33a0738a
commit f6fd94ea69
102 changed files with 15181 additions and 370 deletions

262
gctscript/README.md Normal file
View File

@@ -0,0 +1,262 @@
# GoCryptoTrader package gctscript
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/page-logo.png?raw=true" width="350px" height="350px" hspace="70">
[![Build Status](https://travis-ci.org/thrasher-corp/gocryptotrader.svg?branch=master)](https://travis-ci.org/thrasher-corp/gocryptotrader)
[![Software License](https://img.shields.io/badge/License-MIT-orange.svg?style=flat-square)](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
[![GoDoc](https://godoc.org/github.com/thrasher-corp/gocryptotrader?status.svg)](https://godoc.org/github.com/thrasher-corp/gocryptotrader/portfolio)
[![Coverage Status](http://codecov.io/github/thrasher-corp/gocryptotrader/coverage.svg?branch=master)](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/thrasher-corp/gocryptotrader)](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
This gctscript package is part of the GoCryptoTrader codebase.
## This is still in active development
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
## Current Features for gctscript package
+ Execute scripts
+ Terminate scripts
+ Autoload scripts on bot startup
+ Current Exchange features supported:
+ Enabled Exchanges
+ Enabled currency pairs
+ Account information
+ Query Order
+ Submit Order
+ Cancel Order
+ Ticker
+ Orderbook
## How to use
##### Prerequisites
To Enable database logging support you must have an active migrated database by following the [database setup guide](../database/README.md)
##### Configuration
The gctscript configuration struct is currently:
```shell script
type Config struct {
Enabled bool `json:"enabled"`
ScriptTimeout time.Duration `json:"timeout"`
AllowImports bool `json:"allow_imports"`
AutoLoad []string `json:"auto_load"`
Verbose bool `json:"Verbose"`
}
```
With an example configuration being:
```sh
"gctscript": {
"enabled": true,
"timeout": 600000000,
"allow_imports": true,
"auto_load": [],
"debug": false
},
```
##### Script Control
+ You can autoload scripts on bot start up by placing their name in the "auto_load" config entry
```shell script
"auto_load": ["one","two"]
```
This will look in your GoCryptoTrader data directory in a folder called "scripts" for files one.gct and two.gct and autoload them
+ Manual control of scripts can be done via the gctcli command with support for the following:
- Enable/Disable GCTScript:
```shell script
gctcli enablesubsystem "gctscript"
gctcli disablesubsystem "gctscript"
```
- Start/Execute:
```shell script
gctcli gctscript execute <scriptname> <pathoverride>
gctcli gctscript execute "timer.gct" "~/gctscript"
{
"status": "ok",
"data": "timer.gct executed"
}
```
- Stop:
```shell script
gctcli gctscript stop <uuid>
gctcli gctscript stop 821bd73e-02b1-4974-9463-874cb49f130d
{
"status": "ok",
"data": "821bd73e-02b1-4974-9463-874cb49f130d terminated"
}
```
- Status:
```shell script
gctcli gctscript status
{
"status": "ok",
"scripts": [
{
"uuid": "821bd73e-02b1-4974-9463-874cb49f130d",
"name": "timer.gct",
"next_run": "2019-11-14 13:11:40.224919456 +1100 AEDT m=+91.062103259"
}
]
}
```
- Read file:
```shell script
gctcli gctscript read <filename>
gctcli gctscript read "timer.gct"
{
"status": "ok",
"script": {
"name": "timer.gct",
"path": "/home/x/.gocryptotrader/scripts"
},
"data": "fmt := import(\"fmt\")\nt := import(\"times\")\n\nname := \"run\"\ntimer := \"5s\"\n\nload := func() {\n\tfmt.printf(\"5s %s\\n\",t.now())\n}\n\nload()\n"
}
```
- Query running script:
```shell script
gctcli gctscript query <uuid>
gctcli gctscript query 821bd73e-02b1-4974-9463-874cb49f130d
{
"status": "ok",
"script": {
"UUID": "bf692e2d-fa1e-4d95-92fd-33d7634d3d77",
"name": "timer.gct",
"path": "/home/x/.gocryptotrader/scripts",
"next_run": "2019-12-12 07:44:19.747572406 +1100 AEDT m=+16.782773385"
},
"data": "fmt := import(\"fmt\")\nt := import(\"times\")\n\nname := \"run\"\ntimer := \"5s\"\n\nload := func() {\n\tfmt.printf(\"5s %s\\n\",t.now())\n}\n\nload()\n"
}
load()
```
- Add script to autoload:
```shell script
gctcli gctscript autoload add timer
{
"status": "success",
"data": "script timer added to autoload list"
}
```
- Remove script from autoload:
```shell script
gctcli gctscript autoload remove timer
{
"status": "success",
"data": "script timer removed from autoload list"
}
```
##### Scripting & Extending modules
The scripting engine utilises [tengo](https://github.com/d5/tengo) an intro tutorial for it can be found [here](https://github.com/d5/tengo/blob/master/docs/tutorial.md)
Modules have been written so far linking up common exchange features including
- Orderbook
- Ticker
- Order Management
- Account information
- Withdraw funds
- Get Deposit Addresses
Extending or creating new modules:
Extending an existing module the exchange module for example is simple
- Open required [module](modules/gct/exchange.go)
- Add to exchangeModule map
- Define function with signature ```(args ...objects.Object) (ret objects.Object, err error)```
Similar steps can be taken to add a new module with a few adjustments
- Open required [GCT](modules/gct/gct_types.go)
- Add module name to GCTModules map
##### GCT module methods
Current supported methods added and exposed to scripts are as follows:
```
accountinfo
-> exchange:string
depositaddress
-> exchange:string
-> currency:string
orderbook
-> exchange:string
-> currency pair:string
-> delimiter:string
-> asset:string
ticker
-> exchange:string
-> currency pair:string
-> delimiter:string
-> asset:string
pairs
-> exchange:string
-> enabled only:bool
-> asset:string
queryorder
-> exchange:string
-> order id:string
submitorder
-> exchange:string
-> currency pair:string
-> delimiter:string
-> order type:string
-> order side:string
-> price:float64
-> amount:float64
-> client_id:string
withdrawfiat
-> exchange:string
-> currency:string
-> description:string
-> amount:float64
-> bank id:string
withdrawcrypto
-> exchange:string
-> currency:string
-> address:string
-> address tag:string
-> amount:float64
-> fee:float64
-> description:string
```
## Contribution
Please feel free to submit any pull requests or suggest any desired features to be added.
When submitting a PR, please abide by our coding guidelines:
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
+ Pull requests need to be based on and opened against the `master` branch.
## Donations
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
***1F5zVDgNjorJ51oGebSvNCrSAHpwGkUdDB***

View File

@@ -0,0 +1,13 @@
// import fmt package
fmt := import("fmt")
// import exchange package
exch := import("exchange")
load := func() {
// retrieve account information from exchange and store in info variable
info := exch.accountinfo("BTC Markets")
// print out info
fmt.print(info)
}
load()

View File

@@ -0,0 +1,9 @@
fmt := import("fmt")
exch := import("exchange")
load := func() {
info := exch.depositaddress("BTC Markets", "BTC", "")
fmt.println(info)
}
load()

View File

@@ -0,0 +1,12 @@
fmt := import("fmt")
exch := import("exchange")
name := "run"
timer := "5s"
load := func() {
tx := exch.orderbook("btc markets", "btc-aud", "-", "spot")
fmt.println(tx)
}
load()

View File

@@ -0,0 +1,9 @@
fmt := import("fmt")
exch := import("exchange")
load := func() {
info := exch.pairs("BTC Markets", false, "SPOT")
fmt.print(info)
}
load()

View File

@@ -0,0 +1,9 @@
fmt := import("fmt")
exch := import("exchange")
load := func() {
info := exch.orderquery("BTC Markets", "4491600698")
fmt.print(info)
}
load()

View File

@@ -0,0 +1,9 @@
fmt := import("fmt")
exch := import("exchange")
load := func() {
info := exch.ordersubmit("BTC Markets","BTC-AUD","-","LIMIT","SELL",1000000, 1,"")
fmt.print(info)
}
load()

View File

@@ -0,0 +1,13 @@
fmt := import("fmt")
exch := import("exchange")
name := "run"
timer := "5s"
load := func() {
tx := exch.ticker("btc markets", "btc-aud", "-", "spot")
fmt.println(tx)
}
load()

View File

@@ -0,0 +1,25 @@
// import fmt package
fmt := import("fmt")
// import exchange package
exch := import("exchange")
load := func() {
// retrieve account information from exchange and store in info variable
// withdrawcrypto
// 1: Exchange name
// 2: currency
// 3: address
// 4: address tag
// 5: amount
// 6: fee amount
// 7: trade password
// 8: OTP
// submit request to withdraw funds
info := exch.withdrawfiat("BTC Markets", "AUD", "hello", 1, "-")
// print out info
fmt.print(info)
}
load()

View File

@@ -0,0 +1,24 @@
// import fmt package
fmt := import("fmt")
// import exchange package
exch := import("exchange")
load := func() {
// retrieve account information from exchange and store in info variable
// withdrawcrypto
// 1: Exchange name
// 2: currency
// 3: address
// 4: address tag
// 5: amount
// 6: fee amount
// 7: trade password
// 8: OTP
info := exch.withdrawcrypto("BTC Markets","BTC", "1234562362", "1231", 1.0, 0.0, "","" )
// print out info
fmt.print(info)
}
load()

View File

@@ -0,0 +1,16 @@
fmt := import("fmt")
timer := "5s"
exit := func() {
timer = 0
}
load := func() {
for x := 0 ; x < 20; x++ {
fmt.printf("Hello %v", x)
}
exit()
}
load()

11
gctscript/gctscript.go Normal file
View File

@@ -0,0 +1,11 @@
package gctscript
import (
"github.com/thrasher-corp/gocryptotrader/gctscript/modules"
"github.com/thrasher-corp/gocryptotrader/gctscript/wrappers/gct"
)
// Setup configures the wrapper interface to use
func Setup() {
modules.SetModuleWrapper(gct.Setup())
}

View File

@@ -0,0 +1,498 @@
package gct
import (
"fmt"
"strings"
objects "github.com/d5/tengo/v2"
"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/withdraw"
"github.com/thrasher-corp/gocryptotrader/gctscript/wrappers"
)
var exchangeModule = map[string]objects.Object{
"orderbook": &objects.UserFunction{Name: "orderbook", Value: ExchangeOrderbook},
"ticker": &objects.UserFunction{Name: "ticker", Value: ExchangeTicker},
"exchanges": &objects.UserFunction{Name: "exchanges", Value: ExchangeExchanges},
"pairs": &objects.UserFunction{Name: "pairs", Value: ExchangePairs},
"accountinfo": &objects.UserFunction{Name: "accountinfo", Value: ExchangeAccountInfo},
"depositaddress": &objects.UserFunction{Name: "depositaddress", Value: ExchangeDepositAddress},
"orderquery": &objects.UserFunction{Name: "orderquery", Value: ExchangeOrderQuery},
"ordercancel": &objects.UserFunction{Name: "ordercancel", Value: ExchangeOrderCancel},
"ordersubmit": &objects.UserFunction{Name: "ordersubmit", Value: ExchangeOrderSubmit},
"withdrawcrypto": &objects.UserFunction{Name: "withdrawcrypto", Value: ExchangeWithdrawCrypto},
"withdrawfiat": &objects.UserFunction{Name: "withdrawfiat", Value: ExchangeWithdrawFiat},
}
// ExchangeOrderbook returns orderbook for requested exchange & currencypair
func ExchangeOrderbook(args ...objects.Object) (objects.Object, error) {
if len(args) != 4 {
return nil, objects.ErrWrongNumArguments
}
exchangeName, ok := objects.ToString(args[0])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, exchangeName)
}
currencyPair, ok := objects.ToString(args[1])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, currencyPair)
}
delimiter, ok := objects.ToString(args[2])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, delimiter)
}
assetTypeParam, ok := objects.ToString(args[3])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, assetTypeParam)
}
pairs := currency.NewPairDelimiter(currencyPair, delimiter)
assetType := asset.Item(assetTypeParam)
ob, err := wrappers.GetWrapper().Orderbook(exchangeName, pairs, assetType)
if err != nil {
return nil, err
}
var asks, bids objects.Array
for x := range ob.Asks {
temp := make(map[string]objects.Object, 2)
temp["amount"] = &objects.Float{Value: ob.Asks[x].Amount}
temp["price"] = &objects.Float{Value: ob.Asks[x].Price}
asks.Value = append(asks.Value, &objects.Map{Value: temp})
}
for x := range ob.Bids {
temp := make(map[string]objects.Object, 2)
temp["amount"] = &objects.Float{Value: ob.Bids[x].Amount}
temp["price"] = &objects.Float{Value: ob.Bids[x].Price}
bids.Value = append(bids.Value, &objects.Map{Value: temp})
}
data := make(map[string]objects.Object, 5)
data["exchange"] = &objects.String{Value: ob.ExchangeName}
data["pair"] = &objects.String{Value: ob.Pair.String()}
data["asks"] = &asks
data["bids"] = &bids
data["asset"] = &objects.String{Value: ob.AssetType.String()}
return &objects.Map{
Value: data,
}, nil
}
// ExchangeTicker returns ticker data for requested exchange and currency pair
func ExchangeTicker(args ...objects.Object) (objects.Object, error) {
if len(args) != 4 {
return nil, objects.ErrWrongNumArguments
}
exchangeName, ok := objects.ToString(args[0])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, exchangeName)
}
currencyPair, ok := objects.ToString(args[1])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, currencyPair)
}
delimiter, ok := objects.ToString(args[2])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, delimiter)
}
assetTypeParam, ok := objects.ToString(args[3])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, assetTypeParam)
}
pairs := currency.NewPairDelimiter(currencyPair, delimiter)
assetType := asset.Item(assetTypeParam)
tx, err := wrappers.GetWrapper().Ticker(exchangeName, pairs, assetType)
if err != nil {
return nil, err
}
data := make(map[string]objects.Object, 14)
data["exchange"] = &objects.String{Value: tx.ExchangeName}
data["last"] = &objects.Float{Value: tx.Last}
data["High"] = &objects.Float{Value: tx.High}
data["Low"] = &objects.Float{Value: tx.Low}
data["bid"] = &objects.Float{Value: tx.Bid}
data["ask"] = &objects.Float{Value: tx.Ask}
data["volume"] = &objects.Float{Value: tx.Volume}
data["quotevolume"] = &objects.Float{Value: tx.QuoteVolume}
data["priceath"] = &objects.Float{Value: tx.PriceATH}
data["open"] = &objects.Float{Value: tx.Open}
data["close"] = &objects.Float{Value: tx.Close}
data["pair"] = &objects.String{Value: tx.Pair.String()}
data["asset"] = &objects.String{Value: tx.AssetType.String()}
data["updated"] = &objects.Time{Value: tx.LastUpdated}
return &objects.Map{
Value: data,
}, nil
}
// ExchangeExchanges returns list of exchanges either enabled or all
func ExchangeExchanges(args ...objects.Object) (objects.Object, error) {
if len(args) != 1 {
return nil, objects.ErrWrongNumArguments
}
enabledOnly, ok := objects.ToBool(args[0])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, enabledOnly)
}
rtnValue := wrappers.GetWrapper().Exchanges(enabledOnly)
r := objects.Array{}
for x := range rtnValue {
r.Value = append(r.Value, &objects.String{Value: rtnValue[x]})
}
return &r, nil
}
// ExchangePairs returns currency pairs for requested exchange
func ExchangePairs(args ...objects.Object) (objects.Object, error) {
if len(args) != 3 {
return nil, objects.ErrWrongNumArguments
}
exchangeName, ok := objects.ToString(args[0])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, exchangeName)
}
enabledOnly, ok := objects.ToBool(args[1])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, enabledOnly)
}
assetTypeParam, ok := objects.ToString(args[2])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, assetTypeParam)
}
assetType := asset.Item(strings.ToLower(assetTypeParam))
rtnValue, err := wrappers.GetWrapper().Pairs(exchangeName, enabledOnly, assetType)
if err != nil {
return nil, err
}
r := objects.Array{}
for x := range rtnValue.Slice() {
r.Value = append(r.Value, &objects.String{Value: rtnValue.Slice()[x].String()})
}
return &r, nil
}
// ExchangeAccountInfo returns account information for requested exchange
func ExchangeAccountInfo(args ...objects.Object) (objects.Object, error) {
if len(args) != 1 {
return nil, objects.ErrWrongNumArguments
}
exchangeName, ok := objects.ToString(args[0])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, exchangeName)
}
rtnValue, err := wrappers.GetWrapper().AccountInformation(exchangeName)
if err != nil {
return nil, err
}
var funds objects.Array
for x := range rtnValue.Accounts {
for y := range rtnValue.Accounts[x].Currencies {
temp := make(map[string]objects.Object, 3)
temp["name"] = &objects.String{Value: rtnValue.Accounts[x].Currencies[y].CurrencyName.String()}
temp["total"] = &objects.Float{Value: rtnValue.Accounts[x].Currencies[y].TotalValue}
temp["hold"] = &objects.Float{Value: rtnValue.Accounts[x].Currencies[y].Hold}
funds.Value = append(funds.Value, &objects.Map{Value: temp})
}
}
data := make(map[string]objects.Object, 2)
data["exchange"] = &objects.String{Value: rtnValue.Exchange}
data["currencies"] = &funds
return &objects.Map{
Value: data,
}, nil
}
// ExchangeOrderQuery query order on exchange
func ExchangeOrderQuery(args ...objects.Object) (objects.Object, error) {
if len(args) != 2 {
return nil, objects.ErrWrongNumArguments
}
exchangeName, ok := objects.ToString(args[0])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, exchangeName)
}
orderID, ok := objects.ToString(args[1])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, orderID)
}
orderDetails, err := wrappers.GetWrapper().QueryOrder(exchangeName, orderID)
if err != nil {
return nil, err
}
var tradeHistory objects.Array
for x := range orderDetails.Trades {
temp := make(map[string]objects.Object, 7)
temp["timestamp"] = &objects.Time{Value: orderDetails.Trades[x].Timestamp}
temp["price"] = &objects.Float{Value: orderDetails.Trades[x].Price}
temp["fee"] = &objects.Float{Value: orderDetails.Trades[x].Fee}
temp["amount"] = &objects.Float{Value: orderDetails.Trades[x].Amount}
temp["type"] = &objects.String{Value: orderDetails.Trades[x].Type.String()}
temp["side"] = &objects.String{Value: orderDetails.Trades[x].Side.String()}
temp["description"] = &objects.String{Value: orderDetails.Trades[x].Description}
tradeHistory.Value = append(tradeHistory.Value, &objects.Map{Value: temp})
}
data := make(map[string]objects.Object, 14)
data["exchange"] = &objects.String{Value: orderDetails.Exchange}
data["id"] = &objects.String{Value: orderDetails.ID}
data["accountid"] = &objects.String{Value: orderDetails.AccountID}
data["currencypair"] = &objects.String{Value: orderDetails.CurrencyPair.String()}
data["price"] = &objects.Float{Value: orderDetails.Price}
data["amount"] = &objects.Float{Value: orderDetails.Amount}
data["amountexecuted"] = &objects.Float{Value: orderDetails.ExecutedAmount}
data["amountremaining"] = &objects.Float{Value: orderDetails.RemainingAmount}
data["fee"] = &objects.Float{Value: orderDetails.Fee}
data["side"] = &objects.String{Value: orderDetails.OrderSide.String()}
data["type"] = &objects.String{Value: orderDetails.OrderType.String()}
data["date"] = &objects.String{Value: orderDetails.OrderDate.String()}
data["status"] = &objects.String{Value: orderDetails.Status.String()}
data["trades"] = &tradeHistory
return &objects.Map{
Value: data,
}, nil
}
// ExchangeOrderCancel cancels order on requested exchange
func ExchangeOrderCancel(args ...objects.Object) (objects.Object, error) {
if len(args) != 2 {
return nil, objects.ErrWrongNumArguments
}
exchangeName, ok := objects.ToString(args[0])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, exchangeName)
}
orderID, ok := objects.ToString(args[1])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, orderID)
}
rtn, err := wrappers.GetWrapper().CancelOrder(exchangeName, orderID)
if err != nil {
return nil, err
}
if rtn {
return objects.TrueValue, nil
}
return objects.FalseValue, nil
}
// ExchangeOrderSubmit submit order on exchange
func ExchangeOrderSubmit(args ...objects.Object) (objects.Object, error) {
if len(args) != 8 {
return nil, objects.ErrWrongNumArguments
}
exchangeName, ok := objects.ToString(args[0])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, exchangeName)
}
currencyPair, ok := objects.ToString(args[1])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, currencyPair)
}
delimiter, ok := objects.ToString(args[2])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, delimiter)
}
orderType, ok := objects.ToString(args[3])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, orderType)
}
orderSide, ok := objects.ToString(args[4])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, orderSide)
}
orderPrice, ok := objects.ToFloat64(args[5])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, orderPrice)
}
orderAmount, ok := objects.ToFloat64(args[6])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, orderAmount)
}
orderClientID, ok := objects.ToString(args[7])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, orderClientID)
}
pair := currency.NewPairDelimiter(currencyPair, delimiter)
tempSubmit := &order.Submit{
Pair: pair,
OrderType: order.Type(orderType),
OrderSide: order.Side(orderSide),
Price: orderPrice,
Amount: orderAmount,
ClientID: orderClientID,
}
err := tempSubmit.Validate()
if err != nil {
return nil, err
}
rtn, err := wrappers.GetWrapper().SubmitOrder(exchangeName, tempSubmit)
if err != nil {
return nil, err
}
data := make(map[string]objects.Object, 2)
data["orderid"] = &objects.String{Value: rtn.OrderID}
if rtn.IsOrderPlaced {
data["isorderplaced"] = objects.TrueValue
} else {
data["isorderplaced"] = objects.FalseValue
}
return &objects.Map{
Value: data,
}, nil
}
// ExchangeDepositAddress returns deposit address (if supported by exchange)
func ExchangeDepositAddress(args ...objects.Object) (objects.Object, error) {
if len(args) != 2 {
return nil, objects.ErrWrongNumArguments
}
exchangeName, ok := objects.ToString(args[0])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, exchangeName)
}
currencyCode, ok := objects.ToString(args[1])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, currencyCode)
}
currCode := currency.NewCode(currencyCode)
rtn, err := wrappers.GetWrapper().DepositAddress(exchangeName, currCode)
if err != nil {
return nil, err
}
return &objects.String{Value: rtn}, nil
}
// ExchangeWithdrawCrypto submit request to withdraw crypto assets
func ExchangeWithdrawCrypto(args ...objects.Object) (objects.Object, error) {
if len(args) != 7 {
return nil, objects.ErrWrongNumArguments
}
exchangeName, ok := objects.ToString(args[0])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, exchangeName)
}
cur, ok := objects.ToString(args[1])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, cur)
}
address, ok := objects.ToString(args[2])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, address)
}
addressTag, ok := objects.ToString(args[3])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, addressTag)
}
amount, ok := objects.ToFloat64(args[4])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, amount)
}
feeAmount, ok := objects.ToFloat64(args[5])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, feeAmount)
}
description, ok := objects.ToString(args[6])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, description)
}
withdrawRequest := &withdraw.CryptoRequest{
GenericInfo: withdraw.GenericInfo{
Currency: currency.NewCode(cur),
Description: description,
Amount: amount,
},
Address: address,
AddressTag: addressTag,
FeeAmount: feeAmount,
}
rtn, err := wrappers.GetWrapper().WithdrawalCryptoFunds(exchangeName, withdrawRequest)
if err != nil {
return nil, err
}
return &objects.String{Value: rtn}, nil
}
// ExchangeWithdrawFiat submit request to withdraw fiat assets
func ExchangeWithdrawFiat(args ...objects.Object) (objects.Object, error) {
if len(args) != 5 {
return nil, objects.ErrWrongNumArguments
}
exchangeName, ok := objects.ToString(args[0])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, exchangeName)
}
cur, ok := objects.ToString(args[1])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, cur)
}
description, ok := objects.ToString(args[2])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, description)
}
amount, ok := objects.ToFloat64(args[3])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, amount)
}
bankAccountID, ok := objects.ToString(args[4])
if !ok {
return nil, fmt.Errorf(ErrParameterConvertFailed, bankAccountID)
}
withdrawRequest := &withdraw.FiatRequest{
GenericInfo: withdraw.GenericInfo{
Currency: currency.NewCode(cur),
Description: description,
Amount: amount,
},
}
rtn, err := wrappers.GetWrapper().WithdrawalFiatFunds(exchangeName, bankAccountID, withdrawRequest)
if err != nil {
return nil, err
}
return &objects.String{Value: rtn}, nil
}

View File

@@ -0,0 +1,10 @@
package gct
// AllModuleNames returns a list of all default module names.
func AllModuleNames() []string {
var names []string
for name := range Modules {
names = append(names, name)
}
return names
}

View File

@@ -0,0 +1,270 @@
package gct
import (
"errors"
"os"
"reflect"
"testing"
objects "github.com/d5/tengo/v2"
"github.com/thrasher-corp/gocryptotrader/gctscript/modules"
"github.com/thrasher-corp/gocryptotrader/gctscript/wrappers/validator"
)
var (
exch = &objects.String{
Value: "BTC Markets",
}
exchError = &objects.String{
Value: "error",
}
currencyPair = &objects.String{
Value: "BTC-AUD",
}
delimiter = &objects.String{
Value: "-",
}
assetType = &objects.String{
Value: "SPOT",
}
orderID = &objects.String{
Value: "1235",
}
tv = objects.TrueValue
fv = objects.FalseValue
errTestFailed = errors.New("test failed")
)
func TestMain(m *testing.M) {
modules.SetModuleWrapper(validator.Wrapper{})
os.Exit(m.Run())
}
func TestExchangeOrderbook(t *testing.T) {
t.Parallel()
_, err := ExchangeOrderbook(exch, currencyPair, delimiter, assetType)
if err != nil {
t.Fatal(err)
}
_, err = ExchangeOrderbook(exchError, currencyPair, delimiter, assetType)
if err != nil && errors.Is(err, errTestFailed) {
t.Fatal(err)
}
_, err = ExchangeOrderbook()
if !errors.Is(err, objects.ErrWrongNumArguments) {
t.Fatal(err)
}
}
func TestExchangeTicker(t *testing.T) {
t.Parallel()
_, err := ExchangeTicker(exch, currencyPair, delimiter, assetType)
if err != nil {
t.Fatal(err)
}
_, err = ExchangeTicker(exchError, currencyPair, delimiter, assetType)
if err != nil && errors.Is(err, errTestFailed) {
t.Fatal(err)
}
_, err = ExchangeTicker()
if !errors.Is(err, objects.ErrWrongNumArguments) {
t.Fatal(err)
}
}
func TestExchangeExchanges(t *testing.T) {
t.Parallel()
_, err := ExchangeExchanges(tv)
if err != nil {
t.Fatal(err)
}
_, err = ExchangeExchanges(exch)
if err != nil {
t.Fatal(err)
}
_, err = ExchangeExchanges(fv)
if err != nil {
t.Fatal(err)
}
_, err = ExchangeExchanges()
if !errors.Is(err, objects.ErrWrongNumArguments) {
t.Fatal(err)
}
}
func TestExchangePairs(t *testing.T) {
t.Parallel()
_, err := ExchangePairs(exch, tv, assetType)
if err != nil {
t.Fatal(err)
}
_, err = ExchangePairs(exchError, tv, assetType)
if err != nil && errors.Is(err, errTestFailed) {
t.Fatal(err)
}
_, err = ExchangePairs()
if !errors.Is(err, objects.ErrWrongNumArguments) {
t.Fatal(err)
}
}
func TestAccountInfo(t *testing.T) {
t.Parallel()
_, err := ExchangeAccountInfo()
if !errors.Is(err, objects.ErrWrongNumArguments) {
t.Fatal(err)
}
_, err = ExchangeAccountInfo(exch)
if err != nil {
t.Fatal(err)
}
_, err = ExchangeAccountInfo(exchError)
if err != nil && !errors.Is(err, errTestFailed) {
t.Fatal(err)
}
}
func TestExchangeOrderQuery(t *testing.T) {
t.Parallel()
_, err := ExchangeOrderQuery()
if !errors.Is(err, objects.ErrWrongNumArguments) {
t.Fatal(err)
}
_, err = ExchangeOrderQuery(exch, orderID)
if err != nil {
t.Fatal(err)
}
_, err = ExchangeOrderQuery(exchError, orderID)
if err != nil && !errors.Is(err, errTestFailed) {
t.Fatal(err)
}
}
func TestExchangeOrderCancel(t *testing.T) {
_, err := ExchangeOrderCancel()
if !errors.Is(err, objects.ErrWrongNumArguments) {
t.Fatal(err)
}
_, err = ExchangeOrderCancel(exch, orderID)
if err != nil {
t.Fatal(err)
}
_, err = ExchangeOrderCancel(exch, objects.FalseValue)
if err != nil {
t.Fatal(err)
}
_, err = ExchangeOrderCancel(exchError, orderID)
if err != nil && !errors.Is(err, errTestFailed) {
t.Fatal(err)
}
}
func TestExchangeOrderSubmit(t *testing.T) {
_, err := ExchangeOrderSubmit()
if !errors.Is(err, objects.ErrWrongNumArguments) {
t.Fatal(err)
}
orderSide := &objects.String{Value: "ASK"}
orderType := &objects.String{Value: "LIMIT"}
orderPrice := &objects.Float{Value: 1}
orderAmount := &objects.Float{Value: 1}
_, err = ExchangeOrderSubmit(exch, currencyPair, delimiter,
orderType, orderSide, orderPrice, orderAmount, orderID)
if err != nil && !errors.Is(err, errTestFailed) {
t.Fatal(err)
}
_, err = ExchangeOrderSubmit(exch, currencyPair, delimiter,
orderType, orderSide, orderPrice, orderAmount, orderID)
if err != nil {
t.Fatal(err)
}
_, err = ExchangeOrderSubmit(objects.TrueValue, currencyPair, delimiter,
orderType, orderSide, orderPrice, orderAmount, orderID)
if err != nil {
t.Fatal(err)
}
}
func TestAllModuleNames(t *testing.T) {
x := AllModuleNames()
xType := reflect.TypeOf(x).Kind()
if xType != reflect.Slice {
t.Fatalf("AllModuleNames() should return slice instead received: %v", x)
}
}
func TestExchangeDepositAddress(t *testing.T) {
_, err := ExchangeDepositAddress()
if !errors.Is(err, objects.ErrWrongNumArguments) {
t.Fatal(err)
}
currCode := &objects.String{Value: "BTC"}
_, err = ExchangeDepositAddress(exch, currCode)
if err != nil {
t.Fatal(err)
}
_, err = ExchangeDepositAddress(exchError, currCode)
if err != nil && !errors.Is(err, errTestFailed) {
t.Fatal(err)
}
}
func TestExchangeWithdrawCrypto(t *testing.T) {
_, err := ExchangeWithdrawCrypto()
if !errors.Is(err, objects.ErrWrongNumArguments) {
t.Fatal(err)
}
currCode := &objects.String{Value: "BTC"}
desc := &objects.String{Value: "HELLO"}
address := &objects.String{Value: "0xTHISISALEGITBTCADDRESSS"}
amount := &objects.Float{Value: 1.0}
_, err = ExchangeWithdrawCrypto(exch, currCode, address, address, amount, amount, desc)
if err != nil {
t.Fatal(err)
}
}
func TestExchangeWithdrawFiat(t *testing.T) {
_, err := ExchangeWithdrawFiat()
if !errors.Is(err, objects.ErrWrongNumArguments) {
t.Fatal(err)
}
currCode := &objects.String{Value: "AUD"}
desc := &objects.String{Value: "Hello"}
amount := &objects.Float{Value: 1.0}
bankID := &objects.String{Value: "test-bank-01"}
_, err = ExchangeWithdrawFiat(exch, currCode, desc, amount, bankID)
if err != nil {
t.Fatal(err)
}
}

View File

@@ -0,0 +1,15 @@
package gct
import (
"github.com/d5/tengo/v2"
)
const (
// ErrParameterConvertFailed error to return when type conversion fails
ErrParameterConvertFailed = "%v failed conversion"
)
// Modules map of all loadable modules
var Modules = map[string]map[string]tengo.Object{
"exchange": exchangeModule,
}

View File

@@ -0,0 +1,32 @@
package loader
import (
"github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/stdlib"
"github.com/thrasher-corp/gocryptotrader/gctscript/modules/gct"
)
// GetModuleMap returns the module map that includes all modules
// for the given module names.
func GetModuleMap() *tengo.ModuleMap {
modules := tengo.NewModuleMap()
gctModuleList := gct.AllModuleNames()
for _, name := range gctModuleList {
if mod := gct.Modules[name]; mod != nil {
modules.AddBuiltinModule(name, mod)
}
}
stdLib := stdlib.AllModuleNames()
for _, name := range stdLib {
if mod := stdlib.BuiltinModules[name]; mod != nil {
modules.AddBuiltinModule(name, mod)
}
if mod := stdlib.SourceModules[name]; mod != "" {
modules.AddSourceModule(name, []byte(mod))
}
}
return modules
}

View File

@@ -0,0 +1,18 @@
package loader
import (
"reflect"
"testing"
)
func TestGetModuleMap(t *testing.T) {
x := GetModuleMap()
xType := reflect.TypeOf(x).String()
if xType != "*tengo.ModuleMap" {
t.Fatalf("GetModuleMap() should return pointer to ModuleMap instead received: %v", x)
}
if x.Len() == 0 {
t.Fatal("expected GetModuleMap() to contain module results instead received 0 value")
}
}

View File

@@ -0,0 +1,59 @@
package modules
import (
"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"
)
// Wrapper instance of GCT to use for modules
var Wrapper GCT
// GCT interface requirements
type GCT interface {
Exchange
}
// Exchange interface requirements
type Exchange interface {
Exchanges(enabledOnly bool) []string
IsEnabled(exch string) bool
Orderbook(exch string, pair currency.Pair, item asset.Item) (*orderbook.Base, error)
Ticker(exch string, pair currency.Pair, item asset.Item) (*ticker.Price, error)
Pairs(exch string, enabledOnly bool, item asset.Item) (*currency.Pairs, error)
QueryOrder(exch, orderid string) (*order.Detail, error)
SubmitOrder(exch string, submit *order.Submit) (*order.SubmitResponse, error)
CancelOrder(exch, orderid string) (bool, error)
AccountInformation(exch string) (*AccountInfo, error)
DepositAddress(exch string, currencyCode currency.Code) (string, error)
WithdrawalFiatFunds(exch, bankaccountid string, request *withdraw.FiatRequest) (out string, err error)
WithdrawalCryptoFunds(exch string, request *withdraw.CryptoRequest) (out string, err error)
}
// SetModuleWrapper link the wrapper and interface to use for modules
func SetModuleWrapper(wrapper GCT) {
Wrapper = wrapper
}
// AccountInfo is a Generic type to hold each exchange's holdings in
// all enabled currencies
type AccountInfo struct {
Exchange string
Accounts []Account
}
// Account defines a singular account type with associated currencies
type Account struct {
ID string
Currencies []AccountCurrencyInfo
}
// AccountCurrencyInfo is a sub type to store currency name and value
type AccountCurrencyInfo struct {
CurrencyName currency.Code
TotalValue float64
Hold float64
}

43
gctscript/vm/autoload.go Normal file
View File

@@ -0,0 +1,43 @@
package vm
import (
"fmt"
"os"
"path/filepath"
log "github.com/thrasher-corp/gocryptotrader/logger"
)
// Autoload remove entry from autoload slice
func Autoload(name string, remove bool) error {
if filepath.Ext(name) != ".gct" {
name += ".gct"
}
if remove {
for x := range GCTScriptConfig.AutoLoad {
if GCTScriptConfig.AutoLoad[x] != name {
continue
}
GCTScriptConfig.AutoLoad = append(GCTScriptConfig.AutoLoad[:x], GCTScriptConfig.AutoLoad[x+1:]...)
if GCTScriptConfig.Verbose {
log.Debugf(log.GCTScriptMgr, "Removing script: %s from autoload", name)
}
return nil
}
return fmt.Errorf("%v - not found", name)
}
script := filepath.Join(ScriptPath, name)
_, err := os.Stat(script)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("%v - not found", script)
}
return err
}
GCTScriptConfig.AutoLoad = append(GCTScriptConfig.AutoLoad, name)
if GCTScriptConfig.Verbose {
log.Debugf(log.GCTScriptMgr, "Adding script: %s to autoload", name)
}
return nil
}

81
gctscript/vm/gctscript.go Normal file
View File

@@ -0,0 +1,81 @@
package vm
import (
"fmt"
"github.com/gofrs/uuid"
"github.com/thrasher-corp/gocryptotrader/gctscript/wrappers/validator"
log "github.com/thrasher-corp/gocryptotrader/logger"
)
// New returns a new instance of VM
func New() *VM {
if VMSCount.Len() >= int32(GCTScriptConfig.MaxVirtualMachines) {
if GCTScriptConfig.Verbose {
log.Warnf(log.GCTScriptMgr, "GCTScript MaxVirtualMachines (%v) hit, unable to start further instances",
GCTScriptConfig.MaxVirtualMachines)
}
return nil
}
VMSCount.add()
vm := NewVM()
if vm == nil {
VMSCount.remove()
} else {
AllVMSync.Store(vm.ID, vm)
}
return vm
}
// Validate will attempt to execute a script in a test/non-live environment
// to confirm it passes requirements for execution
func Validate(file string) (err error) {
validator.IsTestExecution.Store(true)
defer validator.IsTestExecution.Store(false)
tempVM := NewVM()
err = tempVM.Load(file)
if err != nil {
return
}
err = tempVM.Compile()
if err != nil {
return
}
return tempVM.Run()
}
// ShutdownAll shutdown all
func ShutdownAll() (err error) {
if GCTScriptConfig.Verbose {
log.Debugln(log.GCTScriptMgr, "Shutting down all Virtual Machines")
}
var errors []error
AllVMSync.Range(func(k, v interface{}) bool {
errShutdown := v.(*VM).Shutdown()
if err != nil {
errors = append(errors, errShutdown)
}
return true
})
if len(errors) > 0 {
err = fmt.Errorf("failed to shutdown the following Virtual Machines: %v", errors)
}
return err
}
// RemoveVM remove VM from list
func RemoveVM(id uuid.UUID) error {
if _, f := AllVMSync.Load(id); !f {
return fmt.Errorf(ErrNoVMFound, id.String())
}
AllVMSync.Delete(id)
VMSCount.remove()
if GCTScriptConfig.Verbose {
log.Debugf(log.GCTScriptMgr, "VM %v removed from AllVMs", id)
}
return nil
}

View File

@@ -0,0 +1,39 @@
package vm
import (
"errors"
"time"
)
const gctScript = "GCT Script"
// Config user configurable options for gctscript
type Config struct {
Enabled bool `json:"enabled"`
ScriptTimeout time.Duration `json:"timeout"`
MaxVirtualMachines uint8 `json:"max_virtual_machines"`
AllowImports bool `json:"allow_imports"`
AutoLoad []string `json:"auto_load"`
Verbose bool `json:"verbose"`
}
// Error interface to meet error requirements
type Error struct {
Script string
Action string
Cause error
}
var (
// GCTScriptConfig initialised global copy of Config{}
GCTScriptConfig = &Config{}
// ScriptPath path to load/save scripts
ScriptPath string
)
var (
// ErrScriptingDisabled error message displayed when gctscript is disabled
ErrScriptingDisabled = errors.New("scripting is disabled")
// ErrNoVMLoaded error message displayed if a virtual machine has not been initialised
ErrNoVMLoaded = errors.New("no virtual machine loaded")
)

302
gctscript/vm/vm.go Normal file
View File

@@ -0,0 +1,302 @@
package vm
import (
"archive/zip"
"bytes"
"context"
"encoding/hex"
"io/ioutil"
"os"
"path/filepath"
"sync/atomic"
"time"
"github.com/d5/tengo/v2"
"github.com/gofrs/uuid"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
scriptevent "github.com/thrasher-corp/gocryptotrader/database/repository/script"
"github.com/thrasher-corp/gocryptotrader/gctscript/modules/loader"
"github.com/thrasher-corp/gocryptotrader/gctscript/wrappers/validator"
log "github.com/thrasher-corp/gocryptotrader/logger"
"github.com/volatiletech/null"
)
// NewVM attempts to create a new Virtual Machine firstly from pool
func NewVM() (vm *VM) {
newUUID, err := uuid.NewV4()
if err != nil {
log.Error(log.GCTScriptMgr, Error{
Action: "New: UUID",
Cause: err,
})
return nil
}
if GCTScriptConfig.Verbose {
log.Debugln(log.GCTScriptMgr, "New GCTScript VM created")
}
vm = &VM{
ID: newUUID,
Script: pool.Get().(*tengo.Script),
}
return
}
// Load parses and creates a new instance of tengo script vm
func (vm *VM) Load(file string) error {
if vm == nil {
return ErrNoVMLoaded
}
if !GCTScriptConfig.Enabled {
return &Error{
Action: "Load",
Cause: ErrScriptingDisabled,
}
}
if filepath.Ext(file) != ".gct" {
file += ".gct"
}
if GCTScriptConfig.Verbose {
log.Debugf(log.GCTScriptMgr, "Loading script: %s ID: %v", vm.ShortName(), vm.ID)
}
f, err := os.Open(file)
if err != nil {
return &Error{
Action: "Load: Open",
Script: file,
Cause: err,
}
}
defer f.Close()
code, err := ioutil.ReadAll(f)
if err != nil {
return &Error{
Action: "Load: Read",
Script: file,
Cause: err,
}
}
vm.File = file
vm.Path = filepath.Dir(file)
vm.Script = tengo.NewScript(code)
vm.Script.SetImports(loader.GetModuleMap())
vm.Hash = vm.getHash()
if GCTScriptConfig.AllowImports {
if GCTScriptConfig.Verbose {
log.Debugf(log.GCTScriptMgr, "File imports enabled for vm: %v", vm.ID)
}
vm.Script.EnableFileImport(true)
}
vm.event(StatusSuccess, TypeLoad)
return nil
}
// Compile compiles to byte code loaded copy of vm script
func (vm *VM) Compile() (err error) {
vm.Compiled = new(tengo.Compiled)
vm.Compiled, err = vm.Script.Compile()
return
}
// Run runs byte code
func (vm *VM) Run() (err error) {
if GCTScriptConfig.Verbose {
log.Debugf(log.GCTScriptMgr, "Running script: %s ID: %v", vm.ShortName(), vm.ID)
}
err = vm.Compiled.Run()
if err != nil {
vm.event(StatusFailure, TypeExecute)
return Error{
Action: "Run",
Cause: err,
}
}
vm.event(StatusSuccess, TypeExecute)
return
}
// RunCtx runs compiled byte code with context.Context support.
func (vm *VM) RunCtx() (err error) {
if vm.ctx == nil {
vm.ctx = context.Background()
}
ct, cancel := context.WithTimeout(vm.ctx, GCTScriptConfig.ScriptTimeout)
defer cancel()
if GCTScriptConfig.Verbose {
log.Debugf(log.GCTScriptMgr, "Running script: %s ID: %v", vm.ShortName(), vm.ID)
}
err = vm.Compiled.RunContext(ct)
if err != nil {
vm.event(StatusFailure, TypeExecute)
return Error{
Action: "RunCtx",
Cause: err,
}
}
vm.event(StatusSuccess, TypeExecute)
return
}
// CompileAndRun Compile and Run script with support for task running
func (vm *VM) CompileAndRun() {
if vm == nil {
return
}
err := vm.Compile()
if err != nil {
log.Error(log.GCTScriptMgr, err)
err = RemoveVM(vm.ID)
if err != nil {
log.Error(log.GCTScriptMgr, err)
}
return
}
err = vm.RunCtx()
if err != nil {
log.Error(log.GCTScriptMgr, err)
err = RemoveVM(vm.ID)
if err != nil {
log.Error(log.GCTScriptMgr, err)
}
return
}
if vm.Compiled.Get("timer").String() != "" {
vm.T, err = time.ParseDuration(vm.Compiled.Get("timer").String())
if err != nil {
log.Error(log.GCTScriptMgr, err)
err = vm.Shutdown()
if err != nil {
log.Error(log.GCTScriptMgr, err)
}
return
}
if vm.T < time.Nanosecond {
log.Error(log.GCTScriptMgr, "Repeat timer cannot be under 1 nano second")
err = vm.Shutdown()
if err != nil {
log.Errorln(log.GCTScriptMgr, err)
}
return
}
vm.runner()
} else {
err = vm.Shutdown()
if err != nil {
log.Error(log.GCTScriptMgr, err)
}
return
}
}
// Shutdown shuts down current VM
func (vm *VM) Shutdown() error {
if vm == nil {
return ErrNoVMLoaded
}
if vm.S != nil {
close(vm.S)
}
if GCTScriptConfig.Verbose {
log.Debugf(log.GCTScriptMgr, "Shutting down script: %s ID: %v", vm.ShortName(), vm.ID)
}
vm.Script = nil
pool.Put(vm.Script)
vm.event(StatusSuccess, TypeStop)
return RemoveVM(vm.ID)
}
// Read contents of script back and create script event
func (vm *VM) Read() ([]byte, error) {
vm.event(StatusSuccess, TypeRead)
return vm.read()
}
// Read contents of script back
func (vm *VM) read() ([]byte, error) {
if GCTScriptConfig.Verbose {
log.Debugf(log.GCTScriptMgr, "Read script: %s ID: %v", vm.ShortName(), vm.ID)
}
return ioutil.ReadFile(vm.File)
}
// ShortName returns short (just filename.extension) of running script
func (vm *VM) ShortName() string {
return filepath.Base(vm.File)
}
func (vm *VM) event(status, executionType string) {
if validator.IsTestExecution.Load() == true {
return
}
var data null.Bytes
if executionType == TypeLoad {
scriptData, err := vm.scriptData()
if err != nil {
log.Errorf(log.GCTScriptMgr, "Failed to retrieve scriptData: %v", err)
}
data.SetValid(scriptData)
}
scriptevent.Event(vm.getHash(), vm.ShortName(), vm.Path, data, executionType, status, time.Now())
}
func (vm *VM) scriptData() ([]byte, error) {
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
f, err := w.Create(vm.ShortName())
if err != nil {
return []byte{}, err
}
contents, err := vm.read()
if err != nil {
return []byte{}, err
}
_, err = f.Write(contents)
if err != nil {
return []byte{}, err
}
err = w.Close()
if err != nil {
return []byte{}, err
}
return buf.Bytes(), nil
}
func (vm *VM) getHash() string {
if vm.Hash != "" {
return vm.Hash
}
contents, err := vm.read()
if err != nil {
log.Errorln(log.GCTScriptMgr, err)
}
contents = append(contents, vm.ShortName()...)
return hex.EncodeToString(crypto.GetSHA256(contents))
}
func (vmc *vmscount) add() {
atomic.AddInt32((*int32)(vmc), 1)
}
func (vmc *vmscount) remove() {
atomic.AddInt32((*int32)(vmc), -1)
}
// Len() returns current length vmscount
func (vmc *vmscount) Len() int32 {
return atomic.LoadInt32((*int32)(vmc))
}

29
gctscript/vm/vm_error.go Normal file
View File

@@ -0,0 +1,29 @@
package vm
import (
"fmt"
"path/filepath"
)
const (
// ErrNoVMFound error returned when no virtual machine is found
ErrNoVMFound = "VM %v not found"
)
func (e Error) Error() string {
var scriptName, action string
if e.Script != "" {
scriptName = fmt.Sprintf("(SCRIPT) %s ", filepath.Base(e.Script))
}
if e.Action != "" {
action = fmt.Sprintf("(ACTION) %s ", e.Action)
}
return fmt.Sprintf("%s: %s%s%s", gctScript, action, scriptName, e.Cause)
}
// Unwrap returns e.Cause meeting errors interface requirements.
func (e Error) Unwrap() error {
return e.Cause
}

30
gctscript/vm/vm_task.go Normal file
View File

@@ -0,0 +1,30 @@
package vm
import (
"time"
log "github.com/thrasher-corp/gocryptotrader/logger"
)
func (vm *VM) runner() {
vm.S = make(chan struct{}, 1)
waitTime := time.NewTicker(vm.T)
vm.NextRun = time.Now().Add(vm.T)
go func() {
for {
select {
case <-waitTime.C:
vm.NextRun = time.Now().Add(vm.T)
err := vm.RunCtx()
if err != nil {
log.Error(log.GCTScriptMgr, err)
return
}
case <-vm.S:
waitTime.Stop()
return
}
}
}()
}

478
gctscript/vm/vm_test.go Normal file
View File

@@ -0,0 +1,478 @@
package vm
import (
"errors"
"os"
"path/filepath"
"reflect"
"testing"
"time"
"github.com/gofrs/uuid"
"github.com/thrasher-corp/gocryptotrader/common/convert"
"github.com/thrasher-corp/gocryptotrader/logger"
)
const (
maxTestVirtualMachines uint8 = 30
testVirtualMachineTimeout = time.Minute
scriptName = "1D01TH0RS3.gct"
)
var (
testScript = filepath.Join("..", "..", "testdata", "gctscript", "once.gct")
testInvalidScript = filepath.Join("..", "..", "testdata", "gctscript", "invalid.gct")
testBrokenScript = filepath.Join("..", "..", "testdata", "gctscript", "broken.gct")
testScriptRunner = filepath.Join("..", "..", "testdata", "gctscript", "timer.gct")
testScriptRunner1s = filepath.Join("..", "..", "testdata", "gctscript", "1s_timer.gct")
testScriptRunnerInvalid = filepath.Join("..", "..", "testdata", "gctscript", "invalid_timer.gct")
testScriptRunnerNegative = filepath.Join("..", "..", "testdata", "gctscript", "negative_timer.gct")
)
func TestMain(m *testing.M) {
c := logger.GenDefaultSettings()
c.Enabled = convert.BoolPtr(false)
logger.GlobalLogConfig = &c
GCTScriptConfig = configHelper(true, true, maxTestVirtualMachines)
os.Exit(m.Run())
}
func TestNewVM(t *testing.T) {
x := New()
xType := reflect.TypeOf(x).String()
if xType != "*vm.VM" {
t.Fatalf("vm.New should return pointer to VM instead received: %v", x)
}
}
func TestVMLoad(t *testing.T) {
GCTScriptConfig = configHelper(true, true, maxTestVirtualMachines)
testVM := New()
err := testVM.Load(testScript)
if err != nil {
t.Fatal(err)
}
testScript = testScript[0 : len(testScript)-4]
testVM = New()
err = testVM.Load(testScript)
if err != nil {
t.Fatal(err)
}
GCTScriptConfig = configHelper(false, false, maxTestVirtualMachines)
err = testVM.Load(testScript)
if err != nil {
if !errors.Is(err, ErrScriptingDisabled) {
t.Fatal(err)
}
}
GCTScriptConfig = configHelper(true, true, maxTestVirtualMachines)
}
func TestVMLoad1s(t *testing.T) {
testVM := New()
err := testVM.Load(testScriptRunner1s)
if err != nil {
t.Fatal(err)
}
testVM.CompileAndRun()
time.Sleep(5000)
err = testVM.Shutdown()
if err != nil {
if !errors.Is(err, ErrNoVMLoaded) {
t.Fatal(err)
}
}
}
func TestVMLoadNegativeTimer(t *testing.T) {
testVM := New()
err := testVM.Load(testScriptRunnerNegative)
if err != nil {
if !errors.Is(err, ErrNoVMLoaded) {
t.Fatal(err)
}
}
testVM.CompileAndRun()
err = testVM.Shutdown()
if err == nil {
t.Fatal("expect error on shutdown due to invalid VM")
}
}
func TestVMLoadNilVM(t *testing.T) {
testVM := New()
err := testVM.Load(testScript)
if err != nil {
if !errors.Is(err, ErrNoVMLoaded) {
t.Fatal(err)
}
}
testVM = nil
err = testVM.Load(testScript)
if err != nil {
if !errors.Is(err, ErrNoVMLoaded) {
t.Fatal(err)
}
}
}
func TestCompileAndRunNilVM(t *testing.T) {
vmcount := VMSCount.Len()
testVM := New()
err := testVM.Load(testScript)
if err != nil {
if !errors.Is(err, ErrNoVMLoaded) {
t.Fatal(err)
}
}
err = testVM.Load(testScript)
if err != nil {
if !errors.Is(err, ErrNoVMLoaded) {
t.Fatal(err)
}
}
testVM = nil
testVM.CompileAndRun()
err = testVM.Shutdown()
if err == nil {
t.Fatal("VM should not be running with invalid timer")
}
if VMSCount.Len() == vmcount-1 {
t.Fatal("expected VM count to decrease")
}
}
func TestVMLoadNoFile(t *testing.T) {
testVM := New()
err := testVM.Load("missing file")
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
t.Fatal(err)
}
}
}
func TestVMCompile(t *testing.T) {
testVM := New()
err := testVM.Load(testScript)
if err != nil {
t.Fatal(err)
}
err = testVM.Compile()
if err != nil {
t.Fatal(err)
}
}
func TestVMRun(t *testing.T) {
testVM := NewVM()
err := testVM.Load(testScript)
if err != nil {
t.Fatal(err)
}
err = testVM.Compile()
if err != nil {
t.Fatal(err)
}
err = testVM.Run()
if err != nil {
t.Fatal(err)
}
}
func TestVMRunTX(t *testing.T) {
testVM := NewVM()
err := testVM.Load(testScript)
if err != nil {
t.Fatal(err)
}
err = testVM.Compile()
if err != nil {
t.Fatal(err)
}
err = testVM.RunCtx()
if err != nil {
t.Fatal(err)
}
}
func TestVMWithRunner(t *testing.T) {
vmCount := VMSCount.Len()
VM := New()
if VM == nil {
t.Fatal("Failed to allocate new VM exiting")
}
err := VM.Load(testScriptRunner)
if err != nil {
t.Fatal(err)
}
if VMSCount.Len() == vmCount {
t.Fatal("expected VM count to increase")
}
VM.CompileAndRun()
err = VM.Shutdown()
if err != nil {
t.Fatal(err)
}
if VMSCount.Len() == vmCount-1 {
t.Fatal("expected VM count to decrease")
}
}
func TestVMWithRunnerOnce(t *testing.T) {
vmCount := VMSCount.Len()
VM := New()
if VM == nil {
t.Fatal("Failed to allocate new VM exiting")
}
err := VM.Load(testScript)
if err != nil {
t.Fatal(err)
}
if VMSCount.Len() == vmCount {
t.Fatal("expected VM count to increase")
}
VM.CompileAndRun()
err = VM.Shutdown()
if err == nil {
t.Fatal("VM should not be running with invalid timer")
}
}
func TestVMWithRunnerNegativeTimer(t *testing.T) {
vmCount := VMSCount.Len()
VM := New()
if VM == nil {
t.Fatal("Failed to allocate new VM exiting")
}
err := VM.Load(testScriptRunnerNegative)
if err != nil {
t.Fatal(err)
}
if VMSCount.Len() == vmCount {
t.Fatal("expected VM count to increase")
}
VM.CompileAndRun()
err = VM.Shutdown()
if err == nil {
t.Fatal("VM should not be running with invalid timer")
}
if VMSCount.Len() == vmCount-1 {
t.Fatal("expected VM count to decrease")
}
}
func TestShutdownAll(t *testing.T) {
vmCount := VMSCount.Len()
VM := New()
err := VM.Load(testScriptRunner)
if err != nil {
t.Fatal(err)
}
VM.CompileAndRun()
if VMSCount.Len() == vmCount {
t.Fatal("expected VM count to increase")
}
err = ShutdownAll()
if err != nil {
t.Fatal(err)
}
if VMSCount.Len() == vmCount-1 {
t.Fatal("expected VM count to decrease")
}
}
func TestRead(t *testing.T) {
VM := NewVM()
err := VM.Load(testScriptRunner)
if err != nil {
t.Fatal(err)
}
ScriptPath = filepath.Join("..", "..", "testdata", "gctscript")
data, err := VM.Read()
if err != nil {
t.Fatal(err)
}
if len(data) < 1 {
t.Fatal("expected data to be returned")
}
_ = VM.Shutdown()
}
func TestRemoveVM(t *testing.T) {
id, _ := uuid.FromString("6f20c907-64a0-48f2-848a-7837dee61672")
err := RemoveVM(id)
if err != nil {
if err.Error() != "VM 6f20c907-64a0-48f2-848a-7837dee61672 not found" {
t.Fatal(err)
}
}
}
func TestError_Error(t *testing.T) {
x := Error{
Script: "noscript.gct",
Action: "test",
Cause: errors.New("HELLO ERROR"),
}
if x.Error() != "GCT Script: (ACTION) test (SCRIPT) noscript.gct HELLO ERROR" {
t.Fatal(x.Error())
}
}
func TestVM_CompileInvalid(t *testing.T) {
testVM := New()
err := testVM.Load(testInvalidScript)
if err != nil {
t.Fatal(err)
}
err = testVM.Compile()
if err != nil {
t.Fatal(err)
}
err = testVM.Run()
if err == nil {
t.Fatal("unexpected result broken script compiled successfully ")
}
testVM = New()
err = testVM.Load(testInvalidScript)
if err != nil {
t.Fatal(err)
}
err = testVM.Compile()
if err != nil {
t.Fatal(err)
}
err = testVM.RunCtx()
if err == nil {
t.Fatal("unexpected result broken script compiled successfully ")
}
testVM = New()
err = testVM.Load(testInvalidScript)
if err != nil {
t.Fatal(err)
}
testVM.CompileAndRun()
err = testVM.Shutdown()
if err == nil {
t.Fatal("Shutdown() passed successfully but expected to fail with invalid script")
}
}
func TestVM_CompileBroken(t *testing.T) {
testVM := New()
err := testVM.Load(testBrokenScript)
if err != nil {
t.Fatal(err)
}
err = testVM.Compile()
if err == nil {
t.Fatal("unexpected result broken script compiled successfully ")
}
}
func TestVM_CompileAndRunBroken(t *testing.T) {
testVM := New()
err := testVM.Load(testBrokenScript)
if err != nil {
t.Fatal(err)
}
testVM.CompileAndRun()
err = testVM.Shutdown()
if err == nil {
t.Fatal("expect error on shutdown due to invalid VM")
}
}
func TestValidate(t *testing.T) {
err := Validate(testBrokenScript)
if err == nil {
t.Fatal(err)
}
err = Validate(testScript)
if err != nil {
t.Fatal(err)
}
}
func TestVMLimit(t *testing.T) {
GCTScriptConfig = configHelper(true, false, 0)
testVM := New()
if testVM != nil {
t.Fatal("expected nil but received pointer to VM")
}
GCTScriptConfig = configHelper(true, true, maxTestVirtualMachines)
}
func TestAutoload(t *testing.T) {
GCTScriptConfig = &Config{
Enabled: true,
AutoLoad: []string{
scriptName,
},
Verbose: true,
}
ScriptPath = filepath.Join("..", "..", "testdata", "gctscript")
err := Autoload(scriptName, true)
if err != nil {
t.Fatal(err)
}
err = Autoload(scriptName, true)
if err == nil {
t.Fatal("expected err to be script not found received nil")
}
err = Autoload("once", false)
if err != nil {
t.Fatal(err)
}
err = Autoload(scriptName, false)
if err == nil {
t.Fatal("expected err to be script not found received nil")
}
}
func TestVMCount(t *testing.T) {
var c vmscount
c.add()
if c.Len() != 1 {
t.Fatalf("expect c len to be 1 instead received %v", c.Len())
}
c.remove()
if c.Len() != 0 {
t.Fatalf("expect c len to be 0 instead received %v", c.Len())
}
}
func configHelper(enabled, imports bool, max uint8) *Config {
return &Config{
Enabled: enabled,
AllowImports: imports,
ScriptTimeout: testVirtualMachineTimeout,
MaxVirtualMachines: max,
Verbose: true,
}
}

61
gctscript/vm/vm_types.go Normal file
View File

@@ -0,0 +1,61 @@
package vm
import (
"context"
"sync"
"time"
"github.com/d5/tengo/v2"
"github.com/gofrs/uuid"
)
const (
// DefaultTimeoutValue default timeout value for virtual machines
DefaultTimeoutValue = 30 * time.Second
// DefaultMaxVirtualMachines max number of virtual machines that can be loaded at one time
DefaultMaxVirtualMachines uint8 = 10
// TypeLoad text to display in script_event table when a VM is loaded
TypeLoad = "load"
// TypeCreate text to display in script_event table when a VM is created
TypeCreate = "create"
// TypeExecute text to display in script_event table when a script is executed
TypeExecute = "execute"
// TypeStop text to display in script_event table when a running script is stopped
TypeStop = "stop"
// TypeRead text to display in script_event table when a script contents is read
TypeRead = "read"
// StatusSuccess text to display in script_event table on successful execution
StatusSuccess = "success"
// StatusFailure text to display in script_event table when script execution fails
StatusFailure = "failure"
)
type vmscount int32
var (
pool = &sync.Pool{
New: func() interface{} {
return new(tengo.Script)
},
}
// AllVMSync stores all current Virtual Machine instances
AllVMSync = &sync.Map{}
// VMSCount running total count of Virtual Machines
VMSCount vmscount
)
// VM contains a pointer to "script" (precompiled source) and "compiled" (compiled byte code) instances
type VM struct {
ID uuid.UUID
Hash string
File string
Path string
Script *tengo.Script
Compiled *tengo.Compiled
ctx context.Context
T time.Duration
NextRun time.Time
S chan struct{}
}

View 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)
}

View 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()
}

View 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{},
}
}

View 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)
}
}

View File

@@ -0,0 +1,8 @@
package gct
import "github.com/thrasher-corp/gocryptotrader/gctscript/wrappers/gct/exchange"
// Wrapper struct
type Wrapper struct {
*exchange.Exchange
}

View 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: &currency.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
}

View 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")
}
}

View 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{}

View 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
}