Skip to content

Commit

Permalink
add: /sapi/v1/convert (#578)
Browse files Browse the repository at this point in the history
* Add: functions for convert

* fix: functions for convert

* fix: fromAmount, toAmount type

* add: debug

* remove: debug

* fix: fix convert quote

* fix: acceptQuote

* fix: orderStatus

* add: convert assetInfo
change:  getQuote service name

* fix: convert assetInfo

* fix: toAsset exchangeInfo

---------

Co-authored-by: qushedo <[email protected]>
  • Loading branch information
nlypage and nlypage committed Jun 24, 2024
1 parent c2c4643 commit 130172e
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 2 deletions.
25 changes: 25 additions & 0 deletions v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,31 @@ func (c *Client) NewConvertTradeHistoryService() *ConvertTradeHistoryService {
return &ConvertTradeHistoryService{c: c}
}

// NewConvertExchangeInfoService init the convert exchange info service
func (c *Client) NewConvertExchangeInfoService() *ConvertExchangeInfoService {
return &ConvertExchangeInfoService{c: c}
}

// NewConvertAssetInfoService init the convert asset info service
func (c *Client) NewConvertAssetInfoService() *ConvertAssetInfoService {
return &ConvertAssetInfoService{c: c}
}

// NewConvertQuoteService init the convert quote service
func (c *Client) NewConvertQuoteService() *ConvertGetQuoteService {
return &ConvertGetQuoteService{c: c}
}

// NewConvertAcceptQuoteService init the convert accept quote service
func (c *Client) NewConvertAcceptQuoteService() *ConvertAcceptQuoteService {
return &ConvertAcceptQuoteService{c: c}
}

// NewConvertOrderStatusService init the convert order status service
func (c *Client) NewConvertOrderStatusService() *ConvertOrderStatusService {
return &ConvertOrderStatusService{c: c}
}

// NewGetIsolatedMarginAllPairsService init get isolated margin all pairs service
func (c *Client) NewGetIsolatedMarginAllPairsService() *GetIsolatedMarginAllPairsService {
return &GetIsolatedMarginAllPairsService{c: c}
Expand Down
272 changes: 272 additions & 0 deletions v2/convert_trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,275 @@ type ConvertTradeHistoryItem struct {
InverseRatio string `json:"inverseRatio"`
CreateTime int64 `json:"createTime"`
}

// ConvertExchangeInfoService create a new convert exchange info service
type ConvertExchangeInfoService struct {
c *Client
fromAsset *string
toAsset *string
}

// FromAsset set fromAsset
func (s *ConvertExchangeInfoService) FromAsset(fromAsset string) *ConvertExchangeInfoService {
s.fromAsset = &fromAsset
return s
}

// ToAsset set toAsset
func (s *ConvertExchangeInfoService) ToAsset(toAsset string) *ConvertExchangeInfoService {
s.toAsset = &toAsset
return s
}

// Do send request
func (s *ConvertExchangeInfoService) Do(ctx context.Context, opts ...RequestOption) ([]*ConvertExchangeInfo, error) {
r := &request{
method: http.MethodGet,
endpoint: "/sapi/v1/convert/exchangeInfo",
secType: secTypeSigned,
}
if s.fromAsset != nil {
r.setParam("fromAsset", *s.fromAsset)
}
if s.toAsset != nil {
r.setParam("toAsset", *s.toAsset)
}

data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
var res []*ConvertExchangeInfo
if err = json.Unmarshal(data, &res); err != nil {
return nil, err
}
return res, nil
}

// ConvertExchangeInfo define the convert exchange info
type ConvertExchangeInfo struct {
FromAsset string `json:"fromAsset"`
ToAsset string `json:"toAsset"`
FromAssetMinAmount string `json:"fromAssetMinAmount"`
FromAssetMaxAmount string `json:"fromAssetMaxAmount"`
ToAssetMinAmount string `json:"toAssetMinAmount"`
ToAssetMaxAmount string `json:"toAssetMaxAmount"`
}

// ConvertGetQuoteService create a new convert quote service
type ConvertGetQuoteService struct {
c *Client
fromAsset string
toAsset string
fromAmount *string
toAmount *string
walletType *string // SPOT, FUNDING
validTime *string // 10s, 30s, 1m, 2m, default 10s
}

// ConvertAssetInfoService create a new convert asset info service
type ConvertAssetInfoService struct {
c *Client
}

// Do send request
func (s *ConvertAssetInfoService) Do(ctx context.Context, opts ...RequestOption) ([]*ConvertAssetInfo, error) {
r := &request{
method: http.MethodGet,
endpoint: "/sapi/v1/convert/assetInfo",
secType: secTypeSigned,
}

data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
var res []*ConvertAssetInfo
if err = json.Unmarshal(data, &res); err != nil {
return nil, err
}
return res, nil
}

// ConvertAssetInfo define the convert asset info
type ConvertAssetInfo struct {
Asset string `json:"asset"`
Fraction int `json:"fraction"`
}

// FromAsset set fromAsset
func (s *ConvertGetQuoteService) FromAsset(fromAsset string) *ConvertGetQuoteService {
s.fromAsset = fromAsset
return s
}

// ToAsset set fromAsset
func (s *ConvertGetQuoteService) ToAsset(toAsset string) *ConvertGetQuoteService {
s.toAsset = toAsset
return s
}

// FromAmount set fromAmount
func (s *ConvertGetQuoteService) FromAmount(fromAmount string) *ConvertGetQuoteService {
s.fromAmount = &fromAmount
return s
}

// ToAmount set toAmount
func (s *ConvertGetQuoteService) ToAmount(toAmount string) *ConvertGetQuoteService {
s.toAmount = &toAmount
return s
}

// WalletType set walletType
// SPOT or FUNDING. Default is SPOT
func (s *ConvertGetQuoteService) WalletType(walletType string) *ConvertGetQuoteService {
s.walletType = &walletType
return s
}

// ValidTime set validTime
// 10s, 30s, 1m, 2m, default 10s
func (s *ConvertGetQuoteService) ValidTime(validTime string) *ConvertGetQuoteService {
s.validTime = &validTime
return s
}

// Do send request
func (s *ConvertGetQuoteService) Do(ctx context.Context, opts ...RequestOption) (*ConvertQuote, error) {
r := &request{
method: http.MethodPost,
endpoint: "/sapi/v1/convert/getQuote",
secType: secTypeSigned,
}

r.setParam("fromAsset", s.fromAsset)
r.setParam("toAsset", s.toAsset)

if s.fromAmount != nil {
r.setParam("fromAmount", *s.fromAmount)
}
if s.toAmount != nil {
r.setParam("toAmount", *s.toAmount)
}
if s.walletType != nil {
r.setParam("walletType", *s.walletType)
}
if s.validTime != nil {
r.setParam("validTime", *s.validTime)
}

data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
var res ConvertQuote
if err = json.Unmarshal(data, &res); err != nil {
return nil, err
}
return &res, nil
}

// ConvertQuote define the convert quote
type ConvertQuote struct {
QuoteId string `json:"quoteId"`
Ratio string `json:"ratio"`
InverseRatio string `json:"inverseRatio"`
ValidTime int64 `json:"validTime"`
ToAmount string `json:"toAmount"`
FromAmount string `json:"fromAmount"`
}

type ConvertAcceptQuoteService struct {
c *Client
quoteId string
}

// QuoteId set quoteId
func (s *ConvertAcceptQuoteService) QuoteId(quoteId string) *ConvertAcceptQuoteService {
s.quoteId = quoteId
return s
}

// Do send request
func (s *ConvertAcceptQuoteService) Do(ctx context.Context, opts ...RequestOption) (*ConvertAcceptQuote, error) {
r := &request{
method: http.MethodPost,
endpoint: "/sapi/v1/convert/acceptQuote",
secType: secTypeSigned,
}
r.setParam("quoteId", s.quoteId)

data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
var res ConvertAcceptQuote
if err = json.Unmarshal(data, &res); err != nil {
return nil, err
}
return &res, nil
}

// ConvertAcceptQuote define the convert accept quote
type ConvertAcceptQuote struct {
OrderId string `json:"orderId"`
CreateTime int64 `json:"createTime"`
OrderStatus string `json:"orderStatus"`
}

// ConvertOrderStatusService check order status
type ConvertOrderStatusService struct {
c *Client
orderId *string
quoteId *string
}

// OrderId set orderId
func (s *ConvertOrderStatusService) OrderId(orderId string) *ConvertOrderStatusService {
s.orderId = &orderId
return s
}

// QuoteId set quoteId
func (s *ConvertOrderStatusService) QuoteId(quoteId string) *ConvertOrderStatusService {
s.quoteId = &quoteId
return s
}

// Do send request
func (s *ConvertOrderStatusService) Do(ctx context.Context, opts ...RequestOption) (*ConvertOrderStatus, error) {
r := &request{
method: http.MethodGet,
endpoint: "/sapi/v1/convert/orderStatus",
secType: secTypeSigned,
}
if s.orderId != nil {
r.setParam("orderId", *s.orderId)
}
if s.quoteId != nil {
r.setParam("quoteId", *s.quoteId)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res := ConvertOrderStatus{}
if err = json.Unmarshal(data, &res); err != nil {
return nil, err
}
return &res, nil
}

// ConvertOrderStatus define the convert order status
type ConvertOrderStatus struct {
OrderId int64 `json:"orderId"`
OrderStatus string `json:"orderStatus"`
FromAsset string `json:"fromAsset"`
FromAmount string `json:"fromAmount"`
ToAsset string `json:"toAsset"`
ToAmount string `json:"toAmount"`
Ratio string `json:"ratio"`
InverseRatio string `json:"inverseRatio"`
CreateTime int64 `json:"createTime"`
}
2 changes: 1 addition & 1 deletion v2/delivery/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/adshao/go-binance/v2/common"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"time"

"github.com/adshao/go-binance/v2/common"
"github.com/bitly/go-simplejson"
)

Expand Down
2 changes: 1 addition & 1 deletion v2/futures/order_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package futures

import (
"context"
"github.com/adshao/go-binance/v2/common"
"strconv"
"testing"

"github.com/adshao/go-binance/v2/common"
"github.com/stretchr/testify/suite"
)

Expand Down

0 comments on commit 130172e

Please sign in to comment.