prediction-market-agent-tooling 0.61.2.dev479__py3-none-any.whl → 0.62.0__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.
- prediction_market_agent_tooling/config.py +2 -3
- prediction_market_agent_tooling/deploy/agent.py +4 -5
- prediction_market_agent_tooling/deploy/betting_strategy.py +53 -69
- prediction_market_agent_tooling/gtypes.py +105 -27
- prediction_market_agent_tooling/jobs/jobs_models.py +5 -7
- prediction_market_agent_tooling/jobs/omen/omen_jobs.py +13 -17
- prediction_market_agent_tooling/markets/agent_market.py +96 -53
- prediction_market_agent_tooling/markets/blockchain_utils.py +1 -27
- prediction_market_agent_tooling/markets/data_models.py +40 -44
- prediction_market_agent_tooling/markets/manifold/api.py +2 -6
- prediction_market_agent_tooling/markets/manifold/data_models.py +33 -25
- prediction_market_agent_tooling/markets/manifold/manifold.py +13 -11
- prediction_market_agent_tooling/markets/market_fees.py +6 -2
- prediction_market_agent_tooling/markets/omen/data_models.py +66 -57
- prediction_market_agent_tooling/markets/omen/omen.py +222 -250
- prediction_market_agent_tooling/markets/omen/omen_contracts.py +31 -53
- prediction_market_agent_tooling/markets/omen/omen_resolving.py +7 -14
- prediction_market_agent_tooling/markets/omen/omen_subgraph_handler.py +20 -14
- prediction_market_agent_tooling/markets/polymarket/data_models.py +3 -3
- prediction_market_agent_tooling/markets/polymarket/data_models_web.py +4 -4
- prediction_market_agent_tooling/markets/polymarket/polymarket.py +3 -5
- prediction_market_agent_tooling/markets/seer/data_models.py +8 -8
- prediction_market_agent_tooling/markets/seer/seer.py +85 -71
- prediction_market_agent_tooling/markets/seer/seer_contracts.py +10 -5
- prediction_market_agent_tooling/markets/seer/seer_subgraph_handler.py +5 -2
- prediction_market_agent_tooling/monitor/monitor.py +2 -2
- prediction_market_agent_tooling/tools/_generic_value.py +261 -0
- prediction_market_agent_tooling/tools/balances.py +14 -11
- prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py +12 -10
- prediction_market_agent_tooling/tools/betting_strategies/market_moving.py +31 -24
- prediction_market_agent_tooling/tools/betting_strategies/utils.py +3 -1
- prediction_market_agent_tooling/tools/contract.py +14 -10
- prediction_market_agent_tooling/tools/cow/cow_manager.py +3 -4
- prediction_market_agent_tooling/tools/cow/cow_order.py +51 -7
- prediction_market_agent_tooling/tools/langfuse_client_utils.py +13 -1
- prediction_market_agent_tooling/tools/omen/sell_positions.py +6 -3
- prediction_market_agent_tooling/tools/safe.py +5 -6
- prediction_market_agent_tooling/tools/tokens/auto_deposit.py +36 -27
- prediction_market_agent_tooling/tools/tokens/auto_withdraw.py +4 -25
- prediction_market_agent_tooling/tools/tokens/main_token.py +2 -2
- prediction_market_agent_tooling/tools/tokens/token_utils.py +46 -0
- prediction_market_agent_tooling/tools/tokens/usd.py +79 -0
- prediction_market_agent_tooling/tools/utils.py +14 -8
- prediction_market_agent_tooling/tools/web3_utils.py +24 -41
- {prediction_market_agent_tooling-0.61.2.dev479.dist-info → prediction_market_agent_tooling-0.62.0.dist-info}/METADATA +2 -1
- {prediction_market_agent_tooling-0.61.2.dev479.dist-info → prediction_market_agent_tooling-0.62.0.dist-info}/RECORD +49 -47
- prediction_market_agent_tooling/abis/gvp2_settlement.abi.json +0 -89
- {prediction_market_agent_tooling-0.61.2.dev479.dist-info → prediction_market_agent_tooling-0.62.0.dist-info}/LICENSE +0 -0
- {prediction_market_agent_tooling-0.61.2.dev479.dist-info → prediction_market_agent_tooling-0.62.0.dist-info}/WHEEL +0 -0
- {prediction_market_agent_tooling-0.61.2.dev479.dist-info → prediction_market_agent_tooling-0.62.0.dist-info}/entry_points.txt +0 -0
@@ -1,32 +1,35 @@
|
|
1
1
|
from pydantic import BaseModel
|
2
2
|
from tenacity import retry, stop_after_attempt, wait_fixed
|
3
3
|
from web3 import Web3
|
4
|
-
from web3.types import Wei
|
5
4
|
|
6
|
-
from prediction_market_agent_tooling.gtypes import
|
5
|
+
from prediction_market_agent_tooling.gtypes import (
|
6
|
+
ChecksumAddress,
|
7
|
+
CollateralToken,
|
8
|
+
xDai,
|
9
|
+
xDaiWei,
|
10
|
+
)
|
7
11
|
from prediction_market_agent_tooling.markets.omen.omen_contracts import (
|
8
12
|
WrappedxDaiContract,
|
9
13
|
sDaiContract,
|
10
14
|
)
|
11
|
-
from prediction_market_agent_tooling.tools.web3_utils import wei_to_xdai
|
12
15
|
|
13
16
|
|
14
17
|
class Balances(BaseModel):
|
15
18
|
xdai: xDai
|
16
|
-
wxdai:
|
17
|
-
sdai:
|
19
|
+
wxdai: CollateralToken
|
20
|
+
sdai: CollateralToken
|
18
21
|
|
19
22
|
@property
|
20
|
-
def total(self) ->
|
21
|
-
return
|
23
|
+
def total(self) -> CollateralToken:
|
24
|
+
return self.xdai.as_token + self.wxdai + self.sdai
|
22
25
|
|
23
26
|
|
24
27
|
@retry(stop=stop_after_attempt(3), wait=wait_fixed(1))
|
25
28
|
def get_balances(address: ChecksumAddress, web3: Web3 | None = None) -> Balances:
|
26
29
|
if not web3:
|
27
30
|
web3 = WrappedxDaiContract().get_web3()
|
28
|
-
xdai_balance =
|
29
|
-
xdai =
|
30
|
-
wxdai =
|
31
|
-
sdai =
|
31
|
+
xdai_balance = xDaiWei(web3.eth.get_balance(address))
|
32
|
+
xdai = xdai_balance.as_xdai
|
33
|
+
wxdai = WrappedxDaiContract().balanceOf(address, web3=web3).as_token
|
34
|
+
sdai = sDaiContract().balanceOf(address, web3=web3).as_token
|
32
35
|
return Balances(xdai=xdai, wxdai=wxdai, sdai=sdai)
|
@@ -1,3 +1,4 @@
|
|
1
|
+
from prediction_market_agent_tooling.gtypes import CollateralToken, OutcomeToken
|
1
2
|
from prediction_market_agent_tooling.markets.market_fees import MarketFees
|
2
3
|
from prediction_market_agent_tooling.tools.betting_strategies.utils import SimpleBet
|
3
4
|
|
@@ -8,7 +9,7 @@ def check_is_valid_probability(probability: float) -> None:
|
|
8
9
|
|
9
10
|
|
10
11
|
def get_kelly_bet_simplified(
|
11
|
-
max_bet:
|
12
|
+
max_bet: CollateralToken,
|
12
13
|
market_p_yes: float,
|
13
14
|
estimated_p_yes: float,
|
14
15
|
confidence: float,
|
@@ -51,17 +52,17 @@ def get_kelly_bet_simplified(
|
|
51
52
|
kelly_fraction = edge / odds
|
52
53
|
|
53
54
|
# Ensure bet size is non-negative does not exceed the wallet balance
|
54
|
-
bet_size = min(kelly_fraction * max_bet, max_bet)
|
55
|
+
bet_size = CollateralToken(min(kelly_fraction * max_bet.value, max_bet.value))
|
55
56
|
|
56
57
|
return SimpleBet(direction=bet_direction, size=bet_size)
|
57
58
|
|
58
59
|
|
59
60
|
def get_kelly_bet_full(
|
60
|
-
yes_outcome_pool_size:
|
61
|
-
no_outcome_pool_size:
|
61
|
+
yes_outcome_pool_size: OutcomeToken,
|
62
|
+
no_outcome_pool_size: OutcomeToken,
|
62
63
|
estimated_p_yes: float,
|
63
64
|
confidence: float,
|
64
|
-
max_bet:
|
65
|
+
max_bet: CollateralToken,
|
65
66
|
fees: MarketFees,
|
66
67
|
) -> SimpleBet:
|
67
68
|
"""
|
@@ -97,13 +98,13 @@ def get_kelly_bet_full(
|
|
97
98
|
check_is_valid_probability(confidence)
|
98
99
|
|
99
100
|
if max_bet == 0:
|
100
|
-
return SimpleBet(direction=True, size=0)
|
101
|
+
return SimpleBet(direction=True, size=CollateralToken(0))
|
101
102
|
|
102
|
-
x = yes_outcome_pool_size
|
103
|
-
y = no_outcome_pool_size
|
103
|
+
x = yes_outcome_pool_size.value
|
104
|
+
y = no_outcome_pool_size.value
|
104
105
|
p = estimated_p_yes
|
105
106
|
c = confidence
|
106
|
-
b = max_bet
|
107
|
+
b = max_bet.value
|
107
108
|
f = 1 - fee
|
108
109
|
|
109
110
|
if x == y:
|
@@ -144,5 +145,6 @@ def get_kelly_bet_full(
|
|
144
145
|
|
145
146
|
# Clip the bet size to max_bet to account for rounding errors.
|
146
147
|
return SimpleBet(
|
147
|
-
direction=kelly_bet_amount > 0,
|
148
|
+
direction=kelly_bet_amount > 0,
|
149
|
+
size=CollateralToken(min(max_bet.value, abs(kelly_bet_amount))),
|
148
150
|
)
|
@@ -2,19 +2,22 @@ from functools import reduce
|
|
2
2
|
|
3
3
|
import numpy as np
|
4
4
|
|
5
|
-
from prediction_market_agent_tooling.gtypes import
|
5
|
+
from prediction_market_agent_tooling.gtypes import (
|
6
|
+
CollateralToken,
|
7
|
+
OutcomeToken,
|
8
|
+
Probability,
|
9
|
+
)
|
6
10
|
from prediction_market_agent_tooling.markets.omen.omen import (
|
7
11
|
MarketFees,
|
8
12
|
OmenAgentMarket,
|
9
13
|
)
|
10
14
|
from prediction_market_agent_tooling.tools.betting_strategies.utils import SimpleBet
|
11
15
|
from prediction_market_agent_tooling.tools.utils import check_not_none
|
12
|
-
from prediction_market_agent_tooling.tools.web3_utils import wei_to_xdai, xdai_to_wei
|
13
16
|
|
14
17
|
|
15
18
|
def get_market_moving_bet(
|
16
|
-
yes_outcome_pool_size:
|
17
|
-
no_outcome_pool_size:
|
19
|
+
yes_outcome_pool_size: OutcomeToken,
|
20
|
+
no_outcome_pool_size: OutcomeToken,
|
18
21
|
market_p_yes: float,
|
19
22
|
target_p_yes: float,
|
20
23
|
fees: MarketFees,
|
@@ -42,20 +45,21 @@ def get_market_moving_bet(
|
|
42
45
|
fixed_product = yes_outcome_pool_size * no_outcome_pool_size
|
43
46
|
bet_direction: bool = target_p_yes > market_p_yes
|
44
47
|
|
45
|
-
min_bet_amount = 0.0
|
46
|
-
max_bet_amount =
|
48
|
+
min_bet_amount = CollateralToken(0.0)
|
49
|
+
max_bet_amount = (
|
47
50
|
yes_outcome_pool_size + no_outcome_pool_size
|
48
|
-
) # TODO set a better upper bound
|
51
|
+
).as_token * 100 # TODO set a better upper bound
|
49
52
|
|
50
53
|
# Binary search for the optimal bet amount
|
51
54
|
for _ in range(max_iters):
|
52
55
|
bet_amount = (min_bet_amount + max_bet_amount) / 2
|
53
|
-
amounts_diff = fees.
|
56
|
+
amounts_diff = fees.get_after_fees(bet_amount)
|
57
|
+
amounts_diff_as_ot = OutcomeToken.from_token(amounts_diff)
|
54
58
|
|
55
59
|
# Initial new amounts are old amounts + equal new amounts for each outcome
|
56
|
-
yes_outcome_new_pool_size = yes_outcome_pool_size +
|
57
|
-
no_outcome_new_pool_size = no_outcome_pool_size +
|
58
|
-
new_amounts = {
|
60
|
+
yes_outcome_new_pool_size = yes_outcome_pool_size + amounts_diff_as_ot
|
61
|
+
no_outcome_new_pool_size = no_outcome_pool_size + amounts_diff_as_ot
|
62
|
+
new_amounts: dict[bool, OutcomeToken] = {
|
59
63
|
True: yes_outcome_new_pool_size,
|
60
64
|
False: no_outcome_new_pool_size,
|
61
65
|
}
|
@@ -63,15 +67,20 @@ def get_market_moving_bet(
|
|
63
67
|
# Now give away tokens at `bet_outcome_index` to restore invariant
|
64
68
|
new_product = yes_outcome_new_pool_size * no_outcome_new_pool_size
|
65
69
|
dx = (new_product - fixed_product) / new_amounts[not bet_direction]
|
66
|
-
new_amounts[bet_direction] -= dx
|
70
|
+
new_amounts[bet_direction] -= OutcomeToken(dx)
|
67
71
|
|
68
72
|
# Check that the invariant is restored
|
69
73
|
assert np.isclose(
|
70
|
-
reduce(lambda x, y: x * y, list(new_amounts.values()), 1.0),
|
74
|
+
reduce(lambda x, y: x * y.value, list(new_amounts.values()), 1.0),
|
71
75
|
float(fixed_product),
|
72
76
|
)
|
73
77
|
|
74
|
-
new_p_yes = Probability(
|
78
|
+
new_p_yes = Probability(
|
79
|
+
(
|
80
|
+
new_amounts[False]
|
81
|
+
/ sum(list(new_amounts.values()), start=OutcomeToken(0))
|
82
|
+
)
|
83
|
+
)
|
75
84
|
if abs(target_p_yes - new_p_yes) < 1e-6:
|
76
85
|
break
|
77
86
|
elif new_p_yes > target_p_yes:
|
@@ -97,33 +106,31 @@ def _sanity_check_omen_market_moving_bet(
|
|
97
106
|
using the adjusted outcome pool sizes to calculate the new p_yes.
|
98
107
|
"""
|
99
108
|
buy_amount_ = market.get_contract().calcBuyAmount(
|
100
|
-
investment_amount=
|
109
|
+
investment_amount=bet_to_check.size.as_wei,
|
101
110
|
outcome_index=market.get_outcome_index(
|
102
111
|
market.get_outcome_str_from_bool(bet_to_check.direction)
|
103
112
|
),
|
104
113
|
)
|
105
|
-
buy_amount =
|
114
|
+
buy_amount = buy_amount_.as_outcome_token
|
106
115
|
|
107
116
|
outcome_token_pool = check_not_none(market.outcome_token_pool)
|
108
117
|
yes_outcome_pool_size = outcome_token_pool[market.get_outcome_str_from_bool(True)]
|
109
118
|
no_outcome_pool_size = outcome_token_pool[market.get_outcome_str_from_bool(False)]
|
110
|
-
market_const = yes_outcome_pool_size * no_outcome_pool_size
|
119
|
+
market_const = yes_outcome_pool_size.value * no_outcome_pool_size.value
|
111
120
|
|
112
|
-
bet_to_check_size_after_fees = market.fees.
|
113
|
-
bet_to_check.size
|
114
|
-
)
|
121
|
+
bet_to_check_size_after_fees = market.fees.get_after_fees(bet_to_check.size).value
|
115
122
|
|
116
123
|
# When you buy 'yes' tokens, you add your bet size to the both pools, then
|
117
124
|
# subtract `buy_amount` from the 'yes' pool. And vice versa for 'no' tokens.
|
118
125
|
new_yes_outcome_pool_size = (
|
119
|
-
yes_outcome_pool_size
|
126
|
+
yes_outcome_pool_size.value
|
120
127
|
+ bet_to_check_size_after_fees
|
121
|
-
- float(bet_to_check.direction) * buy_amount
|
128
|
+
- float(bet_to_check.direction) * buy_amount.value
|
122
129
|
)
|
123
130
|
new_no_outcome_pool_size = (
|
124
|
-
no_outcome_pool_size
|
131
|
+
no_outcome_pool_size.value
|
125
132
|
+ bet_to_check_size_after_fees
|
126
|
-
- float(not bet_to_check.direction) * buy_amount
|
133
|
+
- float(not bet_to_check.direction) * buy_amount.value
|
127
134
|
)
|
128
135
|
new_market_const = new_yes_outcome_pool_size * new_no_outcome_pool_size
|
129
136
|
# Check the invariant is restored
|
@@ -14,6 +14,7 @@ from prediction_market_agent_tooling.gtypes import (
|
|
14
14
|
ABI,
|
15
15
|
ChainID,
|
16
16
|
ChecksumAddress,
|
17
|
+
CollateralToken,
|
17
18
|
Nonce,
|
18
19
|
TxParams,
|
19
20
|
TxReceipt,
|
@@ -153,7 +154,7 @@ class ContractBaseClass(BaseModel):
|
|
153
154
|
api_keys=api_keys,
|
154
155
|
function_name=function_name,
|
155
156
|
function_params=function_params,
|
156
|
-
tx_params={"value": amount_wei, **(tx_params or {})},
|
157
|
+
tx_params={"value": amount_wei.value, **(tx_params or {})},
|
157
158
|
timeout=timeout,
|
158
159
|
web3=web3,
|
159
160
|
)
|
@@ -243,12 +244,13 @@ class ContractERC20BaseClass(ContractBaseClass):
|
|
243
244
|
)
|
244
245
|
|
245
246
|
def balanceOf(self, for_address: ChecksumAddress, web3: Web3 | None = None) -> Wei:
|
246
|
-
balance
|
247
|
+
balance = Wei(self.call("balanceOf", [for_address], web3=web3))
|
247
248
|
return balance
|
248
249
|
|
249
|
-
def
|
250
|
-
|
251
|
-
|
250
|
+
def balance_of_in_tokens(
|
251
|
+
self, for_address: ChecksumAddress, web3: Web3 | None = None
|
252
|
+
) -> CollateralToken:
|
253
|
+
return self.balanceOf(for_address, web3=web3).as_token
|
252
254
|
|
253
255
|
|
254
256
|
class ContractDepositableWrapperERC20BaseClass(ContractERC20BaseClass):
|
@@ -355,11 +357,11 @@ class ContractERC4626BaseClass(ContractERC20BaseClass):
|
|
355
357
|
return self.withdraw(api_keys=api_keys, assets_wei=assets, web3=web3)
|
356
358
|
|
357
359
|
def convertToShares(self, assets: Wei, web3: Web3 | None = None) -> Wei:
|
358
|
-
shares
|
360
|
+
shares = Wei(self.call("convertToShares", [assets], web3=web3))
|
359
361
|
return shares
|
360
362
|
|
361
363
|
def convertToAssets(self, shares: Wei, web3: Web3 | None = None) -> Wei:
|
362
|
-
assets
|
364
|
+
assets = Wei(self.call("convertToAssets", [shares], web3=web3))
|
363
365
|
return assets
|
364
366
|
|
365
367
|
def get_asset_token_contract(
|
@@ -434,8 +436,8 @@ class ContractERC721BaseClass(ContractBaseClass):
|
|
434
436
|
web3=web3,
|
435
437
|
)
|
436
438
|
|
437
|
-
def balanceOf(self, owner: ChecksumAddress, web3: Web3 | None = None) ->
|
438
|
-
balance
|
439
|
+
def balanceOf(self, owner: ChecksumAddress, web3: Web3 | None = None) -> Wei:
|
440
|
+
balance = Wei(self.call("balanceOf", [owner], web3=web3))
|
439
441
|
return balance
|
440
442
|
|
441
443
|
def owner_of(self, token_id: int, web3: Web3 | None = None) -> ChecksumAddress:
|
@@ -604,12 +606,14 @@ def contract_implements_function(
|
|
604
606
|
|
605
607
|
|
606
608
|
def init_collateral_token_contract(
|
607
|
-
address: ChecksumAddress, web3: Web3
|
609
|
+
address: ChecksumAddress, web3: Web3 | None
|
608
610
|
) -> ContractERC20BaseClass:
|
609
611
|
"""
|
610
612
|
Checks if the given contract is Depositable ERC-20, ERC-20 or ERC-4626 and returns the appropriate class instance.
|
611
613
|
Throws an error if the contract is neither of them.
|
612
614
|
"""
|
615
|
+
web3 = web3 or RPCConfig().get_web3()
|
616
|
+
|
613
617
|
if contract_implements_function(address, "asset", web3=web3):
|
614
618
|
return ContractERC4626BaseClass(address=address)
|
615
619
|
|
@@ -19,10 +19,9 @@ from web3 import Web3
|
|
19
19
|
from web3.constants import ADDRESS_ZERO
|
20
20
|
|
21
21
|
from prediction_market_agent_tooling.config import APIKeys
|
22
|
-
from prediction_market_agent_tooling.gtypes import ChecksumAddress,
|
22
|
+
from prediction_market_agent_tooling.gtypes import ChecksumAddress, CollateralToken, Wei
|
23
23
|
from prediction_market_agent_tooling.loggers import logger
|
24
24
|
from prediction_market_agent_tooling.tools.cow.cow_order import swap_tokens_waiting
|
25
|
-
from prediction_market_agent_tooling.tools.web3_utils import xdai_to_wei
|
26
25
|
|
27
26
|
COW_ENV: Envs = "prod"
|
28
27
|
|
@@ -94,14 +93,14 @@ class CowManager:
|
|
94
93
|
|
95
94
|
@staticmethod
|
96
95
|
def swap(
|
97
|
-
amount:
|
96
|
+
amount: CollateralToken,
|
98
97
|
sell_token: ChecksumAddress,
|
99
98
|
buy_token: ChecksumAddress,
|
100
99
|
api_keys: APIKeys,
|
101
100
|
web3: Web3 | None = None,
|
102
101
|
) -> OrderMetaData:
|
103
102
|
order_metadata = swap_tokens_waiting(
|
104
|
-
amount_wei=
|
103
|
+
amount_wei=amount.as_wei,
|
105
104
|
sell_token=sell_token,
|
106
105
|
buy_token=buy_token,
|
107
106
|
api_keys=api_keys,
|
@@ -14,6 +14,8 @@ from cowdao_cowpy.order_book.generated.model import (
|
|
14
14
|
OrderMetaData,
|
15
15
|
OrderQuoteRequest,
|
16
16
|
OrderQuoteSide1,
|
17
|
+
OrderQuoteSide3,
|
18
|
+
OrderQuoteSideKindBuy,
|
17
19
|
OrderQuoteSideKindSell,
|
18
20
|
OrderStatus,
|
19
21
|
TokenAmount,
|
@@ -21,27 +23,63 @@ from cowdao_cowpy.order_book.generated.model import (
|
|
21
23
|
from eth_account.signers.local import LocalAccount
|
22
24
|
from eth_typing.evm import ChecksumAddress
|
23
25
|
from web3 import Web3
|
24
|
-
from web3.types import Wei
|
25
26
|
|
26
27
|
from prediction_market_agent_tooling.config import APIKeys
|
27
|
-
from prediction_market_agent_tooling.gtypes import ChecksumAddress, Wei
|
28
|
+
from prediction_market_agent_tooling.gtypes import ChecksumAddress, Wei
|
28
29
|
from prediction_market_agent_tooling.loggers import logger
|
29
30
|
from prediction_market_agent_tooling.tools.contract import ContractERC20OnGnosisChain
|
30
31
|
from prediction_market_agent_tooling.tools.utils import utcnow
|
31
32
|
|
32
33
|
|
34
|
+
class OrderStatusError(Exception):
|
35
|
+
pass
|
36
|
+
|
37
|
+
|
33
38
|
def get_order_book_api(env: Envs, chain: Chain) -> OrderBookApi:
|
34
39
|
chain_id = SupportedChainId(chain.value[0])
|
35
40
|
return OrderBookApi(OrderBookAPIConfigFactory.get_config(env, chain_id))
|
36
41
|
|
37
42
|
|
43
|
+
@tenacity.retry(
|
44
|
+
stop=tenacity.stop_after_attempt(3),
|
45
|
+
wait=tenacity.wait_fixed(1),
|
46
|
+
after=lambda x: logger.debug(f"get_sell_token_amount failed, {x.attempt_number=}."),
|
47
|
+
)
|
48
|
+
def get_sell_token_amount(
|
49
|
+
buy_amount: Wei,
|
50
|
+
sell_token: ChecksumAddress,
|
51
|
+
buy_token: ChecksumAddress,
|
52
|
+
chain: Chain = Chain.GNOSIS,
|
53
|
+
env: Envs = "prod",
|
54
|
+
) -> Wei:
|
55
|
+
"""
|
56
|
+
Calculate how much of the sell_token is needed to obtain a specified amount of buy_token.
|
57
|
+
"""
|
58
|
+
order_book_api = get_order_book_api(env, chain)
|
59
|
+
order_quote_request = OrderQuoteRequest(
|
60
|
+
sellToken=Address(sell_token),
|
61
|
+
buyToken=Address(buy_token),
|
62
|
+
from_=Address(
|
63
|
+
"0x1234567890abcdef1234567890abcdef12345678"
|
64
|
+
), # Just random address, doesn't matter.
|
65
|
+
)
|
66
|
+
order_side = OrderQuoteSide3(
|
67
|
+
kind=OrderQuoteSideKindBuy.buy,
|
68
|
+
buyAmountAfterFee=TokenAmount(str(buy_amount)),
|
69
|
+
)
|
70
|
+
order_quote = asyncio.run(
|
71
|
+
order_book_api.post_quote(order_quote_request, order_side)
|
72
|
+
)
|
73
|
+
return Wei(order_quote.quote.sellAmount.root)
|
74
|
+
|
75
|
+
|
38
76
|
@tenacity.retry(
|
39
77
|
stop=tenacity.stop_after_attempt(3),
|
40
78
|
wait=tenacity.wait_fixed(1),
|
41
79
|
after=lambda x: logger.debug(f"get_buy_token_amount failed, {x.attempt_number=}."),
|
42
80
|
)
|
43
81
|
def get_buy_token_amount(
|
44
|
-
|
82
|
+
sell_amount: Wei,
|
45
83
|
sell_token: ChecksumAddress,
|
46
84
|
buy_token: ChecksumAddress,
|
47
85
|
chain: Chain = Chain.GNOSIS,
|
@@ -57,14 +95,20 @@ def get_buy_token_amount(
|
|
57
95
|
)
|
58
96
|
order_side = OrderQuoteSide1(
|
59
97
|
kind=OrderQuoteSideKindSell.sell,
|
60
|
-
sellAmountBeforeFee=TokenAmount(str(
|
98
|
+
sellAmountBeforeFee=TokenAmount(str(sell_amount)),
|
61
99
|
)
|
62
100
|
order_quote = asyncio.run(
|
63
101
|
order_book_api.post_quote(order_quote_request, order_side)
|
64
102
|
)
|
65
|
-
return
|
103
|
+
return Wei(order_quote.quote.buyAmount.root)
|
66
104
|
|
67
105
|
|
106
|
+
@tenacity.retry(
|
107
|
+
stop=tenacity.stop_after_attempt(3),
|
108
|
+
wait=tenacity.wait_fixed(1),
|
109
|
+
retry=tenacity.retry_if_not_exception_type((TimeoutError, OrderStatusError)),
|
110
|
+
after=lambda x: logger.debug(f"swap_tokens_waiting failed, {x.attempt_number=}."),
|
111
|
+
)
|
68
112
|
def swap_tokens_waiting(
|
69
113
|
amount_wei: Wei,
|
70
114
|
sell_token: ChecksumAddress,
|
@@ -102,7 +146,7 @@ async def swap_tokens_waiting_async(
|
|
102
146
|
timeout: timedelta = timedelta(seconds=60),
|
103
147
|
) -> OrderMetaData:
|
104
148
|
order = await swap_tokens(
|
105
|
-
amount=amount_wei,
|
149
|
+
amount=amount_wei.value,
|
106
150
|
sell_token=sell_token,
|
107
151
|
buy_token=buy_token,
|
108
152
|
account=account,
|
@@ -125,7 +169,7 @@ async def swap_tokens_waiting_async(
|
|
125
169
|
OrderStatus.cancelled,
|
126
170
|
OrderStatus.expired,
|
127
171
|
):
|
128
|
-
raise
|
172
|
+
raise OrderStatusError(f"Order {order.uid} failed. {order.url}")
|
129
173
|
|
130
174
|
if utcnow() - start_time > timeout:
|
131
175
|
raise TimeoutError(
|
@@ -13,6 +13,9 @@ from prediction_market_agent_tooling.markets.data_models import (
|
|
13
13
|
TradeType,
|
14
14
|
)
|
15
15
|
from prediction_market_agent_tooling.markets.omen.omen import OmenAgentMarket
|
16
|
+
from prediction_market_agent_tooling.markets.omen.omen_constants import (
|
17
|
+
WRAPPED_XDAI_CONTRACT_ADDRESS,
|
18
|
+
)
|
16
19
|
from prediction_market_agent_tooling.tools.utils import DatetimeUTC
|
17
20
|
|
18
21
|
|
@@ -147,11 +150,20 @@ def get_trace_for_bet(
|
|
147
150
|
# Filter for traces with the same bet outcome and amount
|
148
151
|
traces_for_bet: list[ProcessMarketTrace] = []
|
149
152
|
for t in traces:
|
153
|
+
if (
|
154
|
+
t.market.collateral_token_contract_address_checksummed
|
155
|
+
not in WRAPPED_XDAI_CONTRACT_ADDRESS
|
156
|
+
):
|
157
|
+
# TODO: We need to compute bet amount token in USD here, but at the time of bet placement!
|
158
|
+
logger.warning(
|
159
|
+
"This currently works only for WXDAI markets, because we need to compare against USD value."
|
160
|
+
)
|
161
|
+
continue
|
150
162
|
# Cannot use exact comparison due to gas fees
|
151
163
|
if (
|
152
164
|
t.buy_trade
|
153
165
|
and t.buy_trade.outcome == bet.outcome
|
154
|
-
and np.isclose(t.buy_trade.amount.
|
166
|
+
and np.isclose(t.buy_trade.amount.value, bet.amount.value)
|
155
167
|
):
|
156
168
|
traces_for_bet.append(t)
|
157
169
|
|
@@ -1,6 +1,7 @@
|
|
1
1
|
from datetime import timedelta
|
2
2
|
|
3
3
|
from prediction_market_agent_tooling.config import APIKeys
|
4
|
+
from prediction_market_agent_tooling.gtypes import USD
|
4
5
|
from prediction_market_agent_tooling.loggers import logger
|
5
6
|
from prediction_market_agent_tooling.markets.omen.omen import OmenAgentMarket
|
6
7
|
from prediction_market_agent_tooling.markets.omen.omen_subgraph_handler import (
|
@@ -23,7 +24,9 @@ def sell_all(
|
|
23
24
|
better_address=better_address,
|
24
25
|
market_opening_after=utcnow() + timedelta(days=closing_later_than_days),
|
25
26
|
)
|
26
|
-
bets_total_usd = sum(
|
27
|
+
bets_total_usd = sum(
|
28
|
+
(b.get_collateral_amount_usd() for b in bets), start=USD.zero()
|
29
|
+
)
|
27
30
|
unique_market_urls = set(b.fpmm.url for b in bets)
|
28
31
|
starting_balance = get_balances(better_address)
|
29
32
|
new_balance = starting_balance
|
@@ -37,9 +40,9 @@ def sell_all(
|
|
37
40
|
outcome = agent_market.outcomes[bet.outcomeIndex]
|
38
41
|
current_token_balance = agent_market.get_token_balance(better_address, outcome)
|
39
42
|
|
40
|
-
if current_token_balance.
|
43
|
+
if current_token_balance.as_token <= agent_market.get_tiny_bet_amount():
|
41
44
|
logger.info(
|
42
|
-
f"Skipping bet on {bet.fpmm.url} because the actual balance is unreasonably low {current_token_balance
|
45
|
+
f"Skipping bet on {bet.fpmm.url} because the actual balance is unreasonably low {current_token_balance}."
|
43
46
|
)
|
44
47
|
continue
|
45
48
|
|
@@ -11,11 +11,10 @@ from safe_eth.eth.constants import NULL_ADDRESS
|
|
11
11
|
from safe_eth.eth.contracts import get_safe_V1_4_1_contract
|
12
12
|
from safe_eth.safe.proxy_factory import ProxyFactoryV141
|
13
13
|
from safe_eth.safe.safe import SafeV141
|
14
|
-
from web3.types import Wei
|
15
14
|
|
15
|
+
from prediction_market_agent_tooling.gtypes import Wei
|
16
16
|
from prediction_market_agent_tooling.loggers import logger
|
17
17
|
from prediction_market_agent_tooling.tools.hexbytes_custom import HexBytes
|
18
|
-
from prediction_market_agent_tooling.tools.web3_utils import wei_to_xdai
|
19
18
|
|
20
19
|
|
21
20
|
def create_safe(
|
@@ -63,10 +62,10 @@ def create_safe(
|
|
63
62
|
f"does not exist on network {ethereum_network.name}"
|
64
63
|
)
|
65
64
|
|
66
|
-
account_balance = ethereum_client.get_balance(account.address)
|
67
|
-
account_balance_xdai =
|
65
|
+
account_balance = Wei(ethereum_client.get_balance(account.address))
|
66
|
+
account_balance_xdai = account_balance.as_token
|
68
67
|
# We set a reasonable expected balance below for Safe deployment not to fail.
|
69
|
-
if account_balance_xdai < 0.01:
|
68
|
+
if account_balance_xdai.value < 0.01:
|
70
69
|
raise ValueError(
|
71
70
|
f"Client's balance is {account_balance_xdai} xDAI, too low for deploying a Safe."
|
72
71
|
)
|
@@ -108,7 +107,7 @@ def create_safe(
|
|
108
107
|
payment_token,
|
109
108
|
payment,
|
110
109
|
payment_receiver,
|
111
|
-
).build_transaction({"gas": 1, "gasPrice": Wei(1)})["data"]
|
110
|
+
).build_transaction({"gas": 1, "gasPrice": Wei(1).value})["data"]
|
112
111
|
)
|
113
112
|
|
114
113
|
proxy_factory = ProxyFactoryV141(proxy_factory_address, ethereum_client)
|