A simple yet powerful decentralized exchange (DEX) built on Ethereum using Solidity and Foundry. This DEX allows users to swap between ETH and a custom ERC-20 token, provide liquidity, and earn trading fees.
- Automated Market Maker (AMM) - Uses a constant product formula (x * y = k) for price determination
- Liquidity Pools - Users can provide liquidity and earn trading fees
- LP Tokens - ERC-20 tokens representing liquidity provider shares
- Token Swaps - Seamless swapping between ETH and the native token
- No Price Oracle - Purely market-driven pricing based on pool reserves
An ERC-20 token contract with a fixed supply of 1,000,000 tokens minted to the deployer.
The core DEX contract that handles:
- Adding/removing liquidity
- Swapping between ETH and tokens
- LP token minting/burning
- Price calculations
-
Clone the repository:
git clone https://github.com/your-username/dex-app.git cd dex-app -
Install dependencies:
forge install
-
Compile contracts:
forge build
Run the test suite:
forge test -vvv-
Set up your environment variables in
.env:PRIVATE_KEY=your_private_key RPC_URL=your_ethereum_node_url ETHERSCAN_API_KEY=your_etherscan_api_key -
Deploy the Token contract:
forge create --rpc-url $RPC_URL --private-key $PRIVATE_KEY src/Token.sol:Token
-
Deploy the Exchange contract (replace
TOKEN_ADDRESSwith the deployed Token address):forge create --rpc-url $RPC_URL --private-key $PRIVATE_KEY src/Exchange.sol:Exchange --constructor-args TOKEN_ADDRESS
- Approve the Exchange to spend your tokens:
await token.approve(exchange.address, amount);
- Call
addLiquiditywith the token amount (ETH is sent as value):await exchange.addLiquidity(tokenAmount, { value: ethAmount });
- ETH to Token: Send ETH to
ethToTokenSwap - Token to ETH: Call
tokenToEthSwapwith token amount
Call removeLiquidity with the amount of LP tokens to burn.
- Uses OpenZeppelin's battle-tested contracts
- Implements reentrancy guards
- Includes input validation and require statements
- Uses SafeMath for arithmetic operations
MIT
Contributions are welcome! Please open an issue or submit a pull request.
-
Uniswap V1 for inspiration
-
Forge: Ethereum testing framework (like Truffle, Hardhat and DappTools).
-
Cast: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
-
Anvil: Local Ethereum node, akin to Ganache, Hardhat Network.
-
Chisel: Fast, utilitarian, and verbose solidity REPL.
$ forge build$ forge test$ forge fmt$ forge snapshot$ anvil$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>$ cast <subcommand>$ forge --help
$ anvil --help
$ cast --help