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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "vertex-protocol"
version = "3.1.6"
version = "3.1.7"
description = "Vertex Protocol SDK"
authors = ["Jeury Mejia <jeury@vertexprotocol.com>"]
homepage = "https://vertexprotocol.com/"
Expand Down
18 changes: 18 additions & 0 deletions tests/engine_client/test_expiration.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,21 @@ def test_expiration_encoding():

assert decoded_unix_epoch == unix_epoch
assert decoded_order_type == order_type


def test_reduce_only():
unix_epoch = 1685939478

def is_reduce_only(expiration: int):
return (expiration & (1 << 61)) != 0

reduced_only_expiration = get_expiration_timestamp(
OrderType.FOK, unix_epoch, reduce_only=True
)
non_reduced_only_expiration = get_expiration_timestamp(OrderType.FOK, unix_epoch)

assert is_reduce_only(reduced_only_expiration)
assert not is_reduce_only(non_reduced_only_expiration)
assert not is_reduce_only(
get_expiration_timestamp(OrderType.FOK, unix_epoch, bool(None))
)
6 changes: 2 additions & 4 deletions vertex_protocol/engine_client/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ def place_market_order(self, params: PlaceMarketOrderParams) -> ExecuteResponse:
nonce=params.market_order.nonce,
priceX18=round_x18(market_price_x18, price_increment_x18),
expiration=get_expiration_timestamp(
OrderType.FOK,
int(time.time()) + 1000,
OrderType.FOK, int(time.time()) + 1000, bool(params.reduce_only)
),
)
return self.place_order(
Expand Down Expand Up @@ -389,8 +388,7 @@ def close_position(
product.book_info.price_increment_x18,
),
expiration=get_expiration_timestamp(
OrderType.FOK,
int(time.time()) + 1000,
OrderType.FOK, int(time.time()) + 1000, reduce_only=True
),
),
)
Expand Down
3 changes: 3 additions & 0 deletions vertex_protocol/engine_client/types/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ class PlaceMarketOrderParams(SignatureParams):
market_order (MarketOrderParams): The parameters of the market order.

spot_leverage (Optional[bool]): An optional flag indicating whether leverage should be used for the order. By default, leverage is assumed.

reduce_only (Optional[bool]): When True, the order can only reduce the size of an existing position. Works only with IOC & FOK.
"""

product_id: int
market_order: MarketOrderParams
slippage: Optional[float]
spot_leverage: Optional[bool]
reduce_only: Optional[bool]


class CancelOrdersParams(BaseParamsSigned):
Expand Down
11 changes: 9 additions & 2 deletions vertex_protocol/utils/expiration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class OrderType(IntEnum):
POST_ONLY = 3


def get_expiration_timestamp(order_type: OrderType, expiration: int) -> int:
def get_expiration_timestamp(
order_type: OrderType, expiration: int, reduce_only: bool = False
) -> int:
"""
Encodes the order type into the expiration timestamp for special order types such as immediate-or-cancel.

Expand All @@ -17,10 +19,15 @@ def get_expiration_timestamp(order_type: OrderType, expiration: int) -> int:

expiration (int): The expiration timestamp in UNIX seconds.

reduce_only (bool): When True, the order can only reduce the size of an existing position. Works only with IOC & FOK.

Returns:
int: The properly formatted timestamp needed for the specified order type.
"""
return int(expiration) | (order_type << 62)
expiration = int(expiration) | (order_type << 62)
if reduce_only:
expiration |= 1 << 61
return expiration


def decode_expiration(expiration: int) -> tuple[OrderType, int]:
Expand Down