Development of Auto-Listing System on DEX at Market Cap Threshold
Task: A token is sold in presale, and when a certain amount is reached (hardcap or softcap), liquidity automatically lists on Uniswap or similar DEX — without manual team intervention. This eliminates human error and rug pull risk: the team cannot "delay" adding liquidity or add it on unfavorable terms.
Mechanics: How It Works
The presale smart contract holds collected funds (ETH or USDC) in escrow. Upon condition fulfillment (target reached) — automatically calls Uniswap Router to create a pool and add liquidity. LP tokens (proof of liquidity) are sent to a lock contract or burned, preventing liquidity withdrawal.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/access/Ownable2Step.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
interface IUniswapV2Router02 {
function addLiquidityETH(
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external payable returns (uint amountToken, uint amountETH, uint liquidity);
function factory() external pure returns (address);
}
interface IUniswapV2Factory {
function createPair(address tokenA, address tokenB) external returns (address pair);
function getPair(address tokenA, address tokenB) external view returns (address pair);
}
contract AutoListingPresale is Ownable2Step, ReentrancyGuard {
using SafeERC20 for IERC20;
IERC20 public immutable token;
IUniswapV2Router02 public immutable router;
uint256 public immutable softCap; // minimum for presale success
uint256 public immutable hardCap; // maximum collection
uint256 public immutable tokenPrice; // wei per token (18 decimals)
uint256 public immutable listingPercent; // % of raised ETH for liquidity
uint256 public immutable listingTokenPercent; // % of tokens for liquidity
uint256 public immutable saleEnd;
uint256 public totalRaised;
bool public finalized;
bool public listed;
address public liquidityPair;
mapping(address => uint256) public contributions;
event Contribution(address indexed contributor, uint256 ethAmount);
event Finalized(bool success, uint256 totalRaised);
event ListedOnDEX(address pair, uint256 ethLiquidity, uint256 tokenLiquidity);
event Refunded(address indexed contributor, uint256 amount);
constructor(
address _token,
address _router,
uint256 _softCap,
uint256 _hardCap,
uint256 _tokenPrice,
uint256 _listingPercent,
uint256 _listingTokenPercent,
uint256 _saleEndTimestamp,
address _owner
) Ownable2Step() {
token = IERC20(_token);
router = IUniswapV2Router02(_router);
softCap = _softCap;
hardCap = _hardCap;
tokenPrice = _tokenPrice;
listingPercent = _listingPercent;
listingTokenPercent = _listingTokenPercent;
saleEnd = _saleEndTimestamp;
_transferOwnership(_owner);
}
// Participation in presale
receive() external payable {
_contribute(msg.sender, msg.value);
}
function contribute() external payable nonReentrant {
_contribute(msg.sender, msg.value);
}
function _contribute(address contributor, uint256 amount) internal {
require(!finalized, "Presale finalized");
require(block.timestamp < saleEnd, "Sale ended");
require(totalRaised + amount <= hardCap, "Hard cap reached");
require(amount > 0, "Zero contribution");
contributions[contributor] += amount;
totalRaised += amount;
emit Contribution(contributor, amount);
// Auto-list at hardcap
if (totalRaised >= hardCap) {
_finalize();
}
}
// Finalization after sale end or at hardcap
function finalize() external {
require(block.timestamp >= saleEnd || totalRaised >= hardCap, "Too early");
require(!finalized, "Already finalized");
_finalize();
}
function _finalize() internal {
finalized = true;
bool success = totalRaised >= softCap;
emit Finalized(success, totalRaised);
if (success) {
_listOnDEX();
}
// If softCap not reached — users receive refund
}
function _listOnDEX() internal {
require(!listed, "Already listed");
listed = true;
// ETH for liquidity
uint256 ethForLiquidity = totalRaised * listingPercent / 100;
// Tokens for liquidity
uint256 totalTokens = token.balanceOf(address(this));
uint256 tokensForLiquidity = totalTokens * listingTokenPercent / 100;
// Approve router
token.safeApprove(address(router), tokensForLiquidity);
// Add liquidity — LP tokens go to address(0) = burn
// This makes liquidity permanent and inseparable
(uint256 addedToken, uint256 addedETH, uint256 lpTokens) = router.addLiquidityETH{
value: ethForLiquidity
}(
address(token),
tokensForLiquidity,
tokensForLiquidity * 95 / 100, // 5% slippage
ethForLiquidity * 95 / 100,
address(0xdead), // LP tokens burned = locked forever
block.timestamp + 600 // 10 minute deadline
);
// Get pool address
address factory = router.factory();
liquidityPair = IUniswapV2Factory(factory).getPair(
address(token),
0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 // WETH
);
emit ListedOnDEX(liquidityPair, addedETH, addedToken);
// Remaining ETH (after liquidity) — to treasury
uint256 remaining = address(this).balance;
if (remaining > 0) {
payable(owner()).transfer(remaining);
}
}
// Refund if failed (softCap not reached)
function claimRefund() external nonReentrant {
require(finalized, "Not finalized");
require(totalRaised < softCap, "Presale successful, no refund");
uint256 amount = contributions[msg.sender];
require(amount > 0, "No contribution");
contributions[msg.sender] = 0;
payable(msg.sender).transfer(amount);
emit Refunded(msg.sender, amount);
}
// Claim tokens on success
function claimTokens() external nonReentrant {
require(finalized && listed, "Not listed yet");
uint256 contribution = contributions[msg.sender];
require(contribution > 0, "Nothing to claim");
// Participant tokens (proportional to contribution)
uint256 participantTokens = token.balanceOf(address(this))
* contribution / totalRaised;
contributions[msg.sender] = 0;
token.safeTransfer(msg.sender, participantTokens);
}
}
Calculating Listing Price
The listing price on DEX is determined by the ratio of tokens and ETH in the pool at creation. This must be clearly communicated to presale participants:
Presale price: 0.001 ETH per token
Hardcap: 100 ETH
listingPercent: 70% (70 ETH to liquidity)
listingTokenPercent: 20% (from presale allocation)
Assume 100,000,000 tokens sold for 100 ETH
Tokens for liquidity: 20,000,000
ETH for liquidity: 70 ETH
Listing price: 70 / 20,000,000 = 0.0000035 ETH
This is higher than presale price → participants profit from listing
If listing price is lower than presale price — presale participants are immediately at a loss. Bad for project reputation. Calculate parameters in advance.
Uniswap V3 Listing
Uniswap V2 is simpler for auto-listing (no price range). Uniswap V3 allows concentrated liquidity, but requires price range specification:
import "@uniswap/v3-periphery/contracts/interfaces/INonfungiblePositionManager.sol";
function _listOnUniswapV3() internal {
INonfungiblePositionManager posManager = INonfungiblePositionManager(0xC36442b4a4522E871399CD717aBDD847Ab11FE88);
// Create pool with initial price
uint160 sqrtPriceX96 = calculateSqrtPrice(ethForLiquidity, tokensForLiquidity);
posManager.createAndInitializePoolIfNecessary(
address(token),
WETH_ADDRESS,
3000, // 0.3% fee tier
sqrtPriceX96
);
// Mint position with wide range (almost full range)
INonfungiblePositionManager.MintParams memory params = INonfungiblePositionManager.MintParams({
token0: address(token) < WETH_ADDRESS ? address(token) : WETH_ADDRESS,
token1: address(token) < WETH_ADDRESS ? WETH_ADDRESS : address(token),
fee: 3000,
tickLower: -887220, // almost minimum
tickUpper: 887220, // almost maximum
amount0Desired: ...,
amount1Desired: ...,
amount0Min: 0,
amount1Min: 0,
recipient: address(0xdead), // burn LP NFT
deadline: block.timestamp + 600
});
posManager.mint{value: ethForLiquidity}(params);
}
For V3, WETH-wrap tokens via WETH.deposit{value: amount}(). More complex, but allows more efficient liquidity.
LP Lock Alternatives
Instead of burning LP tokens (address(0xdead)) — send to Lock contract with time lock:
contract LPLocker {
struct Lock {
address token; // LP token address
uint256 amount;
uint256 unlockAt;
address owner;
}
mapping(uint256 => Lock) public locks;
uint256 public lockCount;
function lock(address lpToken, uint256 amount, uint256 unlockTimestamp)
external returns (uint256 lockId)
{
require(unlockTimestamp > block.timestamp, "Invalid unlock time");
lockId = ++lockCount;
IERC20(lpToken).safeTransferFrom(msg.sender, address(this), amount);
locks[lockId] = Lock({
token: lpToken,
amount: amount,
unlockAt: unlockTimestamp,
owner: msg.sender
});
}
function unlock(uint256 lockId) external {
Lock storage lk = locks[lockId];
require(msg.sender == lk.owner, "Not owner");
require(block.timestamp >= lk.unlockAt, "Still locked");
IERC20(lk.token).safeTransfer(lk.owner, lk.amount);
delete locks[lockId];
}
}
Public lock service (UNCX, Team.Finance, PinkLock) more convenient from trust perspective — users can verify lock in known interface.
Security
Reentrancy: functions with ETH transfer + token transfer potentially vulnerable. nonReentrant on all public functions mandatory.
Front-running listing: MEV bots may front-run listing transaction, buying tokens before liquidity added. If token trades before listing (unlikely with proper architecture) — use commit-reveal or flashbots bundle.
Slippage in addLiquidityETH: amountTokenMin and amountETHMin should not be 0 — otherwise sandwich attack can add disproportionate liquidity. 3–5% slippage tolerance sufficient.
Development time: 2–3 weeks including presale contract, LP locker, tests on fork. Audit mandatory — contract holds participant funds.







