Senior Solidity Engineer with 5+ years crafting secure Ethereum/DeFi contracts. TypeScript pro for bulletproof dApps. DM for collabs! π
- Decentralized Exchange (DEX) with Staking:
This repository showcases a production-grade Decentralized Exchange (DEX) with an Automated Market Maker (AMM) mechanism and staking for liquidity providers. As a Senior Solidity Developer with 5+ years of experience, this project demonstrates expertise in secure, gas-optimized smart contracts, including ERC-20 standards, liquidity pools, constant-product AMM (x * y = k, where k is the invariant ensuring price stability through reserve balances), multi-hop swaps, and time-weighted reward staking. The code adheres to best practices like the Checks-Effects-Interactions (CEI) pattern (validate inputs first, update state second, interact externally last to prevent reentrancy attacks) and formal verification readiness.
This DEX enables token creation, pair factories, liquidity provision/removal with LP tokens, token swaps with slippage controls, and staking of LP tokens for rewards. It's fully functional out-of-the-box, deployable to any EVM-compatible chain, and optimized for low gas costs (e.g., using immutable variables and native overflow checks in Solidity 0.8.30).
- ERC-20 Tokens: Custom tokens with mint/burn, role-based access, permits for gasless approvals, and pausability.
- Factory & Pairs: Dynamic pair creation with sorted tokens; AMM pools with invariant checks and 0.3% swap fees (deducted from input, added to reserves for LP incentives).
- Router: User-friendly for adding/removing liquidity, exact-input/output swaps, multi-hop paths, and deadline enforcement.
- Staking: Yield farming with accumulator pattern (cumulative rewards per staked unit for O(1) efficiency, avoiding loops), APY calculations, and emergency functions.
- Security: ReentrancyGuard, Pausable, flash loan resistance (post-swap invariant checks), and role separation (e.g., MINTER_ROLE).
- Testing: 100% coverage with Hardhat/Chai, including edge cases like zero liquidity and reentrancy simulations.
- Token.sol: Extensible ERC-20 with permits and roles.
- Factory.sol: Pair factory with event indexing and feeTo control.
- Pair.sol: Core AMM with optimized math (Babylonian sqrt for roots) and fee-on-transfer support.
- Router.sol: Routing for swaps and liquidity with multi-path efficiency.
- Staking.sol: Proportional rewards with time multipliers and pause/emergency withdraw.
- Node.js v22+: Install from nodejs.org (Windows 11 compatible).
- Hardhat ^2.22.10: Global install with
npm i -g hardhat. - MetaMask: For testnet interactions.
- Ethereum Testnet: Sepolia recommended; use Alchemy for RPC (free key at alchemy.com).
- Windows 11 Notes: Use VS Code terminal; no issues with TS/Python 3.11 env for scripts.
- Clone the repo:
git clone https://github.com/1337niy/DEX.gitthencd DEX. - Install dependencies:
npm install. - Initialize Hardhat (if not done):
npx hardhat init(select TypeScript). - Configure .env`: Add PRIVATE_KEY, ALCHEMY_API_KEY, ETHERSCAN_API_KEY (never commit!).
- Update hardhat.config.ts if needed: Solidity 0.8.30 with optimizer enabled.
Run full suite: npm run test (or npx hardhat test).
- Covers deployment, liquidity ops, swaps, staking accrual, and security simulations.
- CI/CD: Automated on every push via GitHub Actions (see badge above).
- Compile:
npx hardhat compile. - Deploy:
npm run deploy(updates scripts/deploy.ts to handle all contracts sequentially on Sepolia). - Verify:
npm run verifyfor Etherscan (public code transparency).
- Add Liquidity: Approve router, call
addLiquiditywith tokens/amounts/min/deadline. - Swap Tokens: Approve input, call
swapExactTokensForTokenswith amountIn/minOut/path/deadline. - Stake LP: Approve staking, call
stake(amount). - Claim Rewards: Call
claimReward(); withdraw withwithdraw(amount).
Interact via ethers.js (TS compatible) or Remix.
- Audits: Code audit-ready with OZ libraries; CEI pattern, no external calls in critical paths.
- Risks Mitigated: Reentrancy (guards), overflow (native checks), flash loans (invariant validation), MEV (deadlines).
- Best Practices: Immutable state, events for off-chain monitoring, role-based access.
- Token β Base assets.
- Factory β Creates pairs.
- Pair β AMM logic.
- Router β User gateway.
- Staking β Rewards engine.
- Integrate React frontend with ethers.js (TS).
- Add 0.3% fees customization.
- Support ERC-721 for NFT staking.
- Formal verification with Certora.
MIT License.
Contribute or connect-let's build DeFi innovations!
Seasoned Solidity dev with 5+ yrs shipping audited DeFi protocols that handle $100M+ TVL-cut gas by 50% in prod swaps, thwarted exploits via CEI mastery. Bring battle-tested Ethereum smarts to your crypto crew: optimize, secure, and scale your dApp game. Remote-ready-let's crush web3!