Merge pull request #1523 from c9s/edwin/binance/update-api-changes

MAJOR: [binance] replace margin/transfer to asset/transfer
This commit is contained in:
c9s 2024-02-05 11:42:25 +08:00 committed by GitHub
commit aad3f89492
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 309 additions and 444 deletions

View File

@ -7,6 +7,7 @@ import (
"os"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
@ -154,3 +155,24 @@ func TestClient_setTimeOffsetFromServer(t *testing.T) {
err := client.SetTimeOffsetFromServer(context.Background())
assert.NoError(t, err)
}
func TestClient_NewTransferAssetRequest(t *testing.T) {
client := getTestClientOrSkip(t)
ctx := context.Background()
err := client.SetTimeOffsetFromServer(ctx)
assert.NoError(t, err)
req := client.NewTransferAssetRequest()
req.Asset("BTC")
req.FromSymbol("BTCUSDT")
req.ToSymbol("BTCUSDT")
req.Amount("0.01")
req.Timestamp(time.Now())
req.TransferType(TransferAssetTypeIsolatedMarginToMain)
res, err := req.Do(ctx)
assert.NoError(t, err)
assert.NotNil(t, res)
assert.NotEmpty(t, res)
t.Logf("result: %+v", res)
}

View File

@ -0,0 +1,40 @@
package binanceapi
import (
"time"
"github.com/c9s/requestgen"
)
type TransferResponse struct {
TranId int `json:"tranId"`
}
type TransferAssetType string
const (
TransferAssetTypeMainToMargin TransferAssetType = "MAIN_MARGIN"
TransferAssetTypeMarginToMain TransferAssetType = "MARGIN_MAIN"
TransferAssetTypeMainToIsolatedMargin TransferAssetType = "MAIN_ISOLATED_MARGIN"
TransferAssetTypeIsolatedMarginToMain TransferAssetType = "ISOLATED_MARGIN_MAIN"
)
//go:generate requestgen -method POST -url "/sapi/v1/asset/transfer" -type TransferAssetRequest -responseType .TransferResponse
type TransferAssetRequest struct {
client requestgen.AuthenticatedAPIClient
asset string `param:"asset"`
transferType TransferAssetType `param:"type"`
amount string `param:"amount"`
timestamp time.Time `param:"timestamp,milliseconds,query"`
fromSymbol *string `param:"fromSymbol"`
toSymbol *string `param:"toSymbol"`
}
func (c *RestClient) NewTransferAssetRequest() *TransferAssetRequest {
return &TransferAssetRequest{client: c}
}

View File

@ -0,0 +1,238 @@
// Code generated by "requestgen -method POST -url /sapi/v1/asset/transfer -type TransferAssetRequest -responseType .TransferResponse"; DO NOT EDIT.
package binanceapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
"strconv"
"time"
)
func (t *TransferAssetRequest) Timestamp(timestamp time.Time) *TransferAssetRequest {
t.timestamp = timestamp
return t
}
func (t *TransferAssetRequest) Asset(asset string) *TransferAssetRequest {
t.asset = asset
return t
}
func (t *TransferAssetRequest) TransferType(transferType TransferAssetType) *TransferAssetRequest {
t.transferType = transferType
return t
}
func (t *TransferAssetRequest) Amount(amount string) *TransferAssetRequest {
t.amount = amount
return t
}
func (t *TransferAssetRequest) FromSymbol(fromSymbol string) *TransferAssetRequest {
t.fromSymbol = &fromSymbol
return t
}
func (t *TransferAssetRequest) ToSymbol(toSymbol string) *TransferAssetRequest {
t.toSymbol = &toSymbol
return t
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (t *TransferAssetRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
// check timestamp field -> json key timestamp
timestamp := t.timestamp
// assign parameter of timestamp
// convert time.Time to milliseconds time stamp
params["timestamp"] = strconv.FormatInt(timestamp.UnixNano()/int64(time.Millisecond), 10)
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 *TransferAssetRequest) 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 transferType field -> json key type
transferType := t.transferType
// TEMPLATE check-valid-values
switch transferType {
case TransferAssetTypeMainToMargin, TransferAssetTypeMarginToMain, TransferAssetTypeMainToIsolatedMargin, TransferAssetTypeIsolatedMarginToMain:
params["type"] = transferType
default:
return nil, fmt.Errorf("type value %v is invalid", transferType)
}
// END TEMPLATE check-valid-values
// assign parameter of transferType
params["type"] = transferType
// check amount field -> json key amount
amount := t.amount
// assign parameter of amount
params["amount"] = amount
// check fromSymbol field -> json key fromSymbol
if t.fromSymbol != nil {
fromSymbol := *t.fromSymbol
// assign parameter of fromSymbol
params["fromSymbol"] = fromSymbol
} else {
}
// check toSymbol field -> json key toSymbol
if t.toSymbol != nil {
toSymbol := *t.toSymbol
// assign parameter of toSymbol
params["toSymbol"] = toSymbol
} else {
}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (t *TransferAssetRequest) 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 *TransferAssetRequest) 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 *TransferAssetRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (t *TransferAssetRequest) 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 *TransferAssetRequest) 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 *TransferAssetRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (t *TransferAssetRequest) 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
}
// GetPath returns the request path of the API
func (t *TransferAssetRequest) GetPath() string {
return "/sapi/v1/asset/transfer"
}
// Do generates the request object and send the request object to the API endpoint
func (t *TransferAssetRequest) Do(ctx context.Context) (*TransferResponse, error) {
params, err := t.GetParameters()
if err != nil {
return nil, err
}
query, err := t.GetQueryParameters()
if err != nil {
return nil, err
}
var apiURL string
apiURL = t.GetPath()
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
}
type responseValidator interface {
Validate() error
}
validator, ok := interface{}(apiResponse).(responseValidator)
if ok {
if err := validator.Validate(); err != nil {
return nil, err
}
}
return &apiResponse, nil
}

View File

@ -1,27 +0,0 @@
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}
}

View File

@ -1,167 +0,0 @@
// 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) TransferType(transferType int) *TransferCrossMarginAccountRequest {
t.transferType = transferType
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 transferType field -> json key type
transferType := t.transferType
// assign parameter of transferType
params["type"] = transferType
// 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
}

View File

@ -1,32 +0,0 @@
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}
}

View File

@ -1,209 +0,0 @@
// 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
}

View File

@ -393,17 +393,17 @@ func (e *Exchange) TransferMarginAccountAsset(
func (e *Exchange) transferIsolatedMarginAccountAsset(
ctx context.Context, asset string, amount fixedpoint.Value, io types.TransferDirection,
) error {
req := e.client2.NewTransferIsolatedMarginAccountRequest()
req.Symbol(e.IsolatedMarginSymbol)
req := e.client2.NewTransferAssetRequest()
req.Asset(asset)
req.FromSymbol(e.IsolatedMarginSymbol)
req.ToSymbol(e.IsolatedMarginSymbol)
switch io {
case types.TransferIn:
req.TransFrom(binanceapi.AccountTypeSpot)
req.TransTo(binanceapi.AccountTypeIsolatedMargin)
req.TransferType(binanceapi.TransferAssetTypeMainToIsolatedMargin)
case types.TransferOut:
req.TransFrom(binanceapi.AccountTypeIsolatedMargin)
req.TransTo(binanceapi.AccountTypeSpot)
req.TransferType(binanceapi.TransferAssetTypeIsolatedMarginToMain)
}
req.Asset(asset)
@ -416,14 +416,14 @@ func (e *Exchange) transferIsolatedMarginAccountAsset(
func (e *Exchange) transferCrossMarginAccountAsset(
ctx context.Context, asset string, amount fixedpoint.Value, io types.TransferDirection,
) error {
req := e.client2.NewTransferCrossMarginAccountRequest()
req := e.client2.NewTransferAssetRequest()
req.Asset(asset)
req.Amount(amount.String())
if io == types.TransferIn {
req.TransferType(int(binance.MarginTransferTypeToMargin))
req.TransferType(binanceapi.TransferAssetTypeMainToMargin)
} else if io == types.TransferOut {
req.TransferType(int(binance.MarginTransferTypeToMain))
req.TransferType(binanceapi.TransferAssetTypeMarginToMain)
} else {
return fmt.Errorf("unexpected transfer direction: %d given", io)
}