// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; /// @title LiquidityVault /// @notice Minimal reentrancy guard (inlined — keeps the vault dependency-free). abstract contract ReentrancyGuard { uint256 private constant _NOT_ENTERED = 1; uint256 private constant _ENTERED = 2; uint256 private _status = _NOT_ENTERED; error ReentrancyGuardReentrantCall(); modifier nonReentrant() { if (_status == _ENTERED) revert ReentrancyGuardReentrantCall(); _status = _ENTERED; _; _status = _NOT_ENTERED; } } /// @notice Minimal BEP-20 interface (approve / transfer / transferFrom / balanceOf). interface IERC20 { function approve(address spender, uint256 amount) external returns (bool); function transfer(address to, uint256 amount) external returns (bool); function transferFrom(address from, address to, uint256 amount) external returns (bool); function balanceOf(address account) external view returns (uint256); } /// @notice Minimal PancakeSwap V2 router interface (addLiquidityETH only). interface IPancakeRouter { function addLiquidityETH( address token, uint256 amountTokenDesired, uint256 amountTokenMin, uint256 amountETHMin, address to, uint256 deadline ) external payable returns (uint256 amountToken, uint256 amountETH, uint256 liquidity); } /// @title LiquidityVault /// @notice Trustless BNB donation vault that deploys BAD/BNB liquidity on PancakeSwap. /// @dev Flow: /// 1. Owner deposits the liquidity BAD allocation into the vault (visible on-chain). /// 2. Donors contribute BNB (capped at the goal). 60-day lock from deployment. /// 3. After the lock, if the goal is met, deployLiquidity() pairs ALL donated /// BNB with the fixed BAD amount and adds it to PancakeSwap. LP tokens go /// DIRECTLY to the burn address — permanent liquidity, no human ever holds it. /// 4. Donors claim bonus BAD proportional to their donation after deployment. /// 5. If the goal is missed by the refund deadline: donors refund in full and /// the owner reclaims all BAD. Nothing can get stuck. /// Only the founder can trigger deployment during days 60–90. After day 90, /// anyone can trigger the exact same fixed path (liveness failsafe). /// No proxy, no upgrade, no backdoor — immutable by design. contract LiquidityVault is ReentrancyGuard { /// @notice Burn address — LP tokens are sent here at creation (permanent liquidity). address public constant DEAD = 0x000000000000000000000000000000000000dEaD; /// @notice The founder wallet (only it can trigger deployment during days 60–90). address public immutable owner; /// @notice The BAD token contract. IERC20 public immutable badToken; /// @notice PancakeSwap V2 router. IPancakeRouter public immutable router; /// @notice BNB funding goal in wei (e.g. 80 BNB). uint256 public immutable goal; /// @notice Fixed BAD amount paired with the raised BNB at deployment (e.g. 300B BAD). uint256 public immutable badForLiquidity; /// @notice Bonus rate: wei-of-BAD per wei-of-BNB donated (2e9 = 1 BNB -> 2B BAD). uint256 public immutable bonusRate; /// @notice Unix timestamp when the 60-day lock ends. uint256 public immutable lockEnd; /// @notice Unix timestamp when the founder-only window ends (day 90). uint256 public immutable founderEnd; /// @notice Unix timestamp after which refunds open if the goal was missed. uint256 public immutable refundDeadline; /// @notice Unix timestamp after which the owner may sweep unclaimed bonus BAD. uint256 public immutable sweepStart; /// @notice Total BNB donated (wei). uint256 public totalDonated; /// @notice True once liquidity has been deployed. bool public deployed; /// @notice Timestamp of deployment (0 if not deployed). uint256 public deployedAt; /// @notice BNB donated per address (wei). mapping(address => uint256) public donations; /// @notice Whether an address already claimed its bonus. mapping(address => bool) public bonusClaimed; /// @notice Whether an address already refunded. mapping(address => bool) public refunded; event BadDeposited(address indexed owner, uint256 amount); event Donated(address indexed donor, uint256 amount, uint256 total); event LiquidityDeployed(uint256 bnbUsed, uint256 badUsed, uint256 lpBurned); event BonusClaimed(address indexed donor, uint256 amount); event Refunded(address indexed donor, uint256 amount); event BadReclaimed(address indexed owner, uint256 amount); event LeftoverSwept(address indexed owner, uint256 amount); error ZeroAddress(); error ZeroAmount(); error NotOwner(); error ZeroDonation(); error GoalReached(); error DonationsClosed(); error LockNotExpired(); error GoalNotMet(); error AlreadyDeployed(); error NotDeployed(); error NotFounder(); error NothingToClaim(); error AlreadyClaimed(); error NothingToRefund(); error AlreadyRefunded(); error RefundsNotOpen(); error SweepNotOpen(); error TransferFailed(); /// @param _owner Founder wallet (triggers deployment, reclaims BAD on failure). /// @param _badToken BAD token contract address. /// @param _router PancakeSwap V2 router address. /// @param _goalWei BNB funding goal in wei. /// @param _badForLiquidityWei Fixed BAD amount paired at deployment (wei). /// @param _bonusRate Wei-of-BAD per wei-of-BNB (2e9 = 1 BNB -> 2B BAD bonus). /// @param _lockDays Days BNB is locked before deployment (60). /// @param _founderDays Days of founder-only deployment trigger (90). /// @param _refundDays Days until refunds open on missed goal (120). /// @param _sweepDays Days after deployment until unclaimed bonus BAD is sweepable (180). constructor( address _owner, address _badToken, address _router, uint256 _goalWei, uint256 _badForLiquidityWei, uint256 _bonusRate, uint256 _lockDays, uint256 _founderDays, uint256 _refundDays, uint256 _sweepDays ) { if (_owner == address(0) || _badToken == address(0) || _router == address(0)) revert ZeroAddress(); if (_goalWei == 0 || _badForLiquidityWei == 0 || _bonusRate == 0) revert ZeroAmount(); require(_lockDays < _founderDays && _founderDays < _refundDays, "bad timeline"); owner = _owner; badToken = IERC20(_badToken); router = IPancakeRouter(_router); goal = _goalWei; badForLiquidity = _badForLiquidityWei; bonusRate = _bonusRate; lockEnd = block.timestamp + _lockDays * 1 days; founderEnd = block.timestamp + _founderDays * 1 days; refundDeadline = block.timestamp + _refundDays * 1 days; sweepStart = block.timestamp + (_refundDays + _sweepDays) * 1 days; } /// @notice Owner deposits BAD for liquidity pairing + bonus pool (approve first). /// @dev Anyone can verify the deposit on-chain — the trust signal for donors. function depositBad(uint256 amount) external nonReentrant { if (msg.sender != owner) revert NotOwner(); if (amount == 0) revert ZeroAmount(); if (!badToken.transferFrom(msg.sender, address(this), amount)) revert TransferFailed(); emit BadDeposited(msg.sender, amount); } /// @notice Donate BNB toward the liquidity goal. Locked until deployment or refund. function donate() public payable nonReentrant { if (deployed) revert AlreadyDeployed(); if (block.timestamp > refundDeadline) revert DonationsClosed(); if (msg.value == 0) revert ZeroDonation(); if (totalDonated + msg.value > goal) revert GoalReached(); donations[msg.sender] += msg.value; totalDonated += msg.value; emit Donated(msg.sender, msg.value, totalDonated); } /// @notice Plain BNB transfers count as donations. receive() external payable { donate(); } /// @notice Deploy BAD/BNB liquidity on PancakeSwap. LP goes straight to the burn address. /// @dev Founder-only during days 60–90; anyone may call after day 90 (liveness failsafe). /// The price is set purely by the BAD/BNB ratio deposited — no oracle. function deployLiquidity() external nonReentrant { if (deployed) revert AlreadyDeployed(); if (block.timestamp < lockEnd) revert LockNotExpired(); if (totalDonated < goal) revert GoalNotMet(); if (block.timestamp < founderEnd && msg.sender != owner) revert NotFounder(); deployed = true; deployedAt = block.timestamp; uint256 bnbAmount = address(this).balance; uint256 badAmount = badForLiquidity; if (badToken.balanceOf(address(this)) < badAmount) revert TransferFailed(); if (!badToken.approve(address(router), badAmount)) revert TransferFailed(); (uint256 usedBad, uint256 usedBnb, uint256 liquidity) = router.addLiquidityETH{value: bnbAmount}( address(badToken), badAmount, (badAmount * 98) / 100, (bnbAmount * 98) / 100, DEAD, block.timestamp + 900 ); emit LiquidityDeployed(usedBnb, usedBad, liquidity); } /// @notice Claim bonus BAD proportional to your donation. Only after deployment. /// @dev Claim-based (not pushed) so a failed recipient can't block others. /// No bonus exists if the goal was missed — donate/refund gaming is impossible. function claimBonus() external nonReentrant { if (!deployed) revert NotDeployed(); if (bonusClaimed[msg.sender]) revert AlreadyClaimed(); uint256 donated = donations[msg.sender]; if (donated == 0) revert NothingToClaim(); bonusClaimed[msg.sender] = true; uint256 bonus = donated * bonusRate; if (!badToken.transfer(msg.sender, bonus)) revert TransferFailed(); emit BonusClaimed(msg.sender, bonus); } /// @notice Refund your BNB in full if the goal was missed by the refund deadline. function refund() external nonReentrant { if (deployed) revert AlreadyDeployed(); if (block.timestamp <= refundDeadline) revert RefundsNotOpen(); if (totalDonated >= goal) revert GoalNotMet(); if (refunded[msg.sender]) revert AlreadyRefunded(); uint256 amount = donations[msg.sender]; if (amount == 0) revert NothingToRefund(); refunded[msg.sender] = true; donations[msg.sender] = 0; (bool ok, ) = msg.sender.call{value: amount}(""); require(ok, "refund failed"); emit Refunded(msg.sender, amount); } /// @notice Owner reclaims ALL BAD if the goal was missed by the refund deadline. function reclaimBad() external nonReentrant { if (msg.sender != owner) revert NotOwner(); if (deployed) revert AlreadyDeployed(); if (block.timestamp <= refundDeadline) revert RefundsNotOpen(); if (totalDonated >= goal) revert GoalNotMet(); uint256 amount = badToken.balanceOf(address(this)); if (amount == 0) revert NothingToClaim(); if (!badToken.transfer(owner, amount)) revert TransferFailed(); emit BadReclaimed(owner, amount); } /// @notice Owner sweeps unclaimed bonus BAD long after deployment (default +180 days). function sweepLeftoverBad() external nonReentrant { if (msg.sender != owner) revert NotOwner(); if (!deployed) revert NotDeployed(); if (block.timestamp < sweepStart) revert SweepNotOpen(); uint256 amount = badToken.balanceOf(address(this)); if (amount == 0) revert NothingToClaim(); if (!badToken.transfer(owner, amount)) revert TransferFailed(); emit LeftoverSwept(owner, amount); } /// @notice Bonus BAD owed to a donor (wei). Zero until deployment succeeds. function bonusOf(address donor) external view returns (uint256) { if (!deployed || bonusClaimed[donor]) return 0; return donations[donor] * bonusRate; } /// @notice True when deployLiquidity() can currently be called. function deployable() external view returns (bool) { return !deployed && block.timestamp >= lockEnd && totalDonated >= goal; } /// @notice True when the founder-only trigger window is active. function founderOnly() external view returns (bool) { return !deployed && block.timestamp >= lockEnd && block.timestamp < founderEnd; } /// @notice True when donors can refund and the owner can reclaim BAD. function refundsOpen() external view returns (bool) { return !deployed && block.timestamp > refundDeadline && totalDonated < goal; } /// @notice Seconds until the lock ends (0 if already ended). function timeUntilUnlock() external view returns (uint256) { return block.timestamp >= lockEnd ? 0 : lockEnd - block.timestamp; } }