![](/img/trans.png)
[英]Calculating price using Web3py and Uniswap pair smart contracts: How to deal with fixed point numbers?
[英]Trying to add liquidity on uniswap v3 via web3py
我正在 Ropsten 上尝试此代码,但一直失败:
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)
交易失败: https ://ropsten.etherscan.io/tx/0xc2f3d6ffff164df331dd4b46fc65dadc5dba8f135f6e13ef1cd383a73a2d0c4b
Web3-Ethereum-Defi Python 库有一个名为add_liquidity 的 Uniswap v3函数,这可能是您正在寻找的。
这是一些示例代码:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.