I am attempting to add liquidity to a Uniswap v3 pool using ethers.js, but I keep running into an error. I’ve successfully created two tokens (token1 and token2) and a liquidity pool using the Uniswap V3 factory contract on a fork of the Ethereum mainnet. However, I can’t seem to add liquidity to this pool despite the pool being deployed and the tokens being approved.
Here are the details of my setup:
-
Ethers.js Version: 5.7.2
-
Uniswap v3-sdk Version: 3.9.0
-
Hardhat Version: 2.2.5
-
Network: Forked Ethereum Mainnet
Below is the code snippet I am using to add liquidity.
const { ethers } = require('ethers');
const { abi: NonfungiblePositionManagerABI } = require('@uniswap/v3-periphery/artifacts/contracts/NonfungiblePositionManager.sol/NonfungiblePositionManager.json');
const { abi: UniswapV3FactoryABI } = require('@uniswap/v3-core/artifacts/contracts/UniswapV3Factory.sol/UniswapV3Factory.json');
const provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545');
const privateKey = 'private_key_here';
const wallet = new ethers.Wallet(privateKey, provider);
const nonfungiblePositionManagerAddress = '0xC36442b4a4522E871399CD717aBDD847Ab11FE88';
const factoryAddress = '0x1F98431c8aD98523631AE4a59f267346ea31F984';
const token1Address = '0x3A906C603F080D96dc08f81CF2889dAB6FF299dE';
const token2Address = '0x18b7CBdfFA52d1e7BB992fd50f394c5b59E20B72';
const nonfungiblePositionManager = new ethers.Contract(
nonfungiblePositionManagerAddress,
NonfungiblePositionManagerABI,
wallet
);
const factory = new ethers.Contract(
factoryAddress,
UniswapV3FactoryABI,
wallet
);
async function addLiquidity() {
const token0 = token1Address.toLowerCase() < token2Address.toLowerCase() ? token1Address : token2Address;
const token1 = token1Address.toLowerCase() < token2Address.toLowerCase() ? token2Address : token1Address;
const fee = 3000;
const tickLower = -60 * 5;
const tickUpper = 60 * 5;
const amount0Desired = ethers.utils.parseUnits('10', 18);
const amount1Desired = ethers.utils.parseUnits('10', 18);
const params = {
token0,
token1,
fee,
tickLower,
tickUpper,
amount0Desired,
amount1Desired,
amount0Min: 0,
amount1Min: 0,
recipient: wallet.address,
deadline: Math.floor(Date.now() / 1000) + 1800, // 30 minutes from now
};
try {
const tx = await nonfungiblePositionManager.mint(params);
await tx.wait();
console.log("Liquidity added");
} catch (error) {
console.error('Error adding liquidity:', error);
}
}
addLiquidity();
Here is what I get :
Error adding liquidity: Error: processing response error (body="{"jsonrpc":"2.0","id":86,"error":{"code":-32603,"message":"Error: Transaction reverted without a reason string","data":{"message":"Error: Transaction reverted without a reason string","txHash":"0xaf943a8e98faec13f1665e3461fe1f5fe4cde676fd7b2d666a643bf3a9f0dca7","data":"0x"}}}", error={"code":-32603,"data":{"message":"Error: Transaction reverted without a reason string","txHash":"0xaf943a8e98faec13f1665e3461fe1f5fe4cde676fd7b2d666a643bf3a9f0dca7","data":"0x"}}, requestBody="{"method":"eth_sendRawTransaction","params":["0x02f901d7827a698202a68459682f0085022056b1a08401c9c38094c36442b4a4522e871399cd717abdd847ab11fe8880b90164883164560000000000000000000000003a906c603f080d96dc08f81cf2889dab6ff299de00000000000000000000000018b7cbdffa52d1e7bb992fd50f394c5b59e20b720000000000000000000000000000000000000000000000000000000000000bb8fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed4000000000000000000000000000000000000000000000000000000000000012c00000000000000000000000000000000000000000000003635c9adc5dea0000000000000000000000000000000000000000000000000003635c9adc5dea00000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb9226600000000000000000000000000000000000000000000000000000000667ac508c001a04ffc7c3bc5fd20f6f367fca6c5f950b0d6886af3db85e3cfeaa5ea8953cf4112a057746cc2143a4187e0f86fabfe4d424aebe730347ce6dc37d823ad3ca610e685"],"id":86,"jsonrpc":"2.0"}", requestMethod="POST", url="http://127.0.0.1:8545", code=SERVER_ERROR, version=web/5.7.1)
at Logger.makeError (uniswap/pool/node_modules/@ethersproject/logger/lib/index.js:238:21)
at Logger.throwError (uniswap/pool/node_modules/@ethersproject/logger/lib/index.js:247:20)
at uniswap/pool/node_modules/@ethersproject/web/lib/index.js:313:32
at step (uniswap/pool/node_modules/@ethersproject/web/lib/index.js:33:23)
at Object.next (uniswap/pool/node_modules/@ethersproject/web/lib/index.js:14:53)
at fulfilled (uniswap/pool/node_modules/@ethersproject/web/lib/index.js:5:58)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
reason: 'processing response error',
code: 'SERVER_ERROR',
body: '{"jsonrpc":"2.0","id":86,"error":{"code":-32603,"message":"Error: Transaction reverted without a reason string","data":{"message":"Error: Transaction reverted without a reason string","txHash":"0xaf943a8e98faec13f1665e3461fe1f5fe4cde676fd7b2d666a643bf3a9f0dca7","data":"0x"}}}',
error: Error: Error: Transaction reverted without a reason string
at getResult (uniswap/pool/node_modules/@ethersproject/providers/lib/json-rpc-provider.js:191:21)
at uniswap/pool/node_modules/@ethersproject/web/lib/index.js:356:22)
at uniswap/pool/node_modules/@ethersproject/web/lib/index.js:288:46
at step (uniswap/pool/node_modules/@ethersproject/web/lib/index.js:33:23)
at Object.next (uniswap/pool/node_modules/@ethersproject/web/lib/index.js:14:53)
at fulfilled (uniswap/pool/node_modules/@ethersproject/web/lib/index.js:5:58)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
code: -32603,
data: {
message: 'Error: Transaction reverted without a reason string',
txHash: '0xaf943a8e98faec13f1665e3461fe1f5fe4cde676fd7b2d666a643bf3a9f0dca7',
data: '0x'
}
},
requestBody: '{"method":"eth_sendRawTransaction","params":["0x02f901d7827a698202a68459682f0085022056b1a08401c9c38094c36442b4a4522e871399cd717abdd847ab11fe8880b90164883164560000000000000000000000003a906c603f080d96dc08f81cf2889dab6ff299de00000000000000000000000018b7cbdffa52d1e7bb992fd50f394c5b59e20b720000000000000000000000000000000000000000000000000000000000000bb8fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed4000000000000000000000000000000000000000000000000000000000000012c00000000000000000000000000000000000000000000003635c9adc5dea0000000000000000000000000000000000000000000000000003635c9adc5dea00000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb9226600000000000000000000000000000000000000000000000000000000667ac508c001a04ffc7c3bc5fd20f6f367fca6c5f950b0d6886af3db85e3cfeaa5ea8953cf4112a057746cc2143a4187e0f86fabfe4d424aebe730347ce6dc37d823ad3ca610e685"],"id":86,"jsonrpc":"2.0"}',
requestMethod: 'POST',
url: 'http://127.0.0.1:8545',
transaction: {
type: 2,
chainId: 31337,
nonce: 678,
maxPriorityFeePerGas: BigNumber { _hex: '0x59682f00', _isBigNumber: true },
maxFeePerGas: BigNumber { _hex: '0x022056b1a0', _isBigNumber: true },
gasPrice: null,
gasLimit: BigNumber { _hex: '0x01c9c380', _isBigNumber: true },
to: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88',
value: BigNumber { _hex: '0x00', _isBigNumber: true },
data: '0x883164560000000000000000000000003a906c603f080d96dc08f81cf2889dab6ff299de00000000000000000000000018b7cbdffa52d1e7bb992fd50f394c5b59e20b720000000000000000000000000000000000000000000000000000000000000bb8fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed4000000000000000000000000000000000000000000000000000000000000012c00000000000000000000000000000000000000000000003635c9adc5dea0000000000000000000000000000000000000000000000000003635c9adc5dea00000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb9226600000000000000000000000000000000000000000000000000000000667ac508',
accessList: [],
hash: '0xaf943a8e98faec13f1665e3461fe1f5fe4cde676fd7b2d666a643bf3a9f0dca7',
v: 1,
r: '0x4ffc7c3bc5fd20f6f367fca6c5f950b0d6886af3db85e3cfeaa5ea8953cf4112',
s: '0x57746cc2143a4187e0f86fabfe4d424aebe730347ce6dc37d823ad3ca610e685',
from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
confirmations: 0
},
transactionHash: '0xaf943a8e98faec13f1665e3461fe1f5fe4cde676fd7b2d666a643bf3a9f0dca7'
}
Transaction details: {
type: 2,
chainId: 31337,
nonce: 678,
maxPriorityFeePerGas: BigNumber { _hex: '0x59682f00', _isBigNumber: true },
maxFeePerGas: BigNumber { _hex: '0x022056b1a0', _isBigNumber: true },
gasPrice: null,
gasLimit: BigNumber { _hex: '0x01c9c380', _isBigNumber: true },
to: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88',
value: BigNumber { _hex: '0x00', _isBigNumber: true },
data: '0x883164560000000000000000000000003a906c603f080d96dc08f81cf2889dab6ff299de00000000000000000000000018b7cbdffa52d1e7bb992fd50f394c5b59e20b720000000000000000000000000000000000000000000000000000000000000bb8fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed4000000000000000000000000000000000000000000000000000000000000012c00000000000000000000000000000000000000000000003635c9adc5dea0000000000000000000000000000000000000000000000000003635c9adc5dea00000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb9226600000000000000000000000000000000000000000000000000000000667ac508',
accessList: [],
hash: '0xaf943a8e98faec13f1665e3461fe1f5fe4cde676fd7b2d666a643bf3a9f0dca7',
v: 1,
r: '0x4ffc7c3bc5fd20f6f367fca6c5f950b0d6886af3db85e3cfeaa5ea8953cf4112',
s: '0x57746cc2143a4187e0f86fabfe4d424aebe730347ce6dc37d823ad3ca610e685',
from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
confirmations: 0
}
Could you help me understand why this error occurs and how to resolve it ?
Thank you.