Skip to content

Commit

Permalink
feat: add binance wallet apis
Browse files Browse the repository at this point in the history
  • Loading branch information
linstohu committed Oct 25, 2023
1 parent ac2c668 commit 1dc4b7c
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
112 changes: 112 additions & 0 deletions binance/spot/wallet/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 14 additions & 0 deletions binance/spot/wallet/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
24 changes: 24 additions & 0 deletions binance/spot/wallet/types/balance.go
Original file line number Diff line number Diff line change
@@ -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"`
}
27 changes: 27 additions & 0 deletions binance/spot/wallet/types/earn.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit 1dc4b7c

Please sign in to comment.