0

I want to write a code to put liquidity on uniswap liquidity pool, but I haven't found any toturial or example code related to this. Additionally, I found a python library to interact with uniswap named python-uniswap, it has some methods related to liquidity pools but as it doesn't have a complete example, I get confused and don't know how to use that.

Can anybody help me with python-uniswap? Or does anybody know another way to interact with uniswap pools using web3 ?

1 Answer 1

1

You can use web3py to interact with function addLiquidity in Uniswap. Here is an example.

Let assume you want to add liquidity of token A with ETH in Uniswap. Thus, the Uniswap function that you want to execute is addLiquidityETH. That function take the following input and their related definition:

function addLiquidityETH(
  address token,  // The token A address 
  uint amountTokenDesired, // Amount of token A to be added in pair with ETH
  uint amountTokenMin, // The min amount of token to be added
  uint amountETHMin, // The min amount of ETH to be added
  address to, // The address that Uniswap will transfer LP-token to
  uint deadline // Deadline timestamp to destruct the transaction of not
) external payable returns (uint amountToken, uint amountETH, uint liquidity)

Then, if you've already knew the input. You can use web3py for adding the LP as shown below.:

from web3 import Web3
import time

rpc = ''
w3 = Web3(Web3.HTTPProvider(rpc))

#
owner_address = '...'
owner_private_key = '...'
uniswap_address = '...' #Router address
token_address = '...'
uniswap_abi = '...'
token_abit = '...'

uniswapContract = w3.eth.contract(address=uniswap_address, abi=uniswap_abi)
tokenContract = w3.eth.contract(address=token_address, abi=token_abi)

amountTokenAToAddLP = 1000000000000 # Let say this number

// Approve the router can use token A
token_txn = tokenContract.functions.approve(
    uniswap_address,
    123123123123123123123123123123, //big amount of token A or any
).buildTransaction({
     'from': owner_address,
     'chainId': 97,
     'value': '0x0',
     'nonce': w3.eth.getTransactionCount(owner_address),
})

signed_txn = w3.eth.account.signTransaction(token_txn, private_key=owner_private_key)
tx_token = w3.eth.sendRawTransaction(signed_txn.rawTransaction) 

// Sleep 10s so that the txn can be minted to blockchain
time.sleep(10)

// Add LP, I choose amountMin and amountMinETH are 10 for example, with a really big timestamp deadline.
addLP_txn = uniswapContract.functions.addLiquidityETH(token_address, amount, 10, 10, owner_address, 23123123123).buildTransaction({
    'from': owner_address,
    'chainId': 97,
    'value': w3.toWei(0.001, 'ether'), // I want to add only 0.001 ETH
    'nonce': w3.eth.getTransactionCount(owner_address),
})

signed_txn = w3.eth.account.signTransaction(addLP_txn, private_key=owner_private_key)
tx_addLP = w3.eth.sendRawTransaction(signed_txn.rawTransaction) 

Then you should be able to add liquidity successfully using web3py. Here is the transaction hash that I used the previous code to test in BSC testnet: https://testnet.bscscan.com/tx/0x29ca08347d40bdfe20cc685f85ec5183364414ff66e7fdc1f94c5db00fd9819f

Hope it helps you!!!

5
  • Thanks for your help, my Your answer clarified the path for mefirend ! yeh, your answer clarified the way for me but I have a problem: I couldn't find any uniswap contract containing addLiquidityETH function on Polygon mainnet. How can I interact with uniswap pools on polygon ? Dec 28, 2023 at 12:25
  • The addLiquidityETH is only existed in Uniswap V2 and V1. Unfortunately, Uniswap only has V3 in Polygon. You can use Quickswap in Polygon for addLiquidityETH. Here is the address contract: polygonscan.com/address/…
    – tqdpham96
    Dec 28, 2023 at 14:30
  • I thank you for your previous helps 🙏🏻.This error raises while building token approving transaction: web3.exceptions.ExtraDataLengthError: The field extraData is 97 bytes, but should be 32. It is quite likely that you are connected to a POA chain. I injected the geth_poa_middleware to layer 0 of my web3 object (w3.middleware_onion.inject(geth_poa_middleware, layer=0)) , but it doesn't have any effect on result. Do you have any idea to fix that ? Jan 3 at 15:41
  • Can you provide for information, i.e., the code you use to deploy, which chain you consider, what is the RPC? so that I can help you better.
    – tqdpham96
    Jan 3 at 23:08
  • Sure, I sent an email to you ([email protected]). Thanks for your responsibility 🙏🏻 Jan 4 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.