prediction-market-agent-tooling 0.61.1.dev483__py3-none-any.whl → 0.61.1.dev484__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.
@@ -12,13 +12,20 @@ from prediction_market_agent_tooling.gtypes import (
12
12
  HexAddress,
13
13
  HexBytes,
14
14
  Probability,
15
+ xdai_type,
15
16
  )
17
+ from prediction_market_agent_tooling.loggers import logger
16
18
  from prediction_market_agent_tooling.markets.data_models import Resolution
17
- from prediction_market_agent_tooling.markets.seer.price_manager import PriceManager
18
19
  from prediction_market_agent_tooling.markets.seer.subgraph_data_models import (
19
20
  SeerParentMarket,
21
+ SeerPool,
20
22
  )
23
+ from prediction_market_agent_tooling.tools.caches.inmemory_cache import (
24
+ persistent_inmemory_cache,
25
+ )
26
+ from prediction_market_agent_tooling.tools.cow.cow_manager import CowManager
21
27
  from prediction_market_agent_tooling.tools.datetime_utc import DatetimeUTC
28
+ from prediction_market_agent_tooling.tools.web3_utils import xdai_to_wei
22
29
 
23
30
 
24
31
  class SeerOutcomeEnum(str, Enum):
@@ -158,10 +165,92 @@ class SeerMarket(BaseModel):
158
165
  def created_time(self) -> DatetimeUTC:
159
166
  return DatetimeUTC.to_datetime_utc(self.block_timestamp)
160
167
 
168
+ @persistent_inmemory_cache
169
+ def get_price_for_token(
170
+ self,
171
+ token: ChecksumAddress,
172
+ ) -> float:
173
+ collateral_exchange_amount = xdai_to_wei(xdai_type(1))
174
+ try:
175
+ quote = CowManager().get_quote(
176
+ collateral_token=self.collateral_token_contract_address_checksummed,
177
+ buy_token=token,
178
+ sell_amount=collateral_exchange_amount,
179
+ )
180
+ except Exception as e:
181
+ logger.warning(
182
+ f"Could not get quote for {token=} from Cow, exception {e=}. Falling back to pools. "
183
+ )
184
+ price = self.get_token_price_from_pools(token=token)
185
+ return price
186
+
187
+ return collateral_exchange_amount / float(quote.quote.buyAmount.root)
188
+
189
+ @staticmethod
190
+ def _pool_token0_matches_token(token: ChecksumAddress, pool: SeerPool) -> bool:
191
+ return pool.token0.id.hex().lower() == token.lower()
192
+
193
+ def get_token_price_from_pools(
194
+ self,
195
+ token: ChecksumAddress,
196
+ collateral_token_contract_address_checksummed: ChecksumAddress,
197
+ ) -> float:
198
+ pool = self.subgraph_handler.get_pool_by_token(token_address=token)
199
+
200
+ if not pool:
201
+ logger.warning(f"Could not find a pool for {token=}, returning 0.")
202
+ return 0
203
+ # Check if other token is market's collateral (sanity check).
204
+
205
+ collateral_address = (
206
+ pool.token0.id
207
+ if self._pool_token0_matches_token(token=token, pool=pool)
208
+ else pool.token1.id
209
+ )
210
+ if (
211
+ collateral_address.hex().lower()
212
+ != collateral_token_contract_address_checksummed.lower()
213
+ ):
214
+ logger.warning(
215
+ f"Pool {pool.id.hex()} has collateral mismatch with market. Collateral from pool {collateral_address.hex()}, collateral from market {collateral_token_contract_address_checksummed}, returning 0."
216
+ )
217
+ return 0
218
+
219
+ price_in_collateral_units = (
220
+ pool.token0Price
221
+ if self._pool_token0_matches_token(pool)
222
+ else pool.token1Price
223
+ )
224
+ return price_in_collateral_units
225
+
161
226
  @property
162
227
  def current_p_yes(self) -> Probability:
163
- p = PriceManager(self)
164
- return p.current_market_p_yes
228
+ price_data = {}
229
+ for idx, wrapped_token in enumerate(self.wrapped_tokens):
230
+ price = self.get_price_for_token(
231
+ token=Web3.to_checksum_address(wrapped_token),
232
+ )
233
+
234
+ price_data[idx] = price
235
+
236
+ if sum(price_data.values()) == 0:
237
+ logger.warning(
238
+ f"Could not get p_yes for market {self.id.hex()}, all price quotes are 0."
239
+ )
240
+ return Probability(0)
241
+
242
+ price_yes = price_data[self.outcome_as_enums[SeerOutcomeEnum.YES]]
243
+ price_no = price_data[self.outcome_as_enums[SeerOutcomeEnum.NO]]
244
+ if price_yes and not price_no:
245
+ # We simply return p_yes since it's probably a bug that p_no wasn't found.
246
+ return Probability(price_yes)
247
+ elif price_no and not price_yes:
248
+ # We return the complement of p_no (and ignore invalid).
249
+ return Probability(1.0 - price_no)
250
+ else:
251
+ # If all prices are available, we normalize price_yes by the other prices for the final probability.
252
+ price_yes = price_yes / sum(price_data.values())
253
+ return Probability(price_yes)
165
254
 
166
255
  @property
167
256
  def url(self) -> str:
@@ -1,124 +1,5 @@
1
- from web3 import Web3
2
-
3
1
  from prediction_market_agent_tooling.gtypes import ChecksumAddress
4
- from prediction_market_agent_tooling.gtypes import Probability, xdai_type
5
- from prediction_market_agent_tooling.loggers import logger
6
- from prediction_market_agent_tooling.markets.seer.data_models import (
7
- SeerMarket,
8
- SeerOutcomeEnum,
9
- )
10
- from prediction_market_agent_tooling.markets.seer.seer_subgraph_handler import (
11
- SeerSubgraphHandler,
12
- )
13
2
  from prediction_market_agent_tooling.markets.seer.subgraph_data_models import SeerPool
14
- from prediction_market_agent_tooling.tools.caches.inmemory_cache import (
15
- persistent_inmemory_cache,
16
- )
17
- from prediction_market_agent_tooling.tools.cow.cow_manager import CowManager
18
- from prediction_market_agent_tooling.tools.web3_utils import xdai_to_wei
19
3
 
20
4
 
21
5
  class PriceManager:
22
- def __init__(self, market: SeerMarket):
23
- self.seer_market = market
24
- self.subgraph_handler = SeerSubgraphHandler()
25
-
26
- def fetch_outcome_price_for_seer_market(self) -> float:
27
- price_data = {}
28
- for idx in range(len(self.seer_market.outcomes)):
29
- wrapped_token = self.seer_market.wrapped_tokens[idx]
30
- price = self.get_price_for_token(
31
- token=Web3.to_checksum_address(wrapped_token)
32
- )
33
- price_data[idx] = price
34
-
35
- if sum(price_data.values()) == 0:
36
- logger.warning(
37
- f"Could not get p_yes for market {self.seer_market.id.hex()}, all price quotes are 0."
38
- )
39
- return Probability(0)
40
-
41
- yes_idx = self.seer_market.outcome_as_enums[SeerOutcomeEnum.YES]
42
- price_yes = price_data[yes_idx] / sum(price_data.values())
43
- return Probability(price_yes)
44
-
45
- def current_market_p_yes(self) -> Probability:
46
- price_data = {}
47
- for idx in range(len(self.seer_market.outcomes)):
48
- wrapped_token = self.seer_market.wrapped_tokens[idx]
49
- price = self.get_price_for_token(
50
- token=Web3.to_checksum_address(wrapped_token)
51
- )
52
- price_data[idx] = price
53
-
54
- if sum(price_data.values()) == 0:
55
- logger.warning(
56
- f"Could not get p_yes for market {self.seer_market.id.hex()}, all price quotes are 0."
57
- )
58
- return Probability(0)
59
-
60
- yes_idx = self.seer_market.outcome_as_enums[SeerOutcomeEnum.YES]
61
- no_idx = self.seer_market.outcome_as_enums[SeerOutcomeEnum.NO]
62
- price_yes = price_data[yes_idx]
63
- price_no = price_data[no_idx]
64
- if price_yes and not price_no:
65
- # We simply return p_yes since it's probably a bug that p_no wasn't found.
66
- return Probability(price_yes)
67
- elif price_no and not price_yes:
68
- # We return the complement of p_no (and ignore invalid).
69
- return Probability(1.0 - price_no)
70
- else:
71
- # If all prices are available, we normalize price_yes by the other prices for the final probability.
72
- price_yes = price_yes / sum(price_data.values())
73
- return Probability(price_yes)
74
-
75
- @persistent_inmemory_cache
76
- def get_price_for_token(self, token: ChecksumAddress) -> float:
77
- collateral_exchange_amount = xdai_to_wei(xdai_type(1))
78
- try:
79
- quote = CowManager().get_quote(
80
- collateral_token=self.seer_market.collateral_token_contract_address_checksummed,
81
- buy_token=token,
82
- sell_amount=collateral_exchange_amount,
83
- )
84
- except Exception as e:
85
- logger.warning(
86
- f"Could not get quote for {token=} from Cow, exception {e=}. Falling back to pools. "
87
- )
88
- price = self.get_token_price_from_pools(token=token)
89
- return price
90
-
91
- return collateral_exchange_amount / float(quote.quote.buyAmount.root)
92
-
93
- @staticmethod
94
- def _pool_token0_matches_token(token: ChecksumAddress, pool: SeerPool) -> bool:
95
- return pool.token0.id.hex().lower() == token.lower()
96
-
97
- def get_token_price_from_pools(self, token: ChecksumAddress) -> float:
98
- pool = self.subgraph_handler.get_pool_by_token(token_address=token)
99
-
100
- if not pool:
101
- logger.warning(f"Could not find a pool for {token=}, returning 0.")
102
- return 0
103
- # Check if other token is market's collateral (sanity check).
104
-
105
- collateral_address = (
106
- pool.token0.id
107
- if self._pool_token0_matches_token(token=token, pool=pool)
108
- else pool.token1.id
109
- )
110
- if (
111
- collateral_address.hex().lower()
112
- != self.seer_market.collateral_token.lower()
113
- ):
114
- logger.warning(
115
- f"Pool {pool.id.hex()} has collateral mismatch with market. Collateral from pool {collateral_address.hex()}, collateral from market {self.seer_market.collateral_token}, returning 0."
116
- )
117
- return 0
118
-
119
- price_in_collateral_units = (
120
- pool.token0Price
121
- if self._pool_token0_matches_token(pool)
122
- else pool.token1Price
123
- )
124
- return price_in_collateral_units
@@ -12,8 +12,8 @@ from prediction_market_agent_tooling.markets.base_subgraph_handler import (
12
12
  )
13
13
  from prediction_market_agent_tooling.markets.seer.data_models import (
14
14
  SeerMarket,
15
- SeerPool,
16
15
  )
16
+ from prediction_market_agent_tooling.markets.seer.subgraph_data_models import SeerPool
17
17
  from prediction_market_agent_tooling.tools.hexbytes_custom import HexBytes
18
18
  from prediction_market_agent_tooling.tools.utils import to_int_timestamp, utcnow
19
19
 
@@ -1,6 +1,7 @@
1
1
  from pydantic import BaseModel, ConfigDict, Field
2
+ from web3.constants import ADDRESS_ZERO
2
3
 
3
- from prediction_market_agent_tooling.gtypes import HexBytes, HexAddress
4
+ from prediction_market_agent_tooling.gtypes import HexBytes, HexAddress, Wei
4
5
 
5
6
 
6
7
  class SeerToken(BaseModel):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: prediction-market-agent-tooling
3
- Version: 0.61.1.dev483
3
+ Version: 0.61.1.dev484
4
4
  Summary: Tools to benchmark, deploy and monitor prediction market agents.
5
5
  Author: Gnosis
6
6
  Requires-Python: >=3.10,<3.13
@@ -61,12 +61,12 @@ prediction_market_agent_tooling/markets/polymarket/data_models.py,sha256=Fd5PI5y
61
61
  prediction_market_agent_tooling/markets/polymarket/data_models_web.py,sha256=VZhVccTApygSKMmy6Au2G02JCJOKJnR_oVeKlaesuSg,12548
62
62
  prediction_market_agent_tooling/markets/polymarket/polymarket.py,sha256=NRoZK71PtH8kkangMqme7twcAXhRJSSabbmOir-UnAI,3418
63
63
  prediction_market_agent_tooling/markets/polymarket/utils.py,sha256=8kTeVjXPcXC6DkDvWYsZQLY7x8DS6CEp_yznSEazsNU,2037
64
- prediction_market_agent_tooling/markets/seer/data_models.py,sha256=EDzVMgBvum400VQl4IUvdnchBszF6M_4LWxMWkeTIHY,5576
65
- prediction_market_agent_tooling/markets/seer/price_manager.py,sha256=tuozfXp2dwgUQXvD4G4UAOzs-rIm6vgevbi804vv8-0,5102
64
+ prediction_market_agent_tooling/markets/seer/data_models.py,sha256=jLfCiwkw4OZyqw5RV3kEimmWWeBBF6SMmn4Oxzlm5pQ,9101
65
+ prediction_market_agent_tooling/markets/seer/price_manager.py,sha256=k0I8xLFs0cXEDrTiOtQUxVPaAVYchfz-D8cijeONlXc,176
66
66
  prediction_market_agent_tooling/markets/seer/seer.py,sha256=Anxp66UUzIIKt5wgLoqtUjUM-5HQ0wC5f5rDiYwrRRg,12959
67
67
  prediction_market_agent_tooling/markets/seer/seer_contracts.py,sha256=E7CYAKZiK6cg3dyj1kJuIPKSYYUft98F64shF5S0g4s,2730
68
- prediction_market_agent_tooling/markets/seer/seer_subgraph_handler.py,sha256=2M6vmUxMKQU61VNCp4Ykican6XH1LOp257--GjCJZx8,8974
69
- prediction_market_agent_tooling/markets/seer/subgraph_data_models.py,sha256=uPTCYTg_lS91Pb7JCU-neVEgAQe3zSOtkttlhQGlcBI,1599
68
+ prediction_market_agent_tooling/markets/seer/seer_subgraph_handler.py,sha256=lcaKbFHxigMYwi4TCePyTb5HXJhGoCu5dJPFWTj5E28,9047
69
+ prediction_market_agent_tooling/markets/seer/subgraph_data_models.py,sha256=RIt2oO-PbykzbYN11Qltt8tjjYTty-PfjH3Q4D2VEoA,1644
70
70
  prediction_market_agent_tooling/monitor/financial_metrics/financial_metrics.py,sha256=fjIgjDIx5MhH5mwf7S0cspLOOSU3elYLhGYoIiM26mU,2746
71
71
  prediction_market_agent_tooling/monitor/markets/manifold.py,sha256=TS4ERwTfQnot8dhekNyVNhJYf5ysYsjF-9v5_kM3aVI,3334
72
72
  prediction_market_agent_tooling/monitor/markets/metaculus.py,sha256=LOnyWWBFdg10-cTWdb76nOsNjDloO8OfMT85GBzRCFI,1455
@@ -119,8 +119,8 @@ prediction_market_agent_tooling/tools/tokens/main_token.py,sha256=7JPgVF4RbiFzLD
119
119
  prediction_market_agent_tooling/tools/transaction_cache.py,sha256=K5YKNL2_tR10Iw2TD9fuP-CTGpBbZtNdgbd0B_R7pjg,1814
120
120
  prediction_market_agent_tooling/tools/utils.py,sha256=jLG4nbEoIzzJiZ4RgMx4Q969Zdl0p0s63p8uET_0Fuw,6440
121
121
  prediction_market_agent_tooling/tools/web3_utils.py,sha256=3wfqNxvMn44ivweFRoeKNVb9QRtFd7kFtp7VUY5juEE,12862
122
- prediction_market_agent_tooling-0.61.1.dev483.dist-info/LICENSE,sha256=6or154nLLU6bELzjh0mCreFjt0m2v72zLi3yHE0QbeE,7650
123
- prediction_market_agent_tooling-0.61.1.dev483.dist-info/METADATA,sha256=078bYVh1iho-ehDfULc_ZXtRaQc71KnHdmJErDU_B5k,8636
124
- prediction_market_agent_tooling-0.61.1.dev483.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
125
- prediction_market_agent_tooling-0.61.1.dev483.dist-info/entry_points.txt,sha256=m8PukHbeH5g0IAAmOf_1Ahm-sGAMdhSSRQmwtpmi2s8,81
126
- prediction_market_agent_tooling-0.61.1.dev483.dist-info/RECORD,,
122
+ prediction_market_agent_tooling-0.61.1.dev484.dist-info/LICENSE,sha256=6or154nLLU6bELzjh0mCreFjt0m2v72zLi3yHE0QbeE,7650
123
+ prediction_market_agent_tooling-0.61.1.dev484.dist-info/METADATA,sha256=IpD8pQ3IicG0EyoDNg33hCkmVHBpXGKbNg-_FpdXve0,8636
124
+ prediction_market_agent_tooling-0.61.1.dev484.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
125
+ prediction_market_agent_tooling-0.61.1.dev484.dist-info/entry_points.txt,sha256=m8PukHbeH5g0IAAmOf_1Ahm-sGAMdhSSRQmwtpmi2s8,81
126
+ prediction_market_agent_tooling-0.61.1.dev484.dist-info/RECORD,,