diff --git a/binance/spot/wallet/client.go b/binance/spot/wallet/client.go index 2dd78c4..a27f509 100644 --- a/binance/spot/wallet/client.go +++ b/binance/spot/wallet/client.go @@ -568,3 +568,115 @@ func (s *SpotWalletClient) GetApiRestrictions(ctx context.Context) (*types.ApiRe return ret, nil } + +func (s *SpotWalletClient) GetWalletBalance(ctx context.Context) ([]*types.Balance, error) { + req := spotutils.HTTPRequest{ + SecurityType: spotutils.USER_DATA, + BaseURL: s.GetBaseURL(), + Path: "/sapi/v1/asset/wallet/balance", + Method: http.MethodGet, + } + + { + headers, err := s.GenHeaders(req.SecurityType) + if err != nil { + return nil, err + } + req.Headers = headers + } + + { + query := bnutils.DefaultParam{ + RecvWindow: s.GetRecvWindow(), + Timestamp: time.Now().UnixMilli(), + } + + err := s.validate.Struct(query) + if err != nil { + return nil, err + } + + if need := s.NeedSignature(req.SecurityType); need { + signString, err := bnutils.NormalizeRequestContent(query, nil) + if err != nil { + return nil, err + } + + h := hmac.New(sha256.New, []byte(s.GetSecret())) + h.Write([]byte(signString)) + signature := hex.EncodeToString(h.Sum(nil)) + + query.Signature = signature + } + + req.Query = query + } + + resp, err := s.SendHTTPRequest(ctx, req) + if err != nil { + return nil, err + } + + var ret []*types.Balance + if err := json.Unmarshal(resp, &ret); err != nil { + return nil, err + } + + return ret, nil +} + +func (s *SpotWalletClient) GetEarnAccount(ctx context.Context) (*types.EarnAccount, error) { + req := spotutils.HTTPRequest{ + SecurityType: spotutils.USER_DATA, + BaseURL: s.GetBaseURL(), + Path: "/sapi/v1/simple-earn/account", + Method: http.MethodGet, + } + + { + headers, err := s.GenHeaders(req.SecurityType) + if err != nil { + return nil, err + } + req.Headers = headers + } + + { + query := bnutils.DefaultParam{ + RecvWindow: s.GetRecvWindow(), + Timestamp: time.Now().UnixMilli(), + } + + err := s.validate.Struct(query) + if err != nil { + return nil, err + } + + if need := s.NeedSignature(req.SecurityType); need { + signString, err := bnutils.NormalizeRequestContent(query, nil) + if err != nil { + return nil, err + } + + h := hmac.New(sha256.New, []byte(s.GetSecret())) + h.Write([]byte(signString)) + signature := hex.EncodeToString(h.Sum(nil)) + + query.Signature = signature + } + + req.Query = query + } + + resp, err := s.SendHTTPRequest(ctx, req) + if err != nil { + return nil, err + } + + var ret types.EarnAccount + if err := json.Unmarshal(resp, &ret); err != nil { + return nil, err + } + + return &ret, nil +} diff --git a/binance/spot/wallet/client_test.go b/binance/spot/wallet/client_test.go index 215cb6c..2163fb1 100644 --- a/binance/spot/wallet/client_test.go +++ b/binance/spot/wallet/client_test.go @@ -112,3 +112,17 @@ func TestGetApiRestrictions(t *testing.T) { _, err := cli.GetApiRestrictions(context.TODO()) assert.Nil(t, err) } + +func TestGetWalletBalance(t *testing.T) { + cli := testNewSpotWalletClient(t) + + _, err := cli.GetWalletBalance(context.TODO()) + assert.Nil(t, err) +} + +func TestGetEarnAccount(t *testing.T) { + cli := testNewSpotWalletClient(t) + + _, err := cli.GetEarnAccount(context.TODO()) + assert.Nil(t, err) +} diff --git a/binance/spot/wallet/types/balance.go b/binance/spot/wallet/types/balance.go new file mode 100644 index 0000000..b079fb6 --- /dev/null +++ b/binance/spot/wallet/types/balance.go @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023, LinstoHu + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package types + +type Balance struct { + Activate bool `json:"activate"` + Balance string `json:"balance"` + WalletName string `json:"walletName"` +} diff --git a/binance/spot/wallet/types/earn.go b/binance/spot/wallet/types/earn.go new file mode 100644 index 0000000..95e7e18 --- /dev/null +++ b/binance/spot/wallet/types/earn.go @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023, LinstoHu + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package types + +type EarnAccount struct { + TotalAmountInBTC string `json:"totalAmountInBTC"` + TotalAmountInUSDT string `json:"totalAmountInUSDT"` + TotalFlexibleAmountInBTC string `json:"totalFlexibleAmountInBTC"` + TotalFlexibleAmountInUSDT string `json:"totalFlexibleAmountInUSDT"` + TotalLockedInBTC string `json:"totalLockedInBTC"` + TotalLockedInUSDT string `json:"totalLockedInUSDT"` +}