DeFi Protocol Patterns
DeFi protocol patterns are the recurring smart contract designs behind decentralized finance on Ethereum and similar blockchains: automated market makers that price trades from pooled reserves, overcollateralized lending markets with automatic liquidation, collateral-backed stablecoins, tokenized vaults, flash loans, and price oracles. Each replaces a financial intermediary with public code whose rules anyone can inspect and call.
itDistributed systems, messaging, and integration | OpenSkills.info
Course pathWalk it in order
Look it upDip in anytime
Go furtherLeaves this page
Don't Panic
Don't Panic: DeFi Protocol Patterns
Decentralized finance takes the jobs of a bank, an exchange and a stablecoin issuer and hands them to smart contracts: programs on a blockchain such as Ethereum that hold assets and enforce rules in code. Nobody approves your account. Anyone with a wallet can call the contract, and anyone can read its books, which is refreshing right up to the moment you realize that includes people looking for mistakes.
The products have cheerful names and dashboards full of numbers. Underneath, they are built from a short list of repeating designs. Learn the designs and most new protocols stop being mysterious. They become a familiar pattern with a new logo.
The first idea is the invariant, a rule the contract checks after every call. A trading pool insists that the product of its two token reserves never shrinks. A lending market insists every borrower keeps a health factor, threshold-weighted collateral divided by debt, above 1. If a call would break the rule, the whole transaction is undone as if it never happened. That all-or-nothing behavior is called atomicity, and it is both the safety net and the loophole.
The second idea is the oracle, the mechanism that brings outside prices into a contract. Lending markets and stablecoins cannot know what collateral is worth without one. The price a pool is showing right now is the one number never to trust, because a trade can push it, a dependent contract can read it, and a second trade can push it back, all inside one transaction.
The third idea is the outside actor. Contracts do not tidy up after themselves. Arbitrageurs trade pools back toward market prices, and liquidators repay unsafe loans in exchange for discounted collateral. They do it because they are paid. When nobody finds the job profitable, the contract's rules still hold in theory and quietly stop working in practice.
The surprise is the flash loan: borrow any amount a pool holds, with no collateral, provided you repay before the transaction ends. It sounds like a typo. It is actually a feature, and it means any rule based on how much of something an account holds for a single moment can be satisfied by anyone. Governance votes counted from current balances are the classic casualty, which is why safer designs count votes from a snapshot taken earlier.
Every contract is public, so protocols stack on each other. A vault deposits into a lending market that reads a price from a pool. This is composability, which is excellent for building quickly and equally excellent at spreading one broken price to everything above it.
The Intro walks through each pattern properly: automated market makers, lending, stablecoins, vaults, flash loans, oracles and governance. The Cheatsheet collects the formulas and comparison tables. The Practice Reference and Exercise let you compute a swap, a health factor, an interest rate and a vault attack yourself, offline, with no money anywhere near it. Field Notes covers how these systems actually break.
Where this skill leads
Relevant careers
See how this topic contributes to broader role-level skill maps.
Sources
- https://ethereum.org/defi/
Supports
- DeFi definition
- smart contracts replacing financial institutions
- public and permissionless access
- https://ethereum.org/developers/docs/smart-contracts/composability/
Supports
- composability
- protocols built from existing contracts
- failure propagation
- https://ethereum.org/developers/docs/oracles/
Supports
- oracle problem
- oracle designs
- trust in oracle operators
- https://ethereum.org/developers/docs/mev/
Supports
- MEV
- arbitrage and liquidation searchers
- transaction ordering
- https://ethereum.org/roadmap/merge/
Supports
- timeline proof of stake transition 2022-09-15
- https://vitalik.eth.limo/general/2017/06/22/marketmakers.html
Supports
- timeline on-chain market makers 2017
- https://docs.uniswap.org/contracts/v2/concepts/protocol-overview/how-uniswap-works
Supports
- constant product pools
- liquidity providers
- fees
- k constant or increasing
- arbitrage
- quiz answers
- https://app.uniswap.org/whitepaper.pdf
Supports
- 0.30 percent fee
- invariant checked net of fee
- price accumulator at beginning of each block
- TWAP formula
- flash swaps
- spot price manipulation warning
- impermanent loss and correlated pairs
- exercise swap arithmetic
- https://docs.uniswap.org/concepts/protocol/concentrated-liquidity
Supports
- concentrated liquidity ranges
- out of range positions
- non-fungible positions
- DAI/USDC 0.50 percent example
- https://blog.uniswap.org/uniswap-history
Supports
- timeline Uniswap mainnet deployment 2018-11-02
- https://blog.uniswap.org/uniswap-v2
Supports
- timeline Uniswap v2 announcement 2020-03-23
- ERC-20 pairs
- price oracles
- flash swaps
- https://blog.uniswap.org/uniswap-v3
Supports
- timeline Uniswap v3 announcement 2021-03-23
- targeted mainnet launch May 5
- https://blog.uniswap.org/uniswap-v4-is-here
Supports
- hooks for pools swaps fees and positions
- timeline Uniswap v4 launch 2025-01-31
- https://blog.uniswap.org/uniswap-v3-oracles
Supports
- proof of stake proposer knowledge 32 blocks
- multi-block TWAP manipulation costs
- validator consecutive block frequency
- field notes
- https://classic.curve.fi/files/stableswap-paper.pdf
Supports
- StableSwap invariant
- constant-sum behavior near balance
- constant-product behavior when imbalanced
- timeline 2019-11-10
- https://aave.com/docs/aave-v3/overview
Supports
- overcollateralized positions
- aTokens
- variable debt tokens
- isolation mode
- efficiency mode
- reserve factor
- quiz answers
- https://aave.com/help/borrowing/liquidations
Supports
- health factor formula
- liquidation below 1
- liquidation bonus
- 50 and 100 percent close factor at 0.95 and 2000 dollars
- quiz answers
- https://aave.com/docs/aave-v3/guides/flash-loans
Supports
- flashLoan and flashLoanSimple
- executeOperation callback
- repayment in the same transaction
- governance-set premium 0.05 percent
- quiz answers
- https://aave.com/docs/resources/changelog
Supports
- timeline Aave v1 2020-01-08
- Aave v2 2020-12-03
- Aave v3 2022-03-16
- Aave v3 Ethereum Core 2023-01-27
- https://docs.compound.xyz/interest-rates/
Supports
- utilization formula
- kinked supply and borrow rate curves
- quiz answers
- https://docs.compound.xyz/liquidation/
Supports
- liquidation collateral factor
- absorb
- buyCollateral at a discount
- borrow and liquidation collateral factors
- quiz answers
- https://docs.morpho.org/learn/concepts/market/
Supports
- isolated markets with immutable loan asset collateral asset oracle LLTV and rate model
- https://makerdao.com/en/whitepaper/
Supports
- Vaults
- Dai generation
- Liquidation Ratio
- Stability Fee
- Liquidation Penalty
- collateral auctions
- Oracle Security Module one-hour delay
- quiz answers
- https://github.com/sky-ecosystem/community/blob/master/governance/votes/Executive%20Vote%20-%20Launch%20Multi-Collateral%20Dai.md
Supports
- timeline Multi-Collateral Dai launch 2019-11-18
- https://forum.skyeco.com/t/black-thursday-response-thread/1433
Supports
- timeline Black Thursday 2020-03-12
- zero-bid auctions
- network congestion
- surplus to 4 million dollar shortfall
- field notes
- https://eips.ethereum.org/EIPS/eip-4626
Supports
- tokenized vault interface
- deposit mint withdraw redeem
- conversion functions
- rounding in the vault's favor
- timeline 2021-12-22
- quiz answers
- https://docs.openzeppelin.com/contracts/5.x/erc4626
Supports
- inflation attack
- virtual shares and assets
- decimal offset
- offset 0 makes the attack unprofitable
- exercise conversion formula
- quiz answers
- https://eips.ethereum.org/EIPS/eip-3156
Supports
- standard single-asset flash loan interface
- onFlashLoan
- https://docs.chain.link/data-feeds
Supports
- deviation threshold and heartbeat updates
- latestRoundData
- updatedAt freshness check
- heartbeats spanning several hours
- field notes
- https://docs.openzeppelin.com/contracts/5.x/governance
Supports
- Governor
- ERC20Votes checkpoints
- voting power from past snapshots
- timelock
- quiz answers
- field notes
- https://bean.money/blog/beanstalk-governance-exploit
Supports
- flash-loan governance exploit 2022-04-17
- about 77 million dollars in user assets
- quiz answers
- field notes
- timeline
- https://scs.owasp.org/SCSVS/
Supports
- update source
- oracle governance and DeFi control groups
- https://github.com/sindresorhus/awesome
Supports
- awesome list discovery
- https://github.com/kareniel/awesome-evm-security
Supports
- awesome links selection and descriptions for Rekt News
- Blockchain Threat Intelligence
- DeFi Safety
- Forta
- Immunefi severity system
- Flash Boys 2.0
- Awesome MEV resources
- Ultimate DeFi Research Base
- https://immunefi.com/severity-updated/
Supports
- critical impacts include emptying or freezing contract holdings through economic attacks and flash loans
- https://arxiv.org/abs/1904.05234
Supports
- paper title
- arbitrage bots on decentralized exchanges
- miner extractable value
- https://docs.forta.network/
Supports
- runtime security monitoring network
- https://uniswap.org/
Supports
- Landscape entry
- https://github.com/Uniswap/v3-core/blob/main/LICENSE
Supports
- Landscape licensing Business Source License
- https://github.com/Uniswap/v4-core/tree/main/licenses
Supports
- Landscape licensing Business Source License 1.1 for Uniswap V4 Core
- https://www.flashbots.net/
Supports
- Landscape entry homepage
- https://curve.finance/
Supports
- Landscape entry
- https://github.com/curvefi/curve-contract/blob/master/LICENSE
Supports
- Landscape licensing source published without open license grant
- https://aave.com/
Supports
- Landscape entry
- https://github.com/aave-dao/aave-v3-origin/blob/main/LICENSE
Supports
- Landscape licensing Business Source License
- https://compound.finance/
Supports
- Landscape entry
- https://github.com/compound-finance/comet/blob/main/LICENSE
Supports
- Landscape licensing Business Source License
- https://morpho.org/
Supports
- Landscape entry
- https://github.com/morpho-org/morpho-blue
Supports
- Landscape licensing GPL-2.0
- https://sky.money/
Supports
- Landscape entry
- Sky Protocol formerly MakerDAO
- https://github.com/sky-ecosystem/dss
Supports
- Landscape licensing AGPL-3.0
- https://chain.link/
Supports
- Landscape entry
- https://github.com/smartcontractkit/chainlink-evm/blob/develop/LICENSE
Supports
- Landscape licensing partly MIT
- https://www.pyth.network/
Supports
- Landscape entry
- price feed network
- https://github.com/pyth-network/pyth-crosschain/blob/main/LICENSE
Supports
- Landscape licensing Apache-2.0
- https://cow.fi/
Supports
- Landscape entry
- https://docs.cow.fi/cow-protocol/concepts/introduction/solvers
Supports
- batch auctions
- competing solvers
- surplus-maximizing settlement
- https://1inch.com/
Supports
- Landscape entry
- liquidity combined across exchanges for better swap rates
- https://docs.flashbots.net/flashbots-auction/overview
Supports
- Landscape entry
- arbitrage and liquidation bots submit bundles to block builders
- https://docs.yearn.fi/getting-started/products/yvaults/v3
Supports
- Landscape entry
- ERC-4626 v3 vaults
- strategies as ERC-4626 vaults in multi-strategy vaults
- https://github.com/cowprotocol/contracts
Supports
- Landscape licensing LGPL-3.0
- https://github.com/1inch/limit-order-protocol
Supports
- Landscape licensing MIT for published protocol contracts
- https://github.com/flashbots/mev-boost
Supports
- Landscape licensing MIT
- https://github.com/yearn/yearn-vaults-v3
Supports
- Landscape licensing AGPL-3.0
- https://yearn.fi/
Supports
- Landscape entry
