Skip to content

Commit

Permalink
Update websocket endpoints, types, and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samuael committed Oct 24, 2024
1 parent 6366cb8 commit e66e793
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 128 deletions.
2 changes: 1 addition & 1 deletion exchanges/cryptodotcom/cryptodotcom.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ func (cr *Cryptodotcom) GetStakingHistory(ctx context.Context, symbol string, st
params["limit"] = limit
}
var resp *StakingRequestsResponse
return resp, cr.SendAuthHTTPRequest(context.Background(), exchange.RestSpot, request.UnAuth, "private/staking/get-stake-history", params, &resp)
return resp, cr.SendAuthHTTPRequest(ctx, exchange.RestSpot, request.UnAuth, "private/staking/get-stake-history", params, &resp)
}

// GetStakingRewardHistory get stake/unstake request history
Expand Down
88 changes: 37 additions & 51 deletions exchanges/cryptodotcom/cryptodotcom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,21 @@ func TestGetSymbols(t *testing.T) {

func TestGetOrderbook(t *testing.T) {
t.Parallel()
result, err := cr.GetOrderbook(context.Background(), "BTC_USDT", 0)
result, err := cr.GetOrderbook(context.Background(), tradablePair.String(), 0)
require.NoError(t, err)
assert.NotNil(t, result)
}

func TestGetCandlestickDetail(t *testing.T) {
t.Parallel()
result, err := cr.GetCandlestickDetail(context.Background(), "BTC_USDT", kline.FiveMin)
result, err := cr.GetCandlestickDetail(context.Background(), tradablePair.String(), kline.FiveMin)
require.NoError(t, err)
assert.NotNil(t, result)
}

func TestGetTickers(t *testing.T) {
t.Parallel()
result, err := cr.GetTickers(context.Background(), "BTC_USDT")
result, err := cr.GetTickers(context.Background(), tradablePair.String())
require.NoError(t, err)
require.NotNil(t, result)

Expand All @@ -128,7 +128,7 @@ func TestGetTrades(t *testing.T) {
_, err := cr.GetTrades(context.Background(), "")
require.ErrorIs(t, err, currency.ErrSymbolStringEmpty)

result, err := cr.GetTrades(context.Background(), "BTC_USDT")
result, err := cr.GetTrades(context.Background(), tradablePair.String())
require.NoError(t, err)
assert.NotNil(t, result)
}
Expand All @@ -137,10 +137,8 @@ func TestWithdrawFunds(t *testing.T) {
t.Parallel()
_, err := cr.WithdrawFunds(context.Background(), currency.EMPTYCODE, 10, core.BitcoinDonationAddress, "", "", "")
require.ErrorIs(t, err, currency.ErrCurrencyCodeEmpty)

_, err = cr.WithdrawFunds(context.Background(), currency.BTC, 0, core.BitcoinDonationAddress, "", "", "")
require.ErrorIs(t, err, order.ErrAmountBelowMin)

_, err = cr.WithdrawFunds(context.Background(), currency.BTC, 10, "", "", "", "")
require.ErrorIs(t, err, errAddressRequired)

Expand All @@ -153,10 +151,8 @@ func TestWsCreateWithdrawal(t *testing.T) {
t.Parallel()
_, err := cr.WsCreateWithdrawal(currency.EMPTYCODE, 10, core.BitcoinDonationAddress, "", "", "")
require.ErrorIs(t, err, currency.ErrCurrencyCodeEmpty)

_, err = cr.WsCreateWithdrawal(currency.BTC, 0, core.BitcoinDonationAddress, "", "", "")
require.ErrorIs(t, err, order.ErrAmountBelowMin)

_, err = cr.WsCreateWithdrawal(currency.BTC, 10, "", "", "", "")
require.ErrorIs(t, err, errAddressRequired)

Expand Down Expand Up @@ -214,19 +210,19 @@ func TestGetPersonalDepositAddress(t *testing.T) {

func TestCreateExportRequest(t *testing.T) {
t.Parallel()
_, err := cr.CreateExportRequest(context.Background(), "BTC_CRO", "", time.Now().Add(-time.Hour*240), time.Now(), []string{})
_, err := cr.CreateExportRequest(context.Background(), tradablePair.String(), "", time.Now().Add(-time.Hour*240), time.Now(), []string{})
require.ErrorIs(t, err, errRequestedDataTypesRequired)

sharedtestvalues.SkipTestIfCredentialsUnset(t, cr, canManipulateRealOrders)
result, err := cr.CreateExportRequest(context.Background(), "BTC_CRO", "", time.Now().Add(-time.Hour*240), time.Now(), []string{"SPOT_ORDER", "SPOT_TRADE"})
result, err := cr.CreateExportRequest(context.Background(), tradablePair.String(), "", time.Now().Add(-time.Hour*240), time.Now(), []string{"SPOT_ORDER", "SPOT_TRADE"})
require.NoError(t, err)
assert.NotNil(t, result)
}

func TestGetExportRequests(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, cr)
result, err := cr.GetExportRequests(context.Background(), "BTC_CRO", time.Time{}, time.Time{}, []string{"SPOT_ORDER"}, 10, 0)
result, err := cr.GetExportRequests(context.Background(), tradablePair.String(), time.Time{}, time.Time{}, []string{"SPOT_ORDER"}, 10, 0)
require.NoError(t, err)
assert.NotNil(t, result)
}
Expand Down Expand Up @@ -303,7 +299,7 @@ func TestCreateOrder(t *testing.T) {

sharedtestvalues.SkipTestIfCredentialsUnset(t, cr, canManipulateRealOrders)
result, err := cr.CreateOrder(context.Background(), &CreateOrderParam{
Symbol: "BTC_USDT",
Symbol: tradablePair.String(),
Side: order.Buy,
OrderType: order.Limit,
Price: 123,
Expand All @@ -315,7 +311,7 @@ func TestCreateOrder(t *testing.T) {
func TestWsPlaceOrder(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, cr, canManipulateRealOrders)
arg := &CreateOrderParam{Symbol: "BTC_USDT", Side: order.Buy, OrderType: order.Limit, Price: 123, Quantity: 12}
arg := &CreateOrderParam{Symbol: tradablePair.String(), Side: order.Buy, OrderType: order.Limit, Price: 123, Quantity: 12}
result, err := cr.WsPlaceOrder(arg)
require.NoError(t, err)
assert.NotNil(t, result)
Expand All @@ -326,22 +322,22 @@ func TestCancelExistingOrder(t *testing.T) {
err := cr.CancelExistingOrder(context.Background(), "", "1232412")
assert.ErrorIs(t, err, currency.ErrSymbolStringEmpty)

err = cr.CancelExistingOrder(context.Background(), "BTC_USDT", "")
err = cr.CancelExistingOrder(context.Background(), tradablePair.String(), "")
assert.ErrorIs(t, err, order.ErrOrderIDNotSet)

sharedtestvalues.SkipTestIfCredentialsUnset(t, cr, canManipulateRealOrders)
err = cr.CancelExistingOrder(context.Background(), "BTC_USDT", "1232412")
err = cr.CancelExistingOrder(context.Background(), tradablePair.String(), "1232412")
assert.NoError(t, err)
}
func TestWsCancelExistingOrder(t *testing.T) {
t.Parallel()
err := cr.WsCancelExistingOrder("", "1232412")
assert.ErrorIs(t, err, currency.ErrSymbolStringEmpty)
err = cr.WsCancelExistingOrder("BTC_USDT", "")
err = cr.WsCancelExistingOrder(tradablePair.String(), "")
assert.ErrorIs(t, err, order.ErrOrderIDNotSet)

sharedtestvalues.SkipTestIfCredentialsUnset(t, cr, canManipulateRealOrders)
err = cr.WsCancelExistingOrder("BTC_USDT", "1232412")
err = cr.WsCancelExistingOrder(tradablePair.String(), "1232412")
assert.NoError(t, err)
}

Expand Down Expand Up @@ -423,7 +419,7 @@ func TestCreateOrderList(t *testing.T) {
sharedtestvalues.SkipTestIfCredentialsUnset(t, cr, canManipulateRealOrders)
result, err := cr.CreateOrderList(context.Background(), "LIST", []CreateOrderParam{
{
Symbol: "BTC_USDT", ClientOrderID: "", TimeInForce: "", Side: order.Buy, OrderType: order.Limit, PostOnly: false, TriggerPrice: 0, Price: 123, Quantity: 12, Notional: 0,
Symbol: tradablePair.String(), ClientOrderID: "", TimeInForce: "", Side: order.Buy, OrderType: order.Limit, PostOnly: false, TriggerPrice: 0, Price: 123, Quantity: 12, Notional: 0,
}})
require.NoError(t, err)
assert.NotNil(t, result)
Expand All @@ -436,7 +432,7 @@ func TestWsCreateOrderList(t *testing.T) {
sharedtestvalues.SkipTestIfCredentialsUnset(t, cr, canManipulateRealOrders)
result, err := cr.WsCreateOrderList("LIST", []CreateOrderParam{
{
Symbol: "BTC_USDT", ClientOrderID: "", TimeInForce: "", Side: order.Buy, OrderType: order.Limit, PostOnly: false, TriggerPrice: 0, Price: 123, Quantity: 12, Notional: 0,
Symbol: tradablePair.String(), ClientOrderID: "", TimeInForce: "", Side: order.Buy, OrderType: order.Limit, PostOnly: false, TriggerPrice: 0, Price: 123, Quantity: 12, Notional: 0,
}})
require.NoError(t, err)
assert.NotNil(t, result)
Expand All @@ -452,7 +448,7 @@ func TestCancelOrderList(t *testing.T) {

sharedtestvalues.SkipTestIfCredentialsUnset(t, cr, canManipulateRealOrders)
result, err := cr.CancelOrderList(context.Background(), []CancelOrderParam{
{InstrumentName: "BTC_USDT", OrderID: "1234567"}, {InstrumentName: "BTC_USDT",
{InstrumentName: tradablePair.String(), OrderID: "1234567"}, {InstrumentName: tradablePair.String(),
OrderID: "123450067"}})
require.NoError(t, err)
assert.NotNil(t, result)
Expand All @@ -468,7 +464,7 @@ func TestWsCancelOrderList(t *testing.T) {

sharedtestvalues.SkipTestIfCredentialsUnset(t, cr, canManipulateRealOrders)
result, err := cr.WsCancelOrderList([]CancelOrderParam{
{InstrumentName: "BTC_USDT", OrderID: "1234567"}, {InstrumentName: "BTC_USDT",
{InstrumentName: tradablePair.String(), OrderID: "1234567"}, {InstrumentName: tradablePair.String(),
OrderID: "123450067"}})
require.NoError(t, err)
assert.NotNil(t, result)
Expand Down Expand Up @@ -544,7 +540,7 @@ func TestGetFeeRateForUserAccount(t *testing.T) {
require.ErrorIs(t, err, currency.ErrSymbolStringEmpty)

sharedtestvalues.SkipTestIfCredentialsUnset(t, cr)
result, err := cr.GetInstrumentFeeRate(context.Background(), "BTC_USDT")
result, err := cr.GetInstrumentFeeRate(context.Background(), tradablePair.String())
require.NoError(t, err)
assert.NotNil(t, result)
}
Expand Down Expand Up @@ -647,15 +643,15 @@ func TestCreateOTCOrder(t *testing.T) {
t.Parallel()
_, err := cr.CreateOTCOrder(context.Background(), "", "BUY", "3427401068340147456", 0.0001, 12321, false)
require.ErrorIs(t, err, currency.ErrSymbolStringEmpty)
_, err = cr.CreateOTCOrder(context.Background(), "BTC_USDT", "BUY", "3427401068340147456", 0, 12321, false)
_, err = cr.CreateOTCOrder(context.Background(), tradablePair.String(), "BUY", "3427401068340147456", 0, 12321, false)
require.ErrorIs(t, err, order.ErrAmountBelowMin)
_, err = cr.CreateOTCOrder(context.Background(), "BTC_USDT", "BUY", "3427401068340147456", 0.0001, 0, false)
_, err = cr.CreateOTCOrder(context.Background(), tradablePair.String(), "BUY", "3427401068340147456", 0.0001, 0, false)
require.ErrorIs(t, err, order.ErrPriceBelowMin)
_, err = cr.CreateOTCOrder(context.Background(), "BTC_USDT", "", "3427401068340147456", 0.0001, 12321, false)
_, err = cr.CreateOTCOrder(context.Background(), tradablePair.String(), "", "3427401068340147456", 0.0001, 12321, false)
require.ErrorIs(t, err, order.ErrSideIsInvalid)

sharedtestvalues.SkipTestIfCredentialsUnset(t, cr, canManipulateRealOrders)
result, err := cr.CreateOTCOrder(context.Background(), "BTC_USDT", "BUY", "3427401068340147456", 0.0001, 12321, false)
result, err := cr.CreateOTCOrder(context.Background(), tradablePair.String(), "BUY", "3427401068340147456", 0.0001, 12321, false)
require.NoError(t, err)
assert.NotNil(t, result)
}
Expand Down Expand Up @@ -936,7 +932,7 @@ func TestGetCreateParamMap(t *testing.T) {
var newone *CreateOrderParam
_, err = newone.getCreateParamMap()
require.ErrorIs(t, err, common.ErrNilPointer)
arg.Symbol = "BTC_USDT"
arg.Symbol = tradablePair.String()
_, err = arg.getCreateParamMap()
require.ErrorIs(t, err, order.ErrSideIsInvalid)
arg.Side = order.Buy
Expand Down Expand Up @@ -1043,25 +1039,15 @@ func TestSafeNumberUnmarshal(t *testing.T) {
assert.NotNil(t, result)
}

func TestWsRetriveTrades(t *testing.T) {
t.Parallel()
_, err := cr.WsRetriveTrades("")
require.ErrorIs(t, err, currency.ErrSymbolStringEmpty)

result, err := cr.WsRetriveTrades("BTC_USDT")
assert.NoError(t, err)
assert.NotNil(t, result)
}

func TestCreateStaking(t *testing.T) {
t.Parallel()
_, err := cr.CreateStaking(context.Background(), "", 123.45)
require.ErrorIs(t, err, currency.ErrSymbolStringEmpty)
_, err = cr.CreateStaking(context.Background(), "BTC_USDT", 0)
_, err = cr.CreateStaking(context.Background(), tradablePair.String(), 0)
require.ErrorIs(t, err, order.ErrAmountBelowMin)

sharedtestvalues.SkipTestIfCredentialsUnset(t, cr, canManipulateRealOrders)
result, err := cr.CreateStaking(context.Background(), "BTC_USDT", 123.45)
result, err := cr.CreateStaking(context.Background(), tradablePair.String(), 123.45)
require.NoError(t, err)
assert.NotNil(t, result)
}
Expand All @@ -1070,19 +1056,19 @@ func TestUnstake(t *testing.T) {
t.Parallel()
_, err := cr.Unstake(context.Background(), "", 123.45)
require.ErrorIs(t, err, currency.ErrSymbolStringEmpty)
_, err = cr.Unstake(context.Background(), "BTC_USDT", 0)
_, err = cr.Unstake(context.Background(), tradablePair.String(), 0)
require.ErrorIs(t, err, order.ErrAmountBelowMin)

sharedtestvalues.SkipTestIfCredentialsUnset(t, cr, canManipulateRealOrders)
result, err := cr.Unstake(context.Background(), "BTC_USDT", 123.45)
result, err := cr.Unstake(context.Background(), tradablePair.String(), 123.45)
require.NoError(t, err)
assert.NotNil(t, result)
}

func TestGetStakingPosition(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, cr)
result, err := cr.GetStakingPosition(context.Background(), "BTC_USDT")
result, err := cr.GetStakingPosition(context.Background(), tradablePair.String())
assert.NoError(t, err)
assert.NotNil(t, result)
}
Expand All @@ -1098,23 +1084,23 @@ func TestGetStakingInstruments(t *testing.T) {
func TestGetOpenStakeUnStakeRequests(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, cr)
result, err := cr.GetOpenStakeUnStakeRequests(context.Background(), "BTC_USDT", time.Now().Add(-time.Hour*25*30), time.Now(), 10)
result, err := cr.GetOpenStakeUnStakeRequests(context.Background(), tradablePair.String(), time.Now().Add(-time.Hour*25*30), time.Now(), 10)
assert.NoError(t, err)
assert.NotNil(t, result)
}

func TestGetStakingHistory(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, cr)
result, err := cr.GetStakingHistory(context.Background(), "BTC_USDT", time.Now().Add(-time.Hour*25*30), time.Now(), 10)
result, err := cr.GetStakingHistory(context.Background(), tradablePair.String(), time.Now().Add(-time.Hour*25*30), time.Now(), 10)
assert.NoError(t, err)
assert.NotNil(t, result)
}

func TestGetStakingReqardHistory(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, cr)
result, err := cr.GetStakingRewardHistory(context.Background(), "BTC_USDT", time.Now().Add(-time.Hour*25*30), time.Now(), 10)
result, err := cr.GetStakingRewardHistory(context.Background(), tradablePair.String(), time.Now().Add(-time.Hour*25*30), time.Now(), 10)
assert.NoError(t, err)
assert.NotNil(t, result)
}
Expand All @@ -1123,17 +1109,17 @@ func TestConvertStakedToken(t *testing.T) {
t.Parallel()
_, err := cr.ConvertStakedToken(context.Background(), "", "ETH_USDT", .5, 12.34, 3)
require.ErrorIs(t, err, currency.ErrSymbolStringEmpty)
_, err = cr.ConvertStakedToken(context.Background(), "BTC_USDT", "", .5, 12.34, 3)
_, err = cr.ConvertStakedToken(context.Background(), tradablePair.String(), "", .5, 12.34, 3)
require.ErrorIs(t, err, currency.ErrSymbolStringEmpty)
_, err = cr.ConvertStakedToken(context.Background(), "BTC_USDT", "ETH_USDT", 0, 12.34, 3)
_, err = cr.ConvertStakedToken(context.Background(), tradablePair.String(), "ETH_USDT", 0, 12.34, 3)
require.ErrorIs(t, err, errInvalidRate)
_, err = cr.ConvertStakedToken(context.Background(), "BTC_USDT", "ETH_USDT", .5, 0, 3)
_, err = cr.ConvertStakedToken(context.Background(), tradablePair.String(), "ETH_USDT", .5, 0, 3)
require.ErrorIs(t, err, order.ErrAmountBelowMin)
_, err = cr.ConvertStakedToken(context.Background(), "BTC_USDT", "ETH_USDT", .5, 12.34, 0)
_, err = cr.ConvertStakedToken(context.Background(), tradablePair.String(), "ETH_USDT", .5, 12.34, 0)
require.ErrorIs(t, err, errInvalidSlippageToleraceBPs)

sharedtestvalues.SkipTestIfCredentialsUnset(t, cr, canManipulateRealOrders)
result, err := cr.ConvertStakedToken(context.Background(), "BTC_USDT", "ETH_USDT", .5, 12.34, 3)
result, err := cr.ConvertStakedToken(context.Background(), tradablePair.String(), "ETH_USDT", .5, 12.34, 3)
require.NoError(t, err)
assert.NotNil(t, result)
}
Expand All @@ -1160,7 +1146,7 @@ func TestStakingConversionRate(t *testing.T) {
require.ErrorIs(t, err, currency.ErrSymbolStringEmpty)

sharedtestvalues.SkipTestIfCredentialsUnset(t, cr)
result, err := cr.StakingConversionRate(context.Background(), "BTC_USDT")
result, err := cr.StakingConversionRate(context.Background(), tradablePair.String())
require.NoError(t, err)
assert.NotNil(t, result)
}
36 changes: 18 additions & 18 deletions exchanges/cryptodotcom/cryptodotcom_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ type Instrument struct {
type OrderbookDetail struct {
Depth int64 `json:"depth"`
Data []struct {
Asks [][3]string `json:"asks"`
Bids [][3]string `json:"bids"`
Asks [][3]types.Number `json:"asks"`
Bids [][3]types.Number `json:"bids"`
} `json:"data"`
InstrumentName string `json:"instrument_name"`
}
Expand Down Expand Up @@ -149,7 +149,7 @@ type PrivateRequestParam struct {

// CurrencyNetworkResponse retrieves the symbol network mapping.
type CurrencyNetworkResponse struct {
UpdateTime int64 `json:"update_time"`
UpdateTime types.Time `json:"update_time"`
CurrencyMap map[string]struct {
FullName string `json:"full_name"`
DefaultNetwork string `json:"default_network"`
Expand Down Expand Up @@ -281,15 +281,15 @@ type PersonalTradeItem struct {
// OrderDetail represents an order detail.
type OrderDetail struct {
TradeList []struct {
Side string `json:"side"`
InstrumentName string `json:"instrument_name"`
Fee float64 `json:"fee"`
TradeID string `json:"trade_id"`
CreateTime int64 `json:"create_time"`
TradedPrice float64 `json:"traded_price"`
TradedQuantity float64 `json:"traded_quantity"`
FeeCurrency string `json:"fee_currency"`
OrderID string `json:"order_id"`
Side string `json:"side"`
InstrumentName string `json:"instrument_name"`
Fee float64 `json:"fee"`
TradeID string `json:"trade_id"`
CreateTime types.Time `json:"create_time"`
TradedPrice float64 `json:"traded_price"`
TradedQuantity float64 `json:"traded_quantity"`
FeeCurrency string `json:"fee_currency"`
OrderID string `json:"order_id"`
} `json:"trade_list"`
OrderInfo OrderItem `json:"order_info"`
}
Expand Down Expand Up @@ -715,12 +715,12 @@ type UserBalance struct {

// WsOrderbook represents an orderbook websocket push data.
type WsOrderbook struct {
Asks [][3]string `json:"asks"`
Bids [][3]string `json:"bids"`
PushTime types.Time `json:"t"`
OrderbookUpdateTime types.Time `json:"tt"`
UpdateSequence int64 `json:"u"`
Cs int64 `json:"cs"`
Asks [][3]types.Number `json:"asks"`
Bids [][3]types.Number `json:"bids"`
PushTime types.Time `json:"t"`
OrderbookUpdateTime types.Time `json:"tt"`
UpdateSequence int64 `json:"u"`
Cs int64 `json:"cs"`
}

// WsRequestPayload represents authentication and request sending payload
Expand Down
Loading

0 comments on commit e66e793

Please sign in to comment.