diff --git a/bitfinex/definition.go b/bitfinex/definition.go deleted file mode 100644 index 84124e7..0000000 --- a/bitfinex/definition.go +++ /dev/null @@ -1,67 +0,0 @@ -package bitfinex - -import ( - "strconv" - "strings" - "time" -) - -type ( - JSONFloat64 float64 - JSONInt64 int64 - JSONTime time.Time -) - -func (t *JSONFloat64) Float64() float64 { return float64(*t) } - -func (t *JSONFloat64) UnmarshalJSON(s []byte) (err error) { - r := strings.Replace(string(s), `"`, ``, -1) - if r == "" { - return - } - - q, err := strconv.ParseFloat(r, 64) - if err != nil { - return err - } - *(*float64)(t) = q - return -} - -func (t *JSONInt64) Int64() int64 { return int64(*t) } - -func (t *JSONInt64) UnmarshalJSON(s []byte) (err error) { - r := strings.Replace(string(s), `"`, ``, -1) - if r == "" { - return - } - - q, err := strconv.ParseInt(r, 10, 64) - if err != nil { - return err - } - *(*int64)(t) = q - return -} - -func (t *JSONTime) Time() time.Time { return time.Time(*t) } - -func (t *JSONTime) UnmarshalJSON(s []byte) (err error) { - r := strings.Replace(string(s), `"`, ``, -1) - if r == "" { - return - } - - q, err := strconv.ParseInt(r, 10, 64) - if err != nil { - return err - } - *(*time.Time)(t) = time.UnixMilli(q) - return -} - -func (t *JSONTime) MarshalJSON() ([]byte, error) { - return []byte(strconv.FormatInt(time.Time(*t).UnixMilli(), 10)), nil -} - -func (t *JSONTime) String() string { return (time.Time)(*t).String() } diff --git a/bitfinex/definition_test.go b/bitfinex/definition_test.go deleted file mode 100644 index 8728da2..0000000 --- a/bitfinex/definition_test.go +++ /dev/null @@ -1,183 +0,0 @@ -package bitfinex - -import ( - "strconv" - "testing" - "time" - - "github.com/google/go-cmp/cmp" -) - -func TestJSONFloat64(t *testing.T) { - tests := []struct { - name string - in string - want float64 - wantErr bool - }{ - { - name: "zero", - in: "0", - want: 0, - }, - { - name: "negative", - in: "-1.0", - want: -1.0, - }, - { - name: "empty", - in: "", - want: 0, - }, - { - name: "float", - in: "0.10203101249012", - want: 0.10203101249012, - }, - { - name: "invalid", - in: "invalid", - wantErr: true, - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - var f JSONFloat64 - err := f.UnmarshalJSON([]byte(test.in)) - if test.wantErr { - if err == nil { - t.Fatal("expected error") - } - return - } - if err != nil { - t.Fatal(err) - } - if f.Float64() != test.want { - t.Errorf("expected %f, got %f", test.want, f.Float64()) - } - }) - } -} - -func TestJSONInt64(t *testing.T) { - tests := []struct { - name string - in string - want int64 - wantErr bool - }{ - { - name: "zero", - in: "0", - want: 0, - }, - { - name: "negative", - in: "-1", - want: -1, - }, - { - name: "empty", - in: "", - want: 0, - }, - { - name: "int", - in: "123", - want: 123, - }, - { - name: "invalid", - in: "invalid", - wantErr: true, - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - var f JSONInt64 - err := f.UnmarshalJSON([]byte(test.in)) - if test.wantErr { - if err == nil { - t.Fatal("expected error") - } - return - } - if err != nil { - t.Fatal(err) - } - if f.Int64() != test.want { - t.Errorf("expected %d, got %d", test.want, f.Int64()) - } - }) - } -} - -func TestJSONTime(t *testing.T) { - tests := []struct { - name string - in string - want time.Time - wantErr bool - }{ - { - name: "zero", - in: "0", - want: time.UnixMilli(0), - }, - { - name: "one", - in: "1", - want: time.UnixMilli(1), - }, - { - name: "empty", - in: "", - want: time.Time{}, - }, - { - name: "time", - in: "1694061154503", - want: time.UnixMilli(1694061154503), - }, - { - name: "invalid", - in: "invalid", - wantErr: true, - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - var f JSONTime - err := f.UnmarshalJSON([]byte(test.in)) - if test.wantErr { - if err == nil { - t.Fatal("expected error") - } - return - } - if err != nil { - t.Fatal(err) - } - if diff := cmp.Diff(f.Time(), test.want); diff != "" { - t.Errorf("unexpected time (-got +want): %s", diff) - } - { - got, err := f.MarshalJSON() - if err != nil { - t.Fatal(err) - } - if diff := cmp.Diff(string(got), strconv.FormatInt(test.want.UnixMilli(), 10)); diff != "" { - t.Errorf("unexpected time (-got +want): %s", diff) - } - } - if diff := cmp.Diff(f.String(), test.want.String()); diff != "" { - t.Errorf("unexpected time (-got +want): %s", diff) - } - }) - } -} diff --git a/bitfinex/v2_funding_service.go b/bitfinex/v2_funding_service.go index 5c2443f..5e424c0 100644 --- a/bitfinex/v2_funding_service.go +++ b/bitfinex/v2_funding_service.go @@ -5,6 +5,8 @@ import ( "encoding/json" "fmt" "net/http" + + "github.com/dasbd72/go-exchange-sdk/bitfinex/pkg/cast" ) type ( @@ -12,32 +14,32 @@ type ( FundingStatArray []FundingStat `json:"funding_stat_array"` } FundingStat struct { - MTS JSONInt64 `json:"mts"` - FRR JSONFloat64 `json:"frr"` - AvgPeriod JSONFloat64 `json:"avg_period"` - FundingAmount JSONFloat64 `json:"funding_amount"` - FundingAmountUsed JSONFloat64 `json:"funding_amount_used"` - FundingAmountBelowThreshold JSONFloat64 `json:"funding_amount_below_threshold"` + MTS cast.NilOrInt `json:"mts"` + FRR cast.NilOrFloat64 `json:"frr"` + AvgPeriod cast.NilOrFloat64 `json:"avg_period"` + FundingAmount cast.NilOrFloat64 `json:"funding_amount"` + FundingAmountUsed cast.NilOrFloat64 `json:"funding_amount_used"` + FundingAmountBelowThreshold cast.NilOrFloat64 `json:"funding_amount_below_threshold"` } GetActiveFundingOffers struct { FundingOfferArray []FundingOffer `json:"funding_offer_array"` } FundingOffer struct { - ID JSONInt64 `json:"id"` - Symbol string `json:"symbol"` - MTSCreated JSONInt64 `json:"mts_create"` - MTSUpdated JSONInt64 `json:"mts_update"` - Amount JSONFloat64 `json:"amount"` - AmountOrig JSONFloat64 `json:"amount_orig"` - Type string `json:"type"` - Flags interface{} `json:"flags"` - Status string `json:"status"` - Rate JSONFloat64 `json:"rate"` - Period JSONInt64 `json:"period"` - Notify bool `json:"notify"` - Hidden bool `json:"hidden"` - Renew bool `json:"renew"` + ID cast.NilOrInt `json:"id"` + Symbol cast.NilOrString `json:"symbol"` + MTSCreated cast.NilOrInt `json:"mts_create"` + MTSUpdated cast.NilOrInt `json:"mts_update"` + Amount cast.NilOrFloat64 `json:"amount"` + AmountOrig cast.NilOrFloat64 `json:"amount_orig"` + Type cast.NilOrString `json:"type"` + Flags interface{} `json:"flags"` + Status cast.NilOrString `json:"status"` + Rate cast.NilOrFloat64 `json:"rate"` + Period cast.NilOrInt `json:"period"` + Notify cast.NilOrInt `json:"notify"` + Hidden cast.NilOrInt `json:"hidden"` + Renew cast.NilOrInt `json:"renew"` } ) @@ -54,12 +56,12 @@ func (data *GetFundingStats) FromRaw(raw []byte) error { } } data.FundingStatArray = append(data.FundingStatArray, FundingStat{ - MTS: JSONInt64(v[0].(float64)), - FRR: JSONFloat64(v[3].(float64)), - AvgPeriod: JSONFloat64(v[4].(float64)), - FundingAmount: JSONFloat64(v[7].(float64)), - FundingAmountUsed: JSONFloat64(v[8].(float64)), - FundingAmountBelowThreshold: JSONFloat64(v[11].(float64)), + MTS: cast.IfToNilOrInt(v[0]), + FRR: cast.IfToNilOrFloat64(v[3]), + AvgPeriod: cast.IfToNilOrFloat64(v[4]), + FundingAmount: cast.IfToNilOrFloat64(v[7]), + FundingAmountUsed: cast.IfToNilOrFloat64(v[8]), + FundingAmountBelowThreshold: cast.IfToNilOrFloat64(v[11]), }) } return nil @@ -95,20 +97,20 @@ func (data *GetActiveFundingOffers) FromRaw(raw []byte) error { } } data.FundingOfferArray = append(data.FundingOfferArray, FundingOffer{ - ID: JSONInt64(v[0].(float64)), - Symbol: v[1].(string), - MTSCreated: JSONInt64(v[2].(float64)), - MTSUpdated: JSONInt64(v[3].(float64)), - Amount: JSONFloat64(v[4].(float64)), - AmountOrig: JSONFloat64(v[5].(float64)), - Type: v[6].(string), - Flags: v[7], - Status: v[8].(string), - Rate: JSONFloat64(v[9].(float64)), - Period: JSONInt64(v[10].(float64)), - Notify: v[11].(bool), - Hidden: v[12].(bool), - Renew: v[13].(bool), + ID: cast.IfToNilOrInt(v[0]), + Symbol: cast.IfToNilOrString(v[1]), + MTSCreated: cast.IfToNilOrInt(v[2]), + MTSUpdated: cast.IfToNilOrInt(v[3]), + Amount: cast.IfToNilOrFloat64(v[4]), + AmountOrig: cast.IfToNilOrFloat64(v[5]), + Type: cast.IfToNilOrString(v[6]), + Flags: cast.IfToNilOrString(v[7]), + Status: cast.IfToNilOrString(v[8]), + Rate: cast.IfToNilOrFloat64(v[9]), + Period: cast.IfToNilOrInt(v[10]), + Notify: cast.IfToNilOrInt(v[11]), + Hidden: cast.IfToNilOrInt(v[12]), + Renew: cast.IfToNilOrInt(v[13]), }) } return nil @@ -123,7 +125,6 @@ func (c *Client) GetActiveFundingOffers(ctx context.Context, symbol string, opts if err != nil { return nil, err } - fmt.Println(string(res)) data := &GetActiveFundingOffers{} err = data.FromRaw(res) if err != nil { diff --git a/bitfinex/v2_wallet_service.go b/bitfinex/v2_wallet_service.go index 9a857e6..1badef2 100644 --- a/bitfinex/v2_wallet_service.go +++ b/bitfinex/v2_wallet_service.go @@ -4,6 +4,8 @@ import ( "context" "encoding/json" "net/http" + + "github.com/dasbd72/go-exchange-sdk/bitfinex/pkg/cast" ) type ( @@ -11,13 +13,13 @@ type ( Wallet []Wallet `json:"wallet"` } Wallet struct { - Type string `json:"type"` - Currency string `json:"currency"` - Balance JSONFloat64 `json:"balance"` - UnsettledInterest JSONFloat64 `json:"unsettled_interest"` - AvailableBalance JSONFloat64 `json:"available_balance"` - LastChange string `json:"last_change"` - LastChangeMetadata interface{} `json:"last_change_metadata"` + Type cast.NilOrString + Currency cast.NilOrString + Balance cast.NilOrFloat64 + UnsettledInterest cast.NilOrFloat64 + AvailableBalance cast.NilOrFloat64 + LastChange cast.NilOrString + LastChangeMetadata interface{} } ) @@ -28,18 +30,13 @@ func (data *GetWalletsResponse) FromRaw(raw []byte) error { return err } for _, v := range container { - for i, vv := range v { - if vv == nil { - v[i] = "" - } - } data.Wallet = append(data.Wallet, Wallet{ - Type: v[0].(string), - Currency: v[1].(string), - Balance: JSONFloat64(v[2].(float64)), - UnsettledInterest: JSONFloat64(v[3].(float64)), - AvailableBalance: JSONFloat64(v[4].(float64)), - LastChange: v[5].(string), + Type: cast.IfToNilOrString(v[0]), + Currency: cast.IfToNilOrString(v[1]), + Balance: cast.IfToNilOrFloat64(v[2]), + UnsettledInterest: cast.IfToNilOrFloat64(v[3]), + AvailableBalance: cast.IfToNilOrFloat64(v[4]), + LastChange: cast.IfToNilOrString(v[5]), LastChangeMetadata: v[6], }) } diff --git a/manager/balance.go b/manager/balance.go index 31878ba..d224cd1 100644 --- a/manager/balance.go +++ b/manager/balance.go @@ -99,7 +99,7 @@ func (c *Client) GetBalance(ctx context.Context) (*Balance, error) { return err } for _, w := range res.Wallet { - if w.Currency == "USD" || w.Currency == "UST" { + if w.Currency.String() == "USD" || w.Currency.String() == "UST" { sum += w.Balance.Float64() } }