From 744be77e0e7b91e57878f210eafd27b0de8fc51d Mon Sep 17 00:00:00 2001 From: Rodrigo <95635797+poly-rodr@users.noreply.github.com> Date: Wed, 22 Feb 2023 16:52:04 -0300 Subject: [PATCH] Feat/ adding fetch balance & allowance endpoint ### Usage ```py collateral = client.get_balance_allowance( params=BalanceAllowanceParams(asset_type=AssetType.COLLATERAL) ) print(collateral) yes = client.get_balance_allowance( params=BalanceAllowanceParams( asset_type=AssetType.CONDITIONAL, token_id="1343197538147866997676250008839231694243646439454152539053893078719042421992", ) ) print(yes) no = client.get_balance_allowance( params=BalanceAllowanceParams(asset_type=AssetType.CONDITIONAL), token_id="16678291189211314787145083999015737376658799626183230671758641503291735614088", ) print(no) ``` --- examples/get_balance_allowance.py | 42 ++++++++++++++++++++++++++ py_clob_client/client.py | 16 ++++++++++ py_clob_client/clob_types.py | 11 +++++++ py_clob_client/endpoints.py | 1 + py_clob_client/http_helpers/helpers.py | 23 ++++++++++++-- setup.py | 2 +- tests/http_helpers/test_helpers.py | 22 +++++++++++++- 7 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 examples/get_balance_allowance.py diff --git a/examples/get_balance_allowance.py b/examples/get_balance_allowance.py new file mode 100644 index 0000000..6afff3e --- /dev/null +++ b/examples/get_balance_allowance.py @@ -0,0 +1,42 @@ +import os + +from py_clob_client.client import ClobClient +from py_clob_client.clob_types import ApiCreds, BalanceAllowanceParams, AssetType +from dotenv import load_dotenv +from py_clob_client.constants import MUMBAI + +load_dotenv() + + +def main(): + host = "http://localhost:8080" + key = os.getenv("PK") + creds = ApiCreds( + api_key=os.getenv("CLOB_API_KEY"), + api_secret=os.getenv("CLOB_SECRET"), + api_passphrase=os.getenv("CLOB_PASS_PHRASE"), + ) + chain_id = MUMBAI + client = ClobClient(host, key=key, chain_id=chain_id, creds=creds) + + collateral = client.get_balance_allowance( + params=BalanceAllowanceParams(asset_type=AssetType.COLLATERAL) + ) + print(collateral) + + yes = client.get_balance_allowance( + params=BalanceAllowanceParams( + asset_type=AssetType.CONDITIONAL, + token_id="1343197538147866997676250008839231694243646439454152539053893078719042421992", + ) + ) + print(yes) + + no = client.get_balance_allowance( + params=BalanceAllowanceParams(asset_type=AssetType.CONDITIONAL), + token_id="16678291189211314787145083999015737376658799626183230671758641503291735614088", + ) + print(no) + + +main() diff --git a/py_clob_client/client.py b/py_clob_client/client.py index e556a4e..3e62228 100644 --- a/py_clob_client/client.py +++ b/py_clob_client/client.py @@ -26,6 +26,7 @@ TRADES, GET_TRADE_NOTIFICATIONS, DROP_TRADE_NOTIFICATIONS, + GET_BALANCE_ALLOWANCE, ) from .clob_types import ( ApiCreds, @@ -34,6 +35,7 @@ RequestArgs, TradeNotificationParams, OrderBookSummary, + BalanceAllowanceParams, ) from .exceptions import PolyException from .http_helpers.helpers import ( @@ -42,6 +44,7 @@ get, post, add_trade_notifications_query_params, + add_balance_allowance_params_to_url, ) from py_order_utils.config import get_contract_config from .constants import L0, L1, L1_AUTH_UNAVAILABLE, L2, L2_AUTH_UNAVAILABLE @@ -398,3 +401,16 @@ def drop_trade_notifications(self, params: TradeNotificationParams = None): "{}{}".format(self.host, DROP_TRADE_NOTIFICATIONS), params ) return delete(url, headers=headers) + + def get_balance_allowance(self, params: BalanceAllowanceParams = None): + """ + Fetches the balance & allowance for a user + Requires Level 2 authentication + """ + self.assert_level_2_auth() + request_args = RequestArgs(method="GET", request_path=GET_BALANCE_ALLOWANCE) + headers = create_level_2_headers(self.signer, self.creds, request_args) + url = add_balance_allowance_params_to_url( + "{}{}".format(self.host, GET_BALANCE_ALLOWANCE), params + ) + return get(url, headers=headers) diff --git a/py_clob_client/clob_types.py b/py_clob_client/clob_types.py index 42edf4f..5b61931 100644 --- a/py_clob_client/clob_types.py +++ b/py_clob_client/clob_types.py @@ -109,3 +109,14 @@ def __dict__(self): @property def json(self): return dumps(self.__dict__, separators=(",", ":")) + + +class AssetType(enumerate): + COLLATERAL = "COLLATERAL" + CONDITIONAL = "CONDITIONAL" + + +@dataclass +class BalanceAllowanceParams: + asset_type: AssetType = None + token_id: str = None diff --git a/py_clob_client/endpoints.py b/py_clob_client/endpoints.py index 0000ef9..5fb6d04 100644 --- a/py_clob_client/endpoints.py +++ b/py_clob_client/endpoints.py @@ -17,3 +17,4 @@ GET_LAST_TRADE_PRICE = "/last-trade-price" GET_TRADE_NOTIFICATIONS = "/trade-notifications" DROP_TRADE_NOTIFICATIONS = "/drop-trade-notifications" +GET_BALANCE_ALLOWANCE = "/balance-allowance" diff --git a/py_clob_client/http_helpers/helpers.py b/py_clob_client/http_helpers/helpers.py index db922d5..55909ec 100644 --- a/py_clob_client/http_helpers/helpers.py +++ b/py_clob_client/http_helpers/helpers.py @@ -1,7 +1,10 @@ import requests -import pkg_resources -from py_clob_client.clob_types import FilterParams, TradeNotificationParams +from py_clob_client.clob_types import ( + FilterParams, + TradeNotificationParams, + BalanceAllowanceParams, +) from ..exceptions import PolyApiException @@ -101,3 +104,19 @@ def add_trade_notifications_query_params( if params.index: url = build_query_params(url, "index", params.index) return url + + +def add_balance_allowance_params_to_url( + base_url: str, params: BalanceAllowanceParams = None +) -> str: + """ + Adds query parameters to a url + """ + url = base_url + if params: + url = url + "?" + if params.asset_type: + url = build_query_params(url, "asset_type", params.asset_type.__str__()) + if params.token_id: + url = build_query_params(url, "token_id", params.token_id) + return url diff --git a/setup.py b/setup.py index 39ca3da..b3bb4e0 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="py_clob_client", - version="0.2.3", + version="0.2.4", author="Polymarket Engineering", author_email="engineering@polymarket.com", maintainer="Polymarket Engineering", diff --git a/tests/http_helpers/test_helpers.py b/tests/http_helpers/test_helpers.py index c7fe224..1b6a8b5 100644 --- a/tests/http_helpers/test_helpers.py +++ b/tests/http_helpers/test_helpers.py @@ -1,10 +1,16 @@ from unittest import TestCase -from py_clob_client.clob_types import FilterParams, TradeNotificationParams +from py_clob_client.clob_types import ( + FilterParams, + TradeNotificationParams, + BalanceAllowanceParams, + AssetType, +) from py_clob_client.http_helpers.helpers import ( build_query_params, add_query_params, add_trade_notifications_query_params, + add_balance_allowance_params_to_url, ) @@ -38,3 +44,17 @@ def test_add_trade_notifications_query_params(self): self.assertIsNotNone(url) self.assertEqual(url, "http://tracker?index=12345") + def test_add_balance_allowance_params_to_url(self): + url = add_balance_allowance_params_to_url( + "http://tracker", + BalanceAllowanceParams(asset_type=AssetType.COLLATERAL), + ) + self.assertIsNotNone(url) + self.assertEqual(url, "http://tracker?asset_type=COLLATERAL") + + url = add_balance_allowance_params_to_url( + "http://tracker", + BalanceAllowanceParams(asset_type=AssetType.CONDITIONAL, token_id="222"), + ) + self.assertIsNotNone(url) + self.assertEqual(url, "http://tracker?asset_type=CONDITIONAL&token_id=222")