Skip to content

Commit

Permalink
add reward history
Browse files Browse the repository at this point in the history
  • Loading branch information
ngocthanh1389 committed May 8, 2024
1 parent c7e4381 commit 7e81697
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
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() *GetFlexibleRewardHistoryService {
return &GetFlexibleRewardHistoryService{c: c}
}

// NewListSavingsFlexibleProductsService get flexible products list (Savings)
func (c *Client) NewListSavingsFlexibleProductsService() *ListSavingsFlexibleProductsService {
return &ListSavingsFlexibleProductsService{c: c}
Expand Down
103 changes: 103 additions & 0 deletions v2/savings_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package binance

import (
"context"
"fmt"
"net/http"
)

Expand Down Expand Up @@ -588,3 +589,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 GetFlexibleRewardHistoryService struct {
c *Client
productID string
asset string
startTime int64
endTime int64
typ string
current int64
size int64
}

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

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

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

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

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

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

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

func (s *GetFlexibleRewardHistoryService) Do(ctx context.Context, opts ...RequestOption) (GetFlexibleRewardHistoryResponse, error) {
r := &request{
method: http.MethodGet,
endpoint: "/sapi/v1/simple-earn/flexible/history/rewardRecord",
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 GetFlexibleRewardHistoryResponse{}, err
}

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

}

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

type GetFlexibleRewardHistory 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(&GetFlexibleRewardHistory{
Asset: "BUSD",
Rewards: "0.00006408",
ProjectID: "USDT001",
Typ: "BONUS",
Time: 1577233578000,
}, &rewardHistoryRes.Rows[0])
s.assertFlexibleRewardHistory(&GetFlexibleRewardHistory{
Asset: "USDT",
Rewards: "0.00687654",
ProjectID: "USDT001",
Typ: "REALTIME",
Time: 1577233562000,
}, &rewardHistoryRes.Rows[1])
}

func (s *savingsServiceTestSuite) assertFlexibleRewardHistory(e, a *GetFlexibleRewardHistory) {
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")
}

0 comments on commit 7e81697

Please sign in to comment.