Skip to content
sergiiogg7Public

About

Multi-phase ERC-20 token presale in Solidity accepting ETH (Chainlink price feed), USDT and USDC. Includes claim-after-sale, pausable purchases, restricted owner withdrawals and fuzz and invariant tests. Built with Foundry.

Topics

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Token Presale Contract

Multi-phase ERC-20 token presale accepting ETH, USDT & USDC — with Chainlink price feeds and on-chain claim mechanics.

Solidity Foundry OpenZeppelin CI License


Overview

A smart contract that manages the full lifecycle of a token presale. Buyers deposit ETH, USDT, or USDC during an active sale window; the contract records each allocation on-chain and releases tokens to claimants after endingTime. The sale runs across up to three independent price phases, each with its own cap and deadline. ETH prices are sourced live from a Chainlink AggregatorV3 oracle.


Contracts

src/Presale.sol
Core presale logic — phase management, payment handling, allocation tracking, and token claiming. Inherits Ownable and uses SafeERC20 from OpenZeppelin v5.
src/interfaces/IAggregator.sol
Minimal Chainlink AggregatorV3 interface used to fetch a live ETH/USD price on each ETH purchase.

Key Features

  • Multi-phase pricing — up to 3 phases, each with an independent token cap and deadline; advances automatically when either threshold is crossed
  • Chainlink oracle — real-time ETH/USD price via AggregatorV3, scaled to 18 decimals
  • Stable coin support — handles both 6-decimal (USDT/USDC) and 18-decimal tokens with automatic normalization
  • Non-custodial payments — funds forwarded directly to fundsReceiver on every purchase; no ETH/stable sits in the contract
  • Claim-after-sale — sale tokens are held in escrow until endingTime, then users pull their allocation
  • Blacklist — owner can block addresses from participating
  • Emergency withdrawal — owner can rescue any ERC-20 or ETH stuck in the contract

Tech Stack

Layer Tool Version
Dev framework Foundry latest
Language Solidity 0.8.24
Access control & safety OpenZeppelin v5.7.0
Price oracle Chainlink AggregatorV3 —
Test utilities forge-std v1.16.2
CI GitHub Actions —

Contract at a Glance

Function Access Description
buyWithEther() public payable Purchase tokens with native ETH; price derived from Chainlink
buyWithStable(address, uint256) public Purchase tokens with USDT or USDC
claim() public Redeem allocated tokens after endingTime
getEtherPrice() public view Returns current ETH/USD price (18 decimals). Reverts if the price is not positive or older than 1 hour
blackList(address) owner Block an address from buying (claims stay available)
removeBlacklist(address) owner Remove a blacklist entry
pause() / unpause() owner Halt or resume purchases. Claims are never paused
withdrawableSaleTokens() public view Sale tokens not owed to buyers (zero until the presale ends)
emergencyERC20Withdraw(address, uint256) owner Rescue ERC-20 tokens. Sale tokens only after the presale ends and never those owed to buyers
emergencyETHWithdraw() owner Rescue any ETH from the contract

Phase System

The presale is divided into up to 3 phases, each defined by three parameters:

Parameter Type Description
cap uint256 Cumulative totalSold threshold at which this phase ends
price uint256 Token price in micro-USD (e.g. 50000 = $0.05 / token)
deadline uint256 Unix timestamp at which this phase auto-expires

A phase advances when either condition is met — cap reached or deadline passed.

Pricing example (Phase 1, price = 50000 ≈ $0.05/token):

// Buyer sends 100 USDC (6 decimals)
stableAmount  = 100e6
normalised    = 100e6 * 1e12       → 100e18  (USD, 18 dec)
tokenAmount   = 100e18 * 1e6 / 50000  → 2,000 tokens

Getting Started

Prerequisites: Foundry

# Clone with submodules (OpenZeppelin + forge-std)
git clone --recurse-submodules https://github.com/sergiiogg7/presale.git
cd presale

# Compile
forge build

# Run tests
forge test -vvv

# Check formatting
forge fmt --check

# Coverage of src/ and gas regression check
forge coverage --no-match-coverage "script|test"
forge snapshot --check

Deployment reads its configuration from the environment (see .env.example). The deployer must hold MAX_SELLING_AMOUNT sale tokens: script/Deploy.s.sol approves the predicted presale address and the constructor escrows the tokens.

cp .env.example .env   # fill in values
source .env
forge script script/Deploy.s.sol --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcast

Security

Measure Location
Two-step ownership transfer Ownable2Step
Reentrancy guard on purchases, claims and withdrawals nonReentrant
Pausable purchases, claims never blocked Pausable
Oracle checks: positive price, maximum age of 1 hour, feed decimals scaling Presale.getEtherPrice
Phase resolved before pricing, index bounded to the last phase Presale._syncPhase
Constructor validation: zero addresses, times, phase caps, prices and end times Presale constructor
Owner cannot withdraw sale tokens owed to buyers, and only after the presale ends Presale.emergencyERC20Withdraw
Payments forwarded on every purchase, nothing kept in the contract buyWithEther, buyWithStable
Custom errors only, full NatSpec Presale.sol
Slither in CI slither.config.json

See SECURITY.md for assumptions and known limits.


Testing

Unit, fuzz and handler-based invariant suites with mock tokens, a mock Chainlink feed and attacker contracts (reentrant funds receiver, account that cannot receive ETH). CI runs forge fmt --check, forge build --sizes, forge test (profile ci, higher fuzz runs), a 95% line-coverage gate on src/, forge snapshot --check and Slither.


License

MIT

About

Multi-phase ERC-20 token presale in Solidity accepting ETH (Chainlink price feed), USDT and USDC. Includes claim-after-sale, pausable purchases, restricted owner withdrawals and fuzz and invariant tests. Built with Foundry.

Topics

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages