Files
gocryptotrader/cmd/exchange_template/exchange_template.go
Samuael A. 3f534a15f1 cmd/exchange_template, exchanges: Update templates and propogate to exchanges (#1777)
* Added TimeInForce type and updated related files

* Linter issue fix and minor coinbasepro type update

* Bitrex consts update

* added unit test and minor changes in bittrex

* Unit tests update

* Fix minor linter issues

* Update TestStringToTimeInForce unit test

* Exchange test template change

* A different approach

* fix conflict with gateio timeInForce

* minor exchange template update

* Minor fix to test_files template

* Update order tests

* Complete updating the order unit tests

* Updating exchange wrapper and test template files

* update kucoin and deribit wrapper to match the time in force change

* minor comment update

* fix time-in-force related test errors

* linter issue fix

* ADD_NEW_EXCHANGE documentation update

* time in force constants, functions and unit tests update

* shift tif policies to TimeInForce

* Update time-in-force, related functions, and unit tests

* fix linter issue and time-in-force processing

* added a good till crossing tif value

* order type fix and fix related tim-in-force entries

* update time-in-force unmarshaling and unit test

* consistency guideline added

* fix time-in-force error in gateio

* linter issue fix

* update based on review comments

* add unit test and fix missing issues

* minor fix and added benchmark unit test

* change GTT to GTC for limit

* fix linter issue

* added time-in-force value to place order param

* fix minor issues based on review comment and move tif code to separate files

* update on exchanges linked to time-in-force

* resolve missing review comments

* minor linter issues fix

* added time-in-force handler and update timeInForce parametered endpoint

* minor fixes based on review

* nits fix

* update based on review

* linter fix

* rm getTimeInForce func and minor change to time-in-force

* minor change

* update based on review comments

* wrappers and time-in-force calling approach

* minor change

* update gateio string to timeInForce conversion and unit test

* update exchange template

* update wrapper template file

* policy comments, and template files update

* rename all exchange types name to Exchange

* update on template files and template generation

* templates and generation code and other updates

* linter issue fix

* added subscriptions and websocket templates

* update ADD_NEW_EXCHANGE.md with recent binance functions and implementations

* rename template files and update unit tests

* minor template and unit test fix

* rename templates and fix on unit tests

* update on template files and documentation

* removed unnecessary tag fix and update templates

* fix Add_NEW_EXCHANGE.md doc file

* formatting, comments, and error checks update on template files

* rename exchange receivers to e and ex for consistency

* rename unit test exchange receiver and minor updates

* linter issues fix

* fix deribit issue and minor style update

* fix test issues caused by receiver change

* raname local variables exchange declaration variables

* update templates comments

* update templates and related comments

* renamed ex to e

* update template comments

* toggle WS to false to improve coverage

* template comments update

* added test coverage to Ws enabled and minor changes

---------

Co-authored-by: Samuel Reid <43227667+cranktakular@users.noreply.github.com>
2025-07-17 10:46:36 +10:00

264 lines
6.2 KiB
Go

package main
import (
"errors"
"flag"
"fmt"
"html/template"
"log"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/file"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/core"
"github.com/thrasher-corp/gocryptotrader/currency"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
const (
exchangeConfigPath = "../../testdata/configtest.json"
targetPath = "../../exchanges"
)
type exchange struct {
Name string
CapitalName string
REST bool
WS bool
}
var errInvalidExchangeName = errors.New("invalid exchange name")
func main() {
var newExchangeName string
var websocketSupport, restSupport bool
flag.StringVar(&newExchangeName, "name", "", "the exchange name")
flag.BoolVar(&websocketSupport, "ws", false, "whether the exchange supports websocket")
flag.BoolVar(&restSupport, "rest", false, "whether the exchange supports REST")
flag.Parse()
fmt.Println("GoCryptoTrader: Exchange templating tool.")
fmt.Println(core.Copyright)
fmt.Println()
if len(os.Args) == 1 {
log.Println("Invalid arguments supplied, please see application usage below:")
flag.Usage()
return
}
if err := checkExchangeName(newExchangeName); err != nil {
log.Fatal(err)
}
newExchangeName = strings.ToLower(newExchangeName)
if !websocketSupport && !restSupport {
log.Println("At least one protocol must be specified (rest/ws)")
flag.Usage()
return
}
fmt.Println("Exchange Name: ", newExchangeName)
fmt.Println("Websocket Supported: ", websocketSupport)
fmt.Println("REST Supported: ", restSupport)
fmt.Println()
fmt.Println("Please check if everything is correct and then type y to continue or n to cancel...")
var choice []byte
_, err := fmt.Scanln(&choice)
if err != nil {
log.Fatal("GoCryptoTrader: Exchange templating tool fmt.Scanln ", err)
}
if !common.YesOrNo(string(choice)) {
log.Fatal("GoCryptoTrader: Exchange templating tool stopped...")
}
exch := exchange{
Name: newExchangeName,
REST: restSupport,
WS: websocketSupport,
}
exchangeDirectory := filepath.Join(targetPath, exch.Name)
configTestFile := config.GetConfig()
var newConfig *config.Exchange
newConfig, err = makeExchange(exchangeDirectory, configTestFile, &exch)
if err != nil {
log.Fatal(err)
}
err = saveConfig(exchangeDirectory, configTestFile, newConfig)
if err != nil {
log.Fatal(err)
}
}
func checkExchangeName(exchName string) error {
if strings.Contains(exchName, " ") ||
len(exchName) <= 2 {
return errInvalidExchangeName
}
return nil
}
func makeExchange(exchangeDirectory string, configTestFile *config.Config, exch *exchange) (*config.Exchange, error) {
err := configTestFile.LoadConfig(exchangeConfigPath, true)
if err != nil {
return nil, err
}
// NOTE need to nullify encrypt configuration
_, err = configTestFile.GetExchangeConfig(exch.Name)
if err == nil {
return nil, errors.New("exchange already exists")
}
_, err = os.Stat(exchangeDirectory)
if !os.IsNotExist(err) {
return nil, errors.New("directory already exists")
}
err = os.MkdirAll(exchangeDirectory, file.DefaultPermissionOctal)
if err != nil {
return nil, err
}
fmt.Printf("Output directory: %s\n", exchangeDirectory)
exch.CapitalName = cases.Title(language.English).String(exch.Name)
newExchConfig := &config.Exchange{}
newExchConfig.Name = exch.CapitalName
newExchConfig.Enabled = true
newExchConfig.API.Credentials.Key = "Key"
newExchConfig.API.Credentials.Secret = "Secret"
newExchConfig.CurrencyPairs = &currency.PairsManager{
UseGlobalFormat: true,
RequestFormat: &currency.PairFormat{
Uppercase: true,
},
ConfigFormat: &currency.PairFormat{
Uppercase: true,
Delimiter: currency.DashDelimiter,
},
}
outputFiles := []struct {
Name string
Filename string
FilePostfix string
TemplateFile string
}{
{
Name: "readme",
Filename: "README.md",
TemplateFile: "readme.tmpl",
},
{
Name: "rest",
Filename: "rest.go",
TemplateFile: "rest.tmpl",
},
{
Name: "test",
Filename: "test_file.tmpl",
FilePostfix: "_test.go",
TemplateFile: "test.tmpl",
},
{
Name: "types",
Filename: "types.go",
TemplateFile: "types.tmpl",
},
{
Name: "wrapper",
Filename: "wrapper.go",
TemplateFile: "wrapper.tmpl",
},
{
Name: "subscriptions",
Filename: "subscriptions.go",
TemplateFile: "subscriptions.tmpl",
},
{
Name: "websocket",
Filename: "websocket.go",
TemplateFile: "websocket.tmpl",
},
}
for x := range outputFiles {
var tmpl *template.Template
tmpl, err = template.New(outputFiles[x].Name).ParseFiles(outputFiles[x].TemplateFile)
if err != nil {
return nil, fmt.Errorf("%s template error: %s", outputFiles[x].Name, err)
}
filename := outputFiles[x].Filename
if !exch.WS && slices.Contains([]string{"websocket", "subscriptions"}, outputFiles[x].Name) {
continue
}
if outputFiles[x].FilePostfix != "" {
filename = exch.Name + outputFiles[x].FilePostfix
}
outputFile := filepath.Join(exchangeDirectory, filename)
newFile(outputFile)
var f *os.File
f, err = os.OpenFile(outputFile, os.O_WRONLY, file.DefaultPermissionOctal)
if err != nil {
return nil, err
}
if err = tmpl.Execute(f, exch); err != nil {
f.Close()
return nil, err
}
f.Close()
}
return newExchConfig, nil
}
func saveConfig(exchangeDirectory string, configTestFile *config.Config, newExchConfig *config.Exchange) error {
if err := runCommand(exchangeDirectory, "fmt"); err != nil {
return err
}
configTestFile.Exchanges = append(configTestFile.Exchanges, *newExchConfig)
if err := configTestFile.SaveConfigToFile(exchangeConfigPath); err != nil {
return err
}
return runCommand(exchangeDirectory, "test")
}
func runCommand(dir, param string) error {
cmd := exec.Command("go", param)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("unable to go %s stdout: %s stderr: %s",
param, out, err)
}
return nil
}
func newFile(path string) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
file, err := os.Create(path)
if err != nil {
log.Fatal(err)
}
file.Close()
}
}