I've seen some scams that :
- In deploy stage all tokens goes to the owner.
- The owner go to PancakeSwap and create a liquidity pool using some BNB he have and all supply of tokens.
- After some time the owner pulls the rug and clean the LP.
I need to create a more transparent and reliable token and then I think I could allow the own contract to receive some BNB (an ICO maybe?) and then decides when to create the liquidity pool.
So after renounce the contract it cannot be any way to pull back the LP. It could give the investors some peace.
I can see any cheap way to lock the LP…. it can be a solution to this question too.
So my question is: how can I use solidity to create (inside the contract) a new Liquidity Pool just the way I would create using PancakeSwap ?
Some MootRat clones have this code:
function addLiquidity(
address routerAddress,
address owner,
uint256 tokenAmount,
uint256 ethAmount
) public {
IPancakeRouter02 pancakeRouter = IPancakeRouter02(routerAddress);
// add the liquidity
pancakeRouter.addLiquidityETH{value : ethAmount}(
address(this),
tokenAmount,
0, // slippage is unavoidable
0, // slippage is unavoidable
owner,
block.timestamp + 360
);
}
But I think it is missing something…
Best Answer
Since the you (the contract) are adding ETH/Token as liquidity, your function needs to be marked as
internal
/private
. Additionally, you need to give thePancakeRouter02
an allowance to spend at leasttokenAmount
of the token.Here's a contract that mints
initialSupply
tokens, then addstokenAmount
/msg.value
as liquidity.PS: I haven't tested it
Source: https://uniswap.org/docs/v2/smart-contracts/router02/#addliquidityeth
EDIT: Decided to test if this would work - it fails on the addLiquidity function. I assume this is because the Token's methods like
_transfer
andapprove
don't actually exist outside of the contract itself. That is, theUniswapV2Factory
doesn't know anything about the token yet (except the address being passed) - there is no code deployed there yet.If you want to add liquidity in one transaction, you can instead do something like this using a
Factory
patternIn the constructor of
Token
, you can do your approval and minting. E.g. Mint tokens to theFactory
and give theFactory
an approval to therouter
using the internal function_approve