prediction-market-agent-tooling 0.63.1__py3-none-any.whl → 0.63.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.
@@ -1,3 +1,6 @@
1
+ import typing as t
2
+
3
+ from cachetools import TTLCache, cached
1
4
  from web3 import Web3
2
5
 
3
6
  from prediction_market_agent_tooling.gtypes import (
@@ -20,6 +23,27 @@ from prediction_market_agent_tooling.tools.cow.cow_order import (
20
23
  from prediction_market_agent_tooling.tools.hexbytes_custom import HexBytes
21
24
 
22
25
 
26
+ def _make_cache_key(
27
+ *args: t.Any,
28
+ token: ChecksumAddress,
29
+ collateral_exchange_amount: CollateralToken | None = None,
30
+ ) -> str:
31
+ """
32
+ Generate a unique cache key based on a token address and optional collateral token.
33
+ """
34
+
35
+ if collateral_exchange_amount is None:
36
+ return f"{token}-no_collateral"
37
+
38
+ return "-".join(
39
+ [
40
+ token,
41
+ collateral_exchange_amount.symbol,
42
+ str(collateral_exchange_amount.value),
43
+ ]
44
+ )
45
+
46
+
23
47
  class PriceManager:
24
48
  def __init__(self, seer_market: SeerMarket, seer_subgraph: SeerSubgraphHandler):
25
49
  self.seer_market = seer_market
@@ -65,6 +89,7 @@ class PriceManager:
65
89
  else:
66
90
  return None
67
91
 
92
+ @cached(TTLCache(maxsize=100, ttl=5 * 60), key=_make_cache_key)
68
93
  def get_price_for_token(
69
94
  self,
70
95
  token: ChecksumAddress,
@@ -78,7 +103,7 @@ class PriceManager:
78
103
 
79
104
  try:
80
105
  buy_token_amount = get_buy_token_amount_else_raise(
81
- amount_wei=collateral_exchange_amount.as_wei,
106
+ sell_amount=collateral_exchange_amount.as_wei,
82
107
  sell_token=self.seer_market.collateral_token_contract_address_checksummed,
83
108
  buy_token=token,
84
109
  )
@@ -46,7 +46,10 @@ from prediction_market_agent_tooling.tools.contract import (
46
46
  init_collateral_token_contract,
47
47
  to_gnosis_chain_contract,
48
48
  )
49
- from prediction_market_agent_tooling.tools.cow.cow_order import swap_tokens_waiting
49
+ from prediction_market_agent_tooling.tools.cow.cow_order import (
50
+ get_buy_token_amount_else_raise,
51
+ swap_tokens_waiting,
52
+ )
50
53
  from prediction_market_agent_tooling.tools.datetime_utc import DatetimeUTC
51
54
  from prediction_market_agent_tooling.tools.tokens.auto_deposit import (
52
55
  auto_deposit_collateral_token,
@@ -122,6 +125,23 @@ class SeerAgentMarket(AgentMarket):
122
125
  amount_outcome_tokens = bet_amount_in_tokens / price
123
126
  return OutcomeToken(amount_outcome_tokens)
124
127
 
128
+ def get_sell_value_of_outcome_token(
129
+ self, outcome: str, amount: OutcomeToken
130
+ ) -> CollateralToken:
131
+ if amount == amount.zero():
132
+ return CollateralToken.zero()
133
+
134
+ outcome_index = self.get_outcome_index(outcome=outcome)
135
+ wrapped_outcome_token = self.wrapped_tokens[outcome_index]
136
+
137
+ # We calculate how much collateral we would get back if we sold `amount` of outcome token.
138
+ value_outcome_token_in_collateral = get_buy_token_amount_else_raise(
139
+ sell_amount=amount.as_outcome_wei.as_wei,
140
+ sell_token=wrapped_outcome_token,
141
+ buy_token=self.collateral_token_contract_address_checksummed,
142
+ )
143
+ return value_outcome_token_in_collateral.as_token
144
+
125
145
  def get_outcome_str_from_bool(self, outcome: bool) -> OutcomeStr:
126
146
  outcome_translated = SeerOutcomeEnum.from_bool(outcome)
127
147
  idx = self.seer_outcomes[outcome_translated]
@@ -134,9 +154,9 @@ class SeerAgentMarket(AgentMarket):
134
154
  def get_tiny_bet_amount(self) -> CollateralToken:
135
155
  return self.get_in_token(SEER_TINY_BET_AMOUNT)
136
156
 
137
- def get_position(
157
+ def get_position_else_raise(
138
158
  self, user_id: str, web3: Web3 | None = None
139
- ) -> ExistingPosition | None:
159
+ ) -> ExistingPosition:
140
160
  """
141
161
  Fetches position from the user in a given market.
142
162
  We ignore the INVALID balances since we are only interested in binary outcomes.
@@ -169,6 +189,15 @@ class SeerAgentMarket(AgentMarket):
169
189
  amounts_ot=amounts_ot,
170
190
  )
171
191
 
192
+ def get_position(
193
+ self, user_id: str, web3: Web3 | None = None
194
+ ) -> ExistingPosition | None:
195
+ try:
196
+ return self.get_position_else_raise(user_id=user_id, web3=web3)
197
+ except Exception as e:
198
+ logger.warning(f"Could not get position for user {user_id}, exception {e}")
199
+ return None
200
+
172
201
  @staticmethod
173
202
  def get_user_id(api_keys: APIKeys) -> str:
174
203
  return OmenAgentMarket.get_user_id(api_keys)
@@ -231,6 +231,9 @@ class _GenericValue(
231
231
  def __bool__(self) -> bool:
232
232
  return bool(self.value)
233
233
 
234
+ def __hash__(self) -> int: # type: ignore[override]
235
+ return hash(tuple(sorted(self.items())))
236
+
234
237
  @classmethod
235
238
  def __get_pydantic_core_schema__(
236
239
  cls, source_type: t.Any, handler: GetCoreSchemaHandler
@@ -127,14 +127,14 @@ def get_quote(
127
127
 
128
128
 
129
129
  def get_buy_token_amount_else_raise(
130
- amount_wei: Wei,
130
+ sell_amount: Wei,
131
131
  sell_token: ChecksumAddress,
132
132
  buy_token: ChecksumAddress,
133
133
  chain: Chain = Chain.GNOSIS,
134
134
  env: Envs = "prod",
135
135
  ) -> Wei:
136
136
  order_quote = get_quote(
137
- amount_wei=amount_wei,
137
+ amount_wei=sell_amount,
138
138
  sell_token=sell_token,
139
139
  buy_token=buy_token,
140
140
  chain=chain,
@@ -50,7 +50,7 @@ def get_single_token_to_usd_rate(token_address: ChecksumAddress) -> USD:
50
50
  .as_token.value
51
51
  )
52
52
  in_wei = get_buy_token_amount_else_raise(
53
- amount_wei=CollateralToken(1).as_wei,
53
+ sell_amount=CollateralToken(1).as_wei,
54
54
  sell_token=token_address,
55
55
  buy_token=WRAPPED_XDAI_CONTRACT_ADDRESS,
56
56
  )
@@ -72,7 +72,7 @@ def get_single_usd_to_token_rate(token_address: ChecksumAddress) -> CollateralTo
72
72
  .as_token.value
73
73
  )
74
74
  in_wei = get_buy_token_amount_else_raise(
75
- amount_wei=CollateralToken(1).as_wei,
75
+ sell_amount=CollateralToken(1).as_wei,
76
76
  sell_token=WRAPPED_XDAI_CONTRACT_ADDRESS,
77
77
  buy_token=token_address,
78
78
  )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: prediction-market-agent-tooling
3
- Version: 0.63.1
3
+ Version: 0.63.3
4
4
  Summary: Tools to benchmark, deploy and monitor prediction market agents.
5
5
  Author: Gnosis
6
6
  Requires-Python: >=3.10,<3.13
@@ -62,8 +62,8 @@ prediction_market_agent_tooling/markets/polymarket/data_models_web.py,sha256=LVE
62
62
  prediction_market_agent_tooling/markets/polymarket/polymarket.py,sha256=6rc9qulPl90MxXKB55XiiWKLhjfAyG_eUzAlqpq1UIE,3339
63
63
  prediction_market_agent_tooling/markets/polymarket/utils.py,sha256=8kTeVjXPcXC6DkDvWYsZQLY7x8DS6CEp_yznSEazsNU,2037
64
64
  prediction_market_agent_tooling/markets/seer/data_models.py,sha256=FwTOq9X2iJ7r3ijtE0evl8pMSbFPm4lUwuc9m7YsMVA,6373
65
- prediction_market_agent_tooling/markets/seer/price_manager.py,sha256=NZdMXRzOiGq8n4YarOQiIGGpYTNm0XUy9x8HZWmS7Es,4836
66
- prediction_market_agent_tooling/markets/seer/seer.py,sha256=PQ685AE9BjH4QZC_zcTCGt7RNBRI6wKkXoCUvQeMDO0,14897
65
+ prediction_market_agent_tooling/markets/seer/price_manager.py,sha256=EuOe6zCmEEWXz1loXaQQOYC7ZNQ0cDJn0rZadK1dBlc,5460
66
+ prediction_market_agent_tooling/markets/seer/seer.py,sha256=_FzFGTkaOHQ2WTkUa_ba-Tc9zqw4KTmbCTrH4EmvOsU,16035
67
67
  prediction_market_agent_tooling/markets/seer/seer_contracts.py,sha256=yufEojZxLa_SQv9xhsG23wLGsyHOWD8Azm7-7E5Zn_8,2714
68
68
  prediction_market_agent_tooling/markets/seer/seer_subgraph_handler.py,sha256=KKRI493VNNAY9tR1AjzNraeH76MvDsBV6GsiLZas0_Y,9859
69
69
  prediction_market_agent_tooling/markets/seer/subgraph_data_models.py,sha256=0izxS8Mtzonfdl9UqvFVXrdj0hVzieroekXhogfZKCw,1817
@@ -76,7 +76,7 @@ prediction_market_agent_tooling/monitor/monitor.py,sha256=yvSXwV-3DSAm3vsFZnFC9j
76
76
  prediction_market_agent_tooling/monitor/monitor_app.py,sha256=-_6w_ZvQ-Ad5qaeuo7NKTXUOOZ_6OrR8jMe25BGOY4k,4615
77
77
  prediction_market_agent_tooling/monitor/monitor_settings.py,sha256=Xiozs3AsufuJ04JOe1vjUri-IAMWHjjmc2ugGGiHNH4,947
78
78
  prediction_market_agent_tooling/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
79
- prediction_market_agent_tooling/tools/_generic_value.py,sha256=0_EDybTsr3iXqPPqy2U1WMw4st_SlYaCrf2p1MZhED4,10511
79
+ prediction_market_agent_tooling/tools/_generic_value.py,sha256=pD_PI13lpPp1gFoljHwa_Lzlp-u2pu0m-Z7LcxwDM2U,10618
80
80
  prediction_market_agent_tooling/tools/balances.py,sha256=Osab21btfJDw2Y-jT_TV-KHGrseCRxcsYeW6WcOMB8E,1050
81
81
  prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py,sha256=L9yp3m_81i-y85dGZH1rYRv9YXYYt1w3RGroFAAYSbw,4873
82
82
  prediction_market_agent_tooling/tools/betting_strategies/market_moving.py,sha256=36ucOuF38bdB4rnSPNjC1qpidBo30Ff4P5Kb-7emKEQ,5469
@@ -88,7 +88,7 @@ prediction_market_agent_tooling/tools/caches/inmemory_cache.py,sha256=ZW5iI5rmjq
88
88
  prediction_market_agent_tooling/tools/caches/serializers.py,sha256=vFDx4fsPxclXp2q0sv27j4al_M_Tj9aR2JJP-xNHQXA,2151
89
89
  prediction_market_agent_tooling/tools/contract.py,sha256=1ZFp_VoqTjM8cqOfAhco2Ht0DTqakjhZpuZUrAXr28Q,21332
90
90
  prediction_market_agent_tooling/tools/costs.py,sha256=EaAJ7v9laD4VEV3d8B44M4u3_oEO_H16jRVCdoZ93Uw,954
91
- prediction_market_agent_tooling/tools/cow/cow_order.py,sha256=jWBRF5UdXjlWNn3X8BxcMXSuU3fegoZzz-NXB7sSIt8,6959
91
+ prediction_market_agent_tooling/tools/cow/cow_order.py,sha256=Kk-J3N3wTbmgdYs_XGp3nXbSi3-A17lQ61j60D_lPGg,6961
92
92
  prediction_market_agent_tooling/tools/custom_exceptions.py,sha256=Fh8z1fbwONvP4-j7AmV_PuEcoqb6-QXa9PJ9m7guMcM,93
93
93
  prediction_market_agent_tooling/tools/datetime_utc.py,sha256=8_WackjtjC8zHXrhQFTGQ6e6Fz_6llWoKR4CSFvIv9I,2766
94
94
  prediction_market_agent_tooling/tools/db/db_manager.py,sha256=GtzHH1NLl8HwqC8Z7s6eTlIQXuV0blxfaV2PeQrBnfQ,3013
@@ -117,12 +117,12 @@ prediction_market_agent_tooling/tools/tokens/auto_deposit.py,sha256=MWIRHYCN-7o2
117
117
  prediction_market_agent_tooling/tools/tokens/auto_withdraw.py,sha256=22g0SIVmLlgITpdt3kPhJOw0sU4OPeBuYk_7xCrQr9U,2491
118
118
  prediction_market_agent_tooling/tools/tokens/main_token.py,sha256=1rbwpdCusPgQIVFuo3m00nBZ_b2lCAoFVm67i-YDcEw,812
119
119
  prediction_market_agent_tooling/tools/tokens/token_utils.py,sha256=fhs-FH9m9IbzGa-30R3ZleSKLeKfLEDoJ7F5Om285Vk,1369
120
- prediction_market_agent_tooling/tools/tokens/usd.py,sha256=Qq8ofVCCMX-eo8mDlHv4gL9DV_bqHFpd63Wv5yE7cjY,3113
120
+ prediction_market_agent_tooling/tools/tokens/usd.py,sha256=yuW8iPPtcpP4eLH2nORMDAfztcq0Nv2ascSrCquF1f8,3115
121
121
  prediction_market_agent_tooling/tools/transaction_cache.py,sha256=K5YKNL2_tR10Iw2TD9fuP-CTGpBbZtNdgbd0B_R7pjg,1814
122
122
  prediction_market_agent_tooling/tools/utils.py,sha256=1xsyBBJfiEdSoMlceB2F8o2sCb6Z8-qNz11pEJFrdyE,6566
123
123
  prediction_market_agent_tooling/tools/web3_utils.py,sha256=eYCc1iWAVtqDKUPTwnMUHuYolPdwh_OTiM3-AdRgDp4,12198
124
- prediction_market_agent_tooling-0.63.1.dist-info/LICENSE,sha256=6or154nLLU6bELzjh0mCreFjt0m2v72zLi3yHE0QbeE,7650
125
- prediction_market_agent_tooling-0.63.1.dist-info/METADATA,sha256=cpCiRflWwNgAuRkyel1R4e_w_FxZMHpp_o9hjvDUHws,8689
126
- prediction_market_agent_tooling-0.63.1.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
127
- prediction_market_agent_tooling-0.63.1.dist-info/entry_points.txt,sha256=m8PukHbeH5g0IAAmOf_1Ahm-sGAMdhSSRQmwtpmi2s8,81
128
- prediction_market_agent_tooling-0.63.1.dist-info/RECORD,,
124
+ prediction_market_agent_tooling-0.63.3.dist-info/LICENSE,sha256=6or154nLLU6bELzjh0mCreFjt0m2v72zLi3yHE0QbeE,7650
125
+ prediction_market_agent_tooling-0.63.3.dist-info/METADATA,sha256=0RZTVpeWNV3EurtBs_MQ55JxJVW1EmHzuxKQsVpAHHU,8689
126
+ prediction_market_agent_tooling-0.63.3.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
127
+ prediction_market_agent_tooling-0.63.3.dist-info/entry_points.txt,sha256=m8PukHbeH5g0IAAmOf_1Ahm-sGAMdhSSRQmwtpmi2s8,81
128
+ prediction_market_agent_tooling-0.63.3.dist-info/RECORD,,