Files
gocryptotrader/cmd/exchange_template/wrapper.tmpl
cranktakular 2fd4f5ec5b cmd/exchange_template: Update wrapper template, improve documentation (#2009)
* Updating wrapper template, updating documentation, regenerating documentation

* Renaming "streaming api" to "websocket"

* Context is now t

* Update docs/ADD_NEW_EXCHANGE.md

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update cmd/documentation/root_templates/root_readme.tmpl

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Fixes an improper example and regenerates documentation

* Corrects typos

* Makes a table label consistent across files

---------

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
2025-11-11 11:07:26 +11:00

529 lines
19 KiB
Cheetah

{{define "wrapper"}}
package {{.Name}}
import (
"context"
"time"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/key"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchange/accounts"
"github.com/thrasher-corp/gocryptotrader/exchange/websocket"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/deposit"
"github.com/thrasher-corp/gocryptotrader/exchanges/fundingrate"
"github.com/thrasher-corp/gocryptotrader/exchanges/futures"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/margin"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/protocol"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
"github.com/thrasher-corp/gocryptotrader/log"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
)
// SetDefaults sets the basic defaults for {{.CapitalName}}
func (e *Exchange) SetDefaults() {
e.Name = "{{.CapitalName}}"
e.Enabled = true
e.Verbose = true
e.API.CredentialsValidator.RequiresKey = true
e.API.CredentialsValidator.RequiresSecret = true
// If using only one pair format for request and configuration, across all supported asset types either SPOT and FUTURES etc. You can use the example below:
// Request format denotes what the pair as a string will be, when you send a request to an exchange.
requestFmt := &currency.PairFormat{/*Set pair request formatting details here for e.g.*/ Uppercase: true, Delimiter: ":"}
// Config format denotes how the currency pair should be represented as a string, including the delimiter between base and quote currency,
// when saved to the config.json file.
configFmt := &currency.PairFormat{/*Set pair request formatting details here*/}
if err := e.SetGlobalPairsManager(requestFmt, configFmt, /*multiple assets can be set here using the asset package ie asset.Spot*/); err != nil {
log.Errorln(log.ExchangeSys, err)
}
// If assets require multiple differences in formatting for request and
// configuration, another exchange method can be used e.g. futures
// contracts require a dash as a delimiter rather than an underscore. You
// can use this example below:
fmt1 := currency.PairStore{
AssetEnabled: true,
RequestFormat: &currency.PairFormat{Uppercase: true, Delimiter: "_"},
ConfigFormat: &currency.PairFormat{Uppercase: true, Delimiter: "_"},
}
fmt2 := currency.PairStore{
AssetEnabled: true,
RequestFormat: &currency.PairFormat{Uppercase: true, Delimiter: "-"},
ConfigFormat: &currency.PairFormat{Uppercase: true, Delimiter: "_"},
}
if err := e.SetAssetPairStore(asset.Spot, fmt1); err != nil {
log.Errorf(log.ExchangeSys, "%s error storing %q default asset formats: %s", e.Name, asset.Spot, err)
}
if err := e.SetAssetPairStore(asset.Margin, fmt2); err != nil {
log.Errorf(log.ExchangeSys, "%s error storing %q default asset formats: %s", e.Name, asset.Margin, err)
}
// Fill out the capabilities/features that the exchange supports
e.Features = exchange.Features{
Supports: exchange.FeaturesSupported{
{{ if .REST }} REST: true, {{ end }}
{{ if .WS }} Websocket: true, {{ end }}
{{ if .REST }}
RESTCapabilities: protocol.Features{
TickerFetching: true,
OrderbookFetching: true,
KlineFetching: true,
TradeFetching: true,
GetOrders: true,
AccountInfo: true,
AuthenticatedEndpoints: true,
},
{{ end }}
{{ if .WS }}
WebsocketCapabilities: protocol.Features{
TickerFetching: true,
OrderbookFetching: true,
KlineFetching: true,
TradeFetching: true,
Subscribe: true,
Unsubscribe: true,
AuthenticatedEndpoints: true,
},
{{ end }}
WithdrawPermissions: exchange.AutoWithdrawCrypto |
exchange.AutoWithdrawFiat,
Kline: kline.ExchangeCapabilitiesSupported{
Intervals: false,
},
},
Enabled: exchange.FeaturesEnabled{
AutoPairUpdates: true,
// Kline: kline.ExchangeCapabilitiesEnabled{
// Intervals: kline.DeployExchangeIntervals(
// kline.IntervalCapacity{Interval: kline.OneMin},
// ),
// GlobalResultLimit: 2000,
// },
},
{{ if .WS }} Subscriptions: defaultSubscriptions,{{ end }}
}
// TODO: SET THE EXCHANGES RATE LIMIT HERE
var err error
e.Requester, err = request.New(e.Name, common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout))
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
// TODO: SET THE URLs HERE
e.API.Endpoints = e.NewEndpoints()
if err = e.API.Endpoints.SetDefaultEndpoints(map[exchange.URL]string{
exchange.RestSpot: apiURL,
{{ if .WS }} exchange.WebsocketSpot: wsAPIURL,{{ end }}
}); err != nil {
log.Errorln(log.ExchangeSys, err)
}
e.Websocket = websocket.NewManager()
e.WebsocketResponseMaxLimit = exchange.DefaultWebsocketResponseMaxLimit
e.WebsocketResponseCheckTimeout = exchange.DefaultWebsocketResponseCheckTimeout
e.WebsocketOrderbookBufferLimit = exchange.DefaultWebsocketOrderbookBufferLimit
}
// Setup takes in the supplied exchange configuration details and sets params
func (e *Exchange) Setup(exch *config.Exchange) error {
if err := exch.Validate(); err != nil {
return err
}
if !exch.Enabled {
e.SetEnabled(false)
return nil
}
if err := e.SetupDefaults(exch); err != nil {
return err
}
/*
wsRunningEndpoint, err := e.API.Endpoints.GetURL(exchange.WebsocketSpot)
if err != nil {
return err
}
// If websocket is not supported, remove these websocket sections
if err := e.Websocket.Setup(
&websocket.ManagerSetup{
ExchangeConfig: exch,
DefaultURL: wsAPIURL,
RunningURL: wsRunningEndpoint,
Connector: e.WsConnect,
Subscriber: e.Subscribe,
Unsubscriber: e.Unsubscribe,
GenerateSubscriptions: e.generateSubscriptions,
Features: &e.Features.Supports.WebsocketCapabilities,
}); err != nil {
return err
}
return e.Websocket.SetupNewConnection(&websocket.ConnectionSetup{
URL: e.Websocket.GetWebsocketURL(),
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
})
*/
return nil
}
// FetchTradablePairs returns a list of the exchanges tradable pairs
func (e *Exchange) FetchTradablePairs(ctx context.Context, a asset.Item) (currency.Pairs, error) {
// Implement fetching the exchange available pairs if supported
return nil, nil
}
// UpdateTradablePairs updates the exchanges available pairs and stores them in the exchanges config
func (e *Exchange) UpdateTradablePairs(ctx context.Context) error {
assetTypes := e.GetAssetTypes(false)
for x := range assetTypes {
pairs, err := e.FetchTradablePairs(ctx, assetTypes[x])
if err != nil {
return err
}
if err := e.UpdatePairs(pairs, assetTypes[x], false); err != nil {
return err
}
}
return nil
}
// UpdateTicker updates and returns the ticker for a currency pair
func (e *Exchange) UpdateTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
// TODO: Replace this example code with exchange specific implementation
/*
tick, err := e.GetTickers()
if err != nil {
return nil, err
}
for y := range tick {
cp, err := currency.NewPairFromString(tick[y].Symbol)
if err != nil {
return nil, err
}
err = ticker.ProcessTicker(&ticker.Price{
Last: tick[y].LastPrice,
High: tick[y].HighPrice,
Low: tick[y].LowPrice,
Bid: tick[y].BidPrice,
Ask: tick[y].AskPrice,
Volume: tick[y].Volume,
QuoteVolume: tick[y].QuoteVolume,
Open: tick[y].OpenPrice,
Close: tick[y].PrevClosePrice,
Pair: cp,
ExchangeName: e.Name,
AssetType: assetType,
})
if err != nil {
return nil, err
}
}
*/
return ticker.GetTicker(e.Name, p, assetType)
}
// UpdateTickers updates all currency pairs of a given asset type
func (e *Exchange) UpdateTickers(ctx context.Context, assetType asset.Item) error {
// TODO: Replace this example code with exchange specific implementation
/*
tick, err := e.GetTickers()
if err != nil {
return err
}
for y := range tick {
cp, err := currency.NewPairFromString(tick[y].Symbol)
if err != nil {
return err
}
err = ticker.ProcessTicker(&ticker.Price{
Last: tick[y].LastPrice,
High: tick[y].HighPrice,
Low: tick[y].LowPrice,
Bid: tick[y].BidPrice,
Ask: tick[y].AskPrice,
Volume: tick[y].Volume,
QuoteVolume: tick[y].QuoteVolume,
Open: tick[y].OpenPrice,
Close: tick[y].PrevClosePrice,
Pair: cp,
ExchangeName: e.Name,
AssetType: assetType,
})
if err != nil {
return err
}
}
*/
return common.ErrNotYetImplemented
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (e *Exchange) UpdateOrderbook(ctx context.Context, pair currency.Pair, assetType asset.Item) (*orderbook.Book, error) {
var err error
pair, err = e.FormatExchangeCurrency(pair, assetType)
if err != nil {
return nil, err
}
// TODO: Replace this example code with exchange specific implementation
/*
ob, err := e.GetOrderBook(pair.String(), 1000)
if err != nil {
return nil, err
}
*/
book := &orderbook.Book{
Exchange: e.Name,
Pair: pair,
Asset: assetType,
ValidateOrderbook: e.ValidateOrderbook,
}
/*
book.Bids = make([]orderbook.Level, len(ob.Bids))
for x := range ob.Bids {
book.Bids[x] = orderbook.Level{
Amount: ob.Bids[x].Quantity,
Price: ob.Bids[x].Price,
}
}
book.Asks = make([]orderbook.Level, len(ob.Asks))
for x := range ob.Asks {
book.Asks[x] = orderbook.Level{
Amount: ob.Asks[x].Quantity,
Price: ob.Asks[x].Price,
}
}
*/
if err := book.Process(); err != nil {
return book, err
}
return orderbook.Get(e.Name, pair, assetType)
}
// UpdateAccountBalances retrieves currency balances
func (e *Exchange) UpdateAccountBalances(ctx context.Context, assetType asset.Item) (accounts.SubAccounts, error) {
// If fetching requires more than one asset type please set
// HasAssetTypeAccountSegregation to true in RESTCapabilities above.
return accounts.SubAccounts{}, common.ErrNotYetImplemented
}
// GetAccountFundingHistory returns funding history, deposits and withdrawals
func (e *Exchange) GetAccountFundingHistory(ctx context.Context) ([]exchange.FundingHistory, error) {
return nil, common.ErrNotYetImplemented
}
// GetWithdrawalsHistory returns previous withdrawals data
func (e *Exchange) GetWithdrawalsHistory(ctx context.Context, c currency.Code, a asset.Item) ([]exchange.WithdrawalHistory, error) {
return nil, common.ErrNotYetImplemented
}
// GetRecentTrades returns the most recent trades for a currency and asset
func (e *Exchange) GetRecentTrades(ctx context.Context, p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
return nil, common.ErrNotYetImplemented
}
// GetHistoricTrades returns historic trade data within the timeframe provided
func (e *Exchange) GetHistoricTrades(ctx context.Context, p currency.Pair, assetType asset.Item, timestampStart, timestampEnd time.Time) ([]trade.Data, error) {
return nil, common.ErrNotYetImplemented
}
// GetServerTime returns the current exchange server time.
func (e *Exchange) GetServerTime(ctx context.Context, a asset.Item) (time.Time, error) {
return time.Time{}, common.ErrNotYetImplemented
}
// SubmitOrder submits a new order
func (e *Exchange) SubmitOrder(ctx context.Context, s *order.Submit) (*order.SubmitResponse, error) {
if err := s.Validate(e.GetTradingRequirements()); err != nil {
return nil, err
}
/* TODO: When an order has been submitted you can use this helpful constructor to
return. Please add any additional order details to the
order.SubmitResponse if you think they are applicable.
resp, err := s.DeriveSubmitResponse(newOrderID)
if err != nil {
return nil, err
}
resp.Date = exampleTime // e.g. If this is supplied by the exchanges API.
return resp, nil
*/
return nil, common.ErrNotYetImplemented
}
// ModifyOrder modifies an existing order
func (e *Exchange) ModifyOrder(ctx context.Context, action *order.Modify) (*order.ModifyResponse, error) {
if err := action.Validate(); err != nil {
return nil, err
}
// TODO: When an order has been modified you can use this helpful constructor to
// return. Please add any additional order details to the
// order.ModifyResponse if you think they are applicable.
// resp, err := action.DeriveModifyResponse()
// if err != nil {
// return nil, err
// }
// resp.OrderID = maybeANewOrderID // e.g. If this is supplied by the exchanges API.
return nil, common.ErrNotYetImplemented
}
// CancelOrder cancels an order by its corresponding ID number
func (e *Exchange) CancelOrder(ctx context.Context, ord *order.Cancel) error {
// if err := ord.Validate(ord.StandardCancel()); err != nil {
// return err
// }
return common.ErrNotYetImplemented
}
// CancelBatchOrders cancels orders by their corresponding ID numbers
func (e *Exchange) CancelBatchOrders(ctx context.Context, orders []order.Cancel) (*order.CancelBatchResponse, error) {
return nil, common.ErrNotYetImplemented
}
// CancelAllOrders cancels all orders associated with a currency pair
func (e *Exchange) CancelAllOrders(ctx context.Context, orderCancellation *order.Cancel) (order.CancelAllResponse, error) {
// if err := orderCancellation.Validate(); err != nil {
// return order.CancelAllResponse{}, err
// }
return order.CancelAllResponse{}, common.ErrNotYetImplemented
}
// GetOrderInfo returns order information based on order ID
func (e *Exchange) GetOrderInfo(ctx context.Context, orderID string, pair currency.Pair, assetType asset.Item) (*order.Detail, error) {
return nil, common.ErrNotYetImplemented
}
// GetDepositAddress returns a deposit address for a specified currency
func (e *Exchange) GetDepositAddress(ctx context.Context, c currency.Code, accountID string, chain string) (*deposit.Address, error) {
return nil, common.ErrNotYetImplemented
}
// GetAvailableTransferChains returns the available transfer blockchains for the specific cryptocurrency
func (e *Exchange) GetAvailableTransferChains(ctx context.Context, cryptocurrency currency.Code) ([]string, error) {
return nil, common.ErrNotYetImplemented
}
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is submitted
func (e *Exchange) WithdrawCryptocurrencyFunds(ctx context.Context, withdrawRequest *withdraw.Request) (*withdraw.ExchangeResponse, error) {
// if err := withdrawRequest.Validate(); err != nil {
// return nil, err
// }
return nil, common.ErrNotYetImplemented
}
// WithdrawFiatFunds returns a withdrawal ID when a withdrawal is submitted
func (e *Exchange) WithdrawFiatFunds(_ context.Context, withdrawRequest *withdraw.Request) (*withdraw.ExchangeResponse, error) {
// if err := withdrawRequest.Validate(); err != nil {
// return nil, err
// }
return nil, common.ErrNotYetImplemented
}
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a withdrawal is submitted
func (e *Exchange) WithdrawFiatFundsToInternationalBank(_ context.Context, withdrawRequest *withdraw.Request) (*withdraw.ExchangeResponse, error) {
// if err := withdrawRequest.Validate(); err != nil {
// return nil, err
// }
return nil, common.ErrNotYetImplemented
}
// GetActiveOrders retrieves any orders that are active/open
func (e *Exchange) GetActiveOrders(ctx context.Context, getOrdersRequest *order.MultiOrderRequest) (order.FilteredOrders, error) {
// if err := getOrdersRequest.Validate(); err != nil {
// return nil, err
// }
return nil, common.ErrNotYetImplemented
}
// GetOrderHistory retrieves account order information
// Can Limit response to specific order status
func (e *Exchange) GetOrderHistory(ctx context.Context, getOrdersRequest *order.MultiOrderRequest) (order.FilteredOrders, error) {
// if err := getOrdersRequest.Validate(); err != nil {
// return nil, err
// }
return nil, common.ErrNotYetImplemented
}
// GetFeeByType returns an estimate of fee based on the type of transaction
func (e *Exchange) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBuilder) (float64, error) {
return 0, common.ErrNotYetImplemented
}
// ValidateAPICredentials validates current credentials used for wrapper
func (e *Exchange) ValidateAPICredentials(ctx context.Context, assetType asset.Item) error {
_, err := e.UpdateAccountBalances(ctx, assetType)
return e.CheckTransientError(err)
}
// GetHistoricCandles returns candles between a time period for a set time interval
func (e *Exchange) GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) {
return nil, common.ErrNotYetImplemented
}
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (e *Exchange) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) {
return nil, common.ErrNotYetImplemented
}
// GetLeverage gets the account's initial leverage for the asset type and pair
func (e *Exchange) GetLeverage(_ context.Context, _ asset.Item, _ currency.Pair, _ margin.Type, _ order.Side) (float64, error) {
return -1, common.ErrNotYetImplemented
}
// GetFuturesContractDetails returns all contracts from the exchange by asset type
func (e *Exchange) GetFuturesContractDetails(context.Context, asset.Item) ([]futures.Contract, error) {
return nil, common.ErrNotYetImplemented
}
// GetLatestFundingRates returns the latest funding rates data
func (e *Exchange) GetLatestFundingRates(_ context.Context, _ *fundingrate.LatestRateRequest) ([]fundingrate.LatestRateResponse, error) {
return nil, common.ErrNotYetImplemented
}
// GetHistoricalFundingRates returns funding rates for a given asset and currency for a time period
func (e *Exchange) GetHistoricalFundingRates(_ context.Context, r *fundingrate.HistoricalRatesRequest) (*fundingrate.HistoricalRates, error) {
if r == nil {
return nil, common.ErrNilPointer
}
return nil, common.ErrNotYetImplemented
}
// GetOpenInterest returns the open interest rate for a given asset pair
func (e *Exchange) GetOpenInterest(_ context.Context, _ ...key.PairAsset) ([]futures.OpenInterest, error) {
return nil, common.ErrNotYetImplemented
}
// GetCurrencyTradeURL returns the URL to the exchange's trade page for the given asset and currency pair
func (e *Exchange) GetCurrencyTradeURL(_ context.Context, a asset.Item, cp currency.Pair) (string, error) {
_, err := e.CurrencyPairs.IsPairEnabled(cp, a)
if err != nil {
return "", err
}
return "", common.ErrNotYetImplemented
}
// UpdateOrderExecutionLimits updates order execution limits
func (e *Exchange) UpdateOrderExecutionLimits(_ context.Context, _ asset.Item) error {
return common.ErrNotYetImplemented
}
// SetLeverage sets the account's initial leverage for the asset type and pair
func (e *Exchange) SetLeverage(_ context.Context, _ asset.Item, _ currency.Pair, _ margin.Type, _ float64, _ order.Side) error {
return common.ErrNotYetImplemented
}
{{end}}