mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-04 15:10:54 +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:
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user