From 7e816979b4ad76d5d59fc2118568045b2ec5d6ad Mon Sep 17 00:00:00 2001 From: "minhthanh.vu" Date: Wed, 8 May 2024 14:02:14 +0700 Subject: [PATCH] add reward history --- v2/client.go | 4 ++ v2/savings_service.go | 103 +++++++++++++++++++++++++++++++++++++ v2/savings_service_test.go | 59 +++++++++++++++++++++ 3 files changed, 166 insertions(+) diff --git a/v2/client.go b/v2/client.go index 84022387..92a01afd 100644 --- a/v2/client.go +++ b/v2/client.go @@ -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} diff --git a/v2/savings_service.go b/v2/savings_service.go index 1849306a..895d9ee1 100644 --- a/v2/savings_service.go +++ b/v2/savings_service.go @@ -2,6 +2,7 @@ package binance import ( "context" + "fmt" "net/http" ) @@ -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"` +} diff --git a/v2/savings_service_test.go b/v2/savings_service_test.go index 22e1eb95..b18c1fc5 100644 --- a/v2/savings_service_test.go +++ b/v2/savings_service_test.go @@ -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") +}