Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dividend Service #309

Merged
merged 1 commit into from Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 88 additions & 0 deletions v2/asset_dividend_service.go
@@ -0,0 +1,88 @@
package binance

import (
"context"
"encoding/json"
)

// AssetDividendService fetches the saving purchases
type AssetDividendService struct {
c *Client
asset *string
startTime *int64
endTime *int64
limit *int
}

// Asset sets the asset parameter.
func (s *AssetDividendService) Asset(asset string) *AssetDividendService {
s.asset = &asset
return s
}

// Limit sets the limit parameter.
func (s *AssetDividendService) Limit(limit int) *AssetDividendService {
s.limit = &limit
return s
}

// StartTime sets the startTime parameter.
// If present, EndTime MUST be specified. The difference between EndTime - StartTime MUST be between 0-90 days.
func (s *AssetDividendService) StartTime(startTime int64) *AssetDividendService {
s.startTime = &startTime
return s
}

// EndTime sets the endTime parameter.
// If present, StartTime MUST be specified. The difference between EndTime - StartTime MUST be between 0-90 days.
func (s *AssetDividendService) EndTime(endTime int64) *AssetDividendService {
s.endTime = &endTime
return s
}

// Do sends the request.
func (s *AssetDividendService) Do(ctx context.Context) (*DividendResponseWrapper, error) {
r := &request{
method: "GET",
endpoint: "/sapi/v1/asset/assetDividend",
secType: secTypeSigned,
}
if s.asset != nil {
r.setParam("asset", *s.asset)
}
if s.limit != nil {
r.setParam("limit", *s.limit)
} else {
r.setParam("limit", 20)
}
if s.startTime != nil {
r.setParam("startTime", *s.startTime)
}
if s.endTime != nil {
r.setParam("endTime", *s.endTime)
}
data, err := s.c.callAPI(ctx, r)
if err != nil {
return nil, err
}
res := new(DividendResponseWrapper)
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}

// DividendResponseWrapper represents a wrapper around a AssetDividendService.
type DividendResponseWrapper struct {
Rows *[]DividendResponse `json:"rows"`
}

// DividendResponse represents a response from AssetDividendService.
type DividendResponse struct {
Amount string `json:"amount"`
Asset string `json:"asset"`
Info string `json:"enInfo"`
Time int64 `json:"divTime"`
TranID int64 `json:"tranId"`
}
91 changes: 91 additions & 0 deletions v2/asset_dividend_service_test.go
@@ -0,0 +1,91 @@
package binance

import (
"context"
"testing"

"github.com/stretchr/testify/suite"
)

type assetDividendServiceTestSuite struct {
baseTestSuite
}

func TestAssetDividendService(t *testing.T) {
suite.Run(t, new(assetDividendServiceTestSuite))
}

func (s *assetDividendServiceTestSuite) TestListAssetDividend() {
data := []byte(`
{
"rows":[
{
"amount":"10.00000000",
"asset":"BHFT",
"divTime":1563189166000,
"enInfo":"BHFT distribution",
"tranId":2968885920
},
{
"amount":"10.00000000",
"asset":"BHFT",
"divTime":1563189165000,
"enInfo":"BHFT distribution",
"tranId":2968885920
}
],
"total":2
}
`)
s.mockDo(data, nil)
defer s.assertDo()

asset := `BHFT`
startTime := int64(1508198532000)
endTime := int64(1508198532001)
s.assertReq(func(r *request) {
e := newSignedRequest().setParams(params{
`asset`: asset,
`limit`: 2,
`startTime`: startTime,
`endTime`: endTime,
})
s.assertRequestEqual(e, r)
})

dividend, err := s.client.NewAssetDividendService().
Asset(asset).
StartTime(startTime).
EndTime(endTime).
Limit(2).
Do(context.Background())
r := s.r()
r.NoError(err)
rows := *dividend.Rows

s.Len(rows, 2)
s.assertDividendEqual(&DividendResponse{
Amount: `10.00000000`,
Asset: `BHFT`,
Time: 1563189166000,
Info: `BHFT distribution`,
TranID: 2968885920,
}, &rows[0])
s.assertDividendEqual(&DividendResponse{
Amount: `10.00000000`,
Asset: `BHFT`,
Time: 1563189165000,
Info: `BHFT distribution`,
TranID: 2968885920,
}, &rows[1])
}

func (s *assetDividendServiceTestSuite) assertDividendEqual(e, a *DividendResponse) {
r := s.r()
r.Equal(e.Amount, `10.00000000`, `Amount`)
r.Equal(e.Amount, a.Amount, `Amount`)
r.Equal(e.Info, a.Info, `Info`)
r.Equal(e.Asset, a.Asset, `Asset`)
r.Equal(e.Time, a.Time, `Time`)
r.Equal(e.TranID, a.TranID, `TranID`)
}
5 changes: 5 additions & 0 deletions v2/client.go
Expand Up @@ -632,3 +632,8 @@ func (c *Client) NewListDustLogService() *ListDustLogService {
func (c *Client) NewDustTransferService() *DustTransferService {
return &DustTransferService{c: c}
}

// NewAssetDividendService init the asset dividend list service
func (c *Client) NewAssetDividendService() *AssetDividendService {
return &AssetDividendService{c: c}
}