Skip to content

Commit

Permalink
Merge pull request #53 from Polymarket/feat/balance-allowance-endpoint
Browse files Browse the repository at this point in the history
Feat/ adding fetch balance & allowance endpoint
  • Loading branch information
poly-rodr authored Feb 24, 2023
2 parents 8abcae5 + 744be77 commit 0b61a69
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 4 deletions.
42 changes: 42 additions & 0 deletions examples/get_balance_allowance.py
Original file line number Diff line number Diff line change
@@ -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()
16 changes: 16 additions & 0 deletions py_clob_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
TRADES,
GET_TRADE_NOTIFICATIONS,
DROP_TRADE_NOTIFICATIONS,
GET_BALANCE_ALLOWANCE,
)
from .clob_types import (
ApiCreds,
Expand All @@ -34,6 +35,7 @@
RequestArgs,
TradeNotificationParams,
OrderBookSummary,
BalanceAllowanceParams,
)
from .exceptions import PolyException
from .http_helpers.helpers import (
Expand All @@ -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
Expand Down Expand Up @@ -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)
11 changes: 11 additions & 0 deletions py_clob_client/clob_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions py_clob_client/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
23 changes: 21 additions & 2 deletions py_clob_client/http_helpers/helpers.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="py_clob_client",
version="0.2.3",
version="0.2.4",
author="Polymarket Engineering",
author_email="[email protected]",
maintainer="Polymarket Engineering",
Expand Down
22 changes: 21 additions & 1 deletion tests/http_helpers/test_helpers.py
Original file line number Diff line number Diff line change
@@ -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,
)


Expand Down Expand Up @@ -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")

0 comments on commit 0b61a69

Please sign in to comment.