// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; /// @title BAD Presale (v2 — with buyer refunds) /// @notice Trustless presale for the Bad Decisions (BAD) token. /// Buyers send BNB, get credited BAD at a fixed price. BNB is forwarded /// straight to the LiquidityVault (counts toward the 80 BNB goal). /// Buyers claim their BAD after trading is enabled on the BAD contract. /// The vault's 2B-BAD-per-BNB donor bonus accrues to this contract and is /// redistributed to buyers pro-rata — same deal as direct vault donors. /// @dev Owner funds this contract with 200B BAD BEFORE the sale opens. /// Owner must call setExcludedFromLimits(presale, true) on the BAD contract /// first, or the 200B funding transfer reverts on the 3% max-wallet rule. /// If the vault goal is missed and vault refunds open, buyers call refund() /// here: this contract pulls its vault donation back via vault.refund() /// (the vault recorded it under THIS contract's address) and pays each /// buyer back their own BNB. No BNB sits here otherwise. interface IBadToken { function tradingEnabled() external view returns (bool); function balanceOf(address a) external view returns (uint256); function transfer(address to, uint256 amount) external returns (bool); } interface ILiquidityVault { function donate() external payable; function claimBonus() external; function deployed() external view returns (bool); function refund() external; function refundsOpen() external view returns (bool); } contract BADPresale { IBadToken public immutable bad; ILiquidityVault public immutable vault; address public immutable owner; /// @notice 3.75B BAD per BNB — $0.0000002 per BAD at $750/BNB. uint256 public constant BAD_PER_BNB = 3_750_000_000 * 1e18; /// @notice 200B BAD total allocation for the presale. uint256 public constant PRESALE_SUPPLY = 200_000_000_000 * 1e18; uint256 public totalSold; // BAD allocated to buyers uint256 public totalRaised; // BNB forwarded to the vault bool public presaleOver; bool public vaultRefunded; // true once we pulled our vault donation back mapping(address => uint256) public badOwed; // presale BAD per buyer mapping(address => uint256) public bnbPaid; // BNB per buyer mapping(address => bool) public claimed; mapping(address => bool) public refunded; uint256 public bonusPool; // bonus BAD pulled from the vault uint256 public claimedBonusTotal; event Bought(address indexed buyer, uint256 bnb, uint256 bad); event Claimed(address indexed buyer, uint256 bad, uint256 bonus); event Refunded(address indexed buyer, uint256 bnb); event BonusPulled(uint256 amount); event PresaleEnded(); modifier onlyOwner() { require(msg.sender == owner, "not owner"); _; } // Reentrancy guard (minimal, no OZ dependency for easy Remix deploy) uint256 private _locked; modifier nonReentrant() { require(_locked == 0, "reentrant"); _locked = 1; _; _locked = 0; } constructor(address _bad, address _vault) { bad = IBadToken(_bad); vault = ILiquidityVault(_vault); owner = msg.sender; } /// @notice Accept BNB back from the vault on refund(). receive() external payable {} /// @notice Buy BAD at the fixed presale price. BNB goes straight to the vault. function buy() external payable nonReentrant { require(!presaleOver, "presale over"); require(!vaultRefunded, "refunds open"); require(msg.value > 0, "no bnb"); uint256 badAmount = (msg.value * BAD_PER_BNB) / 1e18; require(totalSold + badAmount <= PRESALE_SUPPLY, "sold out"); badOwed[msg.sender] += badAmount; bnbPaid[msg.sender] += msg.value; totalSold += badAmount; totalRaised += msg.value; // Forward BNB to the vault in the same tx. The 2B/BNB donor bonus // accrues to THIS contract (donations[msg.sender]) for redistribution. vault.donate{value: msg.value}(); emit Bought(msg.sender, msg.value, badAmount); } /// @notice Refund your BNB if the vault goal was missed and vault refunds are open. /// @dev The vault recorded our donation under THIS contract's address, so we /// pull it back once (first refunder triggers it) and pay each buyer /// from this contract's balance. Refunding forfeits your BAD claim. function refund() external nonReentrant { uint256 paid = bnbPaid[msg.sender]; require(paid > 0, "nothing to refund"); require(!refunded[msg.sender], "already refunded"); require(vault.refundsOpen(), "refunds not open"); if (!vaultRefunded) { vault.refund(); // pulls our full donations balance back here vaultRefunded = true; } refunded[msg.sender] = true; // Forfeit the BAD allocation so sweepUnsold math stays exact. totalSold -= badOwed[msg.sender]; totalRaised -= paid; badOwed[msg.sender] = 0; bnbPaid[msg.sender] = 0; (bool ok, ) = msg.sender.call{value: paid}(""); require(ok, "refund failed"); emit Refunded(msg.sender, paid); } /// @notice Claim presale BAD (+ pro-rata vault bonus) after trading is live. function claim() external nonReentrant { require(bad.tradingEnabled(), "trading not live"); require(!claimed[msg.sender], "already claimed"); require(!refunded[msg.sender], "refunded"); uint256 amount = badOwed[msg.sender]; require(amount > 0, "nothing to claim"); claimed[msg.sender] = true; uint256 bonus = 0; if (bonusPool > 0 && totalRaised > 0) { bonus = (bnbPaid[msg.sender] * bonusPool) / totalRaised; claimedBonusTotal += bonus; } require(bad.transfer(msg.sender, amount + bonus), "transfer failed"); emit Claimed(msg.sender, amount, bonus); } /// @notice Pull this contract's vault donor bonus in for redistribution. /// @dev Only works after the vault deploys liquidity. function pullBonus() external onlyOwner nonReentrant { require(vault.deployed(), "vault not deployed"); uint256 beforeBal = bad.balanceOf(address(this)); vault.claimBonus(); uint256 received = bad.balanceOf(address(this)) - beforeBal; bonusPool += received; emit BonusPulled(received); } /// @notice End the sale. After this, unsold BAD can be swept. function endPresale() external onlyOwner { presaleOver = true; emit PresaleEnded(); } /// @notice Recover unsold presale BAD after the sale ends. /// @dev Only the unallocated portion — buyer funds are never touched. function sweepUnsold() external onlyOwner nonReentrant { require(presaleOver, "sale not over"); // Owed but unclaimed presale BAD + unclaimed bonus must stay. // Refunded buyers already had their allocations zeroed, so totalSold // is exact — no approximation needed. uint256 reserved = totalSold + (bonusPool - claimedBonusTotal); uint256 bal = bad.balanceOf(address(this)); require(bal > reserved, "nothing to sweep"); uint256 sweepable = bal - reserved; require(bad.transfer(owner, sweepable), "transfer failed"); } }