Skip to main content

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.

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:

AMM Module

A Uniswap V2-style automated market maker with three contracts.

Factory.sol

Creates and tracks liquidity pairs using CREATE2 for deterministic addresses. Uses CREATE2 with salt keccak256(abi.encodePacked(token0, token1)).

Pair.sol

Liquidity pool implementing the x * y = k constant product invariant. Key Constants:
  • MINIMUM_LIQUIDITY = 1000 — Permanently locked on first mint
  • SWAP_FEE = 3 — 0.3% swap fee (3/1000)
Features:
  • ERC-20 LP tokens for liquidity providers
  • TWAP (Time-Weighted Average Price) oracle via price0CumulativeLast and price1CumulativeLast
  • Flash swap support
Events: Mint, Burn, Swap, Sync

Router.sol

High-level interface with deadline protection and slippage guards. All functions have deadline parameter — reverts if block.timestamp > deadline. Events: LiquidityAdded, LiquidityRemoved, SwapExecuted

LendingVault.sol

Collateralized lending with liquidation mechanics. Constants: Functions: Liquidation Flow:
  1. Borrower’s health factor drops below threshold (collateral value / debt > 80%)
  2. Liquidator calls liquidate() with the debt amount to repay
  3. Liquidator receives collateral worth 105% of repaid debt (5% bonus)
  4. Borrower’s debt and collateral are reduced accordingly
Events: CollateralDeposited, CollateralWithdrawn, Borrowed, Repaid, Liquidated, InterestAccrued

InterestModel.sol

Kinked linear interest rate model. Rates increase slowly at low utilization and steeply at high utilization.

StakingRewards.sol

Same Synthetix-style staking as the evm-token template. See evm-token StakeRewards for full details.

PriceOracle.sol

Simple owner-managed price feed for the lending vault.

Deployment

Deploy AMM

Deploys Factory, Router, test tokens, creates a pair, and adds initial liquidity.

Deploy Lending

Deploys borrow token (USDT), collateral token (WETH), InterestModel, PriceOracle, and LendingVault. Configures prices, adds collateral support, and supplies initial tokens.

Deploy Staking

Deploys staking and reward tokens, StakingRewards contract, transfers reward funds, sets duration, and notifies the contract.

Testing Workflows

AMM Testing

Lending Testing with Price Manipulation

Staking with Time Travel