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,7 +1,7 @@
|
|
1
1
|
from web3 import Web3
|
2
2
|
|
3
3
|
from prediction_market_agent_tooling.config import APIKeys
|
4
|
-
from prediction_market_agent_tooling.gtypes import
|
4
|
+
from prediction_market_agent_tooling.gtypes import USD, Wei
|
5
5
|
from prediction_market_agent_tooling.loggers import logger
|
6
6
|
from prediction_market_agent_tooling.tools.contract import (
|
7
7
|
ContractDepositableWrapperERC20BaseClass,
|
@@ -10,31 +10,46 @@ from prediction_market_agent_tooling.tools.contract import (
|
|
10
10
|
ContractERC4626BaseClass,
|
11
11
|
)
|
12
12
|
from prediction_market_agent_tooling.tools.cow.cow_order import (
|
13
|
-
|
13
|
+
get_sell_token_amount,
|
14
14
|
swap_tokens_waiting,
|
15
15
|
)
|
16
16
|
from prediction_market_agent_tooling.tools.tokens.main_token import KEEPING_ERC20_TOKEN
|
17
|
+
from prediction_market_agent_tooling.tools.tokens.usd import get_usd_in_token
|
17
18
|
from prediction_market_agent_tooling.tools.utils import should_not_happen
|
18
|
-
from prediction_market_agent_tooling.tools.web3_utils import wei_to_xdai
|
19
19
|
|
20
20
|
|
21
21
|
def auto_deposit_collateral_token(
|
22
22
|
collateral_token_contract: ContractERC20BaseClass,
|
23
|
-
|
23
|
+
collateral_amount_wei_or_usd: Wei | USD,
|
24
24
|
api_keys: APIKeys,
|
25
25
|
web3: Web3 | None = None,
|
26
|
+
surplus: float = 0.01,
|
26
27
|
) -> None:
|
28
|
+
collateral_amount_wei = (
|
29
|
+
collateral_amount_wei_or_usd
|
30
|
+
if isinstance(collateral_amount_wei_or_usd, Wei)
|
31
|
+
else get_usd_in_token(
|
32
|
+
collateral_amount_wei_or_usd, collateral_token_contract.address
|
33
|
+
).as_wei
|
34
|
+
)
|
35
|
+
# Deposit a bit more to cover any small changes in conversions.
|
36
|
+
collateral_amount_wei = collateral_amount_wei.with_fraction(surplus)
|
37
|
+
|
27
38
|
if isinstance(collateral_token_contract, ContractDepositableWrapperERC20BaseClass):
|
28
39
|
# In this case, we can use deposit function directly, no need to go through DEX.
|
29
40
|
auto_deposit_depositable_wrapper_erc20(
|
30
|
-
collateral_token_contract,
|
41
|
+
collateral_token_contract, collateral_amount_wei, api_keys, web3
|
31
42
|
)
|
32
43
|
|
33
44
|
elif isinstance(collateral_token_contract, ContractERC4626BaseClass):
|
34
|
-
auto_deposit_erc4626(
|
45
|
+
auto_deposit_erc4626(
|
46
|
+
collateral_token_contract, collateral_amount_wei, api_keys, web3
|
47
|
+
)
|
35
48
|
|
36
49
|
elif isinstance(collateral_token_contract, ContractERC20BaseClass):
|
37
|
-
auto_deposit_erc20(
|
50
|
+
auto_deposit_erc20(
|
51
|
+
collateral_token_contract, collateral_amount_wei, api_keys, web3
|
52
|
+
)
|
38
53
|
|
39
54
|
else:
|
40
55
|
should_not_happen("Unsupported ERC20 contract type.")
|
@@ -55,16 +70,16 @@ def auto_deposit_depositable_wrapper_erc20(
|
|
55
70
|
return
|
56
71
|
|
57
72
|
# If we don't have enough, we need to deposit the difference.
|
58
|
-
left_to_deposit =
|
73
|
+
left_to_deposit = amount_wei - collateral_token_balance
|
59
74
|
logger.info(
|
60
|
-
f"Depositing {
|
75
|
+
f"Depositing {left_to_deposit.as_token} {collateral_token_contract.symbol()}."
|
61
76
|
)
|
62
77
|
collateral_token_contract.deposit(api_keys, left_to_deposit, web3=web3)
|
63
78
|
|
64
79
|
|
65
80
|
def auto_deposit_erc4626(
|
66
81
|
collateral_token_contract: ContractERC4626BaseClass,
|
67
|
-
|
82
|
+
collateral_amount_wei: Wei,
|
68
83
|
api_keys: APIKeys,
|
69
84
|
web3: Web3 | None,
|
70
85
|
) -> None:
|
@@ -72,12 +87,12 @@ def auto_deposit_erc4626(
|
|
72
87
|
collateral_token_balance_in_shares = collateral_token_contract.balanceOf(
|
73
88
|
for_address=for_address, web3=web3
|
74
89
|
)
|
75
|
-
|
76
|
-
|
90
|
+
asset_amount_wei = collateral_token_contract.convertToAssets(
|
91
|
+
collateral_amount_wei, web3
|
77
92
|
)
|
78
93
|
|
79
94
|
# If we have enough shares, we don't need to deposit.
|
80
|
-
if collateral_token_balance_in_shares >=
|
95
|
+
if collateral_token_balance_in_shares >= collateral_amount_wei:
|
81
96
|
return
|
82
97
|
|
83
98
|
# If we need to deposit into erc4626, we first need to have enough of the asset token.
|
@@ -89,7 +104,7 @@ def auto_deposit_erc4626(
|
|
89
104
|
collateral_token_balance_in_assets = collateral_token_contract.convertToAssets(
|
90
105
|
collateral_token_balance_in_shares, web3
|
91
106
|
)
|
92
|
-
left_to_deposit =
|
107
|
+
left_to_deposit = asset_amount_wei - collateral_token_balance_in_assets
|
93
108
|
if (
|
94
109
|
collateral_token_contract.get_asset_token_balance(for_address, web3)
|
95
110
|
< left_to_deposit
|
@@ -108,31 +123,25 @@ def auto_deposit_erc4626(
|
|
108
123
|
|
109
124
|
def auto_deposit_erc20(
|
110
125
|
collateral_token_contract: ContractERC20BaseClass,
|
111
|
-
|
126
|
+
collateral_amount_wei: Wei,
|
112
127
|
api_keys: APIKeys,
|
113
128
|
web3: Web3 | None,
|
114
129
|
) -> None:
|
115
|
-
# How much it is in the other token (collateral token).
|
116
|
-
collateral_amount_wei = get_buy_token_amount(
|
117
|
-
amount_xdai_wei,
|
118
|
-
KEEPING_ERC20_TOKEN.address,
|
119
|
-
collateral_token_contract.address,
|
120
|
-
)
|
121
130
|
# How much do we have already in the other token (collateral token).
|
122
131
|
collateral_balance_wei = collateral_token_contract.balanceOf(
|
123
132
|
api_keys.bet_from_address
|
124
133
|
)
|
125
134
|
# Amount of collateral token remaining to get.
|
126
135
|
remaining_to_get_in_collateral_wei = max(
|
127
|
-
0, collateral_amount_wei - collateral_balance_wei
|
136
|
+
Wei(0), collateral_amount_wei - collateral_balance_wei
|
128
137
|
)
|
129
138
|
if not remaining_to_get_in_collateral_wei:
|
130
139
|
return
|
131
|
-
#
|
132
|
-
amount_to_sell_wei =
|
133
|
-
|
134
|
-
|
135
|
-
|
140
|
+
# Get of how much of the source token we need to sell in order to fill the remaining collateral amount.
|
141
|
+
amount_to_sell_wei = get_sell_token_amount(
|
142
|
+
remaining_to_get_in_collateral_wei,
|
143
|
+
sell_token=KEEPING_ERC20_TOKEN.address,
|
144
|
+
buy_token=collateral_token_contract.address,
|
136
145
|
)
|
137
146
|
# If we don't have enough of the source token.
|
138
147
|
if amount_to_sell_wei > ContractERC20OnGnosisChain(
|
@@ -7,16 +7,9 @@ from prediction_market_agent_tooling.tools.contract import (
|
|
7
7
|
ContractERC20BaseClass,
|
8
8
|
ContractERC4626BaseClass,
|
9
9
|
)
|
10
|
-
from prediction_market_agent_tooling.tools.cow.cow_order import
|
11
|
-
get_buy_token_amount,
|
12
|
-
swap_tokens_waiting,
|
13
|
-
)
|
10
|
+
from prediction_market_agent_tooling.tools.cow.cow_order import swap_tokens_waiting
|
14
11
|
from prediction_market_agent_tooling.tools.tokens.main_token import KEEPING_ERC20_TOKEN
|
15
12
|
from prediction_market_agent_tooling.tools.utils import should_not_happen
|
16
|
-
from prediction_market_agent_tooling.tools.web3_utils import (
|
17
|
-
remove_fraction,
|
18
|
-
wei_to_xdai,
|
19
|
-
)
|
20
13
|
|
21
14
|
|
22
15
|
def auto_withdraw_collateral_token(
|
@@ -24,14 +17,7 @@ def auto_withdraw_collateral_token(
|
|
24
17
|
amount_wei: Wei,
|
25
18
|
api_keys: APIKeys,
|
26
19
|
web3: Web3 | None = None,
|
27
|
-
slippage: float = 0.001,
|
28
20
|
) -> None:
|
29
|
-
# Small slippage as exact exchange rate constantly changes and we don't care about small differences.
|
30
|
-
amount_wei = remove_fraction(
|
31
|
-
amount_wei,
|
32
|
-
slippage,
|
33
|
-
)
|
34
|
-
|
35
21
|
if not amount_wei:
|
36
22
|
logger.warning(
|
37
23
|
f"Amount to withdraw is zero, skipping withdrawal of {collateral_token_contract.symbol_cached(web3)}."
|
@@ -51,7 +37,7 @@ def auto_withdraw_collateral_token(
|
|
51
37
|
):
|
52
38
|
# If the ERC4626 is backed by KEEPING_ERC20_TOKEN, we can withdraw it directly, no need to go through DEX.
|
53
39
|
logger.info(
|
54
|
-
f"Withdrawing {
|
40
|
+
f"Withdrawing {amount_wei.as_token} from {collateral_token_contract.symbol_cached(web3)} into {KEEPING_ERC20_TOKEN.symbol_cached(web3)}"
|
55
41
|
)
|
56
42
|
collateral_token_contract.withdraw_in_shares(
|
57
43
|
api_keys,
|
@@ -60,18 +46,11 @@ def auto_withdraw_collateral_token(
|
|
60
46
|
)
|
61
47
|
elif isinstance(collateral_token_contract, ContractERC20BaseClass):
|
62
48
|
logger.info(
|
63
|
-
f"Swapping {
|
49
|
+
f"Swapping {amount_wei.as_token} from {collateral_token_contract.symbol_cached(web3)} into {KEEPING_ERC20_TOKEN.symbol_cached(web3)}"
|
64
50
|
)
|
65
51
|
# Otherwise, DEX will handle the rest of token swaps.
|
66
|
-
# First, convert `amount_wei` from xDai-based value into the collateral token-based value.
|
67
|
-
collateral_amount_wei = get_buy_token_amount(
|
68
|
-
amount_wei,
|
69
|
-
KEEPING_ERC20_TOKEN.address,
|
70
|
-
collateral_token_contract.address,
|
71
|
-
)
|
72
|
-
# And then sell it.
|
73
52
|
swap_tokens_waiting(
|
74
|
-
amount_wei=
|
53
|
+
amount_wei=amount_wei,
|
75
54
|
sell_token=collateral_token_contract.address,
|
76
55
|
buy_token=KEEPING_ERC20_TOKEN.address,
|
77
56
|
api_keys=api_keys,
|
@@ -1,4 +1,4 @@
|
|
1
|
-
from prediction_market_agent_tooling.gtypes import
|
1
|
+
from prediction_market_agent_tooling.gtypes import xDai
|
2
2
|
from prediction_market_agent_tooling.markets.omen.omen_constants import (
|
3
3
|
WRAPPED_XDAI_CONTRACT_ADDRESS,
|
4
4
|
)
|
@@ -15,4 +15,4 @@ KEEPING_ERC20_TOKEN = ContractDepositableWrapperERC20OnGnosisChain(
|
|
15
15
|
address=WRAPPED_XDAI_CONTRACT_ADDRESS
|
16
16
|
)
|
17
17
|
|
18
|
-
MINIMUM_NATIVE_TOKEN_IN_EOA_FOR_FEES =
|
18
|
+
MINIMUM_NATIVE_TOKEN_IN_EOA_FOR_FEES = xDai(0.1)
|
@@ -0,0 +1,46 @@
|
|
1
|
+
from eth_typing.evm import ChecksumAddress
|
2
|
+
from web3 import Web3
|
3
|
+
|
4
|
+
from prediction_market_agent_tooling.gtypes import ChecksumAddress, Wei
|
5
|
+
from prediction_market_agent_tooling.tools.contract import (
|
6
|
+
ContractERC4626BaseClass,
|
7
|
+
init_collateral_token_contract,
|
8
|
+
to_gnosis_chain_contract,
|
9
|
+
)
|
10
|
+
from prediction_market_agent_tooling.tools.cow.cow_order import get_buy_token_amount
|
11
|
+
|
12
|
+
|
13
|
+
def convert_to_another_token(
|
14
|
+
amount: Wei,
|
15
|
+
from_token: ChecksumAddress,
|
16
|
+
to_token: ChecksumAddress,
|
17
|
+
web3: Web3 | None = None,
|
18
|
+
) -> Wei:
|
19
|
+
from_token_contract = to_gnosis_chain_contract(
|
20
|
+
init_collateral_token_contract(from_token, web3)
|
21
|
+
)
|
22
|
+
to_token_contract = to_gnosis_chain_contract(
|
23
|
+
init_collateral_token_contract(to_token, web3)
|
24
|
+
)
|
25
|
+
|
26
|
+
if from_token == to_token:
|
27
|
+
return amount
|
28
|
+
|
29
|
+
elif (
|
30
|
+
isinstance(to_token_contract, ContractERC4626BaseClass)
|
31
|
+
and to_token_contract.get_asset_token_contract().address == from_token
|
32
|
+
):
|
33
|
+
return to_token_contract.convertToShares(amount)
|
34
|
+
|
35
|
+
elif (
|
36
|
+
isinstance(from_token_contract, ContractERC4626BaseClass)
|
37
|
+
and from_token_contract.get_asset_token_contract().address == to_token
|
38
|
+
):
|
39
|
+
return from_token_contract.convertToAssets(amount)
|
40
|
+
|
41
|
+
else:
|
42
|
+
return get_buy_token_amount(
|
43
|
+
amount,
|
44
|
+
from_token,
|
45
|
+
to_token,
|
46
|
+
)
|
@@ -0,0 +1,79 @@
|
|
1
|
+
from cachetools import TTLCache, cached
|
2
|
+
from eth_typing.evm import ChecksumAddress
|
3
|
+
|
4
|
+
from prediction_market_agent_tooling.gtypes import (
|
5
|
+
USD,
|
6
|
+
ChecksumAddress,
|
7
|
+
CollateralToken,
|
8
|
+
xDai,
|
9
|
+
)
|
10
|
+
from prediction_market_agent_tooling.markets.omen.omen_constants import (
|
11
|
+
SDAI_CONTRACT_ADDRESS,
|
12
|
+
WRAPPED_XDAI_CONTRACT_ADDRESS,
|
13
|
+
)
|
14
|
+
from prediction_market_agent_tooling.tools.contract import ContractERC4626OnGnosisChain
|
15
|
+
from prediction_market_agent_tooling.tools.cow.cow_order import get_buy_token_amount
|
16
|
+
|
17
|
+
|
18
|
+
def get_usd_in_xdai(amount: USD) -> xDai:
|
19
|
+
# xDai is stable coin against USD, so for simplicity we just cast it.
|
20
|
+
return xDai(amount.value)
|
21
|
+
|
22
|
+
|
23
|
+
def get_xdai_in_usd(amount: xDai) -> USD:
|
24
|
+
# xDai is stable coin against USD, so for simplicity we just cast it.
|
25
|
+
return USD(amount.value)
|
26
|
+
|
27
|
+
|
28
|
+
def get_usd_in_token(amount: USD, token_address: ChecksumAddress) -> CollateralToken:
|
29
|
+
rate = get_single_usd_to_token_rate(token_address)
|
30
|
+
return CollateralToken(amount.value * rate.value)
|
31
|
+
|
32
|
+
|
33
|
+
def get_token_in_usd(amount: CollateralToken, token_address: ChecksumAddress) -> USD:
|
34
|
+
rate = get_single_token_to_usd_rate(token_address)
|
35
|
+
return USD(amount.value * rate.value)
|
36
|
+
|
37
|
+
|
38
|
+
# A short cache to not spam CoW and prevent timeouts, but still have relatively fresh data.
|
39
|
+
@cached(TTLCache(maxsize=100, ttl=5 * 60))
|
40
|
+
def get_single_token_to_usd_rate(token_address: ChecksumAddress) -> USD:
|
41
|
+
# (w)xDai is a stable coin against USD, so use it to estimate USD worth.
|
42
|
+
if WRAPPED_XDAI_CONTRACT_ADDRESS == token_address:
|
43
|
+
return USD(1.0)
|
44
|
+
# sDai is ERC4626 with wxDai as asset, we can take the rate directly from there instead of calling CoW.
|
45
|
+
if SDAI_CONTRACT_ADDRESS == token_address:
|
46
|
+
return USD(
|
47
|
+
ContractERC4626OnGnosisChain(address=SDAI_CONTRACT_ADDRESS)
|
48
|
+
.convertToAssets(CollateralToken(1).as_wei)
|
49
|
+
.as_token.value
|
50
|
+
)
|
51
|
+
in_wei = get_buy_token_amount(
|
52
|
+
sell_amount=CollateralToken(1).as_wei,
|
53
|
+
sell_token=token_address,
|
54
|
+
buy_token=WRAPPED_XDAI_CONTRACT_ADDRESS,
|
55
|
+
)
|
56
|
+
in_token = in_wei.as_token
|
57
|
+
return USD(in_token.value)
|
58
|
+
|
59
|
+
|
60
|
+
# A short cache to not spam CoW and prevent timeouts, but still have relatively fresh data.
|
61
|
+
@cached(TTLCache(maxsize=100, ttl=5 * 60))
|
62
|
+
def get_single_usd_to_token_rate(token_address: ChecksumAddress) -> CollateralToken:
|
63
|
+
# (w)xDai is a stable coin against USD, so use it to estimate USD worth.
|
64
|
+
if WRAPPED_XDAI_CONTRACT_ADDRESS == token_address:
|
65
|
+
return CollateralToken(1.0)
|
66
|
+
# sDai is ERC4626 with wxDai as asset, we can take the rate directly from there instead of calling CoW.
|
67
|
+
if SDAI_CONTRACT_ADDRESS == token_address:
|
68
|
+
return CollateralToken(
|
69
|
+
ContractERC4626OnGnosisChain(address=SDAI_CONTRACT_ADDRESS)
|
70
|
+
.convertToShares(CollateralToken(1).as_wei)
|
71
|
+
.as_token.value
|
72
|
+
)
|
73
|
+
in_wei = get_buy_token_amount(
|
74
|
+
sell_amount=CollateralToken(1).as_wei,
|
75
|
+
sell_token=WRAPPED_XDAI_CONTRACT_ADDRESS,
|
76
|
+
buy_token=token_address,
|
77
|
+
)
|
78
|
+
in_token = in_wei.as_token
|
79
|
+
return CollateralToken(in_token.value)
|
@@ -9,7 +9,13 @@ from pydantic import BaseModel, ValidationError
|
|
9
9
|
from scipy.optimize import newton
|
10
10
|
from scipy.stats import entropy
|
11
11
|
|
12
|
-
from prediction_market_agent_tooling.gtypes import
|
12
|
+
from prediction_market_agent_tooling.gtypes import (
|
13
|
+
CollateralToken,
|
14
|
+
DatetimeUTC,
|
15
|
+
OutcomeToken,
|
16
|
+
Probability,
|
17
|
+
SecretStr,
|
18
|
+
)
|
13
19
|
from prediction_market_agent_tooling.loggers import logger
|
14
20
|
from prediction_market_agent_tooling.markets.market_fees import MarketFees
|
15
21
|
|
@@ -182,11 +188,11 @@ def prob_uncertainty(prob: Probability) -> float:
|
|
182
188
|
|
183
189
|
|
184
190
|
def calculate_sell_amount_in_collateral(
|
185
|
-
shares_to_sell:
|
186
|
-
holdings:
|
187
|
-
other_holdings:
|
191
|
+
shares_to_sell: OutcomeToken,
|
192
|
+
holdings: OutcomeToken,
|
193
|
+
other_holdings: OutcomeToken,
|
188
194
|
fees: MarketFees,
|
189
|
-
) ->
|
195
|
+
) -> CollateralToken:
|
190
196
|
"""
|
191
197
|
Computes the amount of collateral that needs to be sold to get `shares`
|
192
198
|
amount of shares. Returns None if the amount can't be computed.
|
@@ -199,11 +205,11 @@ def calculate_sell_amount_in_collateral(
|
|
199
205
|
raise ValueError("All share args must be greater than 0")
|
200
206
|
|
201
207
|
def f(r: float) -> float:
|
202
|
-
R = (r + fees.absolute) / (1 - fees.bet_proportion)
|
208
|
+
R = OutcomeToken((r + fees.absolute) / (1 - fees.bet_proportion))
|
203
209
|
first_term = other_holdings - R
|
204
210
|
second_term = holdings + shares_to_sell - R
|
205
211
|
third_term = holdings * other_holdings
|
206
|
-
return (first_term * second_term) - third_term
|
212
|
+
return ((first_term * second_term) - third_term).value
|
207
213
|
|
208
214
|
amount_to_sell = newton(f, 0)
|
209
|
-
return float(amount_to_sell) * 0.999999 # Avoid rounding errors
|
215
|
+
return CollateralToken(float(amount_to_sell) * 0.999999) # Avoid rounding errors
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import binascii
|
2
2
|
import secrets
|
3
|
-
from typing import Any, Optional
|
3
|
+
from typing import Any, Optional
|
4
4
|
|
5
5
|
import base58
|
6
6
|
import tenacity
|
@@ -11,7 +11,7 @@ from safe_eth.eth import EthereumClient
|
|
11
11
|
from safe_eth.safe.safe import SafeV141
|
12
12
|
from web3 import Web3
|
13
13
|
from web3.constants import HASH_ZERO
|
14
|
-
from web3.types import AccessList, AccessListEntry, Nonce, TxParams, TxReceipt
|
14
|
+
from web3.types import AccessList, AccessListEntry, Nonce, TxParams, TxReceipt
|
15
15
|
|
16
16
|
from prediction_market_agent_tooling.gtypes import (
|
17
17
|
ABI,
|
@@ -23,12 +23,13 @@ from prediction_market_agent_tooling.gtypes import (
|
|
23
23
|
PrivateKey,
|
24
24
|
private_key_type,
|
25
25
|
xDai,
|
26
|
-
|
26
|
+
xDaiWei,
|
27
27
|
)
|
28
28
|
from prediction_market_agent_tooling.loggers import logger
|
29
|
+
from prediction_market_agent_tooling.tools._generic_value import _GenericValue
|
29
30
|
|
30
31
|
ONE_NONCE = Nonce(1)
|
31
|
-
ONE_XDAI =
|
32
|
+
ONE_XDAI = xDai(1)
|
32
33
|
ZERO_BYTES = HexBytes(HASH_ZERO)
|
33
34
|
NOT_REVERTED_ICASE_REGEX_PATTERN = "(?i)(?!.*reverted.*)"
|
34
35
|
|
@@ -42,17 +43,6 @@ def private_key_to_public_key(private_key: SecretStr) -> ChecksumAddress:
|
|
42
43
|
return verify_address(account.address)
|
43
44
|
|
44
45
|
|
45
|
-
def wei_to_xdai(wei: Wei) -> xDai:
|
46
|
-
return xDai(float(Web3.from_wei(wei, "ether")))
|
47
|
-
|
48
|
-
|
49
|
-
def xdai_to_wei(native: xDai) -> Wei:
|
50
|
-
return Web3.to_wei(native, "ether")
|
51
|
-
|
52
|
-
|
53
|
-
RemoveOrAddFractionAmountType = TypeVar("RemoveOrAddFractionAmountType", bound=int)
|
54
|
-
|
55
|
-
|
56
46
|
def verify_address(address: str) -> ChecksumAddress:
|
57
47
|
if not Web3.is_checksum_address(address):
|
58
48
|
raise ValueError(
|
@@ -61,26 +51,6 @@ def verify_address(address: str) -> ChecksumAddress:
|
|
61
51
|
return ChecksumAddress(HexAddress(HexStr(address)))
|
62
52
|
|
63
53
|
|
64
|
-
def remove_fraction(
|
65
|
-
amount: RemoveOrAddFractionAmountType, fraction: float
|
66
|
-
) -> RemoveOrAddFractionAmountType:
|
67
|
-
"""Removes the given fraction from the given integer-bounded amount and returns the value as an original type."""
|
68
|
-
if 0 <= fraction <= 1:
|
69
|
-
keep_percentage = 1 - fraction
|
70
|
-
return type(amount)(int(amount * keep_percentage))
|
71
|
-
raise ValueError(f"The given fraction {fraction!r} is not in the range [0, 1].")
|
72
|
-
|
73
|
-
|
74
|
-
def add_fraction(
|
75
|
-
amount: RemoveOrAddFractionAmountType, fraction: float
|
76
|
-
) -> RemoveOrAddFractionAmountType:
|
77
|
-
"""Adds the given fraction to the given integer-bounded amount and returns the value as an original type."""
|
78
|
-
if 0 <= fraction <= 1:
|
79
|
-
keep_percentage = 1 + fraction
|
80
|
-
return type(amount)(int(amount * keep_percentage))
|
81
|
-
raise ValueError(f"The given fraction {fraction!r} is not in the range [0, 1].")
|
82
|
-
|
83
|
-
|
84
54
|
def check_tx_receipt(receipt: TxReceipt) -> None:
|
85
55
|
if receipt["status"] != 1:
|
86
56
|
raise ValueError(
|
@@ -88,7 +58,20 @@ def check_tx_receipt(receipt: TxReceipt) -> None:
|
|
88
58
|
)
|
89
59
|
|
90
60
|
|
61
|
+
def unwrap_generic_value(value: Any) -> Any:
|
62
|
+
if value is None:
|
63
|
+
return None
|
64
|
+
if isinstance(value, _GenericValue):
|
65
|
+
return value.value
|
66
|
+
elif isinstance(value, list):
|
67
|
+
return [unwrap_generic_value(v) for v in value]
|
68
|
+
elif isinstance(value, dict):
|
69
|
+
return {k: unwrap_generic_value(v) for k, v in value.items()}
|
70
|
+
return value
|
71
|
+
|
72
|
+
|
91
73
|
def parse_function_params(params: Optional[list[Any] | dict[str, Any]]) -> list[Any]:
|
74
|
+
params = unwrap_generic_value(params)
|
92
75
|
if params is None:
|
93
76
|
return []
|
94
77
|
if isinstance(params, list):
|
@@ -172,8 +155,8 @@ def _prepare_tx_params(
|
|
172
155
|
# Don't retry on `reverted` messages, as they would always fail again.
|
173
156
|
# TODO: Check this, see https://github.com/gnosis/prediction-market-agent-tooling/issues/625.
|
174
157
|
# retry=tenacity.retry_if_exception_message(match=NOT_REVERTED_ICASE_REGEX_PATTERN),
|
175
|
-
wait=tenacity.wait_chain(*[tenacity.wait_fixed(n) for n in range(1,
|
176
|
-
stop=tenacity.stop_after_attempt(
|
158
|
+
wait=tenacity.wait_chain(*[tenacity.wait_fixed(n) for n in range(1, 6)]),
|
159
|
+
stop=tenacity.stop_after_attempt(5),
|
177
160
|
after=lambda x: logger.debug(
|
178
161
|
f"send_function_on_contract_tx failed, {x.attempt_number=}."
|
179
162
|
),
|
@@ -210,7 +193,7 @@ def send_function_on_contract_tx(
|
|
210
193
|
# Don't retry on `reverted` messages, as they would always fail again.
|
211
194
|
# TODO: Check this, see https://github.com/gnosis/prediction-market-agent-tooling/issues/625.
|
212
195
|
# retry=tenacity.retry_if_exception_message(match=NOT_REVERTED_ICASE_REGEX_PATTERN),
|
213
|
-
wait=tenacity.wait_chain(*[tenacity.wait_fixed(n) for n in range(1,
|
196
|
+
wait=tenacity.wait_chain(*[tenacity.wait_fixed(n) for n in range(1, 6)]),
|
214
197
|
stop=tenacity.stop_after_attempt(5),
|
215
198
|
after=lambda x: logger.debug(
|
216
199
|
f"send_function_on_contract_tx_using_safe failed, {x.attempt_number=}."
|
@@ -307,14 +290,14 @@ def send_xdai_to(
|
|
307
290
|
web3: Web3,
|
308
291
|
from_private_key: PrivateKey,
|
309
292
|
to_address: ChecksumAddress,
|
310
|
-
value:
|
293
|
+
value: xDaiWei,
|
311
294
|
data_text: Optional[str | bytes] = None,
|
312
295
|
tx_params: Optional[TxParams] = None,
|
313
296
|
timeout: int = 180,
|
314
297
|
) -> TxReceipt:
|
315
298
|
from_address = private_key_to_public_key(from_private_key)
|
316
299
|
|
317
|
-
tx_params_new: TxParams = {"value": value, "to": to_address}
|
300
|
+
tx_params_new: TxParams = {"value": value.value, "to": to_address}
|
318
301
|
if data_text is not None:
|
319
302
|
tx_params_new["data"] = (
|
320
303
|
Web3.to_bytes(text=data_text)
|
@@ -360,7 +343,7 @@ def byte32_to_ipfscidv0(hex: HexBytes) -> IPFSCIDVersion0:
|
|
360
343
|
|
361
344
|
|
362
345
|
@tenacity.retry(
|
363
|
-
wait=tenacity.wait_chain(*[tenacity.wait_fixed(n) for n in range(1,
|
346
|
+
wait=tenacity.wait_chain(*[tenacity.wait_fixed(n) for n in range(1, 6)]),
|
364
347
|
stop=tenacity.stop_after_attempt(5),
|
365
348
|
after=lambda x: logger.debug(
|
366
349
|
f"get_receipt_block_timestamp failed, {x.attempt_number=}."
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.3
|
2
2
|
Name: prediction-market-agent-tooling
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.62.0
|
4
4
|
Summary: Tools to benchmark, deploy and monitor prediction market agents.
|
5
5
|
Author: Gnosis
|
6
6
|
Requires-Python: >=3.10,<3.13
|
@@ -56,6 +56,7 @@ Requires-Dist: tabulate (>=0.9.0,<0.10.0)
|
|
56
56
|
Requires-Dist: tavily-python (>=0.5.0,<0.6.0)
|
57
57
|
Requires-Dist: tqdm (>=4.66.2,<5.0.0)
|
58
58
|
Requires-Dist: typer (>=0.9.0,<1.0.0)
|
59
|
+
Requires-Dist: types-cachetools (>=5.5.0.20240820,<6.0.0.0)
|
59
60
|
Requires-Dist: types-python-dateutil (>=2.9.0.20240906,<3.0.0.0)
|
60
61
|
Requires-Dist: types-pytz (>=2024.1.0.20240203,<2025.0.0.0)
|
61
62
|
Requires-Dist: types-requests (>=2.31.0.0,<3.0.0.0)
|