Skip to content

Commit

Permalink
Feature/subaccount service (#288)
Browse files Browse the repository at this point in the history
* add TransferToSubAccountService

* fix type

* remove test method

Co-authored-by: adshao <tjusgj@gmail.com>
  • Loading branch information
nandubatchu and adshao committed Dec 16, 2021
1 parent 06aa4e0 commit db2e228
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
5 changes: 5 additions & 0 deletions v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,11 @@ func (c *Client) NewDustTransferService() *DustTransferService {
return &DustTransferService{c: c}
}

// NewTransferToSubAccountService transfer to subaccount service
func (c *Client) NewTransferToSubAccountService() *TransferToSubAccountService {
return &TransferToSubAccountService{c: c}
}

// NewAssetDividendService init the asset dividend list service
func (c *Client) NewAssetDividendService() *AssetDividendService {
return &AssetDividendService{c: c}
Expand Down
70 changes: 70 additions & 0 deletions v2/subaccount_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package binance

import (
"context"
"encoding/json"
)

// TransferToSubAccountService transfer to subaccount
type TransferToSubAccountService struct {
c *Client
toEmail string
asset string
amount string
}

// ToEmail set toEmail
func (s *TransferToSubAccountService) ToEmail(toEmail string) *TransferToSubAccountService {
s.toEmail = toEmail
return s
}

// Asset set asset
func (s *TransferToSubAccountService) Asset(asset string) *TransferToSubAccountService {
s.asset = asset
return s
}

// Amount set amount
func (s *TransferToSubAccountService) Amount(amount string) *TransferToSubAccountService {
s.amount = amount
return s
}

func (s *TransferToSubAccountService) transferToSubaccount(ctx context.Context, endpoint string, opts ...RequestOption) (data []byte, err error) {
r := &request{
method: "POST",
endpoint: endpoint,
secType: secTypeSigned,
}
m := params{
"toEmail": s.toEmail,
"asset": s.asset,
"amount": s.amount,
}
r.setParams(m)
data, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return []byte{}, err
}
return data, nil
}

// Do send request
func (s *TransferToSubAccountService) Do(ctx context.Context, opts ...RequestOption) (res *TransferToSubAccountResponse, err error) {
data, err := s.transferToSubaccount(ctx, "/sapi/v1/sub-account/transfer/subToSub", opts...)
if err != nil {
return nil, err
}
res = &TransferToSubAccountResponse{}
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}

// TransferToSubAccountResponse define transfer to subaccount response
type TransferToSubAccountResponse struct {
TxnID int64 `json:"txnId"`
}

0 comments on commit db2e228

Please sign in to comment.