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

Implement subaccount transfer history and refactor user universal transfer #567

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Changes from 1 commit
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
Next Next commit
feat: add sub-account transfer history api
wayne163 committed Jun 5, 2024
commit 3d133984d343859b8711388f458925ddfe241b46
11 changes: 11 additions & 0 deletions v2/client.go
Original file line number Diff line number Diff line change
@@ -102,6 +102,9 @@ type RateLimitInterval string
// AccountType define the account types
type AccountType string

// SubAccountTransferType define the sub account transfer types
type SubAccountTransferType int

// Endpoints
var (
BaseAPIMainURL = "https://api.binance.com"
@@ -239,6 +242,9 @@ const (
AccountTypeIsolatedMargin AccountType = "ISOLATED_MARGIN"
AccountTypeUSDTFuture AccountType = "USDT_FUTURE"
AccountTypeCoinFuture AccountType = "COIN_FUTURE"

SubAccountTransferTypeTransferIn SubAccountTransferType = 1
SubAccountTransferTypeTransferOut SubAccountTransferType = 2
)

func currentTimestamp() int64 {
@@ -1027,3 +1033,8 @@ func (c *Client) NewSubAccountFuturesSummaryV1Service() *SubAccountFuturesSummar
func (c *Client) NewSubAccountFuturesTransferV1Service() *SubAccountFuturesTransferV1Service {
return &SubAccountFuturesTransferV1Service{c: c}
}

// NewSubAccountTransferHistoryService Transfer History for Sub-account (For Sub-account)
func (c *Client) NewSubAccountTransferHistoryService() *SubAccountTransferHistoryService {
return &SubAccountTransferHistoryService{c: c}
}
94 changes: 94 additions & 0 deletions v2/subaccount_service.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package binance
import (
"context"
"net/http"
"time"
)

// TransferToSubAccountService transfer to subaccount
@@ -674,3 +675,96 @@ type SubAccountFuturesTransferResponse struct {
// seems api doc bug, return `tranId` as int64 actually in production environment
TranID int64 `json:"tranId"`
}

// Sub-account Transfer History (For Sub-account)
// https://binance-docs.github.io/apidocs/spot/en/#sub-account-transfer-history-for-sub-account
type SubAccountTransferHistoryService struct {
c *Client
asset *string
transferType *SubAccountTransferType
startTime *int64
endTime *int64
limit *int
returnFailHistory *bool
}

func (s *SubAccountTransferHistoryService) Asset(v string) *SubAccountTransferHistoryService {
s.asset = &v
return s
}

func (s *SubAccountTransferHistoryService) TransferType(v SubAccountTransferType) *SubAccountTransferHistoryService {
s.transferType = &v
return s
}

func (s *SubAccountTransferHistoryService) StartTime(t time.Time) *SubAccountTransferHistoryService {
startTimestamp := t.UnixMilli()
s.startTime = &startTimestamp
return s
}

func (s *SubAccountTransferHistoryService) EndTime(t time.Time) *SubAccountTransferHistoryService {
endTimestamp := t.UnixMilli()
s.endTime = &endTimestamp
return s
}

func (s *SubAccountTransferHistoryService) Limit(v int) *SubAccountTransferHistoryService {
s.limit = &v
return s
}

func (s *SubAccountTransferHistoryService) ReturnFailHistory(v bool) *SubAccountTransferHistoryService {
s.returnFailHistory = &v
return s
}

func (s *SubAccountTransferHistoryService) Do(ctx context.Context, opts ...RequestOption) (res []*SubAccountTransferHistory, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/sapi/v1/sub-account/transfer/subUserHistory",
secType: secTypeSigned,
}
if s.asset != nil {
r.setParam("asset", *s.asset)
}
if s.transferType != nil {
r.setParam("type", *s.transferType)
}
if s.startTime != nil {
r.setParam("startTime", *s.startTime)
}
if s.endTime != nil {
r.setParam("endTime", *s.endTime)
}
if s.limit != nil {
r.setParam("limit", *s.limit)
}
if s.returnFailHistory != nil {
r.setParam("returnFailHistory", *s.returnFailHistory)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res = make([]*SubAccountTransferHistory, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return nil, err
}
return res, nil
}

type SubAccountTransferHistory struct {
CounterParty string `json:"counterParty"`
Email string `json:"email"`
Type SubAccountTransferType `json:"type"`
Asset string `json:"asset"`
Qty string `json:"qty"`
FromAccountType AccountType `json:"fromAccountType"`
ToAccountType AccountType `json:"toAccountType"`
Status string `json:"status"`
TranID int64 `json:"tranId"`
Time int64 `json:"time"`
}
82 changes: 82 additions & 0 deletions v2/subaccount_service_test.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package binance

import (
"testing"
"time"

"github.com/stretchr/testify/suite"
)
@@ -333,3 +334,84 @@ func (s *subAccountServiceTestSuite) TestSubAccountFuturesTransferService() {
r.Equal(int64(123456789), response.TranID, "TranID")

}

func (s *subAccountServiceTestSuite) TestSubAccountTransferHistoryService() {
data := []byte(`
[
{
"counterParty":"master",
"email":"[email protected]",
"type":1,
"asset":"BTC",
"qty":"1",
"fromAccountType":"SPOT",
"toAccountType":"SPOT",
"status":"SUCCESS",
"tranId":11798835829,
"time":1544433325000
},
{
"counterParty": "subAccount",
"email": "[email protected]",
"type": 2,
"asset":"ETH",
"qty":"2",
"fromAccountType":"SPOT",
"toAccountType":"COIN_FUTURE",
"status":"SUCCESS",
"tranId":11798829519,
"time":1544433326000
}
]
`)
s.mockDo(data, nil)
defer s.assertDo()

transferType := SubAccountTransferTypeTransferIn
startTime := time.Date(2018, 9, 15, 0, 0, 0, 0, time.UTC)
endTime := time.Date(2018, 9, 16, 0, 0, 0, 0, time.UTC)

s.assertReq(func(r *request) {
e := newSignedRequest().setParams(params{
"type": int(transferType),
"startTime": startTime.UnixMilli(),
"endTime": endTime.UnixMilli(),
})
s.assertRequestEqual(e, r)
})

response, err := s.client.NewSubAccountTransferHistoryService().
TransferType(transferType).
StartTime(startTime).
EndTime(endTime).
Do(newContext())

r := s.r()
r.NoError(err)
s.assertSubAccountTransferHistoryEqual(&SubAccountTransferHistory{
CounterParty: "master",
Email: "[email protected]",
Type: 1,
Asset: "BTC",
Qty: "1",
FromAccountType: "SPOT",
ToAccountType: "SPOT",
Status: "SUCCESS",
TranID: 11798835829,
Time: 1544433325000,
}, response[0])
}

func (s *subAccountServiceTestSuite) assertSubAccountTransferHistoryEqual(e, a *SubAccountTransferHistory) {
r := s.r()
r.Equal(e.CounterParty, a.CounterParty, "CounterParty")
r.Equal(e.Email, a.Email, "Email")
r.Equal(e.Type, a.Type, "Type")
r.Equal(e.Asset, a.Asset, "Asset")
r.Equal(e.Qty, a.Qty, "Qty")
r.Equal(e.FromAccountType, a.FromAccountType, "FromAccountType")
r.Equal(e.ToAccountType, a.ToAccountType, "ToAccountType")
r.Equal(e.Status, a.Status, "Status")
r.Equal(e.TranID, a.TranID, "TranId")
r.Equal(e.Time, a.Time, "Time")
}