mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
binanceapi: add margin transfer api
This commit is contained in:
parent
0c6b68c4f6
commit
92691eda24
|
@ -0,0 +1,27 @@
|
|||
package binanceapi
|
||||
|
||||
import (
|
||||
"github.com/c9s/requestgen"
|
||||
)
|
||||
|
||||
type TransferResponse struct {
|
||||
TranId int `json:"tranId"`
|
||||
}
|
||||
|
||||
//go:generate requestgen -method POST -url "/sapi/v1/margin/transfer" -type TransferCrossMarginAccountRequest -responseType .TransferResponse
|
||||
type TransferCrossMarginAccountRequest struct {
|
||||
client requestgen.AuthenticatedAPIClient
|
||||
|
||||
asset string `param:"asset"`
|
||||
|
||||
// transferType:
|
||||
// 1: transfer from main account to cross margin account
|
||||
// 2: transfer from cross margin account to main account
|
||||
transferType int `param:"type"`
|
||||
|
||||
amount string `param:"amount"`
|
||||
}
|
||||
|
||||
func (c *RestClient) NewTransferCrossMarginAccountRequest() *TransferCrossMarginAccountRequest {
|
||||
return &TransferCrossMarginAccountRequest{client: c}
|
||||
}
|
|
@ -0,0 +1,157 @@
|
|||
// Code generated by "requestgen -method POST -url /sapi/v1/margin/transfer -type TransferCrossMarginAccountRequest -responseType .TransferResponse"; DO NOT EDIT.
|
||||
|
||||
package binanceapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func (t *TransferCrossMarginAccountRequest) Asset(asset string) *TransferCrossMarginAccountRequest {
|
||||
t.asset = asset
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *TransferCrossMarginAccountRequest) Amount(amount string) *TransferCrossMarginAccountRequest {
|
||||
t.amount = amount
|
||||
return t
|
||||
}
|
||||
|
||||
// GetQueryParameters builds and checks the query parameters and returns url.Values
|
||||
func (t *TransferCrossMarginAccountRequest) GetQueryParameters() (url.Values, error) {
|
||||
var params = map[string]interface{}{}
|
||||
|
||||
query := url.Values{}
|
||||
for _k, _v := range params {
|
||||
query.Add(_k, fmt.Sprintf("%v", _v))
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
// GetParameters builds and checks the parameters and return the result in a map object
|
||||
func (t *TransferCrossMarginAccountRequest) GetParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
// check asset field -> json key asset
|
||||
asset := t.asset
|
||||
|
||||
// assign parameter of asset
|
||||
params["asset"] = asset
|
||||
// check amount field -> json key amount
|
||||
amount := t.amount
|
||||
|
||||
// assign parameter of amount
|
||||
params["amount"] = amount
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
|
||||
func (t *TransferCrossMarginAccountRequest) GetParametersQuery() (url.Values, error) {
|
||||
query := url.Values{}
|
||||
|
||||
params, err := t.GetParameters()
|
||||
if err != nil {
|
||||
return query, err
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
if t.isVarSlice(_v) {
|
||||
t.iterateSlice(_v, func(it interface{}) {
|
||||
query.Add(_k+"[]", fmt.Sprintf("%v", it))
|
||||
})
|
||||
} else {
|
||||
query.Add(_k, fmt.Sprintf("%v", _v))
|
||||
}
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
// GetParametersJSON converts the parameters from GetParameters into the JSON format
|
||||
func (t *TransferCrossMarginAccountRequest) GetParametersJSON() ([]byte, error) {
|
||||
params, err := t.GetParameters()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return json.Marshal(params)
|
||||
}
|
||||
|
||||
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
|
||||
func (t *TransferCrossMarginAccountRequest) GetSlugParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
func (t *TransferCrossMarginAccountRequest) applySlugsToUrl(url string, slugs map[string]string) string {
|
||||
for _k, _v := range slugs {
|
||||
needleRE := regexp.MustCompile(":" + _k + "\\b")
|
||||
url = needleRE.ReplaceAllString(url, _v)
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
func (t *TransferCrossMarginAccountRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
|
||||
sliceValue := reflect.ValueOf(slice)
|
||||
for _i := 0; _i < sliceValue.Len(); _i++ {
|
||||
it := sliceValue.Index(_i).Interface()
|
||||
_f(it)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TransferCrossMarginAccountRequest) isVarSlice(_v interface{}) bool {
|
||||
rt := reflect.TypeOf(_v)
|
||||
switch rt.Kind() {
|
||||
case reflect.Slice:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (t *TransferCrossMarginAccountRequest) GetSlugsMap() (map[string]string, error) {
|
||||
slugs := map[string]string{}
|
||||
params, err := t.GetSlugParameters()
|
||||
if err != nil {
|
||||
return slugs, nil
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
slugs[_k] = fmt.Sprintf("%v", _v)
|
||||
}
|
||||
|
||||
return slugs, nil
|
||||
}
|
||||
|
||||
func (t *TransferCrossMarginAccountRequest) Do(ctx context.Context) (*TransferResponse, error) {
|
||||
|
||||
params, err := t.GetParameters()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query := url.Values{}
|
||||
|
||||
apiURL := "/sapi/v1/margin/transfer"
|
||||
|
||||
req, err := t.client.NewAuthenticatedRequest(ctx, "POST", apiURL, query, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := t.client.SendRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var apiResponse TransferResponse
|
||||
if err := response.DecodeJSON(&apiResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &apiResponse, nil
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package binanceapi
|
||||
|
||||
import (
|
||||
"github.com/c9s/requestgen"
|
||||
)
|
||||
|
||||
type AccountType string
|
||||
|
||||
const (
|
||||
AccountTypeSpot AccountType = "SPOT"
|
||||
AccountTypeIsolatedMargin AccountType = "ISOLATED_MARGIN"
|
||||
)
|
||||
|
||||
//go:generate requestgen -method POST -url "/sapi/v1/margin/isolated/transfer" -type TransferIsolatedMarginAccountRequest -responseType .TransferResponse
|
||||
type TransferIsolatedMarginAccountRequest struct {
|
||||
client requestgen.AuthenticatedAPIClient
|
||||
|
||||
asset string `param:"asset"`
|
||||
symbol string `param:"symbol"`
|
||||
|
||||
// transFrom: "SPOT", "ISOLATED_MARGIN"
|
||||
transFrom AccountType `param:"transFrom"`
|
||||
|
||||
// transTo: "SPOT", "ISOLATED_MARGIN"
|
||||
transTo AccountType `param:"transTo"`
|
||||
|
||||
amount string `param:"amount"`
|
||||
}
|
||||
|
||||
func (c *RestClient) NewTransferIsolatedMarginAccountRequest() *TransferIsolatedMarginAccountRequest {
|
||||
return &TransferIsolatedMarginAccountRequest{client: c}
|
||||
}
|
|
@ -0,0 +1,209 @@
|
|||
// Code generated by "requestgen -method POST -url /sapi/v1/margin/isolated/transfer -type TransferIsolatedMarginAccountRequest -responseType .TransferResponse"; DO NOT EDIT.
|
||||
|
||||
package binanceapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func (t *TransferIsolatedMarginAccountRequest) Asset(asset string) *TransferIsolatedMarginAccountRequest {
|
||||
t.asset = asset
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *TransferIsolatedMarginAccountRequest) Symbol(symbol string) *TransferIsolatedMarginAccountRequest {
|
||||
t.symbol = symbol
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *TransferIsolatedMarginAccountRequest) TransFrom(transFrom AccountType) *TransferIsolatedMarginAccountRequest {
|
||||
t.transFrom = transFrom
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *TransferIsolatedMarginAccountRequest) TransTo(transTo AccountType) *TransferIsolatedMarginAccountRequest {
|
||||
t.transTo = transTo
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *TransferIsolatedMarginAccountRequest) Amount(amount string) *TransferIsolatedMarginAccountRequest {
|
||||
t.amount = amount
|
||||
return t
|
||||
}
|
||||
|
||||
// GetQueryParameters builds and checks the query parameters and returns url.Values
|
||||
func (t *TransferIsolatedMarginAccountRequest) GetQueryParameters() (url.Values, error) {
|
||||
var params = map[string]interface{}{}
|
||||
|
||||
query := url.Values{}
|
||||
for _k, _v := range params {
|
||||
query.Add(_k, fmt.Sprintf("%v", _v))
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
// GetParameters builds and checks the parameters and return the result in a map object
|
||||
func (t *TransferIsolatedMarginAccountRequest) GetParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
// check asset field -> json key asset
|
||||
asset := t.asset
|
||||
|
||||
// assign parameter of asset
|
||||
params["asset"] = asset
|
||||
// check symbol field -> json key symbol
|
||||
symbol := t.symbol
|
||||
|
||||
// assign parameter of symbol
|
||||
params["symbol"] = symbol
|
||||
// check transFrom field -> json key transFrom
|
||||
transFrom := t.transFrom
|
||||
|
||||
// TEMPLATE check-valid-values
|
||||
switch transFrom {
|
||||
case AccountTypeSpot, AccountTypeIsolatedMargin:
|
||||
params["transFrom"] = transFrom
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("transFrom value %v is invalid", transFrom)
|
||||
|
||||
}
|
||||
// END TEMPLATE check-valid-values
|
||||
|
||||
// assign parameter of transFrom
|
||||
params["transFrom"] = transFrom
|
||||
// check transTo field -> json key transTo
|
||||
transTo := t.transTo
|
||||
|
||||
// TEMPLATE check-valid-values
|
||||
switch transTo {
|
||||
case AccountTypeSpot, AccountTypeIsolatedMargin:
|
||||
params["transTo"] = transTo
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("transTo value %v is invalid", transTo)
|
||||
|
||||
}
|
||||
// END TEMPLATE check-valid-values
|
||||
|
||||
// assign parameter of transTo
|
||||
params["transTo"] = transTo
|
||||
// check amount field -> json key amount
|
||||
amount := t.amount
|
||||
|
||||
// assign parameter of amount
|
||||
params["amount"] = amount
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
|
||||
func (t *TransferIsolatedMarginAccountRequest) GetParametersQuery() (url.Values, error) {
|
||||
query := url.Values{}
|
||||
|
||||
params, err := t.GetParameters()
|
||||
if err != nil {
|
||||
return query, err
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
if t.isVarSlice(_v) {
|
||||
t.iterateSlice(_v, func(it interface{}) {
|
||||
query.Add(_k+"[]", fmt.Sprintf("%v", it))
|
||||
})
|
||||
} else {
|
||||
query.Add(_k, fmt.Sprintf("%v", _v))
|
||||
}
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
// GetParametersJSON converts the parameters from GetParameters into the JSON format
|
||||
func (t *TransferIsolatedMarginAccountRequest) GetParametersJSON() ([]byte, error) {
|
||||
params, err := t.GetParameters()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return json.Marshal(params)
|
||||
}
|
||||
|
||||
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
|
||||
func (t *TransferIsolatedMarginAccountRequest) GetSlugParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
func (t *TransferIsolatedMarginAccountRequest) applySlugsToUrl(url string, slugs map[string]string) string {
|
||||
for _k, _v := range slugs {
|
||||
needleRE := regexp.MustCompile(":" + _k + "\\b")
|
||||
url = needleRE.ReplaceAllString(url, _v)
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
func (t *TransferIsolatedMarginAccountRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
|
||||
sliceValue := reflect.ValueOf(slice)
|
||||
for _i := 0; _i < sliceValue.Len(); _i++ {
|
||||
it := sliceValue.Index(_i).Interface()
|
||||
_f(it)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TransferIsolatedMarginAccountRequest) isVarSlice(_v interface{}) bool {
|
||||
rt := reflect.TypeOf(_v)
|
||||
switch rt.Kind() {
|
||||
case reflect.Slice:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (t *TransferIsolatedMarginAccountRequest) GetSlugsMap() (map[string]string, error) {
|
||||
slugs := map[string]string{}
|
||||
params, err := t.GetSlugParameters()
|
||||
if err != nil {
|
||||
return slugs, nil
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
slugs[_k] = fmt.Sprintf("%v", _v)
|
||||
}
|
||||
|
||||
return slugs, nil
|
||||
}
|
||||
|
||||
func (t *TransferIsolatedMarginAccountRequest) Do(ctx context.Context) (*TransferResponse, error) {
|
||||
|
||||
params, err := t.GetParameters()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query := url.Values{}
|
||||
|
||||
apiURL := "/sapi/v1/margin/isolated/transfer"
|
||||
|
||||
req, err := t.client.NewAuthenticatedRequest(ctx, "POST", apiURL, query, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := t.client.SendRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var apiResponse TransferResponse
|
||||
if err := response.DecodeJSON(&apiResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &apiResponse, nil
|
||||
}
|
|
@ -372,6 +372,14 @@ func (e *Exchange) QueryMarginBorrowHistory(ctx context.Context, asset string) e
|
|||
return nil
|
||||
}
|
||||
|
||||
func (e *Exchange) TransferMarginAsset(ctx context.Context, asset string, amount fixedpoint.Value, io types.TransferDirection) error {
|
||||
if e.IsMargin && !e.IsIsolatedMargin {
|
||||
return e.transferCrossMarginAccountAsset(ctx, asset, amount, io)
|
||||
}
|
||||
|
||||
return errors.New("isolated margin transfer is not supported")
|
||||
}
|
||||
|
||||
// transferCrossMarginAccountAsset transfer asset to the cross margin account or to the main account
|
||||
func (e *Exchange) transferCrossMarginAccountAsset(ctx context.Context, asset string, amount fixedpoint.Value, io types.TransferDirection) error {
|
||||
req := e.client.NewMarginTransferService()
|
||||
|
|
Loading…
Reference in New Issue
Block a user