Category: Defi

get_D() and get_y() in Curve StableSwap
get_D() and get_y() in Curve StableSwap This article shows algebraically step-by-step how the code for get_D() and get_y() are derived from the StableSwap invariant. Given the StableSwap Invariant: $$ An^n\sum…

Fixed Point Arithmetic in Solidity (Using Solady, Solmate, and ABDK as Examples)
Fixed Point Arithmetic in Solidity (Using Solady, Solmate, and ABDK as Examples) A fixed-point number is an integer that stores only the numerator of a fraction — while the denominator…

Uniswap V2: Calculating the Settlement Price of an AMM Swap
Uniswap V2: Calculating the Settlement Price of an AMM Swap This article explains how to determine the price settlement of a trading pair in an Automated Market Maker (AMM). It…

How Chainlink Price Feeds Work
How Chainlink Price Feeds Work Chainlink price oracles are smart contracts with public view functions that return the price of a particular asset denominated in USD. Off-chain nodes collect the…

How Compound V3 Allocates COMP Rewards
How Compound V3 Allocates COMP Rewards Compound issues rewards in COMP tokens to lenders and borrowers in proportion to their share of the a market’s lending and borrowing. The algorithm…

Bulkers in Compound V3
Bulkers in Compound V3 The bulker contracts in Compound V3 are multicall-like contracts for batching several transactions. For example, if we wanted to supply Ether, LINK, and wBTC as collateral…

Understanding Collateral, Liquidations, and Reserves in Compound V3
Understanding Collateral, Liquidations, and Reserves in Compound V3 In this chapter we will examine the following topics about Compound V3: collateral valuation absorbing insufficiently collateralized loans (liquidations) selling absorbed collateral…

cUSDC V3 (Compound V3) as a non-standard Rebasing Token, CometExt.sol
cUSDC V3 (Compound V3) as a non-standard Rebasing Token, CometExt.sol The Compound V3 contract behaves like a rebasing ERC 20 token. A rebasing token is a token which has an…

DeFi Interest Rate Indexes: Principal value and Present Value in Compound V3
DeFi Interest Rate Indexes: Principal value and Present Value in Compound V3 The intuitive way to track lender deposits is to record the amount of USDC they deposited and the…

Compound V3 Interest Per Second
Compound V3 Interest Per Second The Compound V3 protocol measures interest on the scale of seconds. The Compound V3 frontend scales the number up to years for human friendliness. When we check…

The Architecture of the Compound V3 Smart Contract
The Architecture of the Compound V3 Smart Contract Introduction and prerequisites Compound is one of the most significant lending protocols in DeFi, having inspired the design of nearly every lending…

DeFi Lending: Liquidations and Collateral
DeFi Lending: Liquidations and Collateral In TradFi, when someone defaults on a loan, the creditor has the right to seize assets or garnish wages. In DeFi, when someone defaults on…

The interest rate model of AAVE V3 and Compound V2
The interest rate model of AAVE V3 and Compound V2 Interest rates in TradFi (traditional finance) are largely determined by central banks and influenced by market factors. In contrast, DeFi…

The staking algorithm of Sushiswap MasterChef and Synthetix
The staking algorithm of Sushiswap MasterChef and Synthetix The MasterChef and Synthetix staking algorithms distribute a fixed reward pool among stakers according to their time-weighted contributions to a pool. To…

How Uniswap V2 computes the mintFee
How Uniswap V2 computes the mintFee Uniswap V2 was designed to collect 1/6th of the swap fees to the protocol. Since a swap fee is 0.3%, 1/6th of that is…

Uniswap v2 router code walkthrough
Uniswap v2 router code walkthrough The Router contracts provide a user-facing smart contract for safely minting and burning LP tokens (adding and removing liquidity) safely swapping pair tokens They add…

Flash Loans and how to hack them: a walk through of ERC 3156
Flash Loans and how to hack them: a walk through of ERC 3156 Flash loans are loans between smart contracts that must be repaid in the same transaction. This article…

UniswapV2Library Code Walkthrough
UniswapV2Library Code Walkthrough UniswapV2Library The Uniswap V2 Library simplifies some interactions with pair contracts and is used heavily by the Router contracts. It contains eight functions that are not state-changing,.…

How the TWAP Oracle in Uniswap v2 Works
How the TWAP Oracle in Uniswap v2 Works What exactly is “price” in Uniswap? Suppose we have 1 Ether and 2,000 USDC in a pool. This implies that the price…

Checklist for building a Uniswap V2 clone
Checklist for building a Uniswap V2 clone It’s very educational to rebuild Uniswap v2 from scratch using modern Solidity (or Huff if you really want to do it in hard…

Uniswap V2 Mint and Burn Functions Explained
Uniswap V2 Mint and Burn Functions Explained The lifecycle of Uniswap V2 is someone mints LP tokens (supplies liquidity, i.e. tokens to the pool) for the first time, then a…

Breaking Down the Uniswap V2 Swap Function
Breaking Down the Uniswap V2 Swap Function Uniswap V2’s swap function is cleverly designed, but many devs find its logic counterintuitive the first time they encounter it. This article explains…

How Tornado Cash Works (Line by Line for Devs)
How Tornado Cash Works (Line by Line for Devs) Introduction to Tornado Cash Tornado cash is a cryptocurrency smart contract mixer that enables users to deposit crypto with one address…