Files
gocryptotrader/gctscript/vm/manager.go
Scott 86d3724507 Futures order position tracking & FTX scaled collateral calculation (#868)
* implements futures functions and GRPC functions on new branch

* lint and test fixes

* Fix uneven split pnl. Adds collateral weight test. docs. New clear func

* Test protection if someone has zero collateral

* Uses string instead of double for accuracy

* Fixes old code panic

* context, match, docs

* Addresses Shazniterinos, var names, expanded tests

* Returns subaccount name, provides USD values when offlinecalc

* Fixes oopsie

* Fixes cool bug which allowed made up subaccount results

* Subaccount override on FTX, subaccount results for collateral

* Strenghten collateral account info checks. Improve FTX test

* English is my first language

* Fixes oopsies

* Fixes for unrealised PNL & collateral rendering

* Fixes lint and tests

* Shaznit fixes

* Secret Shaznit

* Updates account information across wrappers to include more fields

* Updates online collateral calculations. Updates RPC data

* Accurately calculates collateral offline and online minus testing

* Tests and lint chocolate

* Simplifies accountinfo results

* Fixes shaznits

* Adds new func

* Increases collateral accuracy again again again x 200

* Increases accuracy of collateral rendering

* Fixes minor merge/test issues

* Linterino

* Fixes ws test. Improves collateral calculations and rendering

* Make it prettier

* Removes the lock I put on 👀

* Adds `additional_collateral_used` field, renders orig currency

* Fixes unrelated test

* Fix test

* Correctly calculate spot margin borrow collateral

* Address fun lint surprise

See https://github.com/golangci/golangci-lint/issues/741#issuecomment-1017014331

* Strange lint fixing x2

* Continued lint journey

* Nolint the nolint to not lint the lint

* Adds two new fields to response

* More linting issues arising

* fIX3s_c4s|NG

* Fixes command flags' incorrect numbering

* FairMarket = Won
2022-02-28 16:39:36 +11:00

107 lines
2.4 KiB
Go

package vm
import (
"errors"
"fmt"
"sync"
"sync/atomic"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/log"
)
const (
caseName = "GCTScript"
// Name is an exported subsystem name
Name = "gctscript"
)
// ErrNilSubsystem returned when script manager has not been set up
var ErrNilSubsystem = errors.New("gct script has not been set up")
// GctScriptManager loads and runs GCT Tengo scripts
type GctScriptManager struct {
config *Config
started int32
shutdown chan struct{}
// Optional values to override stored config ('nil' if not overridden)
MaxVirtualMachines *uint8
}
// NewManager creates a new instance of script manager
func NewManager(config *Config) (*GctScriptManager, error) {
if config == nil {
return nil, errors.New("config must be provided for script manager")
}
return &GctScriptManager{
config: config,
}, nil
}
// IsRunning returns if gctscript manager subsystem is started
func (g *GctScriptManager) IsRunning() bool {
if g == nil {
return false
}
return atomic.LoadInt32(&g.started) == 1
}
// Start starts gctscript subsystem and creates shutdown channel
func (g *GctScriptManager) Start(wg *sync.WaitGroup) (err error) {
if wg == nil {
return fmt.Errorf("%T %w", wg, common.ErrNilPointer)
}
if !atomic.CompareAndSwapInt32(&g.started, 0, 1) {
return fmt.Errorf("%s %s", caseName, ErrScriptFailedValidation)
}
defer func() {
if err != nil {
atomic.CompareAndSwapInt32(&g.started, 1, 0)
}
}()
g.shutdown = make(chan struct{})
wg.Add(1)
go g.run(wg)
return nil
}
// Stop stops gctscript subsystem along with all running Virtual Machines
func (g *GctScriptManager) Stop() error {
if g == nil {
return fmt.Errorf("%s %w", caseName, ErrNilSubsystem)
}
if atomic.LoadInt32(&g.started) == 0 {
return fmt.Errorf("%s not running", caseName)
}
defer func() {
atomic.CompareAndSwapInt32(&g.started, 1, 0)
}()
if err := g.ShutdownAll(); err != nil {
return err
}
close(g.shutdown)
return nil
}
func (g *GctScriptManager) run(wg *sync.WaitGroup) {
log.Debugf(log.Global, "%s starting", caseName)
SetDefaultScriptOutput()
g.autoLoad()
defer func() {
wg.Done()
}()
<-g.shutdown
}
// GetMaxVirtualMachines returns the max number of VMs to create
func (g *GctScriptManager) GetMaxVirtualMachines() uint8 {
if g.MaxVirtualMachines != nil {
return *g.MaxVirtualMachines
}
return g.config.MaxVirtualMachines
}