polymarket-apis 0.3.2__py3-none-any.whl → 0.3.4__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.

@@ -0,0 +1,224 @@
1
+ [
2
+ {
3
+ "inputs": [
4
+ {
5
+ "internalType": "address",
6
+ "name": "_masterCopy",
7
+ "type": "address"
8
+ },
9
+ {
10
+ "internalType": "address",
11
+ "name": "_fallbackHandler",
12
+ "type": "address"
13
+ }
14
+ ],
15
+ "stateMutability": "nonpayable",
16
+ "type": "constructor"
17
+ },
18
+ {
19
+ "anonymous": false,
20
+ "inputs": [
21
+ {
22
+ "indexed": false,
23
+ "internalType": "contract GnosisSafe",
24
+ "name": "proxy",
25
+ "type": "address"
26
+ },
27
+ {
28
+ "indexed": false,
29
+ "internalType": "address",
30
+ "name": "owner",
31
+ "type": "address"
32
+ }
33
+ ],
34
+ "name": "ProxyCreation",
35
+ "type": "event"
36
+ },
37
+ {
38
+ "inputs": [ ],
39
+ "name": "CREATE_PROXY_TYPEHASH",
40
+ "outputs": [
41
+ {
42
+ "internalType": "bytes32",
43
+ "name": "",
44
+ "type": "bytes32"
45
+ }
46
+ ],
47
+ "stateMutability": "view",
48
+ "type": "function"
49
+ },
50
+ {
51
+ "inputs": [ ],
52
+ "name": "DOMAIN_TYPEHASH",
53
+ "outputs": [
54
+ {
55
+ "internalType": "bytes32",
56
+ "name": "",
57
+ "type": "bytes32"
58
+ }
59
+ ],
60
+ "stateMutability": "view",
61
+ "type": "function"
62
+ },
63
+ {
64
+ "inputs": [ ],
65
+ "name": "NAME",
66
+ "outputs": [
67
+ {
68
+ "internalType": "string",
69
+ "name": "",
70
+ "type": "string"
71
+ }
72
+ ],
73
+ "stateMutability": "view",
74
+ "type": "function"
75
+ },
76
+ {
77
+ "inputs": [
78
+ {
79
+ "internalType": "address",
80
+ "name": "user",
81
+ "type": "address"
82
+ }
83
+ ],
84
+ "name": "computeProxyAddress",
85
+ "outputs": [
86
+ {
87
+ "internalType": "address",
88
+ "name": "",
89
+ "type": "address"
90
+ }
91
+ ],
92
+ "stateMutability": "view",
93
+ "type": "function"
94
+ },
95
+ {
96
+ "inputs": [
97
+ {
98
+ "internalType": "address",
99
+ "name": "paymentToken",
100
+ "type": "address"
101
+ },
102
+ {
103
+ "internalType": "uint256",
104
+ "name": "payment",
105
+ "type": "uint256"
106
+ },
107
+ {
108
+ "internalType": "address payable",
109
+ "name": "paymentReceiver",
110
+ "type": "address"
111
+ },
112
+ {
113
+ "components": [
114
+ {
115
+ "internalType": "uint8",
116
+ "name": "v",
117
+ "type": "uint8"
118
+ },
119
+ {
120
+ "internalType": "bytes32",
121
+ "name": "r",
122
+ "type": "bytes32"
123
+ },
124
+ {
125
+ "internalType": "bytes32",
126
+ "name": "s",
127
+ "type": "bytes32"
128
+ }
129
+ ],
130
+ "internalType": "struct SafeProxyFactory.Sig",
131
+ "name": "createSig",
132
+ "type": "tuple"
133
+ }
134
+ ],
135
+ "name": "createProxy",
136
+ "outputs": [ ],
137
+ "stateMutability": "nonpayable",
138
+ "type": "function"
139
+ },
140
+ {
141
+ "inputs": [ ],
142
+ "name": "domainSeparator",
143
+ "outputs": [
144
+ {
145
+ "internalType": "bytes32",
146
+ "name": "",
147
+ "type": "bytes32"
148
+ }
149
+ ],
150
+ "stateMutability": "view",
151
+ "type": "function"
152
+ },
153
+ {
154
+ "inputs": [ ],
155
+ "name": "fallbackHandler",
156
+ "outputs": [
157
+ {
158
+ "internalType": "address",
159
+ "name": "",
160
+ "type": "address"
161
+ }
162
+ ],
163
+ "stateMutability": "view",
164
+ "type": "function"
165
+ },
166
+ {
167
+ "inputs": [ ],
168
+ "name": "getContractBytecode",
169
+ "outputs": [
170
+ {
171
+ "internalType": "bytes",
172
+ "name": "",
173
+ "type": "bytes"
174
+ }
175
+ ],
176
+ "stateMutability": "view",
177
+ "type": "function"
178
+ },
179
+ {
180
+ "inputs": [
181
+ {
182
+ "internalType": "address",
183
+ "name": "user",
184
+ "type": "address"
185
+ }
186
+ ],
187
+ "name": "getSalt",
188
+ "outputs": [
189
+ {
190
+ "internalType": "bytes32",
191
+ "name": "",
192
+ "type": "bytes32"
193
+ }
194
+ ],
195
+ "stateMutability": "pure",
196
+ "type": "function"
197
+ },
198
+ {
199
+ "inputs": [ ],
200
+ "name": "masterCopy",
201
+ "outputs": [
202
+ {
203
+ "internalType": "address",
204
+ "name": "",
205
+ "type": "address"
206
+ }
207
+ ],
208
+ "stateMutability": "view",
209
+ "type": "function"
210
+ },
211
+ {
212
+ "inputs": [ ],
213
+ "name": "proxyCreationCode",
214
+ "outputs": [
215
+ {
216
+ "internalType": "bytes",
217
+ "name": "",
218
+ "type": "bytes"
219
+ }
220
+ ],
221
+ "stateMutability": "pure",
222
+ "type": "function"
223
+ }
224
+ ]
@@ -1,3 +1,13 @@
1
+ import re
2
+ from collections.abc import Iterable
3
+ from typing import Any
4
+
5
+ from eth_account import Account
6
+ from eth_account.messages import encode_defunct
7
+ from web3.constants import ADDRESS_ZERO
8
+ from web3.contract import Contract
9
+
10
+
1
11
  def get_market_index(question_id: str) -> int:
2
12
  """Extract the market index from a question ID (last 2 hex characters)."""
3
13
  return int(question_id[-2:], 16)
@@ -7,3 +17,145 @@ def get_index_set(question_ids: list[str]) -> int:
7
17
  """Calculate bitwise index set from question IDs."""
8
18
  indices = [get_market_index(question_id) for question_id in question_ids]
9
19
  return sum(1 << index for index in set(indices))
20
+
21
+
22
+ def _pack_primitive(typ: str, val: Any) -> bytes:
23
+ if isinstance(val, str) and val.startswith("0x"):
24
+ raw = bytes.fromhex(val[2:])
25
+ elif isinstance(val, bytes):
26
+ raw = val
27
+ else:
28
+ raw = val
29
+
30
+ if typ == "string":
31
+ if not isinstance(raw, (bytes, str)):
32
+ msg = "string value must be str or bytes"
33
+ raise TypeError(msg)
34
+ return raw.encode() if isinstance(raw, str) else raw
35
+ if typ == "bytes":
36
+ if isinstance(raw, int):
37
+ msg = "bytes value must be hex/bytes/str"
38
+ raise TypeError(msg)
39
+ if isinstance(raw, str):
40
+ return raw.encode()
41
+ return raw
42
+
43
+ m = re.match(r"^bytes(\d+)$", typ)
44
+ if m:
45
+ n = int(m.group(1))
46
+ if isinstance(raw, int):
47
+ b = raw.to_bytes(n, "big")
48
+ elif isinstance(raw, str) and raw.startswith("0x"):
49
+ b = bytes.fromhex(raw[2:])
50
+ elif isinstance(raw, (bytes, bytearray)):
51
+ b = bytes(raw)
52
+ else:
53
+ msg = f"unsupported value for {typ}"
54
+ raise TypeError(msg)
55
+ if len(b) != n:
56
+ if len(b) > n:
57
+ return b[:n]
58
+ return b.ljust(n, b"\x00")
59
+ return b
60
+
61
+ if typ == "address":
62
+ if isinstance(raw, str) and raw.startswith("0x"):
63
+ addr = raw[2:]
64
+ elif isinstance(raw, str):
65
+ addr = raw
66
+ elif isinstance(raw, (bytes, bytearray)):
67
+ return bytes(raw[-20:])
68
+ else:
69
+ msg = "address must be hex string or bytes"
70
+ raise TypeError(msg)
71
+ addr = addr.rjust(40, "0")[-40:]
72
+ return bytes.fromhex(addr)
73
+
74
+ m = re.match(r"^u?int(\d*)$", typ)
75
+ if m:
76
+ bits = int(m.group(1)) if m.group(1) else 256
77
+ size = bits // 8
78
+ if isinstance(raw, (bytes, bytearray)):
79
+ intval = int.from_bytes(raw, "big")
80
+ elif isinstance(raw, str) and raw.startswith("0x"):
81
+ intval = int(raw, 16)
82
+ elif isinstance(raw, int):
83
+ intval = raw
84
+ else:
85
+ msg = f"unsupported value for {typ}"
86
+ raise TypeError(msg)
87
+ if intval < 0:
88
+ intval &= (1 << bits) - 1
89
+ return intval.to_bytes(size, "big")
90
+
91
+ msg = f"unsupported type {typ}"
92
+ raise ValueError(msg)
93
+
94
+
95
+ def abi_encode_packed(*params: dict) -> bytes:
96
+ """
97
+ Takes in sequence of {'type': str, 'value': any}.
98
+
99
+ returns: concatenated packed bytes (no 0x prefix).
100
+ """
101
+ parts: list[bytes] = []
102
+ for p in params:
103
+ typ = p["type"]
104
+ val = p["value"]
105
+ if typ.endswith("[]"):
106
+ inner = typ[:-2]
107
+ if not isinstance(val, Iterable):
108
+ msg = f"expected array value for {typ}"
109
+ raise TypeError(msg)
110
+ for elem in val:
111
+ parts.append(abi_encode_packed({"type": inner, "value": elem}))
112
+ continue
113
+ parts.append(_pack_primitive(typ, val))
114
+ return b"".join(parts)
115
+
116
+
117
+ def sign_safe_transaction(
118
+ account: Account, safe: Contract, safe_txn: dict, nonce: int
119
+ ) -> bytes:
120
+ safe_tx_gas = 0
121
+ base_gas = 0
122
+ gas_price = 0
123
+ gas_token = ADDRESS_ZERO
124
+ refund_receiver = ADDRESS_ZERO
125
+
126
+ tx_hash_bytes = safe.functions.getTransactionHash(
127
+ safe_txn["to"],
128
+ safe_txn["value"],
129
+ safe_txn["data"],
130
+ safe_txn.get("operation", 0),
131
+ safe_tx_gas,
132
+ base_gas,
133
+ gas_price,
134
+ gas_token,
135
+ refund_receiver,
136
+ nonce,
137
+ ).call()
138
+
139
+ tx_hash_hex = tx_hash_bytes.hex()
140
+ message = encode_defunct(hexstr=tx_hash_hex)
141
+ signed = account.sign_message(message)
142
+ r = signed.r
143
+ s = signed.s
144
+ v = signed.v
145
+
146
+ match v:
147
+ case 0 | 1:
148
+ v += 31
149
+ case 27 | 28:
150
+ v += 4
151
+ case _:
152
+ msg = "Invalid signature V value"
153
+ raise ValueError(msg)
154
+
155
+ packed_sig = abi_encode_packed(
156
+ {"type": "uint256", "value": r},
157
+ {"type": "uint256", "value": s},
158
+ {"type": "uint8", "value": v},
159
+ )
160
+
161
+ return packed_sig
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: polymarket-apis
3
- Version: 0.3.2
3
+ Version: 0.3.4
4
4
  Summary: Unified Polymarket APIs with Pydantic data validation - Clob, Gamma, Data, Web3, Websockets, GraphQL clients.
5
5
  Author-email: Razvan Gheorghe <razvan@gheorghe.me>
6
6
  Requires-Python: >=3.12
@@ -10,6 +10,7 @@ Requires-Dist: lomond>=0.3.3
10
10
  Requires-Dist: poly-eip712-structs>=0.0.1
11
11
  Requires-Dist: py-order-utils>=0.3.2
12
12
  Requires-Dist: pydantic>=2.10.5
13
+ Requires-Dist: python-dateutil>=2.9.0
13
14
  Requires-Dist: web3>=7.0
14
15
  Requires-Dist: wsaccel>=0.6.7
15
16
  Description-Content-Type: text/markdown
@@ -151,6 +152,7 @@ flowchart LR
151
152
  - get top users on the profit/volume leaderboards (at most 100) for a recent window (1d, 7d, 30d, all)
152
153
 
153
154
  ### PolymarketWeb3Client - Blockchain related operations
155
+ - #### Supporting both Email/Magic wallets (signature_type=1) and Safe/Gnosis wallets (signature_type=2)
154
156
  - #### Balance
155
157
  - get usdc balance by user address
156
158
  - get token balance by `token_id` and user address
@@ -1,20 +1,20 @@
1
- polymarket_apis/__init__.py,sha256=UATdxksEshjBCHE_f8Jk666sKjoFUxHIn0T1yDKnfqs,1098
1
+ polymarket_apis/__init__.py,sha256=LR_GjibN6oQ92cZYbqhff0vVUtXhrVUfL5f8m2lWCMo,1126
2
2
  polymarket_apis/clients/__init__.py,sha256=ruMvFEA4HNkbWEYqbnrCuYXR4PUwkV1XWG0w63we-LA,759
3
3
  polymarket_apis/clients/clob_client.py,sha256=dDXkEf31tQnLHC7KsN7UIMfnLDJJFHrJ4VD_WwaTFbI,32102
4
4
  polymarket_apis/clients/data_client.py,sha256=SAPxgfWwPK_YuNBOvDxKgUIzdh-hMo2YlnTZ0rFDMXE,12000
5
- polymarket_apis/clients/gamma_client.py,sha256=_wY5gU-H-qLBHNjaIxxFY7XilYapqUT70hLNRiVFCJI,27566
5
+ polymarket_apis/clients/gamma_client.py,sha256=tBR_z03c3wh1_sxFbjNx54DcrFEjFB0TmJiOYNUc_pk,27632
6
6
  polymarket_apis/clients/graphql_client.py,sha256=KgjxbXNWEXp82ZEz464in5mCn1PydnZqWq-g11xu9YU,1839
7
- polymarket_apis/clients/web3_client.py,sha256=spCZzftMS19D62GBBpIN5I8JosZuNITNXDr7MrGoHwc,12191
7
+ polymarket_apis/clients/web3_client.py,sha256=J3j7T39cgZVb4PrYs7FoPoasO3PBP_7SbSuQzA--m64,20162
8
8
  polymarket_apis/clients/websockets_client.py,sha256=gaGcsjqp1ZRyxrL6EWvZ9XaTv7koTPDzlcShj0u0B2A,7737
9
9
  polymarket_apis/types/__init__.py,sha256=cyrAPNX0ty0wkMwZqB0lNA7n3JoOJCSeTpclmamMh9k,3258
10
- polymarket_apis/types/clob_types.py,sha256=xWnHmsYoilJWfkw2Fxga8cUm9JXuymDRYggJhPKt50Q,11744
10
+ polymarket_apis/types/clob_types.py,sha256=PmD_afx7zowX0cFJ39PQQ4jywGtwNjLfoke1H7iZZnI,11744
11
11
  polymarket_apis/types/common.py,sha256=RYu6Dfh0HUIogpK_kZ1AQQQEiqQD3hHACU5fMKSUoMU,1003
12
12
  polymarket_apis/types/data_types.py,sha256=F6GDhWNtIoQ8kvLvUdlRt596aUfnuc1aHg8m8UweT_M,4641
13
- polymarket_apis/types/gamma_types.py,sha256=BqoVOfHvTFII3sYkNmjT1S-J-0_R90bVdx_9d5urmMQ,30159
13
+ polymarket_apis/types/gamma_types.py,sha256=MoIUI6csqEp_vij7mkFi2IlIA99c5UNzI3zODSONWzQ,30641
14
14
  polymarket_apis/types/websockets_types.py,sha256=XGTpfix2rFFQg-97ZkPagS_tHJJpSnB5mCMf_sT2MOI,11180
15
15
  polymarket_apis/utilities/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
16
16
  polymarket_apis/utilities/config.py,sha256=kQymQRy9fVg5jt8CcQJxsSgIZFbfjPx2q_gNnZI5b24,2449
17
- polymarket_apis/utilities/constants.py,sha256=OdxaAsyYzAK1RXty5VtvidMgan8YW-JfHS3Rdxp6n_U,580
17
+ polymarket_apis/utilities/constants.py,sha256=rGX-XXu5tLy2yUurFQCQNRtSvZPPcRAUCmjlqVtTAvY,580
18
18
  polymarket_apis/utilities/endpoints.py,sha256=bxZyrJBPbVauWc-eR0RMh6KDqU-SmO_3LfQwVMNJ6vE,1235
19
19
  polymarket_apis/utilities/exceptions.py,sha256=nLVkwGNkX8mBhOi3L3lLEJ5UCPd5OBjl2f7kcct3K3A,368
20
20
  polymarket_apis/utilities/headers.py,sha256=Cc5WEnIBLYAgfwvmCXRBwA2zUYME8fDy4PbwlwlB6Oo,1510
@@ -27,15 +27,17 @@ polymarket_apis/utilities/signing/hmac.py,sha256=1VPfO2yT8nyStk6U4AQeyTzQTt5-69P
27
27
  polymarket_apis/utilities/signing/model.py,sha256=kVduuJGth7WSCUDCVVydCgPd4yEVI85gEmMxohXsvp0,191
28
28
  polymarket_apis/utilities/signing/signer.py,sha256=7-nFALFrz0qg4F4lZRyHI41S7IWxr7t0WMLOHqHzJcg,732
29
29
  polymarket_apis/utilities/web3/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
30
- polymarket_apis/utilities/web3/helpers.py,sha256=EZ5FiU54vCMeRQMSXbCDWAiGqrFRd1rS7D_CtUn8fHQ,404
30
+ polymarket_apis/utilities/web3/helpers.py,sha256=QEpBaA_g7DRzFUZAfiKr6kg5T5_u5FaVZ0mGGNzo6mo,4698
31
31
  polymarket_apis/utilities/web3/abis/CTFExchange.json,sha256=zt8fZnUaOrD8Vh5njM0EEUpeITWhuu0SZrIZigWxgV8,38499
32
32
  polymarket_apis/utilities/web3/abis/ConditionalTokens.json,sha256=3TUcX7He74VMkoL1kxbDbtULZ70VY_EBe01pfByprsk,12584
33
33
  polymarket_apis/utilities/web3/abis/NegRiskAdapter.json,sha256=HABIoRF1s1NgctpRTdaaNDqzODzgdZLE-s2E6ef4nAY,18867
34
34
  polymarket_apis/utilities/web3/abis/NegRiskCtfExchange.json,sha256=QOgLKekWnPVcMGXExcLjEIOHLS89tPUoZFkVm-yRnbY,38612
35
35
  polymarket_apis/utilities/web3/abis/ProxyWalletFactory.json,sha256=5KjBHUWdkc_kdlWPNax84o1vStpFuLgZKTMn3jc4zvU,5553
36
+ polymarket_apis/utilities/web3/abis/Safe.json,sha256=iXxTY7XM1hBOifAjHO9FK8e2FaQfCHrW7jK7WPvJiEM,24361
37
+ polymarket_apis/utilities/web3/abis/SafeProxyFactory.json,sha256=bdr2WdYCRClXLTT0jarNs4C-Rz9bxozuwZgmcew-uAY,4211
36
38
  polymarket_apis/utilities/web3/abis/UChildERC20Proxy.json,sha256=ZyQC38U0uxInlmnW2VXDVD3TJfTIRmSNMkTxQsaG7oA,27396
37
39
  polymarket_apis/utilities/web3/abis/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
38
40
  polymarket_apis/utilities/web3/abis/custom_contract_errors.py,sha256=GjCVn2b6iRheT7s-kc8Po9uwH9LfaHA1yRpJyjXRcxs,1172
39
- polymarket_apis-0.3.2.dist-info/METADATA,sha256=9ov4uwnSra_kcTD31KLg977T82YnN56HPiPKqQ6ERVY,12086
40
- polymarket_apis-0.3.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
41
- polymarket_apis-0.3.2.dist-info/RECORD,,
41
+ polymarket_apis-0.3.4.dist-info/METADATA,sha256=36D_8S5Nbt5dNd-oAZDEvndcd-bLoR5RlfP8ypVj5GA,12231
42
+ polymarket_apis-0.3.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
43
+ polymarket_apis-0.3.4.dist-info/RECORD,,