verbs_examples.agents.uniswap_agent.DummyUniswapAgent#

class DummyUniswapAgent[source]#

Dummy uniswap agent used for cache generation

Uniswap agent that queries the EVM database for a wide range of Uniswap price ticks. Useful to initialise the cache of a simulation

__init__(env, i: int, swap_router_abi, uniswap_pool_abi, quoter_abi, fee: int, swap_router_address: bytes, uniswap_pool_address: bytes, quoter_address: bytes, token_a_address: bytes, token_b_address: bytes, sim_n_steps: int, **kwargs)[source]#

Initialise the Uniswap agent and create the corresponding account in the EVM.

The agent stores the ABIs of the Uniswap contracts and the token contracts that they will be interacting with. ABIs are previously loaded using the function verbs.abi.load_abi().

The agent also has access to an external market, modelled by a Gbm, that is set as an attribute of the agents.

Notes

This agent should only be used in a simulation to initialise the Cache of the EVM database. The drift and the volatility of the external market are artificially calibrated in order for the agent to explore a wide range of Uniswap price ticks and thus find out the right storage slots to be saved in the Cache.

Parameters:
  • env (verbs.types.Env) – Simulation environment

  • i (int) – Agent index in the simulation

  • swap_router_abi (type) – abi of the Uniswap v3 SwapRouter contract

  • uniswap_pool_abi (type) – abi of the Uniswap v3 pool contract

  • quoter_abi (type) – abi of the Uniswap v3 QuoterV2 contract

  • fee (int) – Fee tier of the Uniswap v3 pool for the pair (token_a, token_b)

  • swap_router_address (bytes) – Address of the SwapRouter contract

  • uniswap_pool_address (bytes) – Addres of Uniswap v3 pool for the pair (token_a, token_b)

  • quoter_address (bytes) – Address of the QuoterV2 contract

  • token_a_address (bytes) – Address of token_a

  • token_b_address (bytes) – Address of token_b

  • mu (float) – Drift of the Gbm

  • sigma (float) – Volatility of the Gbm

  • dt (float) – Time step of time discretisation for the Gbm solver.

get_sqrt_price_x96_uniswap(env) int#

Get sqrt price from uniswap pool

Uniswap returns price of token0 in terms of token1

Notes

Uniswap sorts of token0 and token1 by their addresses.

Parameters:

env (verbs.types.Env) – Simulation environment

Returns:

int – Square root of the price times 296 of token0 in terms of token1

get_swap_size_to_decrease_uniswap_price(env, sqrt_target_price_x96: int, sqrt_price_uniswap_x96: int, liquidity: int, exact: bool = True) Tuple[bytes, bytes, bytes, bool, int | None, int | None, int | None]#

Get swap parameters to match target price

Gets the swap parameters so that, after the swap, the price in Uniswap is the same as the target price. We know that in Uniswap v3 (or v2), we have \(L = \frac{\Delta y}{\Delta \sqrt{P}}\) where y is the numeraire (in our case the debt asset), and P is the price of the collateral in terms of the numeraire.

If there is a tick range and exact=True, the agent performs an iterative calculation to find the right trade.

References

  1. https://atiselsts.github.io/pdfs/uniswap-v3-liquidity-math.pdf

Parameters:
  • env (verbs.types.Env) – Simulation environment

  • sqrt_target_price_x96 (int) – Sqrt of target price times 296

  • sqrt_price_uniswap_x96 (int) – Sqrt of current uniswap price times 296

  • liquidity (int) – Liquidity of Uniswap in the current tick range

  • exact (bool) – Boolean indicating whether to perform the iterative calculation to find the right trade.

Returns:

verbs.types.Transaction – Trade transaction

get_swap_size_to_increase_uniswap_price(env, sqrt_target_price_x96: int, sqrt_price_uniswap_x96: int, liquidity: int, exact: bool = True) Tuple[bytes, bytes, bytes, bool, int | None, int | None, int | None]#

Get swap parameters to match target price

Gets the swap parameters so that, after the swap, the price in Uniswap is the same as the target price. We know that in Uniswap v2 (or v3 if there is not a tick range change), we have \(L = \frac{\Delta y}{\Delta \sqrt{P}}\) where y is the numeraire (in our case the debt asset), and P is the price of the collateral in terms of the numeraire.

If there is a tick range and exact=True, the agent performs an iterative calculation to find the right trade.

References

  1. https://atiselsts.github.io/pdfs/uniswap-v3-liquidity-math.pdf

Parameters:
  • env (verbs.types.Env) – Simulation environment

  • sqrt_target_price_x96 (int) – Sqrt of target price times 296

  • sqrt_price_uniswap_x96 (int) – Sqrt of current uniswap price times 296

  • liquidity (int) – Liquidity of Uniswap in the current tick range

  • exact (bool) – Boolean indicating whether to perform the iterative calculation to find the right trade.

Returns:

verbs.types.Transaction – Trade transaction

record(env) Tuple[float, float][source]#

Record the state of the agent

This method is called at the end of each step for all agents. It should return any data to be recorded over the course of the simulation.

Parameters:

env (verbs.types.Env) – Network/EVM that the simulation interacts with.

Returns:

tuple[float, float] – Tuple containing: - Price in Uniswap of token0 in terms of token1 - Price in the external market of token0 in terms of token1

update(rng: Generator, env) List[Tuple[bytes, bytes, bytes, bool, int | None, int | None, int | None]][source]#

Update the state of the agent

Makes an exploratory update by manually changing the drift of the external market.

Parameters:
  • rng (np.random.Generator) – Numpy random generator, used for any random sampling to ensure determinism of the simulation.

  • env (verbs.types.Env) – Network/EVM that the simulation interacts with.

Returns:

list – List of transactions to be processed in the next block of the simulation. This can be an empty list if the agent is not submitting any transactions.