Skip to content

Commit

Permalink
Auto-withdraw wxDai so the agent can keep paying for fees (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii authored Jul 2, 2024
1 parent 591863a commit e80eebc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
10 changes: 5 additions & 5 deletions prediction_market_agent_tooling/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ def bet_from_private_key(self) -> PrivateKey:
"BET_FROM_PRIVATE_KEY missing in the environment.",
)

@property
def public_key(self) -> ChecksumAddress:
return private_key_to_public_key(self.bet_from_private_key)

@property
def bet_from_address(self) -> ChecksumAddress:
"""If the SAFE is available, we always route transactions via SAFE. Otherwise we use the EOA."""
return (
self.SAFE_ADDRESS
if self.SAFE_ADDRESS
else private_key_to_public_key(self.bet_from_private_key)
)
return self.SAFE_ADDRESS if self.SAFE_ADDRESS else self.public_key

@property
def openai_api_key(self) -> SecretStr:
Expand Down
10 changes: 8 additions & 2 deletions prediction_market_agent_tooling/deploy/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
gcp_function_is_active,
gcp_resolve_api_keys_secrets,
)
from prediction_market_agent_tooling.gtypes import Probability
from prediction_market_agent_tooling.gtypes import Probability, xdai_type
from prediction_market_agent_tooling.loggers import logger
from prediction_market_agent_tooling.markets.agent_market import (
AgentMarket,
Expand All @@ -36,6 +36,7 @@
)
from prediction_market_agent_tooling.markets.omen.omen import (
redeem_from_all_user_positions,
withdraw_wxdai_to_xdai_to_keep_balance,
)
from prediction_market_agent_tooling.monitor.langfuse.langfuse_wrapper import (
LangfuseWrapper,
Expand Down Expand Up @@ -260,9 +261,14 @@ def before(self, market_type: MarketType) -> None:
"""
Executes actions that occur before bets are placed.
"""
api_keys = APIKeys()
if market_type == MarketType.OMEN:
# Omen is specific, because the user (agent) needs to manually withdraw winnings from the market.
redeem_from_all_user_positions(APIKeys())
redeem_from_all_user_positions(api_keys)
# Exchange wxdai back to xdai if the balance is getting low, so we can keep paying for fees.
withdraw_wxdai_to_xdai_to_keep_balance(
api_keys, min_required_balance=xdai_type(1), withdraw_multiplier=2
)

def process_bets(self, market_type: MarketType) -> None:
"""
Expand Down
37 changes: 37 additions & 0 deletions prediction_market_agent_tooling/markets/omen/omen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,3 +1000,40 @@ def get_binary_market_p_yes_history(market: OmenAgentMarket) -> list[Probability
)

return history


def withdraw_wxdai_to_xdai_to_keep_balance(
api_keys: APIKeys,
min_required_balance: xDai,
withdraw_multiplier: float = 1.0,
web3: Web3 | None = None,
) -> None:
"""
Keeps xDai balance above the minimum required balance by withdrawing wxDai to xDai.
Optionally, the amount to withdraw can be multiplied by the `withdraw_multiplier`, which can be useful to keep a buffer.
"""
# xDai needs to be in our wallet where we pay transaction fees, so do not check for Safe's balance.
current_balances = get_balances(api_keys.public_key, web3)

if current_balances.xdai >= min_required_balance:
logger.info(
f"Current xDai balance {current_balances.xdai} is more or equal than the required minimum balance {min_required_balance}."
)
return

need_to_withdraw = xDai(
(min_required_balance - current_balances.xdai) * withdraw_multiplier
)

if current_balances.wxdai < need_to_withdraw:
raise ValueError(
f"Current wxDai balance {current_balances.wxdai} is less than the required minimum wxDai to withdraw {need_to_withdraw}."
)

collateral_token_contract = OmenCollateralTokenContract()
collateral_token_contract.withdraw(
api_keys=api_keys, amount_wei=xdai_to_wei(need_to_withdraw), web3=web3
)
logger.info(
f"Withdrew {need_to_withdraw} wxDai to keep the balance above the minimum required balance {min_required_balance}."
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "prediction-market-agent-tooling"
version = "0.39.5"
version = "0.40.0"
description = "Tools to benchmark, deploy and monitor prediction market agents."
authors = ["Gnosis"]
readme = "README.md"
Expand Down

0 comments on commit e80eebc

Please sign in to comment.