dojo.agents

This module contains the agents that can be used in the Dojo environment.

class AAVEv3Agent(dojo.agents.BaseAgent):

The BaseAgent class is the base class for AAVEv3 agents.

Agents in UniswapV3 environments can inherit from BaseAgent instead. This is a placeholder for future helper functions to go into.

class BaseAgent(abc.ABC):

The BaseAgent class is the base class for all agents.

The agents can be viewed as part of the environment in that they are only responsible for handling their own state given new data, and do not make any decisions on how to act in the environment. They are effectively a data wrapper around the on-chain brownie accounts. You should override the reward() method to define the reward generating function for your agent.

BaseAgent( initial_portfolio: dict[str, decimal.Decimal], name: Optional[str] = None)

Initialize the agent.

Parameters
  • initial_portfolio: initial asset portfolio to load at reset. e.g. {"ETH": Decimal(10), "USDC": Decimal(1000)}
  • name: optional name for the agent, if not set will use the class name.
def set_backend(self, backend: dojo.network.BaseBackend) -> None:

Set the agent backend.

def set_id(self, id: int) -> None:

Set the agent ID.

def cache( self, token_data: dict[str, dict[datetime.datetime, decimal.Decimal]]) -> None:

Cache token price data.

def add_asset(self, asset: str) -> None:

Need to keep track of which assets the agent has.

def add_nft(self, token: str, token_id: int) -> None:

Need to keep track of which NFTs the agent has.

def create_web3_account(self) -> None:

Create an on-chain account associated with this agent.

def quantity(self, token: str) -> decimal.Decimal:

Get the agent quantity of a token.

:token: The token symbol or address.

def portfolio(self) -> dict[str, decimal.Decimal]:

Get the agent portfolio.

def erc20_portfolio(self) -> dict[str, decimal.Decimal]:

Get the agent portfolio of ERC20 tokens.

def erc721_portfolio(self) -> dict[str, list[int]]:

Get the agent portfolio of ERC721 NFTs.

def wealth(self, date: datetime.datetime) -> decimal.Decimal:

Get the agent wealth in $.

def erc20_wealth(self, date: datetime.datetime) -> decimal.Decimal:

Get the agent wealth of ERC20 tokens in $.

def fund_erc20( self, token: str, source: str, quantity: Union[decimal.Decimal, int]) -> None:

Transfer quantity ERC20 tokens from the source address to the agent.

def fund_erc721(self, token: str, token_id: int, source: str) -> None:

Transfer token_id NFT from the source address to the agent.

def fund_eth(self, quantity: decimal.Decimal) -> None:

Transfer quantity ETH to the agent.

def setup(self) -> None:

Setup the agent on-chain account and initialize the token portfolio.

Raises
  • NotImplementedError: if backend type is not recognized.
def setup_live(self) -> None:

Setup the agent on-chain account and initialize the token portfolio.

def approve(self, grantee: str, token: str) -> web3.types.PendingTx:

Approve a grantee to spend ERC20 tokens on the agent's behalf.

Parameters
  • grantee: the grantee contract name.
  • token: the token symbol.
def done(self) -> bool:

Get the agent done.

def info(self) -> dict[typing.Any, typing.Any]:

Get the agent info.

@abstractmethod
def reward(self, obs: ~Observation) -> float:

Get the agent reward.

Parameters
  • obs: The observation from the environment.

There are many inbuilt methods and data structures that can be used to calculate the reward: - self.wealth(): agent wealth. - self.portfolio(): agent portfolio. - self.erc20_portfolio(): agent ERC20 portfolio. - self.erc721_portfolio(): agent ERC721 portfolio. - self.erc20_wealth(): agent ERC20 wealth.

class MarketAgent(dojo.agents.BaseAgent):

This agent executes the actions given by the market impact model.

MarketAgent()

Initialize the agent.

def set_obs(self, obs: dojo.observations.BaseObservation) -> None:

Set the observation for the agent.

Parameters
  • obs: The observation from the environment.
def setup(self) -> None:

Setup the agent.

def setup_live(self) -> None:

TODO.

def reward(self, obs: dojo.observations.BaseObservation) -> float:

No reward for replay agent.

Parameters
  • obs: The observation from the environment.
class DummyAgent(dojo.agents.BaseAgent):

A placeholder agent that does nothing.

DummyAgent( initial_portfolio: Optional[dict[str, decimal.Decimal]] = None, name: str = 'DummyAgent')

Initialize the agent.

def reward(self, obs: dojo.observations.BaseObservation) -> float:

No reward for the dummy agent.

class UniswapV3Agent(dojo.agents.BaseAgent):

The BaseAgent class is the base class for UniswapV3 agents.

Agents in UniswapV3 environments can inherit from BaseAgent instead, but will be missing certain helper methods.

def get_liquidity_ownership_tokens(self) -> list[int]:

Get the list of erc721 IDs representing our liquidity positions.