Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #309 from egelis/master
Add Dividend Service
- Loading branch information
Showing
3 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters