poly-web3 0.0.2__py3-none-any.whl → 0.0.3__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.
- examples/example_redeem.py +63 -63
- poly_web3/__init__.py +33 -33
- poly_web3/const.py +171 -171
- poly_web3/log.py +18 -0
- poly_web3/schema.py +22 -22
- poly_web3/signature/__init__.py +6 -6
- poly_web3/signature/build.py +113 -113
- poly_web3/signature/hash_message.py +48 -48
- poly_web3/signature/secp256k1.py +57 -57
- poly_web3/web3_service/__init__.py +9 -9
- poly_web3/web3_service/base.py +182 -181
- poly_web3/web3_service/eoa_service.py +17 -17
- poly_web3/web3_service/proxy_service.py +166 -163
- poly_web3/web3_service/safe_service.py +17 -17
- {poly_web3-0.0.2.dist-info → poly_web3-0.0.3.dist-info}/METADATA +302 -302
- poly_web3-0.0.3.dist-info/RECORD +18 -0
- poly_web3-0.0.2.dist-info/RECORD +0 -17
- {poly_web3-0.0.2.dist-info → poly_web3-0.0.3.dist-info}/WHEEL +0 -0
- {poly_web3-0.0.2.dist-info → poly_web3-0.0.3.dist-info}/top_level.txt +0 -0
poly_web3/web3_service/base.py
CHANGED
|
@@ -1,181 +1,182 @@
|
|
|
1
|
-
# -*- coding = utf-8 -*-
|
|
2
|
-
# @Time: 2025-12-27 15:57:07
|
|
3
|
-
# @Author: PinBar
|
|
4
|
-
# @Site:
|
|
5
|
-
# @File: base.py
|
|
6
|
-
# @Software: PyCharm
|
|
7
|
-
import requests
|
|
8
|
-
from py_builder_relayer_client.client import RelayClient
|
|
9
|
-
from py_clob_client.client import ClobClient
|
|
10
|
-
from web3 import Web3
|
|
11
|
-
|
|
12
|
-
from poly_web3.const import (
|
|
13
|
-
RPC_URL,
|
|
14
|
-
CTF_ADDRESS,
|
|
15
|
-
CTF_ABI_PAYOUT,
|
|
16
|
-
ZERO_BYTES32,
|
|
17
|
-
USDC_POLYGON,
|
|
18
|
-
CTF_ABI_REDEEM,
|
|
19
|
-
NEG_RISK_ADAPTER_ADDRESS,
|
|
20
|
-
RELAYER_URL,
|
|
21
|
-
POL,
|
|
22
|
-
AMOY,
|
|
23
|
-
GET_RELAY_PAYLOAD,
|
|
24
|
-
NEG_RISK_ADAPTER_ABI_REDEEM,
|
|
25
|
-
)
|
|
26
|
-
from poly_web3.schema import WalletType
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
self.
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
self.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
:
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
response.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
"
|
|
158
|
-
"
|
|
159
|
-
"
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
1
|
+
# -*- coding = utf-8 -*-
|
|
2
|
+
# @Time: 2025-12-27 15:57:07
|
|
3
|
+
# @Author: PinBar
|
|
4
|
+
# @Site:
|
|
5
|
+
# @File: base.py
|
|
6
|
+
# @Software: PyCharm
|
|
7
|
+
import requests
|
|
8
|
+
from py_builder_relayer_client.client import RelayClient
|
|
9
|
+
from py_clob_client.client import ClobClient
|
|
10
|
+
from web3 import Web3
|
|
11
|
+
|
|
12
|
+
from poly_web3.const import (
|
|
13
|
+
RPC_URL,
|
|
14
|
+
CTF_ADDRESS,
|
|
15
|
+
CTF_ABI_PAYOUT,
|
|
16
|
+
ZERO_BYTES32,
|
|
17
|
+
USDC_POLYGON,
|
|
18
|
+
CTF_ABI_REDEEM,
|
|
19
|
+
NEG_RISK_ADAPTER_ADDRESS,
|
|
20
|
+
RELAYER_URL,
|
|
21
|
+
POL,
|
|
22
|
+
AMOY,
|
|
23
|
+
GET_RELAY_PAYLOAD,
|
|
24
|
+
NEG_RISK_ADAPTER_ABI_REDEEM,
|
|
25
|
+
)
|
|
26
|
+
from poly_web3.schema import WalletType
|
|
27
|
+
from poly_web3.log import logger
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class BaseWeb3Service:
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
clob_client: ClobClient = None,
|
|
34
|
+
relayer_client: RelayClient = None,
|
|
35
|
+
rpc_url: str | None = None,
|
|
36
|
+
):
|
|
37
|
+
self.relayer_client = relayer_client
|
|
38
|
+
self.clob_client: ClobClient = clob_client
|
|
39
|
+
if self.clob_client:
|
|
40
|
+
self.wallet_type: WalletType = WalletType.get_with_code(
|
|
41
|
+
self.clob_client.builder.sig_type
|
|
42
|
+
)
|
|
43
|
+
else:
|
|
44
|
+
self.wallet_type = WalletType.PROXY
|
|
45
|
+
self.rpc_url = rpc_url or RPC_URL
|
|
46
|
+
self.w3: Web3 = Web3(Web3.HTTPProvider(self.rpc_url))
|
|
47
|
+
if self.wallet_type == WalletType.PROXY and relayer_client is None:
|
|
48
|
+
raise Exception("relayer_client must be provided")
|
|
49
|
+
|
|
50
|
+
def _resolve_user_address(self):
|
|
51
|
+
funder = getattr(getattr(self.clob_client, "builder", None), "funder", None)
|
|
52
|
+
if funder:
|
|
53
|
+
return funder
|
|
54
|
+
return self.clob_client.get_address()
|
|
55
|
+
|
|
56
|
+
@classmethod
|
|
57
|
+
def fetch_positions(cls, user_address: str) -> list[dict]:
|
|
58
|
+
"""
|
|
59
|
+
Fetches current positions for a user from the official Polymarket API.
|
|
60
|
+
|
|
61
|
+
:param user_address: User wallet address (0x-prefixed, 40 hex chars)
|
|
62
|
+
:return: List of position dictionaries from the API
|
|
63
|
+
"""
|
|
64
|
+
url = "https://data-api.polymarket.com/positions"
|
|
65
|
+
params = {
|
|
66
|
+
"user": user_address,
|
|
67
|
+
"sizeThreshold": 1,
|
|
68
|
+
"limit": 100,
|
|
69
|
+
"redeemable": True,
|
|
70
|
+
"sortBy": "RESOLVING",
|
|
71
|
+
"sortDirection": "DESC",
|
|
72
|
+
}
|
|
73
|
+
try:
|
|
74
|
+
response = requests.get(url, params=params)
|
|
75
|
+
response.raise_for_status()
|
|
76
|
+
positions = response.json()
|
|
77
|
+
return [i for i in positions if i.get("percentPnl") > 0]
|
|
78
|
+
except Exception as e:
|
|
79
|
+
logger.error(f"Failed to fetch positions from API: {e}")
|
|
80
|
+
return []
|
|
81
|
+
|
|
82
|
+
def is_condition_resolved(self, condition_id: str) -> bool:
|
|
83
|
+
ctf = self.w3.eth.contract(address=CTF_ADDRESS, abi=CTF_ABI_PAYOUT)
|
|
84
|
+
return ctf.functions.payoutDenominator(condition_id).call() > 0
|
|
85
|
+
|
|
86
|
+
def get_winning_indexes(self, condition_id: str) -> list[int]:
|
|
87
|
+
ctf = self.w3.eth.contract(address=CTF_ADDRESS, abi=CTF_ABI_PAYOUT)
|
|
88
|
+
if not self.is_condition_resolved(condition_id):
|
|
89
|
+
return []
|
|
90
|
+
outcome_count = ctf.functions.getOutcomeSlotCount(condition_id).call()
|
|
91
|
+
winners: list[int] = []
|
|
92
|
+
for i in range(outcome_count):
|
|
93
|
+
if ctf.functions.payoutNumerators(condition_id, i).call() > 0:
|
|
94
|
+
winners.append(i)
|
|
95
|
+
return winners
|
|
96
|
+
|
|
97
|
+
def get_redeemable_index_and_balance(
|
|
98
|
+
self, condition_id: str
|
|
99
|
+
) -> list[tuple]:
|
|
100
|
+
owner = self._resolve_user_address()
|
|
101
|
+
winners = self.get_winning_indexes(condition_id)
|
|
102
|
+
if not winners:
|
|
103
|
+
return []
|
|
104
|
+
ctf = self.w3.eth.contract(address=CTF_ADDRESS, abi=CTF_ABI_PAYOUT)
|
|
105
|
+
owner_checksum = Web3.to_checksum_address(owner)
|
|
106
|
+
redeemable: list[tuple] = []
|
|
107
|
+
for index in winners:
|
|
108
|
+
index_set = 1 << index
|
|
109
|
+
collection_id = ctf.functions.getCollectionId(
|
|
110
|
+
ZERO_BYTES32, condition_id, index_set
|
|
111
|
+
).call()
|
|
112
|
+
position_id = ctf.functions.getPositionId(
|
|
113
|
+
USDC_POLYGON, collection_id
|
|
114
|
+
).call()
|
|
115
|
+
balance = ctf.functions.balanceOf(owner_checksum, position_id).call()
|
|
116
|
+
if balance > 0:
|
|
117
|
+
redeemable.append((index, balance / 1000000))
|
|
118
|
+
return redeemable
|
|
119
|
+
|
|
120
|
+
def build_ctf_redeem_tx_data(self, condition_id: str) -> str:
|
|
121
|
+
ctf = self.w3.eth.contract(address=CTF_ADDRESS, abi=CTF_ABI_REDEEM)
|
|
122
|
+
# 只需要 calldata:encodeABI 即可
|
|
123
|
+
return ctf.functions.redeemPositions(
|
|
124
|
+
USDC_POLYGON,
|
|
125
|
+
ZERO_BYTES32,
|
|
126
|
+
condition_id,
|
|
127
|
+
[1, 2],
|
|
128
|
+
)._encode_transaction_data()
|
|
129
|
+
|
|
130
|
+
def build_neg_risk_redeem_tx_data(
|
|
131
|
+
self, condition_id: str, redeem_amounts: list[int]
|
|
132
|
+
) -> str:
|
|
133
|
+
nr_adapter = self.w3.eth.contract(
|
|
134
|
+
address=NEG_RISK_ADAPTER_ADDRESS, abi=NEG_RISK_ADAPTER_ABI_REDEEM
|
|
135
|
+
)
|
|
136
|
+
return nr_adapter.functions.redeemPositions(
|
|
137
|
+
condition_id,
|
|
138
|
+
redeem_amounts,
|
|
139
|
+
)._encode_transaction_data()
|
|
140
|
+
|
|
141
|
+
@classmethod
|
|
142
|
+
def _get_relay_payload(cls, address: str, wallet_type: WalletType):
|
|
143
|
+
return requests.get(
|
|
144
|
+
RELAYER_URL + GET_RELAY_PAYLOAD,
|
|
145
|
+
params={"address": address, "type": wallet_type},
|
|
146
|
+
).json()
|
|
147
|
+
|
|
148
|
+
def get_contract_config(self) -> dict:
|
|
149
|
+
if self.clob_client.chain_id == 137:
|
|
150
|
+
return POL
|
|
151
|
+
elif self.clob_client.chain_id == 80002:
|
|
152
|
+
return AMOY
|
|
153
|
+
raise Exception("Invalid network")
|
|
154
|
+
|
|
155
|
+
def estimate_gas(self, tx):
|
|
156
|
+
payload = {
|
|
157
|
+
"jsonrpc": "2.0",
|
|
158
|
+
"method": "eth_estimateGas",
|
|
159
|
+
"params": [tx],
|
|
160
|
+
"id": 1,
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
response = requests.post(self.rpc_url, json=payload)
|
|
164
|
+
result = response.json()
|
|
165
|
+
|
|
166
|
+
if "result" in result:
|
|
167
|
+
# 返回的是16进制 gas 数量
|
|
168
|
+
gas_hex = result["result"]
|
|
169
|
+
return str(int(gas_hex, 16))
|
|
170
|
+
else:
|
|
171
|
+
raise Exception("Estimate gas error: " + str(result))
|
|
172
|
+
|
|
173
|
+
def redeem(
|
|
174
|
+
self,
|
|
175
|
+
condition_id: str,
|
|
176
|
+
neg_risk: bool = False,
|
|
177
|
+
redeem_amounts: list[int] | None = None,
|
|
178
|
+
): # noqa:
|
|
179
|
+
raise ImportError()
|
|
180
|
+
|
|
181
|
+
def redeem_all(self) -> list[dict] | None:
|
|
182
|
+
raise ImportError()
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
# -*- coding = utf-8 -*-
|
|
2
|
-
# @Time: 2025-12-27 16:01:09
|
|
3
|
-
# @Author: PinBar
|
|
4
|
-
# @Site:
|
|
5
|
-
# @File: eoa_service.py
|
|
6
|
-
# @Software: PyCharm
|
|
7
|
-
from poly_web3.web3_service.base import BaseWeb3Service
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class EOAWeb3Service(BaseWeb3Service):
|
|
11
|
-
def redeem(
|
|
12
|
-
self,
|
|
13
|
-
condition_id: str,
|
|
14
|
-
neg_risk: bool = False,
|
|
15
|
-
redeem_amounts: list[int] | None = None,
|
|
16
|
-
):
|
|
17
|
-
raise ImportError("EOA wallet redeem not supported")
|
|
1
|
+
# -*- coding = utf-8 -*-
|
|
2
|
+
# @Time: 2025-12-27 16:01:09
|
|
3
|
+
# @Author: PinBar
|
|
4
|
+
# @Site:
|
|
5
|
+
# @File: eoa_service.py
|
|
6
|
+
# @Software: PyCharm
|
|
7
|
+
from poly_web3.web3_service.base import BaseWeb3Service
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class EOAWeb3Service(BaseWeb3Service):
|
|
11
|
+
def redeem(
|
|
12
|
+
self,
|
|
13
|
+
condition_id: str,
|
|
14
|
+
neg_risk: bool = False,
|
|
15
|
+
redeem_amounts: list[int] | None = None,
|
|
16
|
+
):
|
|
17
|
+
raise ImportError("EOA wallet redeem not supported")
|