mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-24 23:16:52 +00:00
Binance,Okx: Add Leverage, MarginType, Positions and CollateralMode support (#1220)
* init * surprise train commit * basic distinctions * the terms of binance are confusing * renames and introduction of allocatedMargin * add new margin funcs * pulling out wires * implement proper getposition stuff * bad coding day * investigate order manager next * a broken mess, but a progressing one * finally completes some usdtmargined stuff * coinMfutures eludes me * expand to okx * imports fix * completes okx wrapper implementations * cleans and polishes before rpc implementations * rpc setup, order manager features, exch features * more rpc, collateral and margin things * mini test * looking at rpc response, expansion of features * reorganising before the storm * changing how futures requests work * cleanup and tests of cli usage * remove silly client side logic * cleanup * collateral package, typo fix, margin err, rpc derive * uses convert.StringToFloat ONLY ON STRUCTS FROM THIS PR * fix binance order history bug * niteroos * adds new funcs to exchange standards testing * more post merge fixes * fix binance * replace simepletimeformat * fix for merge * merge fixes * micro fixes * order side now required for leverage * fix up the rest * global -> portfolio collateral * Update exchanges/collateral/collateral_test.go Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io> * adds fields and todos * rm field redundancy * lint fix oopsie daisy * fixes panic, expands error and cli explanations (sorry shaz) * ensures casing is appropriate for underlying * Adds a shiny TODO --------- Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
71
exchanges/collateral/collateral.go
Normal file
71
exchanges/collateral/collateral.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package collateral
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Valid returns whether the collateral mode is valid
|
||||
func (t Mode) Valid() bool {
|
||||
return t != UnsetMode && supportedCollateralModes&t == t
|
||||
}
|
||||
|
||||
// UnmarshalJSON converts json into collateral mode
|
||||
func (t *Mode) UnmarshalJSON(d []byte) error {
|
||||
var mode string
|
||||
err := json.Unmarshal(d, &mode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*t, err = StringToMode(mode)
|
||||
return err
|
||||
}
|
||||
|
||||
// String returns the string representation of the collateral mode in lowercase
|
||||
// the absence of a lower func should hopefully highlight that String is lower
|
||||
func (t Mode) String() string {
|
||||
switch t {
|
||||
case UnsetMode:
|
||||
return unsetCollateralStr
|
||||
case SingleMode:
|
||||
return singleCollateralStr
|
||||
case MultiMode:
|
||||
return multiCollateralStr
|
||||
case PortfolioMode:
|
||||
return portfolioCollateralStr
|
||||
case UnknownMode:
|
||||
return unknownCollateralStr
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Upper returns the upper case string representation of the collateral mode
|
||||
func (t Mode) Upper() string {
|
||||
return strings.ToUpper(t.String())
|
||||
}
|
||||
|
||||
// IsValidCollateralModeString checks to see if the supplied string is a valid collateral mode
|
||||
func IsValidCollateralModeString(m string) bool {
|
||||
switch strings.ToLower(m) {
|
||||
case singleCollateralStr, multiCollateralStr, portfolioCollateralStr, unsetCollateralStr:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// StringToMode converts a string to a collateral mode
|
||||
// doesn't error, just returns unknown if the string is not recognised
|
||||
func StringToMode(m string) (Mode, error) {
|
||||
switch strings.ToLower(m) {
|
||||
case singleCollateralStr:
|
||||
return SingleMode, nil
|
||||
case multiCollateralStr:
|
||||
return MultiMode, nil
|
||||
case portfolioCollateralStr:
|
||||
return PortfolioMode, nil
|
||||
case "":
|
||||
return UnsetMode, nil
|
||||
}
|
||||
return UnknownMode, fmt.Errorf("%w %v", ErrInvalidCollateralMode, m)
|
||||
}
|
||||
180
exchanges/collateral/collateral_test.go
Normal file
180
exchanges/collateral/collateral_test.go
Normal file
@@ -0,0 +1,180 @@
|
||||
package collateral
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidCollateralType(t *testing.T) {
|
||||
t.Parallel()
|
||||
if !SingleMode.Valid() {
|
||||
t.Fatal("expected 'true', received 'false'")
|
||||
}
|
||||
if !MultiMode.Valid() {
|
||||
t.Fatal("expected 'true', received 'false'")
|
||||
}
|
||||
if !PortfolioMode.Valid() {
|
||||
t.Fatal("expected 'true', received 'false'")
|
||||
}
|
||||
if UnsetMode.Valid() {
|
||||
t.Fatal("expected 'false', received 'true'")
|
||||
}
|
||||
if UnknownMode.Valid() {
|
||||
t.Fatal("expected 'false', received 'true'")
|
||||
}
|
||||
if Mode(137).Valid() {
|
||||
t.Fatal("expected 'false', received 'true'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalJSONCollateralType(t *testing.T) {
|
||||
t.Parallel()
|
||||
type martian struct {
|
||||
M Mode `json:"collateral"`
|
||||
}
|
||||
|
||||
var alien martian
|
||||
jason := []byte(`{"collateral":"single"}`)
|
||||
err := json.Unmarshal(jason, &alien)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if alien.M != SingleMode {
|
||||
t.Errorf("received '%v' expected 'single'", alien.M)
|
||||
}
|
||||
|
||||
jason = []byte(`{"collateral":"multi"}`)
|
||||
err = json.Unmarshal(jason, &alien)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if alien.M != MultiMode {
|
||||
t.Errorf("received '%v' expected 'Multi'", alien.M)
|
||||
}
|
||||
|
||||
jason = []byte(`{"collateral":"portfolio"}`)
|
||||
err = json.Unmarshal(jason, &alien)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if alien.M != PortfolioMode {
|
||||
t.Errorf("received '%v' expected 'Portfolio'", alien.M)
|
||||
}
|
||||
|
||||
jason = []byte(`{"collateral":"hello moto"}`)
|
||||
err = json.Unmarshal(jason, &alien)
|
||||
if !errors.Is(err, ErrInvalidCollateralMode) {
|
||||
t.Error(err)
|
||||
}
|
||||
if alien.M != UnknownMode {
|
||||
t.Errorf("received '%v' expected 'UnknownMode'", alien.M)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringCollateralType(t *testing.T) {
|
||||
t.Parallel()
|
||||
if UnknownMode.String() != unknownCollateralStr {
|
||||
t.Errorf("received '%v' expected '%v'", UnknownMode.String(), unknownCollateralStr)
|
||||
}
|
||||
if SingleMode.String() != singleCollateralStr {
|
||||
t.Errorf("received '%v' expected '%v'", SingleMode.String(), singleCollateralStr)
|
||||
}
|
||||
if MultiMode.String() != multiCollateralStr {
|
||||
t.Errorf("received '%v' expected '%v'", MultiMode.String(), multiCollateralStr)
|
||||
}
|
||||
if PortfolioMode.String() != portfolioCollateralStr {
|
||||
t.Errorf("received '%v' expected '%v'", PortfolioMode.String(), portfolioCollateralStr)
|
||||
}
|
||||
if UnsetMode.String() != unsetCollateralStr {
|
||||
t.Errorf("received '%v' expected '%v'", UnsetMode.String(), unsetCollateralStr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpperCollateralType(t *testing.T) {
|
||||
t.Parallel()
|
||||
if UnknownMode.Upper() != strings.ToUpper(unknownCollateralStr) {
|
||||
t.Errorf("received '%v' expected '%v'", UnknownMode.Upper(), strings.ToUpper(unknownCollateralStr))
|
||||
}
|
||||
if SingleMode.Upper() != strings.ToUpper(singleCollateralStr) {
|
||||
t.Errorf("received '%v' expected '%v'", SingleMode.Upper(), strings.ToUpper(singleCollateralStr))
|
||||
}
|
||||
if MultiMode.Upper() != strings.ToUpper(multiCollateralStr) {
|
||||
t.Errorf("received '%v' expected '%v'", MultiMode.Upper(), strings.ToUpper(multiCollateralStr))
|
||||
}
|
||||
if PortfolioMode.Upper() != strings.ToUpper(portfolioCollateralStr) {
|
||||
t.Errorf("received '%v' expected '%v'", PortfolioMode.Upper(), strings.ToUpper(portfolioCollateralStr))
|
||||
}
|
||||
if UnsetMode.Upper() != strings.ToUpper(unsetCollateralStr) {
|
||||
t.Errorf("received '%v' expected '%v'", UnsetMode.Upper(), strings.ToUpper(unsetCollateralStr))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidCollateralTypeString(t *testing.T) {
|
||||
t.Parallel()
|
||||
if IsValidCollateralModeString("lol") {
|
||||
t.Fatal("expected 'false', received 'true'")
|
||||
}
|
||||
if !IsValidCollateralModeString("single") {
|
||||
t.Fatal("expected 'true', received 'false'")
|
||||
}
|
||||
if !IsValidCollateralModeString("multi") {
|
||||
t.Fatal("expected 'true', received 'false'")
|
||||
}
|
||||
if !IsValidCollateralModeString("portfolio") {
|
||||
t.Fatal("expected 'true', received 'false'")
|
||||
}
|
||||
if !IsValidCollateralModeString("unset") {
|
||||
t.Fatal("expected 'true', received 'false'")
|
||||
}
|
||||
if IsValidCollateralModeString("") {
|
||||
t.Fatal("expected 'false', received 'true'")
|
||||
}
|
||||
if IsValidCollateralModeString("unknown") {
|
||||
t.Fatal("expected 'false', received 'true'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringToCollateralType(t *testing.T) {
|
||||
t.Parallel()
|
||||
resp, err := StringToMode("lol")
|
||||
if !errors.Is(err, ErrInvalidCollateralMode) {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp != UnknownMode {
|
||||
t.Errorf("received '%v' expected '%v'", resp, UnknownMode)
|
||||
}
|
||||
|
||||
resp, err = StringToMode("")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp != UnsetMode {
|
||||
t.Errorf("received '%v' expected '%v'", resp, UnsetMode)
|
||||
}
|
||||
|
||||
resp, err = StringToMode("single")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp != SingleMode {
|
||||
t.Errorf("received '%v' expected '%v'", resp, SingleMode)
|
||||
}
|
||||
|
||||
resp, err = StringToMode("multi")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp != MultiMode {
|
||||
t.Errorf("received '%v' expected '%v'", resp, MultiMode)
|
||||
}
|
||||
|
||||
resp, err = StringToMode("portfolio")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp != PortfolioMode {
|
||||
t.Errorf("received '%v' expected '%v'", resp, PortfolioMode)
|
||||
}
|
||||
}
|
||||
85
exchanges/collateral/collateral_types.go
Normal file
85
exchanges/collateral/collateral_types.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package collateral
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
)
|
||||
|
||||
// Mode defines the different collateral types supported by exchanges
|
||||
// For example, FTX had a global collateral pool
|
||||
// Binance has either singular position collateral calculation
|
||||
// or cross aka asset level collateral calculation
|
||||
type Mode uint8
|
||||
|
||||
const (
|
||||
// UnsetMode is the default value
|
||||
UnsetMode Mode = 0
|
||||
// SingleMode has allocated collateral per position
|
||||
SingleMode Mode = 1 << (iota - 1)
|
||||
// MultiMode has collateral allocated across the whole asset
|
||||
MultiMode
|
||||
// PortfolioMode has collateral allocated across account
|
||||
PortfolioMode
|
||||
// UnknownMode has collateral allocated in an unknown manner at present, but is not unset
|
||||
UnknownMode
|
||||
)
|
||||
|
||||
const (
|
||||
unsetCollateralStr = "unset"
|
||||
singleCollateralStr = "single"
|
||||
multiCollateralStr = "multi"
|
||||
portfolioCollateralStr = "portfolio"
|
||||
unknownCollateralStr = "unknown"
|
||||
)
|
||||
|
||||
// ErrInvalidCollateralMode is returned when converting invalid string to collateral mode
|
||||
var ErrInvalidCollateralMode = errors.New("invalid collateral mode")
|
||||
|
||||
var supportedCollateralModes = SingleMode | MultiMode | PortfolioMode
|
||||
|
||||
// ByPosition shows how much collateral is used
|
||||
// from positions
|
||||
type ByPosition struct {
|
||||
PositionCurrency currency.Pair
|
||||
Size decimal.Decimal
|
||||
OpenOrderSize decimal.Decimal
|
||||
PositionSize decimal.Decimal
|
||||
MarkPrice decimal.Decimal
|
||||
RequiredMargin decimal.Decimal
|
||||
CollateralUsed decimal.Decimal
|
||||
}
|
||||
|
||||
// ByCurrency individual collateral contribution
|
||||
// along with what the potentially scaled collateral
|
||||
// currency it is represented as
|
||||
// eg in Bybit ScaledCurrency is USDC
|
||||
type ByCurrency struct {
|
||||
Currency currency.Code
|
||||
SkipContribution bool
|
||||
TotalFunds decimal.Decimal
|
||||
AvailableForUseAsCollateral decimal.Decimal
|
||||
CollateralContribution decimal.Decimal
|
||||
AdditionalCollateralUsed decimal.Decimal
|
||||
FairMarketValue decimal.Decimal
|
||||
Weighting decimal.Decimal
|
||||
ScaledCurrency currency.Code
|
||||
UnrealisedPNL decimal.Decimal
|
||||
ScaledUsed decimal.Decimal
|
||||
ScaledUsedBreakdown *UsedBreakdown
|
||||
Error error
|
||||
}
|
||||
|
||||
// UsedBreakdown provides a detailed
|
||||
// breakdown of where collateral is currently being allocated
|
||||
type UsedBreakdown struct {
|
||||
LockedInStakes decimal.Decimal
|
||||
LockedInNFTBids decimal.Decimal
|
||||
LockedInFeeVoucher decimal.Decimal
|
||||
LockedInSpotMarginFundingOffers decimal.Decimal
|
||||
LockedInSpotOrders decimal.Decimal
|
||||
LockedAsCollateral decimal.Decimal
|
||||
UsedInPositions decimal.Decimal
|
||||
UsedInSpotMarginBorrows decimal.Decimal
|
||||
}
|
||||
Reference in New Issue
Block a user