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 reward history #11

Merged
merged 2 commits into from
May 9, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,10 @@ func (c *Client) NewGetLockedProductPosition() *GetLockedProductPositionService
return &GetLockedProductPositionService{c: c}
}

func (c *Client) NewGetFlexibleRewardHistory() *FlexibleRewardHistoryService {
return &FlexibleRewardHistoryService{c: c}
}

// NewListSavingsFlexibleProductsService get flexible products list (Savings)
func (c *Client) NewListSavingsFlexibleProductsService() *ListSavingsFlexibleProductsService {
return &ListSavingsFlexibleProductsService{c: c}
Expand Down
2 changes: 0 additions & 2 deletions v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ require (
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
102 changes: 102 additions & 0 deletions v2/savings_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,105 @@ type LockedProductPosition struct {
IsAutoRenew bool `json:"isAutoRenew,omitempty"`
RedeemDate string `json:"redeemDate,omitempty"`
}

// https://binance-docs.github.io/apidocs/spot/en/#get-flexible-rewards-history-user_data
type FlexibleRewardHistoryService struct {
c *Client
productID string
asset string
startTime int64
endTime int64
typ string
current int64
size int64
}

func (s *FlexibleRewardHistoryService) ProductID(productID string) *FlexibleRewardHistoryService {
s.productID = productID
return s
}

func (s *FlexibleRewardHistoryService) Asset(asset string) *FlexibleRewardHistoryService {
s.asset = asset
return s
}

func (s *FlexibleRewardHistoryService) StartTime(startTime int64) *FlexibleRewardHistoryService {
s.startTime = startTime
return s
}

func (s *FlexibleRewardHistoryService) EndTime(endTime int64) *FlexibleRewardHistoryService {
s.endTime = endTime
return s
}

func (s *FlexibleRewardHistoryService) Typ(typ string) *FlexibleRewardHistoryService {
s.typ = typ
return s
}

func (s *FlexibleRewardHistoryService) Current(current int64) *FlexibleRewardHistoryService {
s.current = current
return s
}

func (s *FlexibleRewardHistoryService) Size(size int64) *FlexibleRewardHistoryService {
s.size = size
return s
}

func (s *FlexibleRewardHistoryService) Do(ctx context.Context, opts ...RequestOption) (FlexibleRewardHistoryResponse, error) {
r := &request{
method: http.MethodGet,
endpoint: "/sapi/v1/simple-earn/flexible/history/rewardsRecord",
secType: secTypeSigned,
}
m := params{}
if s.asset != "" {
m["asset"] = s.asset
}
if s.startTime != 0 {
m["startTime"] = s.startTime
}
if s.endTime != 0 {
m["endTime"] = s.startTime
}
if s.productID != "" {
m["productId"] = s.productID
}
if s.current != 0 {
m["current"] = s.current
}
if s.size != 0 {
m["size"] = s.size
}
if s.typ != "" {
m["type"] = s.typ
}
r.setParams(m)
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return FlexibleRewardHistoryResponse{}, err
}

var res FlexibleRewardHistoryResponse
if err = json.Unmarshal(data, &res); err != nil {
return FlexibleRewardHistoryResponse{}, err
}
return res, nil

}

type FlexibleRewardHistoryResponse struct {
Rows []FlexibleRewardHistory `json:"rows,omitempty"`
Total int `json:"total,omitempty"`
}

type FlexibleRewardHistory struct {
Asset string `json:"asset,omitempty"`
Rewards string `json:"rewards,omitempty"`
ProjectID string `json:"projectId,omitempty"`
Typ string `json:"type,omitempty"`
Time int64 `json:"time,omitempty"`
}
59 changes: 59 additions & 0 deletions v2/savings_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,62 @@ func (s *savingsServiceTestSuite) assertLockedProductPosition(e, a *LockedProduc
r.Equal(e.IsAutoRenew, a.IsAutoRenew, "IsAutoRenew")
r.Equal(e.RedeemDate, a.RedeemDate, "RedeemDate")
}

func (s *savingsServiceTestSuite) TestGetFlexibleRewardHistoryService() {
data := []byte(`{
"rows": [
{
"asset": "BUSD",
"rewards": "0.00006408",
"projectId": "USDT001",
"type": "BONUS",
"time": 1577233578000
},
{
"asset": "USDT",
"rewards": "0.00687654",
"projectId": "USDT001",
"type": "REALTIME",
"time": 1577233562000
}
],
"total": 2
}`)

s.mockDo(data, nil)
defer s.assertDo()
s.assertReq(func(r *request) {
e := newSignedRequest().setParams(params{})
s.assertRequestEqual(e, r)
})

rewardHistoryRes, err := s.client.NewGetFlexibleRewardHistory().
Do(newContext())
r := s.r()
r.NoError(err)

r.Len(rewardHistoryRes.Rows, 2)
s.assertFlexibleRewardHistory(&FlexibleRewardHistory{
Asset: "BUSD",
Rewards: "0.00006408",
ProjectID: "USDT001",
Typ: "BONUS",
Time: 1577233578000,
}, &rewardHistoryRes.Rows[0])
s.assertFlexibleRewardHistory(&FlexibleRewardHistory{
Asset: "USDT",
Rewards: "0.00687654",
ProjectID: "USDT001",
Typ: "REALTIME",
Time: 1577233562000,
}, &rewardHistoryRes.Rows[1])
}

func (s *savingsServiceTestSuite) assertFlexibleRewardHistory(e, a *FlexibleRewardHistory) {
r := s.r()
r.Equal(e.ProjectID, a.ProjectID, "ProjectID")
r.Equal(e.Asset, a.Asset, "Asset")
r.Equal(e.Rewards, a.Rewards, "Rewards")
r.Equal(e.Typ, a.Typ, "Type")
r.Equal(e.Time, a.Time, "Time")
}
Loading