Files
gocryptotrader/exchanges/asset/asset.go
cranktakular fd9aaf00a2 Coinbase: Update exchange implementation (#1480)
* Slight enhance of Coinbase tests

Continual enhance of Coinbase tests

The revamp continues

Oh jeez the Orderbook part's unfinished don't look

Coinbase revamp, Orderbook still unfinished

* Coinbase revamp; CreateReport is still WIP

* More coinbase improvements; onto sandbox testing

* Coinbase revamp continues

* Coinbase revamp continues

* Coinbasepro revamp is ceaseless

* Coinbase revamp, starting on advanced trade API

* Coinbase Advanced Trade Starts in Ernest

V3 done, onto V2

Coinbase revamp nears completion

Coinbase revamp nears completion

Test commit should fail

Coinbase revamp nears completion

* Coinbase revamp stage wrapper

* Coinbase wrapper coherence continues

* Coinbase wrapper continues writhing

* Coinbase wrapper & codebase cleanup

* Coinbase updates & wrap progress

* More Coinbase wrapper progress

* Wrapper is wrapped, kinda

* Test & type checking

* Coinbase REST revamp finished

* Post-merge fix

* WS revamp begins

* WS Main Revamp Done?

* CB websocket tidying up

* Coinbase WS wrapperupperer

* Coinbase revamp done??

* Linter progress

* Continued lint cleanup

* Further lint cleanup

* Increased lint coverage

* Does this fix all sloppy reassigns & shadowing?

* Undoing retry policy change

* Documentation regeneration

* Coinbase code improvements

* Providing warning about known issue

* Updating an error to new format

* Making gocritic happy

* Review adherence

* Endpoints moved to V3 & nil pointer fixes

* Removing seemingly superfluous constant

* Glorious improvements

* Removing unused error

* Partial public endpoint addition

* Slight improvements

* Wrapper improvements; still a few errors left in other packages

* A lil Coinbase progress

* Json cleaning

* Lint appeasement

* Config repair

* Config fix (real)

* Little fix

* New public endpoint incorporation

* Additional fixes

* Improvements & Appeasements

* LineSaver

* Additional fixes

* Another fix

* Fixing picked nits

* Quick fixies

* Lil fixes

* Subscriptions: Add List.Enabled

* CoinbasePro: Add subscription templating

* fixup! CoinbasePro: Add subscription templating

* fixup! CoinbasePro: Add subscription templating

* Comment fix

* Subsequent fixes

* Issues hopefully fixed

* Lint fix

* Glorious fixes

* Json formatting

* ShazNits

* (L/N)i(n/)t

* Adding a test

* Tiny test improvement

* Template patch testing

* Fixes

* Further shaznits

* Lint nit

* JWT move and other fixes

* Small nits

* Shaznit, singular

* Post-merge fix

* Post-merge fixes

* Typo fix

* Some glorious nits

* Required changes

* Stop going

* Alias attempt

* Alias fix & test cleanup

* Test fix

* GetDepositAddress logic improvement

* Status update: Fixed

* Lint fix

* Happy birthday to PR 1480

* Cleanups

* Necessary nit corrections

* Fixing sillybug

* As per request

* Programming progress

* Order fixes

* Further fixies

* Test fix

* Pre-merge fixes

* More shaznits

* Context

* Sonic error handling

* Import fix

* Better Sonic error handling

* Perfect Sonic error handling?

* F purge

* Coinbase improvements

* API Update Conformity

* Coinbase continuation

* Coinbase order improvements

* Coinbase order improvements

* CreateOrderConfig improvements

* Managing API updates

* Coinbase API update progression

* jwt rename

* Comment link fix

* Coinbase v2 cleanup

* Post-merge fixes

* Review fixes

* GK's suggestions

* Linter fix

* Minor gbjk fixes

* Nit fixes

* Merge fix

* Lint fixes

* Coinbase rename stage 1

* Coinbase rename stage 2

* Coinbase rename stage 3

* Coinbase rename stage 4

* Coinbase rename final fix

* Coinbase: PoC on converting to request structs

* Applying requested changes

* Many review fixes, handled

* Thrashed by nits

* More minor modifications

* The last nit!?

---------

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>
2025-09-16 13:37:00 +10:00

258 lines
6.1 KiB
Go

package asset
import (
"errors"
"fmt"
"slices"
"strings"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
)
// Public errors related to assets
var (
ErrNotSupported = errors.New("unsupported asset type")
ErrNotEnabled = errors.New("asset type not enabled")
ErrInvalidAsset = errors.New("asset is invalid")
)
// Item stores the asset type
type Item uint32
// Items stores a list of assets types
type Items []Item
// Supported Assets
const (
Empty Item = iota
Spot
Margin
CrossMargin
MarginFunding
Index
Binary
// Futures asset consts must come below this comment for method `IsFutures`
Futures
PerpetualContract
PerpetualSwap
DeliveryFutures
UpsideProfitContract
DownsideProfitContract
CoinMarginedFutures
USDTMarginedFutures
USDCMarginedFutures
FutureCombo
LinearContract
Spread
// Options asset consts must come below this comment for method `IsOptions`
Options
OptionCombo
// All asset const must come immediately after all valid assets for method `IsValid`
All
)
const (
spot = "spot"
margin = "margin"
crossMargin = "cross_margin"
marginFunding = "marginfunding"
index = "index"
binary = "binary"
perpetualContract = "perpetualcontract"
perpetualSwap = "perpetualswap"
swap = "swap"
spread = "spread"
futures = "futures"
deliveryFutures = "delivery"
upsideProfitContract = "upsideprofitcontract"
downsideProfitContract = "downsideprofitcontract"
coinMarginedFutures = "coinmarginedfutures"
usdtMarginedFutures = "usdtmarginedfutures"
usdcMarginedFutures = "usdcmarginedfutures"
options = "options"
optionCombo = "option_combo"
futureCombo = "future_combo"
linearContract = "linearcontract"
all = "all"
)
var supportedList = Items{Spot, Margin, CrossMargin, MarginFunding, Index, Binary, PerpetualContract, PerpetualSwap, Futures, DeliveryFutures, UpsideProfitContract, DownsideProfitContract, CoinMarginedFutures, USDTMarginedFutures, USDCMarginedFutures, Options, LinearContract, OptionCombo, FutureCombo, Spread}
// Supported returns a list of supported asset types
func Supported() Items {
return supportedList
}
// String converts an Item to its string representation
func (a Item) String() string {
switch a {
case Spot:
return spot
case Margin:
return margin
case CrossMargin:
return crossMargin
case MarginFunding:
return marginFunding
case Index:
return index
case Binary:
return binary
case PerpetualContract:
return perpetualContract
case PerpetualSwap:
return perpetualSwap
case Spread:
return spread
case Futures:
return futures
case DeliveryFutures:
return deliveryFutures
case UpsideProfitContract:
return upsideProfitContract
case DownsideProfitContract:
return downsideProfitContract
case CoinMarginedFutures:
return coinMarginedFutures
case USDTMarginedFutures:
return usdtMarginedFutures
case USDCMarginedFutures:
return usdcMarginedFutures
case Options:
return options
case OptionCombo:
return optionCombo
case FutureCombo:
return futureCombo
case LinearContract:
return linearContract
case All:
return all
default:
return ""
}
}
// Upper returns the item's upper case string
func (a Item) Upper() string {
return strings.ToUpper(a.String())
}
// Strings converts an asset type array to a string array
func (a Items) Strings() []string {
assets := make([]string, len(a))
for x := range a {
assets[x] = a[x].String()
}
return assets
}
// Contains returns whether or not the supplied asset exists
// in the list of Items
func (a Items) Contains(i Item) bool {
return slices.Contains(a, i)
}
// JoinToString joins an asset type array and converts it to a string
// with the supplied separator
func (a Items) JoinToString(separator string) string {
return strings.Join(a.Strings(), separator)
}
// IsValid returns whether or not the supplied asset type is valid or not
func (a Item) IsValid() bool {
return a > Empty && a < All
}
// IsFutures checks if the asset type is a futures contract based asset
func (a Item) IsFutures() bool {
return a >= Futures && a < Options
}
// IsOptions checks if the asset type is options contract based asset
func (a Item) IsOptions() bool {
return a >= Options && a < All
}
// UnmarshalJSON conforms type to the umarshaler interface
func (a *Item) UnmarshalJSON(d []byte) error {
var assetString string
err := json.Unmarshal(d, &assetString)
if err != nil {
return err
}
if assetString == "" {
return nil
}
ai, err := New(assetString)
if err != nil {
return err
}
*a = ai
return nil
}
// MarshalJSON conforms type to the marshaller interface
func (a Item) MarshalJSON() ([]byte, error) {
return json.Marshal(a.String())
}
// New takes an input matches to relevant package assets
func New(input string) (Item, error) {
input = strings.ToLower(input)
switch input {
case spot:
return Spot, nil
case margin:
return Margin, nil
case marginFunding:
return MarginFunding, nil
case crossMargin:
return CrossMargin, nil
case deliveryFutures:
return DeliveryFutures, nil
case index:
return Index, nil
case binary:
return Binary, nil
case perpetualContract:
return PerpetualContract, nil
case perpetualSwap, swap:
return PerpetualSwap, nil
case spread:
return Spread, nil
case futures:
return Futures, nil
case upsideProfitContract:
return UpsideProfitContract, nil
case downsideProfitContract:
return DownsideProfitContract, nil
case coinMarginedFutures:
return CoinMarginedFutures, nil
case usdtMarginedFutures:
return USDTMarginedFutures, nil
case usdcMarginedFutures:
return USDCMarginedFutures, nil
case options, "option":
return Options, nil
case optionCombo:
return OptionCombo, nil
case futureCombo:
return FutureCombo, nil
case linearContract:
return LinearContract, nil
case all:
return All, nil
default:
return 0, fmt.Errorf("%w '%v', only supports %s", ErrNotSupported, input, supportedList)
}
}
// UseDefault returns default asset type
func UseDefault() Item {
return Spot
}