polymarket-apis 0.2.6__py3-none-any.whl → 0.3.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of polymarket-apis might be problematic. Click here for more details.
- polymarket_apis/__init__.py +41 -2
- polymarket_apis/clients/__init__.py +23 -0
- polymarket_apis/clients/clob_client.py +184 -88
- polymarket_apis/clients/data_client.py +90 -65
- polymarket_apis/clients/gamma_client.py +96 -75
- polymarket_apis/clients/graphql_client.py +60 -0
- polymarket_apis/clients/web3_client.py +132 -60
- polymarket_apis/clients/websockets_client.py +25 -9
- polymarket_apis/types/__init__.py +195 -0
- polymarket_apis/types/clob_types.py +30 -10
- polymarket_apis/types/common.py +5 -3
- polymarket_apis/types/data_types.py +6 -3
- polymarket_apis/types/gamma_types.py +27 -8
- polymarket_apis/types/websockets_types.py +109 -30
- polymarket_apis/utilities/config.py +1 -0
- polymarket_apis/utilities/exceptions.py +5 -0
- polymarket_apis/utilities/order_builder/builder.py +32 -14
- polymarket_apis/utilities/order_builder/helpers.py +13 -12
- polymarket_apis/utilities/signing/hmac.py +5 -1
- polymarket_apis/utilities/web3/abis/custom_contract_errors.py +1 -1
- polymarket_apis/utilities/web3/helpers.py +1 -0
- polymarket_apis-0.3.1.dist-info/METADATA +164 -0
- polymarket_apis-0.3.1.dist-info/RECORD +41 -0
- polymarket_apis-0.2.6.dist-info/METADATA +0 -18
- polymarket_apis-0.2.6.dist-info/RECORD +0 -40
- {polymarket_apis-0.2.6.dist-info → polymarket_apis-0.3.1.dist-info}/WHEEL +0 -0
|
@@ -14,16 +14,24 @@ from ..utilities.web3.helpers import get_index_set
|
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
def _load_abi(contract_name: str) -> list:
|
|
17
|
-
abi_path =
|
|
17
|
+
abi_path = (
|
|
18
|
+
Path(__file__).parent.parent
|
|
19
|
+
/ "utilities"
|
|
20
|
+
/ "web3"
|
|
21
|
+
/ "abis"
|
|
22
|
+
/ f"{contract_name}.json"
|
|
23
|
+
)
|
|
18
24
|
with Path.open(abi_path) as f:
|
|
19
25
|
return load(f)
|
|
20
26
|
|
|
21
|
-
class PolymarketWeb3Client:
|
|
22
|
-
def __init__(self, private_key: str , chain_id: Literal[137, 80002] = POLYGON):
|
|
23
27
|
|
|
28
|
+
class PolymarketWeb3Client:
|
|
29
|
+
def __init__(self, private_key: str, chain_id: Literal[137, 80002] = POLYGON):
|
|
24
30
|
self.w3 = Web3(Web3.HTTPProvider("https://polygon-rpc.com"))
|
|
25
31
|
self.w3.middleware_onion.inject(ExtraDataToPOAMiddleware, layer=0)
|
|
26
|
-
self.w3.middleware_onion.inject(
|
|
32
|
+
self.w3.middleware_onion.inject(
|
|
33
|
+
SignAndSendRawMiddlewareBuilder.build(private_key), layer=0
|
|
34
|
+
)
|
|
27
35
|
|
|
28
36
|
self.account = self.w3.eth.account.from_key(private_key)
|
|
29
37
|
|
|
@@ -34,25 +42,37 @@ class PolymarketWeb3Client:
|
|
|
34
42
|
self.usdc_abi = _load_abi("UChildERC20Proxy")
|
|
35
43
|
self.usdc = self.contract(self.usdc_address, self.usdc_abi)
|
|
36
44
|
|
|
37
|
-
self.conditional_tokens_address = Web3.to_checksum_address(
|
|
45
|
+
self.conditional_tokens_address = Web3.to_checksum_address(
|
|
46
|
+
self.config.conditional_tokens
|
|
47
|
+
)
|
|
38
48
|
self.conditional_tokens_abi = _load_abi("ConditionalTokens")
|
|
39
|
-
self.conditional_tokens = self.contract(
|
|
49
|
+
self.conditional_tokens = self.contract(
|
|
50
|
+
self.conditional_tokens_address, self.conditional_tokens_abi
|
|
51
|
+
)
|
|
40
52
|
|
|
41
53
|
self.exchange_address = Web3.to_checksum_address(self.config.exchange)
|
|
42
54
|
self.exchange_abi = _load_abi("CTFExchange")
|
|
43
55
|
self.exchange = self.contract(self.exchange_address, self.exchange_abi)
|
|
44
56
|
|
|
45
|
-
self.neg_risk_exchange_address = Web3.to_checksum_address(
|
|
57
|
+
self.neg_risk_exchange_address = Web3.to_checksum_address(
|
|
58
|
+
self.neg_risk_config.exchange
|
|
59
|
+
)
|
|
46
60
|
self.neg_risk_exchange_abi = _load_abi("NegRiskCtfExchange")
|
|
47
|
-
self.neg_risk_exchange = self.contract(
|
|
61
|
+
self.neg_risk_exchange = self.contract(
|
|
62
|
+
self.neg_risk_exchange_address, self.neg_risk_exchange_abi
|
|
63
|
+
)
|
|
48
64
|
|
|
49
65
|
self.neg_risk_adapter_address = "0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296"
|
|
50
66
|
self.neg_risk_adapter_abi = _load_abi("NegRiskAdapter")
|
|
51
|
-
self.neg_risk_adapter = self.contract(
|
|
67
|
+
self.neg_risk_adapter = self.contract(
|
|
68
|
+
self.neg_risk_adapter_address, self.neg_risk_adapter_abi
|
|
69
|
+
)
|
|
52
70
|
|
|
53
71
|
self.proxy_factory_address = "0xaB45c5A4B0c941a2F231C04C3f49182e1A254052"
|
|
54
72
|
self.proxy_factory_abi = _load_abi("ProxyWalletFactory")
|
|
55
|
-
self.proxy_factory = self.contract(
|
|
73
|
+
self.proxy_factory = self.contract(
|
|
74
|
+
self.proxy_factory_address, self.proxy_factory_abi
|
|
75
|
+
)
|
|
56
76
|
|
|
57
77
|
def _encode_split(self, condition_id: Keccak256, amount: int) -> str:
|
|
58
78
|
return self.conditional_tokens.encode_abi(
|
|
@@ -72,12 +92,17 @@ class PolymarketWeb3Client:
|
|
|
72
92
|
args=[self.usdc_address, HASH_ZERO, condition_id, [1, 2]],
|
|
73
93
|
)
|
|
74
94
|
|
|
75
|
-
def _encode_redeem_neg_risk(
|
|
95
|
+
def _encode_redeem_neg_risk(
|
|
96
|
+
self, condition_id: Keccak256, amounts: list[int]
|
|
97
|
+
) -> str:
|
|
76
98
|
return self.neg_risk_adapter.encode_abi(
|
|
77
99
|
abi_element_identifier="redeemPositions",
|
|
78
100
|
args=[condition_id, amounts],
|
|
79
101
|
)
|
|
80
|
-
|
|
102
|
+
|
|
103
|
+
def _encode_convert(
|
|
104
|
+
self, neg_risk_market_id: Keccak256, index_set: int, amount: int
|
|
105
|
+
) -> str:
|
|
81
106
|
return self.neg_risk_adapter.encode_abi(
|
|
82
107
|
abi_element_identifier="convertPositions",
|
|
83
108
|
args=[neg_risk_market_id, index_set, amount],
|
|
@@ -98,25 +123,37 @@ class PolymarketWeb3Client:
|
|
|
98
123
|
Explicitly passing the proxy address is faster due to only one contract function call.
|
|
99
124
|
"""
|
|
100
125
|
if address is None:
|
|
101
|
-
address = self.exchange.functions.getPolyProxyWalletAddress(
|
|
126
|
+
address = self.exchange.functions.getPolyProxyWalletAddress(
|
|
127
|
+
self.account.address
|
|
128
|
+
).call()
|
|
102
129
|
balance_res = self.usdc.functions.balanceOf(address).call()
|
|
103
130
|
return float(balance_res / 1e6)
|
|
104
131
|
|
|
105
|
-
def get_token_balance(
|
|
132
|
+
def get_token_balance(
|
|
133
|
+
self, token_id: str, address: EthAddress | None = None
|
|
134
|
+
) -> float:
|
|
106
135
|
"""Get the token balance of the given address."""
|
|
107
136
|
if address is None:
|
|
108
|
-
address = self.exchange.functions.getPolyProxyWalletAddress(
|
|
109
|
-
|
|
137
|
+
address = self.exchange.functions.getPolyProxyWalletAddress(
|
|
138
|
+
self.account.address
|
|
139
|
+
).call()
|
|
140
|
+
balance_res = self.conditional_tokens.functions.balanceOf(
|
|
141
|
+
address, int(token_id)
|
|
142
|
+
).call()
|
|
110
143
|
return float(balance_res / 1e6)
|
|
111
144
|
|
|
112
145
|
def get_token_complement(self, token_id: str) -> Optional[str]:
|
|
113
146
|
"""Get the complement of the given token."""
|
|
114
147
|
try:
|
|
115
|
-
return str(
|
|
148
|
+
return str(
|
|
149
|
+
self.neg_risk_exchange.functions.getComplement(int(token_id)).call()
|
|
150
|
+
)
|
|
116
151
|
except ContractCustomError as e:
|
|
117
152
|
if e.args[0] in CUSTOM_ERROR_DICT:
|
|
118
153
|
try:
|
|
119
|
-
return str(
|
|
154
|
+
return str(
|
|
155
|
+
self.exchange.functions.getComplement(int(token_id)).call()
|
|
156
|
+
)
|
|
120
157
|
except ContractCustomError as e2:
|
|
121
158
|
if e2.args[0] in CUSTOM_ERROR_DICT:
|
|
122
159
|
msg = f"{CUSTOM_ERROR_DICT[e2.args[0]]}"
|
|
@@ -132,70 +169,90 @@ class PolymarketWeb3Client:
|
|
|
132
169
|
outcomeSlotCount is represented by the last two digits of question id). Returns a keccak256 hash of
|
|
133
170
|
the oracle and question id.
|
|
134
171
|
"""
|
|
135
|
-
return
|
|
172
|
+
return (
|
|
173
|
+
"0x"
|
|
174
|
+
+ self.neg_risk_adapter.functions.getConditionId(question_id).call().hex()
|
|
175
|
+
)
|
|
136
176
|
|
|
137
|
-
def split_position(
|
|
177
|
+
def split_position(
|
|
178
|
+
self, condition_id: Keccak256, amount: int, neg_risk: bool = True
|
|
179
|
+
):
|
|
138
180
|
"""Splits usdc into two complementary positions of equal size."""
|
|
139
181
|
nonce = self.w3.eth.get_transaction_count(self.account.address)
|
|
140
182
|
amount = int(amount * 1e6)
|
|
141
183
|
|
|
142
184
|
proxy_txn = {
|
|
143
185
|
"typeCode": 1,
|
|
144
|
-
"to": self.neg_risk_adapter_address
|
|
186
|
+
"to": self.neg_risk_adapter_address
|
|
187
|
+
if neg_risk
|
|
188
|
+
else self.conditional_tokens_address,
|
|
145
189
|
"value": 0,
|
|
146
190
|
"data": self._encode_split(condition_id, amount),
|
|
147
191
|
}
|
|
148
192
|
|
|
149
193
|
# Send transaction through proxy factory
|
|
150
|
-
txn_data = self.proxy_factory.functions.proxy([proxy_txn]).build_transaction(
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
194
|
+
txn_data = self.proxy_factory.functions.proxy([proxy_txn]).build_transaction(
|
|
195
|
+
{
|
|
196
|
+
"nonce": nonce,
|
|
197
|
+
"gasPrice": int(1.05 * self.w3.eth.gas_price),
|
|
198
|
+
"gas": 1000000,
|
|
199
|
+
"from": self.account.address,
|
|
200
|
+
}
|
|
201
|
+
)
|
|
156
202
|
|
|
157
203
|
# Sign and send transaction
|
|
158
204
|
signed_txn = self.account.sign_transaction(txn_data)
|
|
159
|
-
tx_hash = self.w3.eth.send_raw_transaction(signed_txn.raw_transaction)
|
|
205
|
+
tx_hash = self.w3.eth.send_raw_transaction(signed_txn.raw_transaction)
|
|
206
|
+
tx_hash_hex = tx_hash.hex()
|
|
160
207
|
|
|
161
|
-
print(f"Txn hash: {
|
|
208
|
+
print(f"Txn hash: {tx_hash_hex}")
|
|
162
209
|
|
|
163
210
|
# Wait for transaction to be mined
|
|
164
211
|
self.w3.eth.wait_for_transaction_receipt(tx_hash)
|
|
165
212
|
|
|
166
213
|
print("Done!")
|
|
167
214
|
|
|
168
|
-
def merge_position(
|
|
215
|
+
def merge_position(
|
|
216
|
+
self, condition_id: Keccak256, amount: int, neg_risk: bool = True
|
|
217
|
+
):
|
|
169
218
|
"""Merges two complementary positions into usdc."""
|
|
170
219
|
nonce = self.w3.eth.get_transaction_count(self.account.address)
|
|
171
220
|
amount = int(amount * 1e6)
|
|
172
221
|
|
|
173
222
|
proxy_txn = {
|
|
174
223
|
"typeCode": 1,
|
|
175
|
-
"to": self.neg_risk_adapter_address
|
|
224
|
+
"to": self.neg_risk_adapter_address
|
|
225
|
+
if neg_risk
|
|
226
|
+
else self.conditional_tokens_address,
|
|
176
227
|
"value": 0,
|
|
177
228
|
"data": self._encode_merge(condition_id, amount),
|
|
178
229
|
}
|
|
179
230
|
|
|
180
231
|
# Send transaction through proxy factory
|
|
181
|
-
txn_data = self.proxy_factory.functions.proxy([proxy_txn]).build_transaction(
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
232
|
+
txn_data = self.proxy_factory.functions.proxy([proxy_txn]).build_transaction(
|
|
233
|
+
{
|
|
234
|
+
"nonce": nonce,
|
|
235
|
+
"gasPrice": int(1.05 * self.w3.eth.gas_price),
|
|
236
|
+
"gas": 1000000,
|
|
237
|
+
"from": self.account.address,
|
|
238
|
+
}
|
|
239
|
+
)
|
|
187
240
|
|
|
188
241
|
# Sign and send transaction
|
|
189
242
|
signed_txn = self.account.sign_transaction(txn_data)
|
|
190
|
-
tx_hash = self.w3.eth.send_raw_transaction(signed_txn.raw_transaction)
|
|
243
|
+
tx_hash = self.w3.eth.send_raw_transaction(signed_txn.raw_transaction)
|
|
244
|
+
tx_hash_hex = tx_hash.hex()
|
|
191
245
|
|
|
192
|
-
print(f"Txn hash: {
|
|
246
|
+
print(f"Txn hash: {tx_hash_hex}")
|
|
193
247
|
|
|
194
248
|
# Wait for transaction to be mined
|
|
195
249
|
self.w3.eth.wait_for_transaction_receipt(tx_hash)
|
|
196
250
|
|
|
197
251
|
print("Done!")
|
|
198
|
-
|
|
252
|
+
|
|
253
|
+
def redeem_position(
|
|
254
|
+
self, condition_id: Keccak256, amounts: list[float], neg_risk: bool = True
|
|
255
|
+
):
|
|
199
256
|
"""
|
|
200
257
|
Redeem a position into usdc.
|
|
201
258
|
|
|
@@ -208,31 +265,40 @@ class PolymarketWeb3Client:
|
|
|
208
265
|
|
|
209
266
|
proxy_txn = {
|
|
210
267
|
"typeCode": 1,
|
|
211
|
-
"to": self.neg_risk_adapter_address
|
|
268
|
+
"to": self.neg_risk_adapter_address
|
|
269
|
+
if neg_risk
|
|
270
|
+
else self.conditional_tokens_address,
|
|
212
271
|
"value": 0,
|
|
213
|
-
"data": self._encode_redeem_neg_risk(condition_id, amounts)
|
|
272
|
+
"data": self._encode_redeem_neg_risk(condition_id, amounts)
|
|
273
|
+
if neg_risk
|
|
274
|
+
else self._encode_redeem(condition_id),
|
|
214
275
|
}
|
|
215
276
|
|
|
216
277
|
# Send transaction through proxy factory
|
|
217
|
-
txn_data = self.proxy_factory.functions.proxy([proxy_txn]).build_transaction(
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
278
|
+
txn_data = self.proxy_factory.functions.proxy([proxy_txn]).build_transaction(
|
|
279
|
+
{
|
|
280
|
+
"nonce": nonce,
|
|
281
|
+
"gasPrice": int(1.05 * self.w3.eth.gas_price),
|
|
282
|
+
"gas": 1000000,
|
|
283
|
+
"from": self.account.address,
|
|
284
|
+
}
|
|
285
|
+
)
|
|
223
286
|
|
|
224
287
|
# Sign and send transaction
|
|
225
288
|
signed_txn = self.account.sign_transaction(txn_data)
|
|
226
|
-
tx_hash = self.w3.eth.send_raw_transaction(signed_txn.raw_transaction)
|
|
289
|
+
tx_hash = self.w3.eth.send_raw_transaction(signed_txn.raw_transaction)
|
|
290
|
+
tx_hash_hex = tx_hash.hex()
|
|
227
291
|
|
|
228
|
-
print(f"Txn hash: {
|
|
292
|
+
print(f"Txn hash: {tx_hash_hex}")
|
|
229
293
|
|
|
230
294
|
# Wait for transaction to be mined
|
|
231
295
|
self.w3.eth.wait_for_transaction_receipt(tx_hash)
|
|
232
296
|
|
|
233
297
|
print("Done!")
|
|
234
298
|
|
|
235
|
-
def convert_positions(
|
|
299
|
+
def convert_positions(
|
|
300
|
+
self, question_ids: list[Keccak256], neg_risk_market_id: Keccak256, amount: int
|
|
301
|
+
):
|
|
236
302
|
nonce = self.w3.eth.get_transaction_count(self.account.address)
|
|
237
303
|
amount = int(amount * 1e6)
|
|
238
304
|
|
|
@@ -240,20 +306,26 @@ class PolymarketWeb3Client:
|
|
|
240
306
|
"typeCode": 1,
|
|
241
307
|
"to": self.neg_risk_adapter_address,
|
|
242
308
|
"value": 0,
|
|
243
|
-
"data": self._encode_convert(
|
|
309
|
+
"data": self._encode_convert(
|
|
310
|
+
neg_risk_market_id, get_index_set(question_ids), amount
|
|
311
|
+
),
|
|
244
312
|
}
|
|
245
313
|
|
|
246
|
-
txn_data = self.proxy_factory.functions.proxy([proxy_txn]).build_transaction(
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
314
|
+
txn_data = self.proxy_factory.functions.proxy([proxy_txn]).build_transaction(
|
|
315
|
+
{
|
|
316
|
+
"nonce": nonce,
|
|
317
|
+
"gasPrice": int(1.05 * self.w3.eth.gas_price),
|
|
318
|
+
"gas": 1000000,
|
|
319
|
+
"from": self.account.address,
|
|
320
|
+
}
|
|
321
|
+
)
|
|
252
322
|
|
|
323
|
+
# Sign and send transaction
|
|
253
324
|
signed_txn = self.account.sign_transaction(txn_data)
|
|
254
|
-
tx_hash = self.w3.eth.send_raw_transaction(signed_txn.raw_transaction)
|
|
325
|
+
tx_hash = self.w3.eth.send_raw_transaction(signed_txn.raw_transaction)
|
|
326
|
+
tx_hash_hex = tx_hash.hex()
|
|
255
327
|
|
|
256
|
-
print(f"Txn hash: {
|
|
328
|
+
print(f"Txn hash: {tx_hash_hex}")
|
|
257
329
|
|
|
258
330
|
# Wait for transaction to be mined
|
|
259
331
|
self.w3.eth.wait_for_transaction_receipt(tx_hash)
|
|
@@ -6,8 +6,6 @@ from lomond import WebSocket
|
|
|
6
6
|
from lomond.persist import persist
|
|
7
7
|
from pydantic import ValidationError
|
|
8
8
|
|
|
9
|
-
from polymarket_apis.utilities.exceptions import AuthenticationRequiredError
|
|
10
|
-
|
|
11
9
|
from ..types.clob_types import ApiCreds
|
|
12
10
|
from ..types.websockets_types import (
|
|
13
11
|
ActivityOrderMatchEvent,
|
|
@@ -32,6 +30,7 @@ from ..types.websockets_types import (
|
|
|
32
30
|
TickSizeChangeEvent,
|
|
33
31
|
TradeEvent,
|
|
34
32
|
)
|
|
33
|
+
from ..utilities.exceptions import AuthenticationRequiredError
|
|
35
34
|
|
|
36
35
|
|
|
37
36
|
def _process_market_event(event):
|
|
@@ -62,6 +61,7 @@ def _process_market_event(event):
|
|
|
62
61
|
print(e.errors())
|
|
63
62
|
print(event.json)
|
|
64
63
|
|
|
64
|
+
|
|
65
65
|
def _process_user_event(event):
|
|
66
66
|
try:
|
|
67
67
|
message = event.json
|
|
@@ -76,6 +76,7 @@ def _process_user_event(event):
|
|
|
76
76
|
print(event.text)
|
|
77
77
|
print(e.errors(), "\n")
|
|
78
78
|
|
|
79
|
+
|
|
79
80
|
def _process_live_data_event(event):
|
|
80
81
|
try:
|
|
81
82
|
message = event.json
|
|
@@ -88,7 +89,12 @@ def _process_live_data_event(event):
|
|
|
88
89
|
print(CommentEvent(**message), "\n")
|
|
89
90
|
case "reaction_created" | "reaction_removed":
|
|
90
91
|
print(ReactionEvent(**message), "\n")
|
|
91
|
-
case
|
|
92
|
+
case (
|
|
93
|
+
"request_created"
|
|
94
|
+
| "request_edited"
|
|
95
|
+
| "request_canceled"
|
|
96
|
+
| "request_expired"
|
|
97
|
+
):
|
|
92
98
|
print(RequestEvent(**message), "\n")
|
|
93
99
|
case "quote_created" | "quote_edited" | "quote_canceled" | "quote_expired":
|
|
94
100
|
print(QuoteEvent(**message), "\n")
|
|
@@ -118,13 +124,16 @@ def _process_live_data_event(event):
|
|
|
118
124
|
print(e.errors(), "\n")
|
|
119
125
|
print(event.text)
|
|
120
126
|
|
|
127
|
+
|
|
121
128
|
class PolymarketWebsocketsClient:
|
|
122
129
|
def __init__(self):
|
|
123
130
|
self.url_market = "wss://ws-subscriptions-clob.polymarket.com/ws/market"
|
|
124
131
|
self.url_user = "wss://ws-subscriptions-clob.polymarket.com/ws/user"
|
|
125
132
|
self.url_live_data = "wss://ws-live-data.polymarket.com"
|
|
126
133
|
|
|
127
|
-
def market_socket(
|
|
134
|
+
def market_socket(
|
|
135
|
+
self, token_ids: list[str], process_event: Callable = _process_market_event
|
|
136
|
+
):
|
|
128
137
|
"""
|
|
129
138
|
Connect to the market websocket and subscribe to market events for specific token IDs.
|
|
130
139
|
|
|
@@ -143,7 +152,9 @@ class PolymarketWebsocketsClient:
|
|
|
143
152
|
elif event.name == "text":
|
|
144
153
|
process_event(event)
|
|
145
154
|
|
|
146
|
-
def user_socket(
|
|
155
|
+
def user_socket(
|
|
156
|
+
self, creds: ApiCreds, process_event: Callable = _process_user_event
|
|
157
|
+
):
|
|
147
158
|
"""
|
|
148
159
|
Connect to the user websocket and subscribe to user events.
|
|
149
160
|
|
|
@@ -162,7 +173,12 @@ class PolymarketWebsocketsClient:
|
|
|
162
173
|
elif event.name == "text":
|
|
163
174
|
process_event(event)
|
|
164
175
|
|
|
165
|
-
def live_data_socket(
|
|
176
|
+
def live_data_socket(
|
|
177
|
+
self,
|
|
178
|
+
subscriptions: list[dict[str, Any]],
|
|
179
|
+
process_event: Callable = _process_live_data_event,
|
|
180
|
+
creds: Optional[ApiCreds] = None,
|
|
181
|
+
):
|
|
166
182
|
# info on how to subscribe found at https://github.com/Polymarket/real-time-data-client?tab=readme-ov-file#subscribe
|
|
167
183
|
"""
|
|
168
184
|
Connect to the live data websocket and subscribe to specified events.
|
|
@@ -176,13 +192,13 @@ class PolymarketWebsocketsClient:
|
|
|
176
192
|
websocket = WebSocket(self.url_live_data)
|
|
177
193
|
|
|
178
194
|
needs_auth = any(sub.get("topic") == "clob_user" for sub in subscriptions)
|
|
179
|
-
if needs_auth and creds is None:
|
|
180
|
-
msg = "ApiCreds credentials are required for the clob_user topic subscriptions"
|
|
181
|
-
raise AuthenticationRequiredError(msg)
|
|
182
195
|
|
|
183
196
|
for event in persist(websocket):
|
|
184
197
|
if event.name == "ready":
|
|
185
198
|
if needs_auth:
|
|
199
|
+
if creds is None:
|
|
200
|
+
msg = "ApiCreds credentials are required for the clob_user topic subscriptions"
|
|
201
|
+
raise AuthenticationRequiredError(msg)
|
|
186
202
|
subscriptions_with_creds = []
|
|
187
203
|
for sub in subscriptions:
|
|
188
204
|
if sub.get("topic") == "clob_user":
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Type definitions for Polymarket APIs.
|
|
3
|
+
|
|
4
|
+
This module contains all the Pydantic models and type definitions used across
|
|
5
|
+
the Polymarket APIs.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from .clob_types import (
|
|
9
|
+
ApiCreds,
|
|
10
|
+
AssetType,
|
|
11
|
+
BidAsk,
|
|
12
|
+
BookParams,
|
|
13
|
+
ClobMarket,
|
|
14
|
+
ContractConfig,
|
|
15
|
+
CreateOrderOptions,
|
|
16
|
+
DailyEarnedReward,
|
|
17
|
+
MarketOrderArgs,
|
|
18
|
+
MarketRewards,
|
|
19
|
+
Midpoint,
|
|
20
|
+
OpenOrder,
|
|
21
|
+
OrderArgs,
|
|
22
|
+
OrderBookSummary,
|
|
23
|
+
OrderCancelResponse,
|
|
24
|
+
OrderPostResponse,
|
|
25
|
+
OrderType,
|
|
26
|
+
PaginatedResponse,
|
|
27
|
+
PartialCreateOrderOptions,
|
|
28
|
+
PolygonTrade,
|
|
29
|
+
PostOrdersArgs,
|
|
30
|
+
Price,
|
|
31
|
+
PriceHistory,
|
|
32
|
+
RequestArgs,
|
|
33
|
+
RewardMarket,
|
|
34
|
+
RoundConfig,
|
|
35
|
+
Spread,
|
|
36
|
+
TickSize,
|
|
37
|
+
Token,
|
|
38
|
+
TokenBidAsk,
|
|
39
|
+
TokenBidAskDict,
|
|
40
|
+
TokenValue,
|
|
41
|
+
TokenValueDict,
|
|
42
|
+
)
|
|
43
|
+
from .common import EmptyString, EthAddress, Keccak256, TimeseriesPoint
|
|
44
|
+
from .data_types import (
|
|
45
|
+
Activity,
|
|
46
|
+
Holder,
|
|
47
|
+
HolderResponse,
|
|
48
|
+
Position,
|
|
49
|
+
Trade,
|
|
50
|
+
User,
|
|
51
|
+
UserMetric,
|
|
52
|
+
UserRank,
|
|
53
|
+
ValueResponse,
|
|
54
|
+
)
|
|
55
|
+
from .gamma_types import (
|
|
56
|
+
ClobReward,
|
|
57
|
+
Event,
|
|
58
|
+
EventList,
|
|
59
|
+
GammaMarket,
|
|
60
|
+
Pagination,
|
|
61
|
+
QueryEvent,
|
|
62
|
+
QueryMarket,
|
|
63
|
+
Series,
|
|
64
|
+
Tag,
|
|
65
|
+
)
|
|
66
|
+
from .websockets_types import (
|
|
67
|
+
ActivityOrderMatchEvent,
|
|
68
|
+
ActivityTrade,
|
|
69
|
+
ActivityTradeEvent,
|
|
70
|
+
AggOrderBookSummary,
|
|
71
|
+
Comment,
|
|
72
|
+
CommentEvent,
|
|
73
|
+
CryptoPriceSubscribe,
|
|
74
|
+
CryptoPriceSubscribeEvent,
|
|
75
|
+
CryptoPriceUpdate,
|
|
76
|
+
CryptoPriceUpdateEvent,
|
|
77
|
+
ErrorEvent,
|
|
78
|
+
LastTradePrice,
|
|
79
|
+
LastTradePriceEvent,
|
|
80
|
+
LiveDataClobMarket,
|
|
81
|
+
LiveDataLastTradePriceEvent,
|
|
82
|
+
LiveDataOrderBookSummaryEvent,
|
|
83
|
+
LiveDataOrderEvent,
|
|
84
|
+
LiveDataPriceChangeEvent,
|
|
85
|
+
LiveDataTickSizeChangeEvent,
|
|
86
|
+
LiveDataTradeEvent,
|
|
87
|
+
MarketStatusChangeEvent,
|
|
88
|
+
OrderBookSummaryEvent,
|
|
89
|
+
OrderEvent,
|
|
90
|
+
PriceChange,
|
|
91
|
+
PriceChangeEvent,
|
|
92
|
+
PriceChanges,
|
|
93
|
+
Quote,
|
|
94
|
+
QuoteEvent,
|
|
95
|
+
Reaction,
|
|
96
|
+
ReactionEvent,
|
|
97
|
+
Request,
|
|
98
|
+
RequestEvent,
|
|
99
|
+
TickSizeChange,
|
|
100
|
+
TickSizeChangeEvent,
|
|
101
|
+
TradeEvent,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
__all__ = [
|
|
105
|
+
"Activity",
|
|
106
|
+
"ActivityOrderMatchEvent",
|
|
107
|
+
"ActivityTrade",
|
|
108
|
+
"ActivityTradeEvent",
|
|
109
|
+
"AggOrderBookSummary",
|
|
110
|
+
"ApiCreds",
|
|
111
|
+
"AssetType",
|
|
112
|
+
"BidAsk",
|
|
113
|
+
"BookParams",
|
|
114
|
+
"ClobMarket",
|
|
115
|
+
"ClobReward",
|
|
116
|
+
"Comment",
|
|
117
|
+
"CommentEvent",
|
|
118
|
+
"ContractConfig",
|
|
119
|
+
"CreateOrderOptions",
|
|
120
|
+
"CryptoPriceSubscribe",
|
|
121
|
+
"CryptoPriceSubscribeEvent",
|
|
122
|
+
"CryptoPriceUpdate",
|
|
123
|
+
"CryptoPriceUpdateEvent",
|
|
124
|
+
"DailyEarnedReward",
|
|
125
|
+
"EmptyString",
|
|
126
|
+
"ErrorEvent",
|
|
127
|
+
"EthAddress",
|
|
128
|
+
"Event",
|
|
129
|
+
"EventList",
|
|
130
|
+
"GammaMarket",
|
|
131
|
+
"Holder",
|
|
132
|
+
"HolderResponse",
|
|
133
|
+
"Keccak256",
|
|
134
|
+
"LastTradePrice",
|
|
135
|
+
"LastTradePriceEvent",
|
|
136
|
+
"LiveDataClobMarket",
|
|
137
|
+
"LiveDataLastTradePriceEvent",
|
|
138
|
+
"LiveDataOrderBookSummaryEvent",
|
|
139
|
+
"LiveDataOrderEvent",
|
|
140
|
+
"LiveDataPriceChangeEvent",
|
|
141
|
+
"LiveDataTickSizeChangeEvent",
|
|
142
|
+
"LiveDataTradeEvent",
|
|
143
|
+
"MarketOrderArgs",
|
|
144
|
+
"MarketRewards",
|
|
145
|
+
"MarketStatusChangeEvent",
|
|
146
|
+
"Midpoint",
|
|
147
|
+
"OpenOrder",
|
|
148
|
+
"OrderArgs",
|
|
149
|
+
"OrderBookSummary",
|
|
150
|
+
"OrderBookSummaryEvent",
|
|
151
|
+
"OrderCancelResponse",
|
|
152
|
+
"OrderEvent",
|
|
153
|
+
"OrderPostResponse",
|
|
154
|
+
"OrderType",
|
|
155
|
+
"PaginatedResponse",
|
|
156
|
+
"Pagination",
|
|
157
|
+
"PartialCreateOrderOptions",
|
|
158
|
+
"PolygonTrade",
|
|
159
|
+
"Position",
|
|
160
|
+
"PostOrdersArgs",
|
|
161
|
+
"Price",
|
|
162
|
+
"PriceChange",
|
|
163
|
+
"PriceChangeEvent",
|
|
164
|
+
"PriceChanges",
|
|
165
|
+
"PriceHistory",
|
|
166
|
+
"QueryEvent",
|
|
167
|
+
"QueryMarket",
|
|
168
|
+
"Quote",
|
|
169
|
+
"QuoteEvent",
|
|
170
|
+
"Reaction",
|
|
171
|
+
"ReactionEvent",
|
|
172
|
+
"Request",
|
|
173
|
+
"RequestArgs",
|
|
174
|
+
"RequestEvent",
|
|
175
|
+
"RewardMarket",
|
|
176
|
+
"RoundConfig",
|
|
177
|
+
"Series",
|
|
178
|
+
"Spread",
|
|
179
|
+
"Tag",
|
|
180
|
+
"TickSize",
|
|
181
|
+
"TickSizeChange",
|
|
182
|
+
"TickSizeChangeEvent",
|
|
183
|
+
"TimeseriesPoint",
|
|
184
|
+
"Token",
|
|
185
|
+
"TokenBidAsk",
|
|
186
|
+
"TokenBidAskDict",
|
|
187
|
+
"TokenValue",
|
|
188
|
+
"TokenValueDict",
|
|
189
|
+
"Trade",
|
|
190
|
+
"TradeEvent",
|
|
191
|
+
"User",
|
|
192
|
+
"UserMetric",
|
|
193
|
+
"UserRank",
|
|
194
|
+
"ValueResponse",
|
|
195
|
+
]
|