Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #19 from Kucoin/dev
Browse files Browse the repository at this point in the history
Update API For August
  • Loading branch information
ISAAC-XXYYZZ authored Sep 4, 2024
2 parents a4c561e + f19b383 commit 9b4605d
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 33 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ if err != nil {
return
}

ch1 := kumex.NewSubscribeMessage("/contractMarket/ticker:XBTUSDM", false)
ch2 := kumex.NewSubscribeMessage("/contractMarket/ticker:XBTUSDM", false)
uch := kumex.NewUnsubscribeMessage("/contractMarket/ticker:XBTUSDM", false)
ch1 := kumex.NewSubscribeMessage("/contractMarket/ticker:XBTUSDTM", false)
ch2 := kumex.NewSubscribeMessage("/contractMarket/ticker:XBTUSDTM", false)
uch := kumex.NewUnsubscribeMessage("/contractMarket/ticker:XBTUSDTM", false)

if err := c.Subscribe(ch1, ch2); err != nil {
// Handle error
Expand All @@ -191,15 +191,15 @@ for {
log.Printf("Ticker: %s, %s, %s, %s", msg.Topic, t.Sequence, t.Price, t.Size)
i++
if i == 5 {
log.Println("Unsubscribe XBTUSDM")
log.Println("Unsubscribe XBTUSDTM")
if err = c.Unsubscribe(uch); err != nil {
log.Printf("Error: %s", err.Error())
// Handle error
return
}
}
if i == 10 {
log.Println("Subscribe XBTUSDM")
log.Println("Subscribe XBTUSDTM")
if err = c.Subscribe(ch2); err != nil {
log.Printf("Error: %s", err.Error())
// Handle error
Expand Down
2 changes: 1 addition & 1 deletion contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestApiService_Contracts(t *testing.T) {
t.SkipNow()

s := NewApiServiceFromEnv()
rsp, err := s.Contracts("XBTUSDM")
rsp, err := s.Contracts("XBTUSDTM")
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 4 additions & 4 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ func publicWebsocket(s *kumex.ApiService) {
return
}

ch1 := kumex.NewSubscribeMessage("/contract/instrument:XBTUSDM", false)
ch2 := kumex.NewSubscribeMessage("/contractMarket/level3:XBTUSDM", false)
//uch := kumex.NewUnsubscribeMessage("/contractMarket/ticker:XBTUSDM", false)
ch1 := kumex.NewSubscribeMessage("/contract/instrument:XBTUSDTM", false)
ch2 := kumex.NewSubscribeMessage("/contractMarket/level3:XBTUSDTM", false)
//uch := kumex.NewUnsubscribeMessage("/contractMarket/ticker:XBTUSDTM", false)

if err := c.Subscribe(ch1, ch2); err != nil {
// Handle error
Expand Down Expand Up @@ -138,7 +138,7 @@ func privateWebsocket(s *kumex.ApiService) {
return
}

ch1 := kumex.NewSubscribeMessage("/contract/position:XBTUSDM", false)
ch1 := kumex.NewSubscribeMessage("/contract/position:XBTUSDTM", false)
ch2 := kumex.NewSubscribeMessage("/contractAccount/wallet", false)

log.Println(kumex.ToJsonString(ch1))
Expand Down
2 changes: 1 addition & 1 deletion fill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestApiService_OpenOrderStatistics(t *testing.T) {
t.SkipNow()

s := NewApiServiceFromEnv()
rsp, err := s.OpenOrderStatistics("XBTUSDM")
rsp, err := s.OpenOrderStatistics("XBTUSDTM")
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion funding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "testing"

func TestApiService_FundingHistory(t *testing.T) {
s := NewApiServiceFromEnv()
rsp, err := s.FundingHistory(map[string]string{"symbol": "XBTUSDM"})
rsp, err := s.FundingHistory(map[string]string{"symbol": "XBTUSDTM"})
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion kline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func TestApiService_KLines(t *testing.T) {
s := NewApiServiceFromEnv()
rsp, err := s.KLines("XBTUSDM", "5", time.Now().UnixNano()/1e6-7*24*3600, time.Now().UnixNano()/1e6)
rsp, err := s.KLines("XBTUSDTM", "5", time.Now().UnixNano()/1e6-7*24*3600, time.Now().UnixNano()/1e6)
if err != nil {
t.Fatal(err)
}
Expand Down
22 changes: 22 additions & 0 deletions market.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,25 @@ func (as *ApiService) TradeStatistics() (*ApiResponse, error) {
req := NewRequest(http.MethodGet, "/api/v1/trade-statistics", nil)
return as.Call(req)
}

type AllTickersModel []TickerItem

type TickerItem struct {
Sequence int64 `json:"sequence"`
Symbol string `json:"symbol"`
Side string `json:"side"`
Size int `json:"size"`
TradeID string `json:"tradeId"`
Price string `json:"price"`
BestBidPrice string `json:"bestBidPrice"`
BestBidSize int `json:"bestBidSize"`
BestAskPrice string `json:"bestAskPrice"`
BestAskSize int `json:"bestAskSize"`
Timestamp int64 `json:"ts"`
}

// AllTickers Get Latest Ticker for All Contracts
func (as *ApiService) AllTickers() (*ApiResponse, error) {
req := NewRequest(http.MethodGet, "/api/v1/allTickers", nil)
return as.Call(req)
}
35 changes: 24 additions & 11 deletions market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func TestApiService_TickerLevel1(t *testing.T) {
s := NewApiServiceFromEnv()
rsp, err := s.Ticker("XBTUSDM")
rsp, err := s.Ticker("XBTUSDTM")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -35,7 +35,7 @@ func TestApiService_TickerLevel1(t *testing.T) {

func TestApiService_Level2Snapshot(t *testing.T) {
s := NewApiServiceFromEnv()
rsp, err := s.Level2Snapshot("XBTUSDM")
rsp, err := s.Level2Snapshot("XBTUSDTM")
if err != nil {
t.Fatal(err)
}
Expand All @@ -62,7 +62,7 @@ func TestApiService_Level2Snapshot(t *testing.T) {

func TestApiService_Level2MessageQuery(t *testing.T) {
s := NewApiServiceFromEnv()
rsp, err := s.Level2MessageQuery("XBTUSDM", 1, 20)
rsp, err := s.Level2MessageQuery("XBTUSDTM", 1, 20)
if err != nil {
t.Fatal(err)
}
Expand All @@ -85,7 +85,7 @@ func TestApiService_Level2MessageQuery(t *testing.T) {

func TestApiService_Level3Snapshot(t *testing.T) {
s := NewApiServiceFromEnv()
rsp, err := s.Level3Snapshot("XBTUSDM")
rsp, err := s.Level3Snapshot("XBTUSDTM")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestApiService_Level3Snapshot(t *testing.T) {

func TestApiService_Level3SnapshotV2(t *testing.T) {
s := NewApiServiceFromEnv()
rsp, err := s.Level3SnapshotV2("XBTUSDM")
rsp, err := s.Level3SnapshotV2("XBTUSDTM")
if err != nil {
t.Fatal(err)
}
Expand All @@ -133,7 +133,7 @@ func TestApiService_Level3SnapshotV2(t *testing.T) {

func TestApiService_Level3MessageQuery(t *testing.T) {
s := NewApiServiceFromEnv()
rsp, err := s.Level3MessageQuery("XBTUSDM", 1, 20)
rsp, err := s.Level3MessageQuery("XBTUSDTM", 1, 20)
if err != nil {
t.Fatal(err)
}
Expand All @@ -156,7 +156,7 @@ func TestApiService_Level3MessageQuery(t *testing.T) {

func TestApiService_TradeHistory(t *testing.T) {
s := NewApiServiceFromEnv()
rsp, err := s.TradeHistory("XBTUSDM")
rsp, err := s.TradeHistory("XBTUSDTM")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -213,7 +213,7 @@ func TestApiService_InterestQuery(t *testing.T) {
func TestApiService_IndexQuery(t *testing.T) {
s := NewApiServiceFromEnv()
p := map[string]string{}
p["symbol"] = "XBTUSDM"
p["symbol"] = "XBTUSDTM"
pp := &PaginationParam{CurrentPage: 1, PageSize: 10}
rsp, err := s.IndexQuery(p, pp)
if err != nil {
Expand All @@ -240,7 +240,7 @@ func TestApiService_IndexQuery(t *testing.T) {

func TestApiService_MarkPrice(t *testing.T) {
s := NewApiServiceFromEnv()
rsp, err := s.MarkPrice("XBTUSDM")
rsp, err := s.MarkPrice("XBTUSDTM")
if err != nil {
t.Fatal(err)
}
Expand All @@ -264,7 +264,7 @@ func TestApiService_MarkPrice(t *testing.T) {
func TestApiService_PremiumQuery(t *testing.T) {
s := NewApiServiceFromEnv()
p := map[string]string{}
p["symbol"] = "XBTUSDM"
p["symbol"] = "XBTUSDTM"
pp := &PaginationParam{CurrentPage: 1, PageSize: 10}
rsp, err := s.PremiumQuery(p, pp)
if err != nil {
Expand All @@ -291,7 +291,7 @@ func TestApiService_PremiumQuery(t *testing.T) {

func TestApiService_FundingRate(t *testing.T) {
s := NewApiServiceFromEnv()
rsp, err := s.FundingRate(".XBTUSDMFPI8H")
rsp, err := s.FundingRate(".XBTUSDTMFPI8H")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -324,3 +324,16 @@ func TestApiService_TradeStatistics(t *testing.T) {
}
t.Log(ToJsonString(tk))
}

func TestApiService_AllTickers(t *testing.T) {
s := NewApiServiceFromEnv()
resp, err := s.AllTickers()
if err != nil {
t.Fatal(err)
}
m := &AllTickersModel{}
if err := resp.ReadData(m); err != nil {
t.Fatal(err)
}
t.Log(ToJsonString(m))
}
38 changes: 37 additions & 1 deletion order.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import "net/http"

// A CreateOrderResultModel represents the result of CreateOrder().
type CreateOrderResultModel struct {
OrderId string `json:"orderId"`
OrderId string `json:"orderId"`
ClientOid string `json:"clientOid"`
}

// CreateOrder places a new order.
Expand Down Expand Up @@ -181,3 +182,38 @@ func (as *ApiService) CreateMultiOrders(p []*CreateOrderReq) (*ApiResponse, erro
req := NewRequest(http.MethodPost, "/api/v1/orders/multi", p)
return as.Call(req)
}

type STOrderReq struct {
ClientOid string `json:"clientOid"`
Side string `json:"side"`
Symbol string `json:"symbol"`
Leverage string `json:"leverage"`
Type string `json:"type"`
Remark string `json:"remark"`
TriggerStopUpPrice string `json:"triggerStopUpPrice"`
StopPriceType string `json:"stopPriceType"`
TriggerStopDownPrice string `json:"triggerStopDownPrice"`
ReduceOnly bool `json:"reduceOnly"`
CloseOrder bool `json:"closeOrder"`
ForceHold bool `json:"forceHold"`
Stp string `json:"stp"`
Price string `json:"price"`
Size int `json:"size"`
TimeInForce string `json:"timeInForce"`
PostOnly bool `json:"postOnly"`
Hidden bool `json:"hidden"`
Iceberg bool `json:"iceberg"`
VisibleSize int `json:"visibleSize"`
MarginMode string `json:"marginMode"`
}

type STOrderRes struct {
OrderId string `json:"orderId"`
ClientOid string `json:"clientOid"`
}

// CreateSTOrder Place take profit and stop loss order
func (as *ApiService) CreateSTOrder(p *STOrderReq) (*ApiResponse, error) {
req := NewRequest(http.MethodPost, "/api/v1/st-orders", p)
return as.Call(req)
}
27 changes: 27 additions & 0 deletions order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,30 @@ func TestApiService_CreateMultiOrders(t *testing.T) {
}
t.Log(ToJsonString(o))
}

func TestApiService_CreateSTOrders(t *testing.T) {
s := NewApiServiceFromEnv()
req := STOrderReq{
ClientOid: IntToString(time.Now().UnixNano()),
Side: "buy",
Symbol: "XBTUSDM",
Leverage: "20",
Type: "limit",
Price: "8000",
Size: 1,
StopPriceType: "TP",
MarginMode: "ISOLATED",
TriggerStopUpPrice: "9000",
TriggerStopDownPrice: "8000",
TimeInForce: "GTC",
}
rsp, err := s.CreateSTOrder(&req)
if err != nil {
t.Fatal(err)
}
o := &STOrderRes{}
if err := rsp.ReadData(o); err != nil {
t.Fatal(err)
}
t.Log(ToJsonString(o))
}
22 changes: 22 additions & 0 deletions position.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,25 @@ func (as *ApiService) GetPositionsHistoryV1(r *GetPositionsHistoryV1Req) (*ApiRe
req := NewRequest(http.MethodGet, "/api/v1/history-positions", v)
return as.Call(req)
}

type GetMaxOpenSizeReq struct {
Symbol string `url:"symbol"`
Price string `url:"price"`
Leverage string `url:"leverage"`
}

type GetMaxOpenSizeResp struct {
Symbol string `json:"symbol"`
MaxBuyOpenSize int `json:"maxBuyOpenSize"`
MaxSellOpenSize int `json:"leverage"`
}

// GetMaxOpenSize Get Maximum Open Position Size
func (as *ApiService) GetMaxOpenSize(r *GetMaxOpenSizeReq) (*ApiResponse, error) {
v, err := query.Values(r)
if err != nil {
return nil, err
}
req := NewRequest(http.MethodGet, "/api/v2/getMaxOpenSize", v)
return as.Call(req)
}
25 changes: 22 additions & 3 deletions position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func TestApiService_Position(t *testing.T) {
t.SkipNow()

s := NewApiServiceFromEnv()
rsp, err := s.Position("XBTUSDM")
rsp, err := s.Position("XBTUSDTM")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestApiService_autoDepositStatus(t *testing.T) {

s := NewApiServiceFromEnv()
p := map[string]string{
"symbol": "XBTUSDM",
"symbol": "XBTUSDTM",
"status": "true",
}
rsp, err := s.AutoDepositStatus(p)
Expand All @@ -88,7 +88,7 @@ func TestApiService_DepositMargin(t *testing.T) {

s := NewApiServiceFromEnv()
p := map[string]string{
"symbol": "XBTUSDM",
"symbol": "XBTUSDTM",
"margin": "0.1111",
"bizNo": IntToString(time.Now().UnixNano()),
}
Expand Down Expand Up @@ -155,3 +155,22 @@ func TestApiService_GetPositionsHistoryV1(t *testing.T) {
}
t.Log(ToJsonString(data))
}

func TestApiService_GetMaxOpenSize(t *testing.T) {
s := NewApiServiceFromEnv()

req := GetMaxOpenSizeReq{}
req.Symbol = "PEPEUSDTM"
req.Price = "0.0000000001"
req.Leverage = "10"

resp, err := s.GetMaxOpenSize(&req)
if err != nil {
t.Fatal(err)
}
m := &GetMaxOpenSizeResp{}
if err := resp.ReadData(m); err != nil {
t.Fatal(err)
}
t.Log(ToJsonString(m))
}
Loading

0 comments on commit 9b4605d

Please sign in to comment.