Skip to content

Commit

Permalink
Adding market endpoints and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samuael committed Dec 16, 2024
1 parent 8d7776d commit c3aed40
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 3 deletions.
50 changes: 49 additions & 1 deletion exchanges/poloniex/poloniex_futures.go
Original file line number Diff line number Diff line change
Expand Up @@ -1186,10 +1186,10 @@ func IntervalString(interval kline.Interval) (string, error) {

// GetV3FuturesKlineData retrieves K-line data of the designated trading pair
func (p *Poloniex) GetV3FuturesKlineData(ctx context.Context, symbol string, interval kline.Interval, startTime, endTime time.Time, limit int64) ([]V3FuturesCandle, error) {
params := url.Values{}
if symbol == "" {
return nil, currency.ErrSymbolStringEmpty
}
params := url.Values{}
intervalString, err := IntervalString(interval)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1271,3 +1271,51 @@ func (p *Poloniex) GetV3FuturesIndexPrice(ctx context.Context, symbol string) (*
var resp *InstrumentIndexPrice
return resp, p.SendHTTPRequest(ctx, exchange.RestSpot, request.UnAuth, common.EncodeURLValues("/v3/market/indexPrice", params), &resp, true)
}

// GetV3IndexPriceComponents get the index price components for a trading pair.
func (p *Poloniex) GetV3IndexPriceComponents(ctx context.Context, symbol string) (*IndexPriceComponent, error) {
if symbol == "" {
return nil, currency.ErrSymbolStringEmpty
}
params := url.Values{}
params.Set("symbol", symbol)
var resp *IndexPriceComponent
return resp, p.SendHTTPRequest(ctx, exchange.RestSpot, request.UnAuth, common.EncodeURLValues("/v3/market/indexPriceComponents", params), &resp, true)
}

// GetIndexPriceKlineData obtain the K-line data for the index price.
func (p *Poloniex) GetIndexPriceKlineData(ctx context.Context, symbol string, interval kline.Interval, startTime, endTime time.Time, limit int64) (interface{}, error) {
if symbol == "" {
return nil, currency.ErrSymbolStringEmpty
}
intervalString, err := IntervalString(interval)
if err != nil {
return nil, err
}
params := url.Values{}
params.Set("symbol", symbol)
params.Set("interval", intervalString)
if !startTime.IsZero() && !endTime.IsZero() {
err := common.StartEndTimeCheck(startTime, endTime)
if err != nil {
return nil, err
}
params.Set("sTime", strconv.FormatInt(startTime.UnixMilli(), 10))
params.Set("eTime", strconv.FormatInt(startTime.UnixMilli(), 10))
}
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
var resp []V3FuturesIndexPriceData
return resp, p.SendHTTPRequest(ctx, exchange.RestSpot, request.UnAuth, common.EncodeURLValues("/v3/market/indexPriceCandlesticks", params), &resp, true)
}

// GetV3FuturesMarkPrice get the current mark price.
func (p *Poloniex) GetV3FuturesMarkPrice(ctx context.Context, symbol string) (*V3FuturesMarkPrice, error) {
params := url.Values{}
if symbol != "" {
params.Set("symbol", symbol)
}
var resp *V3FuturesMarkPrice
return resp, p.SendHTTPRequest(ctx, exchange.RestSpot, request.UnAuth, common.EncodeURLValues("/v3/market/markPrice", params), &resp, true)
}
34 changes: 34 additions & 0 deletions exchanges/poloniex/poloniex_futures_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,37 @@ type InstrumentIndexPrice struct {
Symbol string `json:"symbol"`
IndexPrice types.Number `json:"iPx"`
}

// IndexPriceComponent represents an index price component detail
type IndexPriceComponent []struct {
Symbol string `json:"s"`
Price types.Number `json:"px"`
Cs []struct {
Exchange string `json:"e"`
WeightFactor types.Number `json:"w"`
TradingPairPrice types.Number `json:"sPx"`
TradingPairIndexPrice types.Number `json:"cPx"`
} `json:"cs"`
}

// V3FuturesIndexPriceData represents a futures index price data detail
type V3FuturesIndexPriceData struct {
OpenPrice types.Number
HighPrice types.Number
LowestPrice types.Number
ClosingPrice types.Number
StartTime types.Time
EndTime types.Time
}

// UnmarshalJSON deserializes candlestick data into a V3FuturesIndexPriceData instance
func (v *V3FuturesIndexPriceData) UnmarshalJSON(data []byte) error {
target := [6]any{&v.OpenPrice, &v.HighPrice, &v.LowestPrice, &v.ClosingPrice, &v.StartTime, &v.EndTime}
return json.Unmarshal(data, &target)
}

// V3FuturesMarkPrice represents a mark price instance
type V3FuturesMarkPrice struct {
MarkPrice types.Number `json:"mPx"`
Symbol string `json:"symbol"`
}
34 changes: 32 additions & 2 deletions exchanges/poloniex/poloniex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (

// Please supply your own APIKEYS here for due diligence testing
const (
apiKey = "WSKMLKNW-JCKF6SGH-VWKQAUS8-RYYWFJYP"
apiSecret = "b1b11137b33e52bd7ae2df3a59e905141b40740edd0568f9141b63ed0cea6bdcab8b2ac8e307ca11a048493fd7d1528a26d7a1a9e3caae53fb82965b3ebf2b57"
apiKey = ""
apiSecret = ""
canManipulateRealOrders = false
)

Expand Down Expand Up @@ -2336,3 +2336,33 @@ func TestGetV3FuturesIndexPrice(t *testing.T) {
require.NoError(t, err)
assert.NotNil(t, result)
}

func TestGetV3IndexPriceComponents(t *testing.T) {
t.Parallel()
_, err := p.GetV3IndexPriceComponents(context.Background(), "")
require.ErrorIs(t, err, currency.ErrSymbolStringEmpty)

result, err := p.GetV3IndexPriceComponents(context.Background(), "BTC_USDT_PERP")
require.NoError(t, err)
assert.NotNil(t, result)
}

func TestGetIndexPriceKlineData(t *testing.T) {
t.Parallel()
_, err := p.GetIndexPriceKlineData(context.Background(), "", kline.FiveMin, time.Time{}, time.Time{}, 10)
require.ErrorIs(t, err, currency.ErrSymbolStringEmpty)

_, err = p.GetIndexPriceKlineData(context.Background(), "BTC_USDT_PERP", kline.SixHour, time.Time{}, time.Time{}, 10)
require.ErrorIs(t, err, kline.ErrUnsupportedInterval)

result, err := p.GetIndexPriceKlineData(context.Background(), "BTC_USDT_PERP", kline.FourHour, time.Time{}, time.Time{}, 10)
require.NoError(t, err)
assert.NotNil(t, result)
}

func TestGetV3FuturesMarkPrice(t *testing.T) {
t.Parallel()
result, err := p.GetV3FuturesMarkPrice(context.Background(), "BTC_USDT_PERP")
require.NoError(t, err)
assert.NotNil(t, result)
}
163 changes: 163 additions & 0 deletions testdata/http_mock/poloniex/poloniex.json
Original file line number Diff line number Diff line change
Expand Up @@ -135864,6 +135864,152 @@
}
]
},
"/v3/market/indexPriceCandlesticks": {
"GET": [
{
"data": {
"code": 200,
"data": [
[
"101583.66",
"102688.38",
"101731.3",
"102668.12",
"1734249600000",
"1734263999999"
],
[
"102471.6",
"103319.9",
"102668.13",
"102704.93",
"1734264000000",
"1734278399999"
],
[
"102584.34",
"103482.19",
"102704.94",
"103253.49",
"1734278400000",
"1734292799999"
],
[
"102624.48",
"105216.38",
"103253.49",
"104460.57",
"1734292800000",
"1734307199999"
],
[
"104283.98",
"106623.84",
"104463.68",
"104582.66",
"1734307200000",
"1734321599999"
],
[
"104472.56",
"105411.15",
"104582.66",
"104988.77",
"1734321600000",
"1734335999999"
],
[
"103637",
"105016.97",
"104988.77",
"103760.28",
"1734336000000",
"1734350399999"
],
[
"103340.64",
"106495.41",
"103765.25",
"106334.12",
"1734350400000",
"1734364799999"
],
[
"105800.93",
"107788.39",
"106333.03",
"106584.75",
"1734364800000",
"1734379199999"
],
[
"106186.03",
"106645.82",
"106584.75",
"106344.93",
"1734379200000",
"1734393599999"
]
],
"msg": "Success"
},
"queryString": "interval=HOUR_4\u0026limit=10\u0026symbol=BTC_USDT_PERP",
"bodyParams": "",
"headers": {}
}
]
},
"/v3/market/indexPriceComponents": {
"GET": [
{
"data": {
"code": 200,
"data": [
{
"cs": [
{
"cPx": "79490.8260397968",
"e": "Binance",
"sPx": "106578.52",
"w": "0.74584284"
},
{
"cPx": "0.00",
"e": "Kucoin",
"sPx": "106584.1",
"w": "0.0"
},
{
"cPx": "0.000",
"e": "huobi",
"sPx": "106561.29",
"w": "0.0"
},
{
"cPx": "24238.676665800",
"e": "Okx",
"sPx": "106582.5",
"w": "0.22741704"
},
{
"cPx": "2849.69458840",
"e": "Poloniex",
"sPx": "106570",
"w": "0.02674012"
}
],
"px": "106579.2000000000",
"s": "BTC_USDT_PERP"
}
],
"msg": "Success"
},
"queryString": "symbol=BTC_USDT_PERP",
"bodyParams": "",
"headers": {}
}
]
},
"/v3/market/liquidationOrder": {
"GET": [
{
Expand Down Expand Up @@ -135959,6 +136105,23 @@
}
]
},
"/v3/market/markPrice": {
"GET": [
{
"data": {
"code": 200,
"data": {
"mPx": "105817.02",
"s": "BTC_USDT_PERP"
},
"msg": "Success"
},
"queryString": "symbol=BTC_USDT_PERP",
"bodyParams": "",
"headers": {}
}
]
},
"/v3/market/orderBook": {
"GET": [
{
Expand Down

0 comments on commit c3aed40

Please sign in to comment.