Uniswap – How to Add Liquidity to Uniswap Pool Using Python or Web3?

pythonuniswap

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 ?

Best Answer

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!!!