swap_with_slippage_protection

Documentation for eth_defi.uniswap_v3.swap.swap_with_slippage_protection function.

swap_with_slippage_protection(uniswap_v3_deployment, *, recipient_address, base_token, quote_token, pool_fees, intermediate_token=None, max_slippage=15, amount_in=None, amount_out=None, deadline=9223372036854775808)[source]

Helper function to prepare a swap from quote token to base token (buy base token with quote token) with price estimation and slippage protection baked in.

:ref:`Read full tutorial <uniswap-v3-swap>`_.

Example:

weth_usdc_pool_trading_fee =

# build transaction to swap from USDC to WETH
swap_func = swap_with_slippage_protection(
    uniswap_v3_deployment=uniswap_v3,
    recipient_address=hot_wallet_address,
    base_token=weth,
    quote_token=usdc,
    pool_fees=[weth_usdc_pool_trading_fee],
    amount_in=usdc_amount_to_pay,
    max_slippage=50,  # 50 bps = 0.5%
)
tx = swap_func.build_transaction(
    {
        "from": hot_wallet_address,
        "chainId": web3.eth.chain_id,
        "gas": 350_000,  # estimate max 350k gas per swap
    }
)
tx = fill_nonce(web3, tx)
gas_fees = estimate_gas_fees(web3)
apply_gas(tx, gas_fees)

signed_tx = hot_wallet.sign_transaction(tx)
tx_hash = web3.eth.send_raw_transaction(signed_tx.rawTransaction)
tx_receipt = web3.eth.wait_for_transaction_receipt(tx_hash)
assert tx_receipt.status == 1

Uniswap v3 has the same trading pair deployed multiple times as multiple pools with different fee tiers. Use DEX and trading pair search to figure out fee tiers.

TODO: Take explicit block_identifier parameter and also return the estimated amounts. This would allow to estimate historical slippages.

Parameters
  • uniswap_v3_deployment (eth_defi.uniswap_v3.deployment.UniswapV3Deployment) – an instance of UniswapV3Deployment

  • recipient_address (eth_typing.evm.HexAddress) – Recipient’s address

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

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

  • intermediate_token (Optional[web3.contract.contract.Contract]) – Intermediate token which the swap can go through

  • pool_fees (list[int]) –

    List of all pools’ trading fees in the path as raw_fee.

    Expressed as BPS * 100, or 1/1,000,000 units.

    For example if your swap is directly between two pools, e.g, WETH-USDC 5 bps, and not routed through additional pools, pool_fees would be [500].

  • amount_in (Optional[int]) – How much of the quote token we want to pay, this has to be None if amount_out is specified

  • amount_out (Optional[int]) – How much of the base token we want to receive, this has to be None if amount_in is specified

  • max_slippage (float) –

    Max slippage express in BPS.

    The default is 15 BPS (0.15%)

  • deadline (int) – Time limit of the swap transaction, by default = forever (no deadline)

Returns

Prepared swap function which can be used directly to build transaction

Return type

Callable