Hey guys!
Do you know how to use UniswapV2Router02.addLiquidityETH() ?
I am trying to call it and I am getting reverts with no reason.
I am wondering what are the prerequisites to use that contract/function.
Nov 9, 2021, 7:32 PM
surely there is a reason for revert 😉
see how safemoon does it here,
line 1100 and 1106/1107 may help u
see how safemoon does it here,
line 1100 and 1106/1107 may help u
Nov 9, 2021, 8:03 PM
Thanks
Nov 9, 2021, 9:01 PM
thanks man... how do you see this piece of code?
async function approve(web3, privateKey, contract, tokenAddr, spender, value) {
const currentGasPrice = await web3.eth.getGasPrice();
const gasPrice = ethers.utils.hexlify(parseInt(currentGasPrice));
const gasNeeded = 59418; //hardcoded
const tx = {
to: tokenAddr,
gasLimit: gasNeeded,
gasPrice: gasPrice,
data: contract.methods.approve(spender, value).encodeABI(),
}
console.log('approve tx: ', tx);
const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction)
console.log(`approve tx hash: ${receipt.transactionHash}\n`);
}
async function addLiquidityETH(web3,
routerAddr, privateKey,
token, amountTokenDesired, amountTokenMin, amountETHMin, deadline)
{
const routerContract = new web3.eth.Contract(ROUTER_ABI, routerAddr);
const accountObj = await _web3.eth.accounts.privateKeyToAccount(privateKey);
const currentGasPrice = await web3.eth.getGasPrice();
const gasPrice = ethers.utils.hexlify(parseInt(currentGasPrice));
const gasNeeded = 2815124; //hardcoded
const tx = {
to: routerContract.options.address,
value: amountETHMin,
gasLimit: gasNeeded,
gasPrice: gasPrice,
data: routerContract.methods.addLiquidityETH(token, amountTokenDesired,
amountTokenMin, amountETHMin, accountObj.address, deadline).encodeABI(),
}
console.log('addLiquidityETH tx: ', tx);
const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction)
console.log(`addLiquidityETH tx hash: ${receipt.transactionHash}\n`);
}
async function approveAndAddLiquidityETH(web3,
routerAddr, privateKey,
tokenAddr, amountTokenDesired, amountTokenMin, amountETHMin, deadline)
{
const accountObj = await _web3.eth.accounts.privateKeyToAccount(privateKey);
const accountContract = new _web3.eth.Contract(ERC20_ABI, accountObj.address);
const routerContract = new web3.eth.Contract(ROUTER_ABI, routerAddr);
// _approve(address(this), address(uniswapV2Router), tokenAmount);
await approve(web3, privateKey, accountContract, tokenAddr, routerContract.options.address, amountTokenDesired);
await addLiquidityETH(web3,
routerAddr, privateKey,
tokenAddr, amountTokenDesired, amountTokenMin, amountETHMin, deadline);
}
const currentGasPrice = await web3.eth.getGasPrice();
const gasPrice = ethers.utils.hexlify(parseInt(currentGasPrice));
const gasNeeded = 59418; //hardcoded
const tx = {
to: tokenAddr,
gasLimit: gasNeeded,
gasPrice: gasPrice,
data: contract.methods.approve(spender, value).encodeABI(),
}
console.log('approve tx: ', tx);
const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction)
console.log(`approve tx hash: ${receipt.transactionHash}\n`);
}
async function addLiquidityETH(web3,
routerAddr, privateKey,
token, amountTokenDesired, amountTokenMin, amountETHMin, deadline)
{
const routerContract = new web3.eth.Contract(ROUTER_ABI, routerAddr);
const accountObj = await _web3.eth.accounts.privateKeyToAccount(privateKey);
const currentGasPrice = await web3.eth.getGasPrice();
const gasPrice = ethers.utils.hexlify(parseInt(currentGasPrice));
const gasNeeded = 2815124; //hardcoded
const tx = {
to: routerContract.options.address,
value: amountETHMin,
gasLimit: gasNeeded,
gasPrice: gasPrice,
data: routerContract.methods.addLiquidityETH(token, amountTokenDesired,
amountTokenMin, amountETHMin, accountObj.address, deadline).encodeABI(),
}
console.log('addLiquidityETH tx: ', tx);
const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction)
console.log(`addLiquidityETH tx hash: ${receipt.transactionHash}\n`);
}
async function approveAndAddLiquidityETH(web3,
routerAddr, privateKey,
tokenAddr, amountTokenDesired, amountTokenMin, amountETHMin, deadline)
{
const accountObj = await _web3.eth.accounts.privateKeyToAccount(privateKey);
const accountContract = new _web3.eth.Contract(ERC20_ABI, accountObj.address);
const routerContract = new web3.eth.Contract(ROUTER_ABI, routerAddr);
// _approve(address(this), address(uniswapV2Router), tokenAmount);
await approve(web3, privateKey, accountContract, tokenAddr, routerContract.options.address, amountTokenDesired);
await addLiquidityETH(web3,
routerAddr, privateKey,
tokenAddr, amountTokenDesired, amountTokenMin, amountETHMin, deadline);
}
Do you think my approveAndAddLiquidityETH is equivalent to SafeMoon
function addLiquidity(uint256 tokenAmount, uint256 ethAmount) private {
// approve token transfer to cover all possible scenarios
_approve(address(this), address(uniswapV2Router), tokenAmount);
// add the liquidity
uniswapV2Router.addLiquidityETH{value: ethAmount}(
address(this),
tokenAmount,
0, // slippage is unavoidable
0, // slippage is unavoidable
owner(),
block.timestamp
);
}
?
function addLiquidity(uint256 tokenAmount, uint256 ethAmount) private {
// approve token transfer to cover all possible scenarios
_approve(address(this), address(uniswapV2Router), tokenAmount);
// add the liquidity
uniswapV2Router.addLiquidityETH{value: ethAmount}(
address(this),
tokenAmount,
0, // slippage is unavoidable
0, // slippage is unavoidable
owner(),
block.timestamp
);
}
?
Guys, did anyone knows how to call UniswapV2Router02.addLiquidityETH() ?
Nov 9, 2021, 11:49 PM