Skip to content

Commit d78c019

Browse files
authored
Merge pull request #673 from XZB-1248/master
Implement WalletBalanceService
2 parents 9c0eac5 + 6e3b837 commit d78c019

7 files changed

+237
-1
lines changed

examples/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ func main() {
44
// Ticker()
55
// Ohlcv()
66
// SpotOrder()
7-
FuturesOrder()
7+
// FuturesOrder()
8+
WalletBalance()
89
}

examples/wallet.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/adshao/go-binance/v2"
8+
)
9+
10+
func WalletBalance() {
11+
apiKey := ""
12+
secret := ""
13+
client := binance.NewClient(apiKey, secret)
14+
15+
quoteAsset := "USDT"
16+
17+
res, err := client.NewWalletBalanceService().
18+
QuoteAsset(quoteAsset).
19+
Do(context.Background())
20+
21+
if err != nil {
22+
fmt.Println(err)
23+
return
24+
}
25+
26+
for _, w := range res {
27+
fmt.Printf("%s: %s\n", w.WalletName, w.Balance)
28+
}
29+
30+
}

v2/client.go

+4
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,10 @@ func (c *Client) NewGetAssetDetailService() *GetAssetDetailService {
769769
return &GetAssetDetailService{c: c}
770770
}
771771

772+
func (c *Client) NewWalletBalanceService() *WalletBalanceService {
773+
return &WalletBalanceService{c: c}
774+
}
775+
772776
// NewAveragePriceService init average price service
773777
func (c *Client) NewAveragePriceService() *AveragePriceService {
774778
return &AveragePriceService{c: c}
File renamed without changes.
File renamed without changes.

v2/wallet_balance_service.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package binance
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
)
8+
9+
// WalletBalanceService fetches all user wallet balance.
10+
//
11+
// See https://developers.binance.com/docs/wallet/asset/query-user-wallet-balance
12+
type WalletBalanceService struct {
13+
c *Client
14+
quoteAsset *string
15+
}
16+
17+
// QuoteAsset sets the quoteAsset parameter.
18+
func (s *WalletBalanceService) QuoteAsset(asset string) *WalletBalanceService {
19+
s.quoteAsset = &asset
20+
return s
21+
}
22+
23+
// Do sends the request.
24+
func (s *WalletBalanceService) Do(ctx context.Context, opts ...RequestOption) (res []*WalletBalance, err error) {
25+
r := &request{
26+
method: http.MethodGet,
27+
endpoint: "/sapi/v1/asset/wallet/balance",
28+
secType: secTypeSigned,
29+
}
30+
if s.quoteAsset != nil {
31+
r.setParam("quoteAsset", *s.quoteAsset)
32+
}
33+
34+
data, err := s.c.callAPI(ctx, r, opts...)
35+
if err != nil {
36+
return nil, err
37+
}
38+
res = make([]*WalletBalance, 0)
39+
err = json.Unmarshal(data, &res)
40+
if err != nil {
41+
return nil, err
42+
}
43+
return res, nil
44+
}
45+
46+
// WalletBalanceResponse defines the response of WalletBalanceService
47+
type WalletBalance struct {
48+
Activate bool `json:"activate"`
49+
Balance string `json:"balance"`
50+
WalletName string `json:"walletName"`
51+
}

v2/wallet_balance_service_test.go

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package binance
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/suite"
7+
)
8+
9+
type walletBalanceServiceTestSuite struct {
10+
baseTestSuite
11+
}
12+
13+
func TestWalletBalanceService(t *testing.T) {
14+
suite.Run(t, new(walletBalanceServiceTestSuite))
15+
}
16+
17+
func (s *walletBalanceServiceTestSuite) TestWalletBalance() {
18+
data := []byte(`[
19+
{
20+
"activate": true,
21+
"balance": "0.00000551",
22+
"walletName": "Spot"
23+
},
24+
{
25+
"activate": true,
26+
"balance": "0",
27+
"walletName": "Funding"
28+
},
29+
{
30+
"activate": true,
31+
"balance": "0",
32+
"walletName": "Cross Margin"
33+
},
34+
{
35+
"activate": true,
36+
"balance": "0",
37+
"walletName": "Isolated Margin"
38+
},
39+
{
40+
"activate": true,
41+
"balance": "0",
42+
"walletName": "USDⓈ-M Futures"
43+
},
44+
{
45+
"activate": true,
46+
"balance": "0",
47+
"walletName": "COIN-M Futures"
48+
},
49+
{
50+
"activate": true,
51+
"balance": "0",
52+
"walletName": "Earn"
53+
},
54+
{
55+
"activate": true,
56+
"balance": "0",
57+
"walletName": "Options"
58+
},
59+
{
60+
"activate": true,
61+
"balance": "0",
62+
"walletName": "Trading Bots"
63+
},
64+
{
65+
"activate": true,
66+
"balance": "0.01630125",
67+
"walletName": "Copy Trading"
68+
}
69+
]`)
70+
s.mockDo(data, nil)
71+
defer s.assertDo()
72+
73+
quoteAsset := "USDT"
74+
75+
s.assertReq(func(r *request) {
76+
e := newSignedRequest().setParam("quoteAsset", quoteAsset)
77+
s.assertRequestEqual(e, r)
78+
})
79+
80+
res, err := s.client.NewWalletBalanceService().QuoteAsset(quoteAsset).Do(newContext())
81+
s.r().NoError(err)
82+
83+
e := []*WalletBalance{
84+
{
85+
Activate: true,
86+
Balance: "0.00000551",
87+
WalletName: "Spot",
88+
},
89+
{
90+
Activate: true,
91+
Balance: "0",
92+
WalletName: "Funding",
93+
},
94+
{
95+
Activate: true,
96+
Balance: "0",
97+
WalletName: "Cross Margin",
98+
},
99+
{
100+
Activate: true,
101+
Balance: "0",
102+
WalletName: "Isolated Margin",
103+
},
104+
{
105+
Activate: true,
106+
Balance: "0",
107+
WalletName: "USDⓈ-M Futures",
108+
},
109+
{
110+
Activate: true,
111+
Balance: "0",
112+
WalletName: "COIN-M Futures",
113+
},
114+
{
115+
Activate: true,
116+
Balance: "0",
117+
WalletName: "Earn",
118+
},
119+
{
120+
Activate: true,
121+
Balance: "0",
122+
WalletName: "Options",
123+
},
124+
{
125+
Activate: true,
126+
Balance: "0",
127+
WalletName: "Trading Bots",
128+
},
129+
{
130+
Activate: true,
131+
Balance: "0.01630125",
132+
WalletName: "Copy Trading",
133+
},
134+
}
135+
s.assertWalletBalancesEqual(e, res)
136+
}
137+
138+
func (s *walletBalanceServiceTestSuite) assertWalletBalanceEqual(e, a *WalletBalance) {
139+
r := s.r()
140+
r.Equal(e.Activate, a.Activate, "Activate")
141+
r.Equal(e.Balance, a.Balance, "Balance")
142+
r.Equal(e.WalletName, a.WalletName, "WalletName")
143+
}
144+
145+
func (s *walletBalanceServiceTestSuite) assertWalletBalancesEqual(e, a []*WalletBalance) {
146+
s.r().Len(e, len(a))
147+
for i := range e {
148+
s.assertWalletBalanceEqual(e[i], a[i])
149+
}
150+
}

0 commit comments

Comments
 (0)