verbs_examples.agents.uniswap_agent.UniswapAgent#

class UniswapAgent[source]#

Agent that makes trades in Uniswap and the external market in order to make arbitrage

__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, mu: float, sigma: float, dt: float)[source]#

Initialise the Uniswap agent and create the account

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.

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_price_impact_in_external_market(env) float[source]#

Estimate Uniswap trade impact on the external market

We assume that a trade in Uniswap has transient impact on the external exchange.

Parameters:

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

Returns:

float – Transient impact

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 and returns list of transactions according to their policy.

The Uniswap agent will * Check the price in the external market and in the Uniswap pool. * Calculate the trade to do in Uniswap in order to realize a profit.

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.