bybit: assortment of updates (#1192)

* bybit: cherry-pickable

* bybit: implement fee fetching v5

* bybit: update to use nullable type

* bybit: fix some tests

* bybit: spell check fix

* remove redunant asset dec, and rm output

* rm comment code

* linter: fixerinos woooo

* bybit: constrict rate limit on public spot to v5

* exchanges/bybit/limits: update (CHERRY PICK ME)

* glorious: nIIIIIIIIIIIIIIITS

* glorious: nits continued

* updated comment

* update even more

* RM LINE!

* glorious: nits

* Update exchanges/sharedtestvalues/sharedtestvalues.go

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

* fix

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
Ryan O'Hara-Reid
2023-06-16 12:27:34 +10:00
committed by GitHub
parent 698b6716ea
commit 981a08af83
27 changed files with 1711 additions and 1216 deletions

View File

@@ -1,6 +1,8 @@
package convert
import (
"bytes"
"errors"
"fmt"
"math"
"strconv"
@@ -10,6 +12,10 @@ import (
"github.com/shopspring/decimal"
)
const jsonStringIdent = `"` // immutable byte sequence
var errUnhandledType = errors.New("unhandled type")
// FloatFromString format
func FloatFromString(raw interface{}) (float64, error) {
str, ok := raw.(string)
@@ -187,3 +193,36 @@ func InterfaceToStringOrZeroValue(r interface{}) string {
}
return ""
}
// StringToFloat64 is a float64 that unmarshals from a string. This is useful
// for APIs that return numbers as strings and return an empty string instead of
// 0.
type StringToFloat64 float64
// UnmarshalJSON implements the json.Unmarshaler interface.
// This implementation is slightly more performant than calling json.Unmarshal
// again.
func (f *StringToFloat64) UnmarshalJSON(data []byte) error {
if !bytes.HasPrefix(data, []byte(jsonStringIdent)) {
return fmt.Errorf("%w: %s", errUnhandledType, string(data))
}
data = data[1 : len(data)-1] // Remove quotes
if len(data) == 0 {
*f = StringToFloat64(0)
return nil
}
val, err := strconv.ParseFloat(string(data), 64)
if err != nil {
return err
}
*f = StringToFloat64(val)
return nil
}
// Float64 returns the float64 value of the FloatString.
func (f *StringToFloat64) Float64() float64 {
return float64(*f)
}

View File

@@ -1,6 +1,8 @@
package convert
import (
"encoding/json"
"errors"
"strings"
"testing"
"time"
@@ -315,3 +317,54 @@ func TestInterfaceToStringOrZeroValue(t *testing.T) {
t.Errorf("expected meow, got: %v", x)
}
}
func TestStringToFloat64(t *testing.T) {
t.Parallel()
resp := struct {
Data StringToFloat64 `json:"data"`
}{}
err := json.Unmarshal([]byte(`{"data":"0.00000001"}`), &resp)
if err != nil {
t.Fatal(err)
}
if resp.Data.Float64() != 1e-8 {
t.Fatalf("expected 1e-8, got %v", resp.Data.Float64())
}
err = json.Unmarshal([]byte(`{"data":""}`), &resp)
if err != nil {
t.Fatal(err)
}
err = json.Unmarshal([]byte(`{"data":1337.37}`), &resp)
if !errors.Is(err, errUnhandledType) {
t.Fatalf("received %v but expected %v", err, errUnhandledType)
}
// Demonstrates that a suffix check is not needed.
err = json.Unmarshal([]byte(`{"data":"1337.37}`), &resp)
if err == nil {
t.Fatal("error cannot be nil")
}
err = json.Unmarshal([]byte(`{"data":"MEOW"}`), &resp)
if err == nil {
t.Fatal("error cannot be nil")
}
}
// 2677173 428.9 ns/op 240 B/op 5 allocs/op
func BenchmarkStringToFloat64(b *testing.B) {
resp := struct {
Data StringToFloat64 `json:"data"`
}{}
for i := 0; i < b.N; i++ {
err := json.Unmarshal([]byte(`{"data":"0.00000001"}`), &resp)
if err != nil {
b.Fatal(err)
}
}
}