swap_with_slippage_protection

Documentation for eth_defi.uniswap_v2.swap.swap_with_slippage_protection function.

swap_with_slippage_protection(uniswap_v2_deployment, *, recipient_address, base_token, quote_token, intermediate_token=None, max_slippage=0.1, amount_in=None, amount_out=None, fee=30, 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.

Example:

# build transaction to swap from USDC to WETH
swap_func = swap_with_slippage_protection(
    uniswap_v2_deployment=uniswap_v2,
    recipient_address=hot_wallet_address,
    base_token=weth,
    quote_token=usdc,
    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)

# sign and broadcast
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

# similarly we can also swap USDC->WETH->DAI
swap_func = swap_with_slippage_protection(
    uniswap_v2_deployment=uniswap_v2,
    recipient_address=hot_wallet_address,
    base_token=dai,
    quote_token=usdc,
    intermediate_token=weth,
    amount_out=dai_amount_expected,
    max_slippage=100,  # 100 bps = 1%
)
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
Parameters
  • uniswap_v2_deployment (eth_defi.uniswap_v2.deployment.UniswapV2Deployment) – Uniswap v2 deployment

  • 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

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

  • amount_in (Optional[int]) –

    How much of the quote token we want to pay, this has to be None if amount_out is specified.

    Must be in raw quote token units.

  • amount_out (Optional[int]) –

    How much of the base token we want to receive, this has to be None if amount_in is specified

    Must be in raw base token units.

  • max_slippage (float) – Max slippage express in bps, default = 0.1 bps (0.001%)

  • fee (int) – Trading fee express in bps, default = 30 bps (0.3%)

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

Returns

Bound ContractFunction that can be used to build a transaction

Return type

web3.contract.contract.ContractFunction