uniswap_v2.deployment

Documentation for eth_defi.uniswap_v2.deployment Python module.

Uniswap v2 and compatible DEX deployments.

Compatible exchanges include, but not limited to

  • Uniswap v2

  • Sushiswap v2

  • Pancakeswap v2 and v3

  • QuickSwap

  • TraderJoe

Under the hood we are using SushiSwap v2 contracts for the deployment.

Module Attributes

INIT_CODE_HASH_MISSING

A constant to tell we do not know or care about pair init code hash for this Uni v2 deploymeny

Functions

deploy_factory_sushi(web3, deployer)

Deploy a Uniswap V2 factory contract.

deploy_trading_pair(web3, deployer, ...)

Deploy a new trading pair on Uniswap v2.

deploy_uniswap_v2_like(web3, deployer[, ...])

Deploy v2=

fetch_deployment(web3, factory_address, ...)

Construct Uniswap deployment based on on-chain data.

mock_partial_deployment_for_analysis(web3, ...)

Create a Uniswap deployment that is only usable in trade analysis.

Classes

UniswapV2Deployment

Uniswap v2 deployment description.

INIT_CODE_HASH_MISSING = '0x01'

A constant to tell we do not know or care about pair init code hash for this Uni v2 deploymeny

class UniswapV2Deployment

Bases: object

Uniswap v2 deployment description.

web3: web3.main.Web3

The Web3 instance for which the instances of this class are bound

factory: web3.contract.contract.Contract

Factory address. Factory deploys new pair contracts and defines the exchange instance on a blockchain. See the Solidity source code.

weth: Optional[web3.contract.contract.Contract]

WETH/WBNB etc. wrapper contract address. See the Solidity source code.

If the router contract ABI has renamed this variable from WETH to something else, e.g. Trader Joe, this is left empty.

TDOO: Allow to provision the exchange with different router ABI files.

router: web3.contract.contract.Contract

Router address. New routers can be deployed to optimise trade routing and price impact. See the Solidity source code.

init_code_hash: eth_typing.encoding.HexStr

The hash of deployed pair contract code. Needed to derive new pair contract addresses. See here for more details.

PairContract: Type[web3.contract.contract.Contract]

Pair contract. Holds the token0 and token1 side of the liquidity pool. Executes actual trades. Mints/burns new liquidity provider (LP) tokens. See UniswapV2Pair smartc contract for details.

pair_for(token_a, token_b)

Calculate CREATE2 contract address for a trading pair.

Parameters
  • token_a (str) –

  • token_b (str) –

Return type

Tuple[eth_typing.evm.ChecksumAddress, eth_typing.evm.HexAddress, eth_typing.evm.HexAddress]

get_pair_contract(token_a, token_b)

Get a contract for a trading pair.

Parameters
  • token_a (str) –

  • token_b (str) –

Return type

web3.contract.contract.Contract

__init__(web3, factory, weth, router, init_code_hash, PairContract)
Parameters
  • web3 (web3.main.Web3) –

  • factory (web3.contract.contract.Contract) –

  • weth (Optional[web3.contract.contract.Contract]) –

  • router (web3.contract.contract.Contract) –

  • init_code_hash (eth_typing.encoding.HexStr) –

  • PairContract (Type[web3.contract.contract.Contract]) –

Return type

None

deploy_factory_sushi(web3, deployer)

Deploy a Uniswap V2 factory contract.

This deployment has to be the init code hash hard coded in UniswapV2Library.sol. We specifically deploy a hash compatible Sushiv2Factory. This makes the Uniswap deployment non-recompilable, as the source code files refer to the hashes of a certain compilation version.

We create a bytecode hash identical deployment by replaying the transaction that deployed Sushiv2Factory contract.

Parameters
  • web3 (web3.main.Web3) – Web3 instance

  • deployer (str) – Deployer adresss

Returns

Factory contract instance

Return type

web3.contract.contract.Contract

deploy_uniswap_v2_like(web3, deployer, give_weth=10000, init_code_hash=None)

Deploy v2=

See this StackOverflow question for commentary.

Example:

deployment = deploy_uniswap_v2_like(web3, deployer)
factory = deployment.factory
print(f"Uniswap factory is {factory.address}")
Parameters
  • web3 (web3.main.Web3) – Web3 instance

  • deployer (str) – Deployer account

  • init_code_hash – The factory code hash needed to derive pair addresses. If None use Sushiswap default.

  • give_weth – Automatically give some Wrapped ETH to the deployer. Express as ETH units.

Returns

Deployment details

Return type

eth_defi.uniswap_v2.deployment.UniswapV2Deployment

deploy_trading_pair(web3, deployer, deployment, token_a, token_b, liquidity_a, liquidity_b)

Deploy a new trading pair on Uniswap v2.

Assumes deployer has enough token balance to add the initial liquidity. The deployer will also receive LP tokens for newly added liquidity.

See UniswapV2Factory.createPair() for details.

Parameters
  • web3 (web3.main.Web3) – Web3 instance

  • deployer (str) – Deployer account

  • deployment (eth_defi.uniswap_v2.deployment.UniswapV2Deployment) – Uniswap v2 deployment

  • token_a (web3.contract.contract.Contract) – Base token of the trading pair

  • token_b (web3.contract.contract.Contract) – Quote token of the trading pair

  • liquidity_a (int) – Initial liquidity added for token_a. Set zero if no liquidity will be added.

  • liquidity_b (int) – Initial liquidity added for token_b. Set zero if no liquidity will be added.

Returns

Pair contract address

Return type

eth_typing.evm.HexAddress

fetch_deployment(web3, factory_address, router_address, init_code_hash=None, allow_different_weth_var=True)

Construct Uniswap deployment based on on-chain data.

Fetches init code hash from on-chain.

Factory does not know about routers, so they must be explicitly given.

Read more details here.

Example how to get PancakeSwap v2 deployment:

@pytest.fixture()
def pancakeswap_v2(web3) -> UniswapV2Deployment:
    deployment = fetch_deployment(
        web3,
        "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73",
        "0x10ED43C718714eb63d5aA57B78B54704E256024E",
        # Taken from https://bscscan.com/address/0xca143ce32fe78f1f7019d7d551a6402fc5350c73#readContract
        init_code_hash="0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a84f69bd5",
    )
    return deployment
Parameters
  • init_code_hash (Optional[Union[eth_typing.encoding.HexStr, str]]) – Read init code hash from the caller. If not given call pairCodeHash (SushiSwap) on the factory.

  • allow_different_weth_var – We assume Uniswap v2 ABI that has router.WETH() accessor. Some DEXes like Trader Joe do not have it, using WAVAX instead, needing a different ABI file. If set (default) ignore this error and just have None as the value for the wrapped token.

  • web3 (web3.main.Web3) –

  • factory_address (Union[eth_typing.evm.HexAddress, str]) –

  • router_address (Union[eth_typing.evm.HexAddress, str]) –

Returns

Data class representing Uniswap v3 exchange deployment

Return type

eth_defi.uniswap_v2.deployment.UniswapV2Deployment

mock_partial_deployment_for_analysis(web3, router_address)

Create a Uniswap deployment that is only usable in trade analysis.

Router and pair is all we need.

TODO: This function is likely to change / relocate.

Parameters
  • web3 (web3.main.Web3) –

  • router_address (str) –

Return type

eth_defi.uniswap_v2.deployment.UniswapV2Deployment