Skip to content

Commit ad5f2fe

Browse files
author
shazbert
committed
glorious: nits
1 parent ce48766 commit ad5f2fe

File tree

2 files changed

+74
-94
lines changed

2 files changed

+74
-94
lines changed

exchanges/bybit/convert.go

Lines changed: 63 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ const (
2828
var supportedAccountTypes = []WalletAccountType{Funding, Uta, Spot, Contract, Inverse}
2929

3030
var (
31-
errUnsupportedAccountType = errors.New("unsupported account type")
32-
errCurrencyCodesEqual = errors.New("from and to currency codes cannot be equal")
33-
errRequestCoinInvalid = errors.New("request coin must match from coin if provided")
34-
errQuoteTxIDEmpty = errors.New("quoteTxID cannot be empty")
31+
errUnsupportedAccountType = errors.New("unsupported account type")
32+
errCurrencyCodesEqual = errors.New("from and to currency codes cannot be equal")
33+
errRequestCoinInvalid = errors.New("request coin must match from coin if provided")
34+
errQuoteTransactionIDEmpty = errors.New("quoteTransactionID cannot be empty")
3535
)
3636

3737
// WalletAccountType represents the different types of wallet accounts
3838
type WalletAccountType string
3939

40-
// Coin represents a coin that can be converted
41-
type Coin struct {
40+
// CoinResponse represents a coin that can be converted
41+
type CoinResponse struct {
4242
Coin currency.Code `json:"coin"`
4343
FullName string `json:"fullName"`
4444
Icon string `json:"icon"`
@@ -62,7 +62,7 @@ type Coin struct {
6262
}
6363

6464
// GetConvertCoinList returns a list of coins you can convert to/from
65-
func (e *Exchange) GetConvertCoinList(ctx context.Context, accountType WalletAccountType, coin currency.Code, isCoinToBuy bool) ([]Coin, error) {
65+
func (e *Exchange) GetConvertCoinList(ctx context.Context, accountType WalletAccountType, coin currency.Code, isCoinToBuy bool) ([]CoinResponse, error) {
6666
if !slices.Contains(supportedAccountTypes, accountType) {
6767
return nil, fmt.Errorf("%w: %q", errUnsupportedAccountType, accountType)
6868
}
@@ -81,14 +81,14 @@ func (e *Exchange) GetConvertCoinList(ctx context.Context, accountType WalletAcc
8181
}
8282

8383
var resp struct {
84-
List []Coin `json:"coins"`
84+
List []CoinResponse `json:"coins"`
8585
}
8686

8787
return resp.List, e.SendAuthHTTPRequestV5(ctx, exchange.RestSpot, http.MethodGet, "/v5/asset/exchange/query-coin-list", params, nil, &resp, defaultEPL)
8888
}
8989

90-
// RequestAQuoteParams holds the parameters for requesting a quote
91-
type RequestAQuoteParams struct {
90+
// RequestAQuoteRequest holds the parameters for requesting a quote
91+
type RequestAQuoteRequest struct {
9292
// Required fields
9393
AccountType WalletAccountType
9494
From currency.Code // Convert from coin (coin to sell)
@@ -105,21 +105,21 @@ type RequestAQuoteParams struct {
105105

106106
// RequestAQuoteResponse represents a response for a request a quote
107107
type RequestAQuoteResponse struct {
108-
QuoteTxID string `json:"quoteTxId"` // Quote transaction ID. It is system generated, and it is used to confirm quote and query the result of transaction
109-
ExchangeRate types.Number `json:"exchangeRate"`
110-
FromCoin currency.Code `json:"fromCoin"`
111-
FromCoinType string `json:"fromCoinType"`
112-
ToCoin currency.Code `json:"toCoin"`
113-
ToCoinType string `json:"toCoinType"`
114-
FromAmount types.Number `json:"fromAmount"`
115-
ToAmount types.Number `json:"toAmount"`
116-
ExpiredTime types.Time `json:"expiredTime"` // The expiry time for this quote (15 seconds)
117-
RequestID string `json:"requestId"`
118-
ExtTaxAndFee json.RawMessage `json:"extTaxAndFee"` // Compliance-related field. Currently returns an empty array, which may be used in the future
108+
QuoteTransactionID string `json:"quoteTxId"` // Quote transaction ID. It is system generated, and it is used to confirm quote and query the result of transaction
109+
ExchangeRate types.Number `json:"exchangeRate"`
110+
FromCoin currency.Code `json:"fromCoin"`
111+
FromCoinType string `json:"fromCoinType"`
112+
ToCoin currency.Code `json:"toCoin"`
113+
ToCoinType string `json:"toCoinType"`
114+
FromAmount types.Number `json:"fromAmount"`
115+
ToAmount types.Number `json:"toAmount"`
116+
ExpiredTime types.Time `json:"expiredTime"` // The expiry time for this quote (15 seconds)
117+
RequestID string `json:"requestId"`
118+
ExtTaxAndFee json.RawMessage `json:"extTaxAndFee"` // Compliance-related field. Currently returns an empty array, which may be used in the future
119119
}
120120

121121
// RequestAQuote requests a conversion quote between two coins with the specified parameters.
122-
func (e *Exchange) RequestAQuote(ctx context.Context, params *RequestAQuoteParams) (*RequestAQuoteResponse, error) {
122+
func (e *Exchange) RequestAQuote(ctx context.Context, params *RequestAQuoteRequest) (*RequestAQuoteResponse, error) {
123123
if !slices.Contains(supportedAccountTypes, params.AccountType) {
124124
return nil, fmt.Errorf("%w: %q", errUnsupportedAccountType, params.AccountType)
125125
}
@@ -147,50 +147,30 @@ func (e *Exchange) RequestAQuote(ctx context.Context, params *RequestAQuoteParam
147147
return nil, fmt.Errorf("amount %w", order.ErrAmountIsInvalid)
148148
}
149149

150-
payload := &struct {
151-
AccountType string `json:"accountType"`
152-
From string `json:"fromCoin"`
153-
To string `json:"toCoin"`
154-
Amount string `json:"requestAmount"`
155-
RequestCoin string `json:"requestCoin"`
156-
FromCoinType string `json:"fromCoinType,omitempty"`
157-
ToCoinType string `json:"toCoinType,omitempty"`
158-
ParamType string `json:"paramType,omitempty"`
159-
ParamValue string `json:"paramValue,omitempty"`
160-
RequestID string `json:"requestId,omitempty"`
161-
}{
162-
AccountType: string(params.AccountType),
163-
From: params.From.Upper().String(),
164-
To: params.To.Upper().String(),
165-
Amount: strconv.FormatFloat(params.Amount, 'f', -1, 64),
166-
RequestCoin: params.RequestCoin.Upper().String(),
167-
FromCoinType: params.FromCoinType,
168-
ToCoinType: params.ToCoinType,
169-
ParamType: params.ParamType,
170-
ParamValue: params.ParamValue,
171-
RequestID: params.RequestID,
172-
}
150+
params.From = params.From.Upper()
151+
params.To = params.To.Upper()
152+
params.RequestCoin = params.RequestCoin.Upper()
173153

174154
var resp *RequestAQuoteResponse
175-
return resp, e.SendAuthHTTPRequestV5(ctx, exchange.RestSpot, http.MethodPost, "/v5/asset/exchange/quote-apply", nil, payload, &resp, defaultEPL)
155+
return resp, e.SendAuthHTTPRequestV5(ctx, exchange.RestSpot, http.MethodPost, "/v5/asset/exchange/quote-apply", nil, params, &resp, defaultEPL)
176156
}
177157

178158
// ConfirmAQuoteResponse represents a response for confirming a quote
179159
type ConfirmAQuoteResponse struct {
180-
ExchangeStatus string `json:"exchangeStatus"`
181-
QuoteTxID string `json:"quoteTxId"`
160+
ExchangeStatus string `json:"exchangeStatus"`
161+
QuoteTransactionID string `json:"quoteTxId"`
182162
}
183163

184164
// ConfirmAQuote confirms a quote transaction and executes the conversion
185-
func (e *Exchange) ConfirmAQuote(ctx context.Context, quoteTxID string) (*ConfirmAQuoteResponse, error) {
186-
if quoteTxID == "" {
187-
return nil, errQuoteTxIDEmpty
165+
func (e *Exchange) ConfirmAQuote(ctx context.Context, quoteTransactionID string) (*ConfirmAQuoteResponse, error) {
166+
if quoteTransactionID == "" {
167+
return nil, errQuoteTransactionIDEmpty
188168
}
189169

190170
payload := struct {
191-
QuoteTxID string `json:"quoteTxId"`
171+
QuoteTransactionID string `json:"quoteTxId"`
192172
}{
193-
QuoteTxID: quoteTxID,
173+
QuoteTransactionID: quoteTransactionID,
194174
}
195175

196176
var resp *ConfirmAQuoteResponse
@@ -200,33 +180,33 @@ func (e *Exchange) ConfirmAQuote(ctx context.Context, quoteTxID string) (*Confir
200180

201181
// ConvertStatusResponse represents the response for a conversion status query
202182
type ConvertStatusResponse struct {
203-
AccountType WalletAccountType `json:"accountType"`
204-
ExchangeTxID string `json:"exchangeTxId"`
205-
UserID string `json:"userId"`
206-
FromCoin currency.Code `json:"fromCoin"`
207-
FromCoinType string `json:"fromCoinType"`
208-
FromAmount types.Number `json:"fromAmount"`
209-
ToCoin currency.Code `json:"toCoin"`
210-
ToCoinType string `json:"toCoinType"`
211-
ToAmount types.Number `json:"toAmount"`
212-
ExchangeStatus string `json:"exchangeStatus"`
213-
ExtInfo json.RawMessage `json:"extInfo"` // Reserved field, ignored for now
214-
ConvertRate types.Number `json:"convertRate"`
215-
CreatedAt types.Time `json:"createdAt"`
183+
AccountType WalletAccountType `json:"accountType"`
184+
ExchangeTransactionID string `json:"exchangeTxId"`
185+
UserID string `json:"userId"`
186+
FromCoin currency.Code `json:"fromCoin"`
187+
FromCoinType string `json:"fromCoinType"`
188+
FromAmount types.Number `json:"fromAmount"`
189+
ToCoin currency.Code `json:"toCoin"`
190+
ToCoinType string `json:"toCoinType"`
191+
ToAmount types.Number `json:"toAmount"`
192+
ExchangeStatus string `json:"exchangeStatus"`
193+
ExtInfo json.RawMessage `json:"extInfo"` // Reserved field, ignored for now
194+
ConvertRate types.Number `json:"convertRate"`
195+
CreatedAt types.Time `json:"createdAt"`
216196
}
217197

218198
// GetConvertStatus retrieves the status of a conversion transaction
219-
func (e *Exchange) GetConvertStatus(ctx context.Context, accountType WalletAccountType, quoteTxID string) (*ConvertStatusResponse, error) {
199+
func (e *Exchange) GetConvertStatus(ctx context.Context, accountType WalletAccountType, quoteTransactionID string) (*ConvertStatusResponse, error) {
220200
if !slices.Contains(supportedAccountTypes, accountType) {
221201
return nil, fmt.Errorf("%w: %q", errUnsupportedAccountType, accountType)
222202
}
223203

224-
if quoteTxID == "" {
225-
return nil, errQuoteTxIDEmpty
204+
if quoteTransactionID == "" {
205+
return nil, errQuoteTransactionIDEmpty
226206
}
227207

228208
params := url.Values{}
229-
params.Set("quoteTxId", quoteTxID)
209+
params.Set("quoteTxId", quoteTransactionID)
230210
params.Set("accountType", string(accountType))
231211

232212
var resp struct {
@@ -237,19 +217,19 @@ func (e *Exchange) GetConvertStatus(ctx context.Context, accountType WalletAccou
237217

238218
// ConvertHistoryResponse represents a response for conversion history
239219
type ConvertHistoryResponse struct {
240-
AccountType WalletAccountType `json:"accountType"`
241-
ExchangeTxID string `json:"exchangeTxId"`
242-
UserID string `json:"userId"`
243-
FromCoin currency.Code `json:"fromCoin"`
244-
FromCoinType string `json:"fromCoinType"`
245-
FromAmount types.Number `json:"fromAmount"`
246-
ToCoin currency.Code `json:"toCoin"`
247-
ToCoinType string `json:"toCoinType"`
248-
ToAmount types.Number `json:"toAmount"`
249-
ExchangeStatus string `json:"exchangeStatus"`
250-
ExtInfo json.RawMessage `json:"extInfo"`
251-
ConvertRate types.Number `json:"convertRate"`
252-
CreatedAt types.Time `json:"createdAt"`
220+
AccountType WalletAccountType `json:"accountType"`
221+
ExchangeTransactionID string `json:"exchangeTxId"`
222+
UserID string `json:"userId"`
223+
FromCoin currency.Code `json:"fromCoin"`
224+
FromCoinType string `json:"fromCoinType"`
225+
FromAmount types.Number `json:"fromAmount"`
226+
ToCoin currency.Code `json:"toCoin"`
227+
ToCoinType string `json:"toCoinType"`
228+
ToAmount types.Number `json:"toAmount"`
229+
ExchangeStatus string `json:"exchangeStatus"`
230+
ExtInfo json.RawMessage `json:"extInfo"`
231+
ConvertRate types.Number `json:"convertRate"`
232+
CreatedAt types.Time `json:"createdAt"`
253233
}
254234

255235
// GetConvertHistory retrieves the conversion history for the specified account types.

exchanges/bybit/convert_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,31 @@ func TestGetConvertCoinList(t *testing.T) {
3232
func TestRequestAQuote(t *testing.T) {
3333
t.Parallel()
3434

35-
_, err := e.RequestAQuote(t.Context(), &RequestAQuoteParams{})
35+
_, err := e.RequestAQuote(t.Context(), &RequestAQuoteRequest{})
3636
assert.ErrorIs(t, err, errUnsupportedAccountType)
3737

38-
_, err = e.RequestAQuote(t.Context(), &RequestAQuoteParams{AccountType: Uta})
38+
_, err = e.RequestAQuote(t.Context(), &RequestAQuoteRequest{AccountType: Uta})
3939
assert.ErrorIs(t, err, currency.ErrCurrencyCodeEmpty)
4040

41-
_, err = e.RequestAQuote(t.Context(), &RequestAQuoteParams{AccountType: Uta, From: currency.BTC})
41+
_, err = e.RequestAQuote(t.Context(), &RequestAQuoteRequest{AccountType: Uta, From: currency.BTC})
4242
assert.ErrorIs(t, err, currency.ErrCurrencyCodeEmpty)
4343

44-
_, err = e.RequestAQuote(t.Context(), &RequestAQuoteParams{
44+
_, err = e.RequestAQuote(t.Context(), &RequestAQuoteRequest{
4545
AccountType: Uta,
4646
From: currency.BTC,
4747
To: currency.BTC,
4848
})
4949
assert.ErrorIs(t, err, errCurrencyCodesEqual)
5050

51-
_, err = e.RequestAQuote(t.Context(), &RequestAQuoteParams{
51+
_, err = e.RequestAQuote(t.Context(), &RequestAQuoteRequest{
5252
AccountType: Uta,
5353
From: currency.BTC,
5454
To: currency.USDT,
5555
RequestCoin: currency.WOO,
5656
})
5757
assert.ErrorIs(t, err, errRequestCoinInvalid)
5858

59-
_, err = e.RequestAQuote(t.Context(), &RequestAQuoteParams{
59+
_, err = e.RequestAQuote(t.Context(), &RequestAQuoteRequest{
6060
AccountType: Uta,
6161
From: currency.BTC,
6262
To: currency.USDT,
@@ -68,21 +68,21 @@ func TestRequestAQuote(t *testing.T) {
6868
}
6969
sharedtestvalues.SkipTestIfCredentialsUnset(t, e)
7070

71-
quote, err := e.RequestAQuote(t.Context(), &RequestAQuoteParams{
71+
quote, err := e.RequestAQuote(t.Context(), &RequestAQuoteRequest{
7272
AccountType: Uta,
7373
From: currency.BTC,
7474
To: currency.USDT,
7575
Amount: 69.420,
7676
})
7777
require.NoError(t, err)
78-
assert.NotEmpty(t, quote.QuoteTxID)
78+
assert.NotEmpty(t, quote.QuoteTransactionID)
7979
}
8080

8181
func TestConfirmAQuote(t *testing.T) {
8282
t.Parallel()
8383

8484
_, err := e.ConfirmAQuote(t.Context(), "")
85-
assert.ErrorIs(t, err, errQuoteTxIDEmpty)
85+
assert.ErrorIs(t, err, errQuoteTransactionIDEmpty)
8686

8787
if mockTests {
8888
t.Skip(skipAuthenticatedFunctionsForMockTesting)
@@ -91,7 +91,7 @@ func TestConfirmAQuote(t *testing.T) {
9191

9292
quote, err := e.ConfirmAQuote(t.Context(), "10414247553864074960678912")
9393
require.NoError(t, err)
94-
assert.NotEmpty(t, quote.QuoteTxID)
94+
assert.NotEmpty(t, quote.QuoteTransactionID)
9595
}
9696

9797
func TestGetConvertStatus(t *testing.T) {
@@ -101,7 +101,7 @@ func TestGetConvertStatus(t *testing.T) {
101101
assert.ErrorIs(t, err, errUnsupportedAccountType)
102102

103103
_, err = e.GetConvertStatus(t.Context(), Uta, "")
104-
assert.ErrorIs(t, err, errQuoteTxIDEmpty)
104+
assert.ErrorIs(t, err, errQuoteTransactionIDEmpty)
105105

106106
if mockTests {
107107
t.Skip(skipAuthenticatedFunctionsForMockTesting)

0 commit comments

Comments
 (0)