简体   繁体   中英

Trying to add liquidity on uniswap v3 via web3py

I'm trying this code on Ropsten, it but keeps failing:

contract_instance = w3.eth.contract(address="0xC36442b4a4522E871399CD717aBDD847Ab11FE88", abi=liq_ABI)

tx_hash = contract_instance.functions.mint(
    (
        '0x31F42841c2db5173425b5223809CF3A38FEde360',
        '0xc778417E063141139Fce010982780140Aa0cD5Ab',
        3000,
        49548,
        50549,
        w3.toWei(0.001,'ether'),
        w3.toWei(0.17,'ether'),
        w3.toWei(0,'ether'),
        w3.toWei(0,'ether'),
        wallet_address,
        round(time.time()) + 60*20,
    )
).buildTransaction({
    'from': wallet_address,
    'chainId': 3,
    'gas': 300000,
    'gasPrice': w3.toWei(500, 'gwei'),
    'nonce': w3.eth.getTransactionCount(wallet_address),
    'value': Web3.toWei(0, 'ether')
})
print(tx_hash)
signed_tx = w3.eth.account.signTransaction(tx_hash, private_key=wallet_key)
tx = w3.eth.sendRawTransaction(signed_tx.rawTransaction)

Failed transaction: https://ropsten.etherscan.io/tx/0xc2f3d6ffff164df331dd4b46fc65dadc5dba8f135f6e13ef1cd383a73a2d0c4b

Web3-Ethereum-Defi Python library has a function called add_liquidity for Uniswap v3 that is probably what you are looking for.

Here is some example code:

  fee = 3000
    pool1 = deploy_pool(
        web3,
        deployer,
        deployment=uniswap_v3,
        token0=weth,
        token1=usdc,
        fee=fee,
    )
    pool2 = deploy_pool(
        web3,
        deployer,
        deployment=uniswap_v3,
        token0=usdc,
        token1=dai,
        fee=fee,
    )

    # add same liquidity amount to both pools as in SDK tests
    min_tick, max_tick = get_default_tick_range(fee)
    add_liquidity(
        web3,
        deployer,
        deployment=uniswap_v3,
        pool=pool1,
        amount0=100_000,
        amount1=100_000,
        lower_tick=min_tick,
        upper_tick=max_tick,
    )
    add_liquidity(
        web3,
        deployer,
        deployment=uniswap_v3,
        pool=pool2,
        amount0=120_000,
        amount1=100_000,
        lower_tick=min_tick,
        upper_tick=max_tick,
    )

    price_helper = UniswapV3PriceHelper(uniswap_v3)

    # test get_amount_out, based on: https://github.com/Uniswap/v3-sdk/blob/1a74d5f0a31040fec4aeb1f83bba01d7c03f4870/src/entities/trade.test.ts#L394
    for slippage, expected_amount_out in [
        (0, 7004),
        (5 * 100, 6670),
        (200 * 100, 2334),
    ]:
        amount_out = price_helper.get_amount_out(
            10_000,
            [
                weth.address,
                usdc.address,
                dai.address,
            ],
            [fee, fee],
            slippage=slippage,
        )

        assert amount_out == expected_amount_out

    # test get_amount_in, based on: https://github.com/Uniswap/v3-sdk/blob/1a74d5f0a31040fec4aeb1f83bba01d7c03f4870/src/entities/trade.test.ts#L361
    for slippage, expected_amount_in in [
        (0, 15488),
        (5 * 100, 16262),
        (200 * 100, 46464),
    ]:
        amount_in = price_helper.get_amount_in(
            10_000,
            [
                weth.address,
                usdc.address,
                dai.address,
            ],
            [fee, fee],
            slippage=slippage,
        )

        assert amount_in == expected_amount_in

See the fulle example code .

More information about Uniswap v3 and Python .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM