Overview
The most comprehensive template. Includes a Uniswap-style AMM (Factory, Pair, Router), a collateralized lending vault, Synthetix-style staking rewards, and a price oracle. Comes with IPFS, a subgraph indexer, Blockscout explorer, and Chainlink mock oracle as services.| Difficulty | Advanced |
| Category | DeFi |
| Chains | Ethereum |
| Services | IPFS, Subgraph, Blockscout, Chainlink Mock |
| License | Apache-2.0 |
Quick Start
Generated Files
my-defi
dokrypt.yaml
foundry.toml
README.md
contracts
token
DeFiToken.sol
amm
Factory.sol
Pair.sol
Router.sol
lending
LendingVault.sol
InterestModel.sol
staking
StakingRewards.sol
oracle
PriceOracle.sol
test
amm
Factory.t.sol
Router.t.sol
lending
LendingVault.t.sol
staking
StakingRewards.t.sol
scripts
deploy-amm.js
deploy-lending.js
deploy-staking.js
Contracts
DeFiToken.sol
ERC-20 with governance delegation, minting, and burning. Constructor:| Function | Access | Description |
|---|---|---|
| Standard ERC-20 | Public | transfer, approve, transferFrom |
mint(to, amount) | Owner | Mint new tokens |
burn(amount) | Public | Burn your tokens |
delegate(delegatee) | Public | Delegate voting power |
AMM Module
A Uniswap V2-style automated market maker with three contracts.Factory.sol
Creates and tracks liquidity pairs using CREATE2 for deterministic addresses.| Function | Access | Description |
|---|---|---|
createPair(tokenA, tokenB) | Public | Deploy a new pair contract |
computePairAddress(tokenA, tokenB) | View | Predict pair address before creation |
allPairsLength() | View | Total number of pairs |
setFeeTo(newFeeTo) | Owner | Set fee recipient |
keccak256(abi.encodePacked(token0, token1)).
Pair.sol
Liquidity pool implementing thex * y = k constant product invariant.
Key Constants:
MINIMUM_LIQUIDITY = 1000— Permanently locked on first mintSWAP_FEE = 3— 0.3% swap fee (3/1000)
| Function | Access | Description |
|---|---|---|
initialize(token0, token1) | Factory | One-time pair setup |
mint(to) | Public | Add liquidity, receive LP tokens |
burn(to) | Public | Remove liquidity, burn LP tokens |
swap(amountOut0, amountOut1, to) | Public | Swap tokens (constant product enforced) |
skim(to) | Public | Withdraw excess tokens |
sync() | Public | Sync reserves to actual balances |
- ERC-20 LP tokens for liquidity providers
- TWAP (Time-Weighted Average Price) oracle via
price0CumulativeLastandprice1CumulativeLast - Flash swap support
Mint, Burn, Swap, Sync
Router.sol
High-level interface with deadline protection and slippage guards.| Function | Access | Description |
|---|---|---|
addLiquidity(tokenA, tokenB, amountADesired, amountBDesired, amountAMin, amountBMin, to, deadline) | Public | Provide liquidity with slippage protection |
removeLiquidity(tokenA, tokenB, liquidity, amountAMin, amountBMin, to, deadline) | Public | Remove liquidity |
swapExactTokensForTokens(amountIn, amountOutMin, path[], to, deadline) | Public | Swap with exact input |
swapTokensForExactTokens(amountOut, amountInMax, path[], to, deadline) | Public | Swap with exact output |
deadline parameter — reverts if block.timestamp > deadline.
Events: LiquidityAdded, LiquidityRemoved, SwapExecuted
LendingVault.sol
Collateralized lending with liquidation mechanics. Constants:| Constant | Value | Description |
|---|---|---|
COLLATERAL_FACTOR | 75% (7500 bp) | Max borrow relative to collateral |
LIQUIDATION_THRESHOLD | 80% (8000 bp) | Position becomes liquidatable |
LIQUIDATION_BONUS | 105% (10500 bp) | 5% bonus for liquidators |
| Function | Access | Description |
|---|---|---|
depositCollateral(token, amount) | Public | Deposit collateral |
withdrawCollateral(token, amount) | Public | Withdraw collateral (health check) |
borrow(amount) | Public | Borrow against collateral |
repay(amount) | Public | Repay debt |
liquidate(borrower, collateralToken, debtRepaid) | Public | Liquidate unhealthy position |
addCollateral(token) | Owner | Add supported collateral type |
accrueInterest() | Public | Update interest accrual |
getUserHealthFactor(user) | View | Check position health |
isLiquidationEligible(user) | View | Check if liquidatable |
getMaxBorrowable(user) | View | Max borrow amount |
- Borrower’s health factor drops below threshold (collateral value / debt > 80%)
- Liquidator calls
liquidate()with the debt amount to repay - Liquidator receives collateral worth 105% of repaid debt (5% bonus)
- Borrower’s debt and collateral are reduced accordingly
CollateralDeposited, CollateralWithdrawn, Borrowed, Repaid, Liquidated, InterestAccrued
InterestModel.sol
Kinked linear interest rate model. Rates increase slowly at low utilization and steeply at high utilization.| Function | Access | Description |
|---|---|---|
calculateInterestRate(totalBorrowed, totalAvailable) | View | Returns annual interest rate |
StakingRewards.sol
Same Synthetix-style staking as theevm-token template. See evm-token StakeRewards for full details.
PriceOracle.sol
Simple owner-managed price feed for the lending vault.| Function | Access | Description |
|---|---|---|
setPrice(token, price) | Owner | Set token price |
setPrices(tokens[], prices[]) | Owner | Batch update prices |
getPrice(token) | View | Get current price |
isPriceStale(token) | View | Check if price is > 1 hour old |