From 46e1ffeafc8f1f76e85ce11e97d1e1f9324f97e1 Mon Sep 17 00:00:00 2001 From: yruej301 Date: Fri, 11 Aug 2023 18:08:43 -0400 Subject: [PATCH 1/5] Improve typing --- vertex_protocol/utils/asserts.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/vertex_protocol/utils/asserts.py b/vertex_protocol/utils/asserts.py index 2314199..050190a 100644 --- a/vertex_protocol/utils/asserts.py +++ b/vertex_protocol/utils/asserts.py @@ -1,4 +1,9 @@ -def assert_book_not_empty(bids: list, asks: list, is_bid: bool): +from typing import Tuple + + +def assert_book_not_empty( + bids: list[Tuple[str, str]], asks: list[Tuple[str, str]], is_bid: bool +): book_is_empty = (is_bid and len(bids) == 0) or (not is_bid and len(asks) == 0) if book_is_empty: raise Exception("Orderbook is empty.") From 24d6904a26966682ccb6d1a4cdd23b8332709d1a Mon Sep 17 00:00:00 2001 From: yruej301 Date: Fri, 11 Aug 2023 18:11:53 -0400 Subject: [PATCH 2/5] Make mypy happy --- vertex_protocol/engine_client/execute.py | 2 +- vertex_protocol/utils/asserts.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/vertex_protocol/engine_client/execute.py b/vertex_protocol/engine_client/execute.py index f405e7f..8f64e8b 100644 --- a/vertex_protocol/engine_client/execute.py +++ b/vertex_protocol/engine_client/execute.py @@ -420,7 +420,7 @@ def place_market_order(self, params: PlaceMarketOrderParams) -> ExecuteResponse: """ orderbook = self._querier.get_market_liquidity(params.product_id, 1) is_bid = int(params.market_order.amount) > 0 - assert_book_not_empty(orderbook.bids, orderbook.asks, is_bid) + assert_book_not_empty(orderbook.bids, orderbook.asks, is_bid) # type: ignore slippage = to_x18(params.slippage or 0.005) # defaults to 0.5% market_price_x18 = ( mul_x18(orderbook.bids[0][0], to_x18(1) + slippage) diff --git a/vertex_protocol/utils/asserts.py b/vertex_protocol/utils/asserts.py index 050190a..2c2d94c 100644 --- a/vertex_protocol/utils/asserts.py +++ b/vertex_protocol/utils/asserts.py @@ -2,7 +2,7 @@ def assert_book_not_empty( - bids: list[Tuple[str, str]], asks: list[Tuple[str, str]], is_bid: bool + bids: list[Tuple[str, str]], asks: list[Tuple[str, str]], is_bid: bool # type: ignore ): book_is_empty = (is_bid and len(bids) == 0) or (not is_bid and len(asks) == 0) if book_is_empty: From fe8698713763d64edd77cef9afa7c64493711ff6 Mon Sep 17 00:00:00 2001 From: yruej301 Date: Fri, 11 Aug 2023 18:20:56 -0400 Subject: [PATCH 3/5] Fix liquidity type --- vertex_protocol/engine_client/execute.py | 8 ++------ vertex_protocol/engine_client/types/__init__.py | 1 - vertex_protocol/engine_client/types/models.py | 4 ---- vertex_protocol/engine_client/types/query.py | 2 +- vertex_protocol/utils/__init__.py | 2 ++ vertex_protocol/utils/asserts.py | 6 ++---- vertex_protocol/utils/types.py | 7 +++++++ 7 files changed, 14 insertions(+), 16 deletions(-) create mode 100644 vertex_protocol/utils/types.py diff --git a/vertex_protocol/engine_client/execute.py b/vertex_protocol/engine_client/execute.py index 8f64e8b..6d970cc 100644 --- a/vertex_protocol/engine_client/execute.py +++ b/vertex_protocol/engine_client/execute.py @@ -41,11 +41,7 @@ ) from vertex_protocol.utils.expiration import OrderType, get_expiration_timestamp from vertex_protocol.utils.math import mul_x18, round_x18, to_x18 -from vertex_protocol.utils.model import ( - VertexBaseModel, - is_instance_of_union, - ensure_data_type, -) +from vertex_protocol.utils.model import VertexBaseModel, is_instance_of_union from vertex_protocol.utils.nonce import gen_order_nonce from vertex_protocol.utils.subaccount import Subaccount, SubaccountParams @@ -420,7 +416,7 @@ def place_market_order(self, params: PlaceMarketOrderParams) -> ExecuteResponse: """ orderbook = self._querier.get_market_liquidity(params.product_id, 1) is_bid = int(params.market_order.amount) > 0 - assert_book_not_empty(orderbook.bids, orderbook.asks, is_bid) # type: ignore + assert_book_not_empty(orderbook.bids, orderbook.asks, is_bid) slippage = to_x18(params.slippage or 0.005) # defaults to 0.5% market_price_x18 = ( mul_x18(orderbook.bids[0][0], to_x18(1) + slippage) diff --git a/vertex_protocol/engine_client/types/__init__.py b/vertex_protocol/engine_client/types/__init__.py index 07097c4..e22ed4e 100644 --- a/vertex_protocol/engine_client/types/__init__.py +++ b/vertex_protocol/engine_client/types/__init__.py @@ -191,5 +191,4 @@ def linked_signer_to_local_account( "PerpProductLpState", "PerpProduct", "MaxOrderSizeDirection", - "Liquidity", ] diff --git a/vertex_protocol/engine_client/types/models.py b/vertex_protocol/engine_client/types/models.py index 6e341db..b434b29 100644 --- a/vertex_protocol/engine_client/types/models.py +++ b/vertex_protocol/engine_client/types/models.py @@ -176,7 +176,3 @@ class ProductSymbol(VertexBaseModel): class SubaccountPosition(VertexBaseModel): balance: Union[PerpProductBalance, SpotProductBalance] product: Union[PerpProduct, SpotProduct] - - -# (price, amount) -Liquidity = Annotated[list, conlist(str, min_items=2, max_items=2)] diff --git a/vertex_protocol/engine_client/types/query.py b/vertex_protocol/engine_client/types/query.py index c09fb56..90d1d25 100644 --- a/vertex_protocol/engine_client/types/query.py +++ b/vertex_protocol/engine_client/types/query.py @@ -1,4 +1,5 @@ from vertex_protocol.utils.enum import StrEnum +from vertex_protocol.utils.types import Liquidity from typing import Optional, Union from pydantic import validator from vertex_protocol.utils.model import VertexBaseModel @@ -6,7 +7,6 @@ ApplyDeltaTx, BurnLpTx, EngineStatus, - Liquidity, MaxOrderSizeDirection, MintLpTx, ProductSymbol, diff --git a/vertex_protocol/utils/__init__.py b/vertex_protocol/utils/__init__.py index 51e3fff..bdd62c9 100644 --- a/vertex_protocol/utils/__init__.py +++ b/vertex_protocol/utils/__init__.py @@ -6,6 +6,7 @@ from vertex_protocol.utils.nonce import * from vertex_protocol.utils.exceptions import * from vertex_protocol.utils.asserts import * +from vertex_protocol.utils.types import * __all__ = [ "VertexBackendURL", @@ -34,4 +35,5 @@ "BadStatusCodeException", "MissingSignerException", "InvalidProductId", + "Liquidity", ] diff --git a/vertex_protocol/utils/asserts.py b/vertex_protocol/utils/asserts.py index 2c2d94c..716b7ce 100644 --- a/vertex_protocol/utils/asserts.py +++ b/vertex_protocol/utils/asserts.py @@ -1,9 +1,7 @@ -from typing import Tuple +from vertex_protocol.utils.types import Liquidity -def assert_book_not_empty( - bids: list[Tuple[str, str]], asks: list[Tuple[str, str]], is_bid: bool # type: ignore -): +def assert_book_not_empty(bids: list[Liquidity], asks: list[Liquidity], is_bid: bool): book_is_empty = (is_bid and len(bids) == 0) or (not is_bid and len(asks) == 0) if book_is_empty: raise Exception("Orderbook is empty.") diff --git a/vertex_protocol/utils/types.py b/vertex_protocol/utils/types.py new file mode 100644 index 0000000..90d2648 --- /dev/null +++ b/vertex_protocol/utils/types.py @@ -0,0 +1,7 @@ +# (price, amount) +from typing import Annotated + +from pydantic import conlist + + +Liquidity = Annotated[list, conlist(str, min_items=2, max_items=2)] From e4be0c10c11682a97ae2635eb121e8847518f5b8 Mon Sep 17 00:00:00 2001 From: yruej301 Date: Fri, 11 Aug 2023 18:22:18 -0400 Subject: [PATCH 4/5] fix comment --- vertex_protocol/utils/types.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vertex_protocol/utils/types.py b/vertex_protocol/utils/types.py index 90d2648..5b3de50 100644 --- a/vertex_protocol/utils/types.py +++ b/vertex_protocol/utils/types.py @@ -1,7 +1,6 @@ -# (price, amount) from typing import Annotated from pydantic import conlist - +# (price, amount) Liquidity = Annotated[list, conlist(str, min_items=2, max_items=2)] From e014d04e28ad0836171790770abcd485caaaa1e3 Mon Sep 17 00:00:00 2001 From: yruej301 Date: Fri, 11 Aug 2023 18:24:39 -0400 Subject: [PATCH 5/5] Rename liquidity -> market_liquidity --- vertex_protocol/engine_client/types/query.py | 6 +++--- vertex_protocol/utils/__init__.py | 2 +- vertex_protocol/utils/asserts.py | 6 ++++-- vertex_protocol/utils/types.py | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/vertex_protocol/engine_client/types/query.py b/vertex_protocol/engine_client/types/query.py index 90d1d25..35feb28 100644 --- a/vertex_protocol/engine_client/types/query.py +++ b/vertex_protocol/engine_client/types/query.py @@ -1,5 +1,5 @@ from vertex_protocol.utils.enum import StrEnum -from vertex_protocol.utils.types import Liquidity +from vertex_protocol.utils.types import MarketLiquidity from typing import Optional, Union from pydantic import validator from vertex_protocol.utils.model import VertexBaseModel @@ -304,8 +304,8 @@ class MarketLiquidityData(VertexBaseModel): Data model for market liquidity details. """ - bids: list[Liquidity] - asks: list[Liquidity] + bids: list[MarketLiquidity] + asks: list[MarketLiquidity] timestamp: str diff --git a/vertex_protocol/utils/__init__.py b/vertex_protocol/utils/__init__.py index bdd62c9..9c25672 100644 --- a/vertex_protocol/utils/__init__.py +++ b/vertex_protocol/utils/__init__.py @@ -35,5 +35,5 @@ "BadStatusCodeException", "MissingSignerException", "InvalidProductId", - "Liquidity", + "MarketLiquidity", ] diff --git a/vertex_protocol/utils/asserts.py b/vertex_protocol/utils/asserts.py index 716b7ce..0a1d39b 100644 --- a/vertex_protocol/utils/asserts.py +++ b/vertex_protocol/utils/asserts.py @@ -1,7 +1,9 @@ -from vertex_protocol.utils.types import Liquidity +from vertex_protocol.utils.types import MarketLiquidity -def assert_book_not_empty(bids: list[Liquidity], asks: list[Liquidity], is_bid: bool): +def assert_book_not_empty( + bids: list[MarketLiquidity], asks: list[MarketLiquidity], is_bid: bool +): book_is_empty = (is_bid and len(bids) == 0) or (not is_bid and len(asks) == 0) if book_is_empty: raise Exception("Orderbook is empty.") diff --git a/vertex_protocol/utils/types.py b/vertex_protocol/utils/types.py index 5b3de50..86c924d 100644 --- a/vertex_protocol/utils/types.py +++ b/vertex_protocol/utils/types.py @@ -3,4 +3,4 @@ from pydantic import conlist # (price, amount) -Liquidity = Annotated[list, conlist(str, min_items=2, max_items=2)] +MarketLiquidity = Annotated[list, conlist(str, min_items=2, max_items=2)]