Skip to content

Commit

Permalink
meh solution to accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaiton committed Aug 1, 2023
1 parent 9f33284 commit ba2efef
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
28 changes: 16 additions & 12 deletions elfpy/eth/accounts/eth_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
) # TODO: should be able to infer this from the market


class EthAgent(LocalAccount, Generic[Policy, Market, MarketAction]):
class EthAgent(Generic[Policy, Market, MarketAction]):
r"""Enact policies on smart contracts and tracks wallet state"""

def __init__(self, policy: Policy | None = None, private_key: str | None = None):
Expand All @@ -35,35 +35,35 @@ def __init__(self, policy: Policy | None = None, private_key: str | None = None)
----------
policy : Policy
Elfpy policy for producing agent actions.
If None, then a policy that executes not actions is used.
If None, then a policy that executes no actions is used.
private_key : str | None, optional
Private key for constructing the agent's blockchain wallet.
If None, then a random private key is created
If None, then a random private key is created.
"""
if policy is None:
self.policy = NoActionPolicy()
else:
self.policy = policy
if private_key is None:
account: LocalAccount = Account().create()
private_key = account._key_obj # pylint: disable=protected-access
self.account: LocalAccount = Account().create()
else:
account: LocalAccount = Account().from_key(private_key)
super().__init__(private_key, account)
self.wallet: EthWallet = EthWallet(
address=HexBytes(self.address), balance=Quantity(amount=self.policy.budget, unit=TokenType.BASE)
self.account: LocalAccount = Account().from_key(private_key)
self.wallet = EthWallet(
address=HexBytes(self.account.address),
balance=Quantity(amount=self.policy.budget, unit=TokenType.BASE),
)
super().__init__()

@property
def checksum_address(self) -> ChecksumAddress:
"""Return the checksum address of the account"""
return Web3.to_checksum_address(self.address)
return Web3.to_checksum_address(self.account.address)

@property
def _private_key(self) -> str:
def _private_key(self) -> bytes:
"""Return the private key for the agent"""
logging.warning("accessing agent private key")
return str(self._key_obj) # pylint: disable=protected-access
return bytes(self)

@property
def liquidation_trades(self) -> list[Trade[MarketAction]]:
Expand Down Expand Up @@ -120,6 +120,10 @@ def liquidation_trades(self) -> list[Trade[MarketAction]]:
)
return action_list

def sign_transaction(self, transaction_dict):
"""Calls the underlying LocalAccount method"""
return self.account.sign_transaction(transaction_dict)

def get_trades(self, market: Market) -> list[Trade[MarketAction]]:
"""Helper function for computing a agent trade
Expand Down
6 changes: 2 additions & 4 deletions elfpy/eth/accounts/eth_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class EthWallet:
Arguments
----------
address : HexBytes
The trader's address.
balance : Quantity
The base assets that held by the trader.
lp_tokens : FixedPoint
Expand Down Expand Up @@ -150,9 +148,9 @@ def update(self, wallet_deltas: WalletDeltas) -> None:
if value_or_dict is None:
continue
match key:
case ["frozen", "no_new_attribs"]:
case "frozen" | "no_new_attribs" | "borrows":
continue
case ["lp_tokens", "withdraw_shares"]:
case "lp_tokens" | "withdraw_shares":
logging.debug(
"agent #%g %s pre-trade = %.0g\npost-trade = %1g\ndelta = %1g",
self.address,
Expand Down
2 changes: 1 addition & 1 deletion examples/eth_bots/eth_bots_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_eth_bots_config() -> tuple[EnvironmentConfig, list[AgentConfig]]:
username_register_url="http://localhost:5002",
artifacts_url="http://localhost:8080",
rpc_url="http://localhost:8545",
username="changeme",
username="changem3",
)

agent_config: list[AgentConfig] = [
Expand Down
2 changes: 1 addition & 1 deletion examples/eth_bots/get_agent_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get_agent_accounts(
key_string = os.environ.get("AGENT_KEYS")
if key_string is None:
raise ValueError("AGENT_KEYS environment variable must be set")
agent_private_keys = json.loads(key_string)
agent_private_keys: list[str] = json.loads(key_string)
# get agent budgets
base_budget_string = os.environ.get("AGENT_BASE_BUDGETS")
if base_budget_string is None:
Expand Down

0 comments on commit ba2efef

Please sign in to comment.