Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions hyperliquid/exchange_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ type IExchangeAPI interface {
IClient

// Open orders
BulkOrders(requests []OrderRequest, grouping Grouping) (*PlaceOrderResponse, error)
Order(request OrderRequest, grouping Grouping) (*PlaceOrderResponse, error)
MarketOrder(coin string, size float64, slippage *float64, clientOID ...string) (*PlaceOrderResponse, error)
LimitOrder(orderType string, coin string, size float64, px float64, isBuy bool, reduceOnly bool, clientOID ...string) (*PlaceOrderResponse, error)
BulkOrders(requests []OrderRequest, grouping Grouping) (*OrderResponse, error)
Order(request OrderRequest, grouping Grouping) (*OrderResponse, error)
MarketOrder(coin string, size float64, slippage *float64, clientOID ...string) (*OrderResponse, error)
LimitOrder(orderType string, coin string, size float64, px float64, isBuy bool, reduceOnly bool, clientOID ...string) (*OrderResponse, error)

// Order management
CancelOrderByOID(coin string, orderID int) (any, error)
CancelOrderByCloid(coin string, clientOID string) (any, error)
BulkCancelOrders(cancels []CancelOidWire) (any, error)
CancelAllOrdersByCoin(coin string) (any, error)
CancelAllOrders() (any, error)
ClosePosition(coin string) (*PlaceOrderResponse, error)
ClosePosition(coin string) (*OrderResponse, error)

// Account management
Withdraw(destination string, amount float64) (*WithdrawResponse, error)
Expand Down Expand Up @@ -99,7 +99,7 @@ func (api *ExchangeAPI) SlippagePriceSpot(coin string, isBuy bool, slippage floa
// MarketOrder("BTC", 0.1, nil) // Buy 0.1 BTC
// MarketOrder("BTC", -0.1, nil) // Sell 0.1 BTC
// MarketOrder("BTC", 0.1, &slippage) // Buy 0.1 BTC with slippage
func (api *ExchangeAPI) MarketOrder(coin string, size float64, slippage *float64, clientOID ...string) (*PlaceOrderResponse, error) {
func (api *ExchangeAPI) MarketOrder(coin string, size float64, slippage *float64, clientOID ...string) (*OrderResponse, error) {
slpg := GetSlippage(slippage)
isBuy := IsBuy(size)
finalPx := api.SlippagePrice(coin, isBuy, slpg)
Expand Down Expand Up @@ -130,7 +130,7 @@ func (api *ExchangeAPI) MarketOrder(coin string, size float64, slippage *float64
// MarketOrderSpot("HYPE", 0.1, nil) // Buy 0.1 HYPE
// MarketOrderSpot("HYPE", -0.1, nil) // Sell 0.1 HYPE
// MarketOrderSpot("HYPE", 0.1, &slippage) // Buy 0.1 HYPE with slippage
func (api *ExchangeAPI) MarketOrderSpot(coin string, size float64, slippage *float64) (*PlaceOrderResponse, error) {
func (api *ExchangeAPI) MarketOrderSpot(coin string, size float64, slippage *float64) (*OrderResponse, error) {
slpg := GetSlippage(slippage)
isBuy := IsBuy(size)
finalPx := api.SlippagePriceSpot(coin, isBuy, slpg)
Expand All @@ -154,7 +154,7 @@ func (api *ExchangeAPI) MarketOrderSpot(coin string, size float64, slippage *flo
// Order type can be Gtc, Ioc, Alo.
// Size determines the amount of the coin to buy/sell.
// See the constants TifGtc, TifIoc, TifAlo.
func (api *ExchangeAPI) LimitOrder(orderType string, coin string, size float64, px float64, reduceOnly bool, clientOID ...string) (*PlaceOrderResponse, error) {
func (api *ExchangeAPI) LimitOrder(orderType string, coin string, size float64, px float64, reduceOnly bool, clientOID ...string) (*OrderResponse, error) {
// check if the order type is valid
if orderType != TifGtc && orderType != TifIoc && orderType != TifAlo {
return nil, APIError{Message: fmt.Sprintf("Invalid order type: %s. Available types: %s, %s, %s", orderType, TifGtc, TifIoc, TifAlo)}
Expand All @@ -179,7 +179,7 @@ func (api *ExchangeAPI) LimitOrder(orderType string, coin string, size float64,
}

// Close all positions for a given coin. They are closing with a market order.
func (api *ExchangeAPI) ClosePosition(coin string) (*PlaceOrderResponse, error) {
func (api *ExchangeAPI) ClosePosition(coin string) (*OrderResponse, error) {
// Get all positions and find the one for the coin
// Then just make MarketOpen with the reverse size
state, err := api.infoAPI.GetUserState(api.AccountAddress())
Expand Down Expand Up @@ -219,18 +219,18 @@ func (api *ExchangeAPI) ClosePosition(coin string) (*PlaceOrderResponse, error)
}

// Place single order
func (api *ExchangeAPI) Order(request OrderRequest, grouping Grouping) (*PlaceOrderResponse, error) {
func (api *ExchangeAPI) Order(request OrderRequest, grouping Grouping) (*OrderResponse, error) {
return api.BulkOrders([]OrderRequest{request}, grouping, false)
}

// OrderSpot places a spot order
func (api *ExchangeAPI) OrderSpot(request OrderRequest, grouping Grouping) (*PlaceOrderResponse, error) {
func (api *ExchangeAPI) OrderSpot(request OrderRequest, grouping Grouping) (*OrderResponse, error) {
return api.BulkOrders([]OrderRequest{request}, grouping, true)
}

// Place orders in bulk
// https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-an-order
func (api *ExchangeAPI) BulkOrders(requests []OrderRequest, grouping Grouping, isSpot bool) (*PlaceOrderResponse, error) {
func (api *ExchangeAPI) BulkOrders(requests []OrderRequest, grouping Grouping, isSpot bool) (*OrderResponse, error) {
var wires []OrderWire
var meta map[string]AssetInfo
if isSpot {
Expand All @@ -254,12 +254,12 @@ func (api *ExchangeAPI) BulkOrders(requests []OrderRequest, grouping Grouping, i
Signature: ToTypedSig(r, s, v),
VaultAddress: nil,
}
return MakeUniversalRequest[PlaceOrderResponse](api, request)
return MakeUniversalRequest[OrderResponse](api, request)
}

// Cancel order(s)
// https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s
func (api *ExchangeAPI) BulkCancelOrders(cancels []CancelOidWire) (*CancelOrderResponse, error) {
func (api *ExchangeAPI) BulkCancelOrders(cancels []CancelOidWire) (*OrderResponse, error) {
timestamp := GetNonce()
action := CancelOidOrderAction{
Type: "cancel",
Expand All @@ -276,12 +276,12 @@ func (api *ExchangeAPI) BulkCancelOrders(cancels []CancelOidWire) (*CancelOrderR
Signature: ToTypedSig(r, s, v),
VaultAddress: nil,
}
return MakeUniversalRequest[CancelOrderResponse](api, request)
return MakeUniversalRequest[OrderResponse](api, request)
}

// Bulk modify orders
// https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-multiple-orders
func (api *ExchangeAPI) BulkModifyOrders(modifyRequests []ModifyOrderRequest, isSpot bool) (*PlaceOrderResponse, error) {
func (api *ExchangeAPI) BulkModifyOrders(modifyRequests []ModifyOrderRequest, isSpot bool) (*OrderResponse, error) {
wires := []ModifyOrderWire{}

for _, req := range modifyRequests {
Expand All @@ -303,17 +303,17 @@ func (api *ExchangeAPI) BulkModifyOrders(modifyRequests []ModifyOrderRequest, is
Signature: ToTypedSig(rVal, sVal, vVal),
VaultAddress: nil,
}
return MakeUniversalRequest[PlaceOrderResponse](api, request)
return MakeUniversalRequest[OrderResponse](api, request)
}

// Cancel exact order by OID
func (api *ExchangeAPI) CancelOrderByOID(coin string, orderID int64) (*CancelOrderResponse, error) {
func (api *ExchangeAPI) CancelOrderByOID(coin string, orderID int64) (*OrderResponse, error) {
return api.BulkCancelOrders([]CancelOidWire{{Asset: api.meta[coin].AssetId, Oid: int(orderID)}})
}

// Cancel exact order by Client Order Id
// https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s-by-cloid
func (api *ExchangeAPI) CancelOrderByCloid(coin string, clientOID string) (*CancelOrderResponse, error) {
func (api *ExchangeAPI) CancelOrderByCloid(coin string, clientOID string) (*OrderResponse, error) {
timestamp := GetNonce()
action := CancelCloidOrderAction{
Type: "cancelByCloid",
Expand All @@ -335,11 +335,11 @@ func (api *ExchangeAPI) CancelOrderByCloid(coin string, clientOID string) (*Canc
Signature: ToTypedSig(r, s, v),
VaultAddress: nil,
}
return MakeUniversalRequest[CancelOrderResponse](api, request)
return MakeUniversalRequest[OrderResponse](api, request)
}

// Cancel all orders for a given coin
func (api *ExchangeAPI) CancelAllOrdersByCoin(coin string) (*CancelOrderResponse, error) {
func (api *ExchangeAPI) CancelAllOrdersByCoin(coin string) (*OrderResponse, error) {
orders, err := api.infoAPI.GetOpenOrders(api.AccountAddress())
if err != nil {
api.debug("Error getting orders: %s", err)
Expand All @@ -356,7 +356,7 @@ func (api *ExchangeAPI) CancelAllOrdersByCoin(coin string) (*CancelOrderResponse
}

// Cancel all open orders
func (api *ExchangeAPI) CancelAllOrders() (*CancelOrderResponse, error) {
func (api *ExchangeAPI) CancelAllOrders() (*OrderResponse, error) {
orders, err := api.infoAPI.GetOpenOrders(api.AccountAddress())
if err != nil {
api.debug("Error getting orders: %s", err)
Expand Down
26 changes: 6 additions & 20 deletions hyperliquid/exchange_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ type OrderWire struct {
Cloid string `msgpack:"c,omitempty" json:"c,omitempty"`
}
type ModifyResponse struct {
Status string `json:"status"`
Response PlaceOrderInnerResponse `json:"response"`
Status string `json:"status"`
Response OrderInnerResponse `json:"response"`
}
type ModifyOrderWire struct {
OrderId int `msgpack:"oid" json:"oid"`
Expand Down Expand Up @@ -111,12 +111,12 @@ type PlaceOrderAction struct {
Grouping Grouping `msgpack:"grouping" json:"grouping"`
}

type PlaceOrderResponse struct {
Status string `json:"status"`
Response PlaceOrderInnerResponse `json:"response"`
type OrderResponse struct {
Status string `json:"status"`
Response OrderInnerResponse `json:"response"`
}

type PlaceOrderInnerResponse struct {
type OrderInnerResponse struct {
Type string `json:"type"`
Data DataResponse `json:"data"`
}
Expand Down Expand Up @@ -156,20 +156,6 @@ type CancelCloidOrderAction struct {
Cancels []CancelCloidWire `msgpack:"cancels" json:"cancels"`
}

type CancelOrderResponse struct {
Status string `json:"status"`
Response InnerCancelResponse `json:"response"`
}

type InnerCancelResponse struct {
Type string `json:"type"`
Data CancelResponseStatuses `json:"data"`
}

type CancelResponseStatuses struct {
Statuses []string `json:"statuses"`
}

type RestingStatus struct {
OrderId int `json:"oid"`
Cloid string `json:"cloid,omitempty"`
Expand Down