verbs_examples.agents.uniswap_agent.BaseUniswapAgent#

class BaseUniswapAgent[source]#

Base agent that makes trades in Uniswap

__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)[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().

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) – Address 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

get_sqrt_price_x96_uniswap(env) int[source]#

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][source]#

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][source]#

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