ERC-4626: Extending ERC-20 for Interest Management
How the newly finalized standard works and can help you with Defi
Many Defi projects have an ERC-20 token which represents ownership over an interest generating asset. This is for example the case for lending/borrowing platforms (money markets) like Compound and Aave. As a lender you will receive aDAI or cDAI. And since lenders receive interest payments for borrowers, the exchange rate from aDAI/cDAI back to DAI changes all the time. The longer you wait, the more DAI you will receive.

Or you have an asset like xSushi which we explained here, that will generate income from the trading fees on SushiSwap. The xSushi will represent ownership over all the Sushi tokens currently in the contract. This is very similar to the previous lending platform.
And then you also have aggregators like Yearn or Rari Vaults. Those are services that you can put your funds in and that have some mechanism to put those funds into various other projects that generate yield. And the ownership over the funds of such so-called vaults are also handled by an ERC-20 token. And since the vault funds will increase over time, you again have a similar situation in as on a lending platform.
So now that we have all these services implementing something similar, it only makes sense to standardize it. Rather than adhering to dozens of different interfaces, an aggregator has to implement only one. Or let's say you want to implement additional features for such tokens, you also benefit from having a single standardized interface. This is ERC-4626. So if you want to implement your own interest generating token, an aggregator or just want to learn more about Defi, this post is for you.

Overview of ERC-4626
The standard describes a vault which generates interests in the form of a single ERC-20 token. The vault itself is an ERC-20 standard extension. In theory vaults may generate interest over various tokens. But the first and most important standard is just for a single ERC-20 token. In the future one could imagine more advanced standards for multiple tokens and possibly not only ERC-20. For now it's kept as simple as possible.
On a high-level there's functionality to deposit and redeem the vault ownership token for the underlying asset. There are two functions each, one for using the amounts of the underlying as input and one for using the amounts of the ownership token (shares) as input. And then you have some additional helper functions for conversions, receiving maximum amounts and previews. Let's get into the details!
Below infographic was provided by MidasCapital, thank you!

1. The Execution Functions
You can call deposit
to pass an amount of the underlying asset, e.g. DAI. The asset will be transferred and in return you will receive shares, e.g. cDAI, in the normal case determined by the current conversion rate.
function deposit(uint256 assets, address receiver)
external
returns (uint256 shares);
Similarly to deposit
, you can also use mint
. Here instead of passing an amount of the underlying asset, you will pass the amount of shares. And the amount of the underlying being used and transferred will be determined when executing the call.
function mint(uint256 shares, address receiver)
external
returns (uint256 assets);
You can then call redeem
to convert shares back into the underlying asset. In redeem you will pass the amount of shares to be burnt and the amount of the underlying asset will be determined when executing the call.
function redeem(
uint256 shares,
address receiver,
address owner
) external returns (uint256 assets);
Like for the deposit
/mint
, you also have a second function here where instead you can pass the amount of assets you'd like to withdraw
. And the amount of shares that have to be burnt will be determined when executing the call.
function withdraw(
uint256 assets,
address receiver,
address owner
) external returns (uint256 shares);
2. Max Functions
Then you will have a bunch of view functions to read the maximally allowed inputs for each previous function.

function maxDeposit(address receiver) external view returns (uint256);
Returns the maximally allowed amount of the underlying asset that can be deposited and passed as input to the deposit function.
function maxMint(address receiver) external view returns (uint256);
Returns the maximally allowed amount of the shares that can be minted and passed as input to the mint function.
function maxWithdraw(address owner) external view returns (uint256);
Returns the maximally allowed amount of the underlying asset that can be withdrawn for the owner. It implies the owner may call the withdraw function with a value up to that amount.
function maxRedeem(address owner) external view returns (uint256);
Returns the maximally allowed amount of shares that can be burnt for the owner. It implies the owner may call the redeem function with a value up to that amount.
3. Assets
This view function will simply return the address of the ERC-20 token contract used as the underlying asset, e.g. the DAI token address.
function asset() external view returns (address);
This view function will return the total amount of the underlying asset that is managed by the current vault.
function totalAssets() external view returns (uint256);
4. Converters
There are also two conversion view functions. Rather than having an exchange rate to return those two functions can directly be used to tell you how much current assets would be in shares and vice versa.
To convert an amount of the underlying asset into shares, you can use:
function convertToShares(uint256 assets) external view returns (uint256);
To convert an amount of shares into the underlying asset, you can use:
function convertToAssets(uint256 shares) external view returns (uint256);
5. Previews
Lastly, to allow for simulating the effects of execution functions at the current block, given current on-chain conditions, there are (pre-)view functions available:
function previewDeposit(uint256 assets) external view returns (uint256);
function previewMint(uint256 shares) external view returns (uint256);
function previewWithdraw(uint256 assets) external view returns (uint256);
function previewRedeem(uint256 shares) external view returns (uint256);
Implementation Example
You can find an opinionated example implementation here.
And here's a version that's slightly easier to understand for educational purposes:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
import {ERC20, IERC4626} from "./interfaces/IERC4626.sol";
contract ERC4626 is IERC4626, ERC20 {
event Deposit(
address indexed caller,
address indexed owner,
uint256 assets,
uint256 shares
);
event Withdraw(
address indexed caller,
address indexed receiver,
address indexed owner,
uint256 assets,
uint256 shares
);
ERC20 public immutable asset;
constructor(
ERC20 _asset,
string memory _name,
string memory _symbol
) ERC20(_name, _symbol, _asset.decimals()) {
asset = _asset;
}
function deposit(uint256 assets, address receiver) external returns (uint256) {
uint256 shares = previewDeposit(assets);
require(shares != 0, "ZERO_SHARES");
asset.safeTransferFrom(msg.sender, address(this), assets);
_mint(receiver, shares);
emit Deposit(msg.sender, receiver, assets, shares);
return shares;
}
function mint(uint256 shares, address receiver) external returns (uint256) {
uint256 assets = previewMint(shares);
asset.safeTransferFrom(msg.sender, address(this), assets);
_mint(receiver, shares);
emit Deposit(msg.sender, receiver, assets, shares);
return shares;
}
function withdraw(
uint256 assets,
address receiver,
address owner
) external returns (uint256) {
uint256 shares = previewWithdraw(assets);
if (msg.sender != owner) {
allowance[owner][msg.sender] -= shares;
}
_burn(owner, shares);
asset.safeTransfer(receiver, assets);
emit Withdraw(msg.sender, receiver, owner, assets, shares);
return shares;
}
function redeem(
uint256 shares,
address receiver,
address owner
) external returns (uint256) {
if (msg.sender != owner) {
allowance[owner][msg.sender] -= shares;
}
uint256 assets = previewRedeem(shares);
require(assets != 0, "ZERO_ASSETS");
_burn(owner, shares);
asset.safeTransfer(receiver, assets);
emit Withdraw(msg.sender, receiver, owner, assets, shares);
return assets;
}
function totalAssets() public view returns (uint256) {
return asset.balanceOf(address(this));
}
function maxDeposit(address) external view returns (uint256) {
return type(uint256).max;
}
function maxMint(address) external view returns (uint256) {
return type(uint256).max;
}
function maxWithdraw(address owner) external view returns (uint256) {
return convertToAssets(balanceOf[owner]);
}
function maxRedeem(address owner) external view returns (uint256) {
return balanceOf[owner];
}
function convertToShares(uint256 assets) public view returns (uint256) {
if (totalSupply == 0) {
return assets;
}
return (assets * totalSupply) / totalAssets();
}
function convertToAssets(uint256) public view returns (uint256) {
if (totalSupply == 0) {
return shares;
}
return (shares * totalAssets()) / totalSupply;
}
function previewDeposit(uint256 assets) public view returns (uint256) {
return convertToShares(assets);
}
function previewMint(uint256 shares) public view returns (uint256) {
return convertToAssets(shares);
}
function previewWithdraw(uint256 assets) public view returns (uint256) {
return convertToShares(assets);
}
function previewRedeem(uint256 shares) public view returns (uint256) {
return convertToAssets(shares);
}
}
Solidity Developer
More great blog posts from Markus Waas
How to use ChatGPT with Solidity
Using the Solidity Scholar and other GPT tips
How to integrate Uniswap 4 and create custom hooks
Let's dive into Uniswap v4's new features and integration
How to integrate Wormhole in your smart contracts
Entering a New Era of Blockchain Interoperability
Solidity Deep Dive: New Opcode 'Prevrandao'
All you need to know about the latest opcode addition
How Ethereum scales with Arbitrum Nitro and how to use it
A blockchain on a blockchain deep dive
The Ultimate Merkle Tree Guide in Solidity
Everything you need to know about Merkle trees and their future
The New Decentralized The Graph Network
What are the new features and how to use it
zkSync Guide - The future of Ethereum scaling
How the zero-knowledge tech works and how to use it
Exploring the Openzeppelin CrossChain Functionality
What is the new CrossChain support and how can you use it.
Deploying Solidity Contracts in Hedera
What is Hedera and how can you use it.
Writing ERC-20 Tests in Solidity with Foundry
Blazing fast tests, no more BigNumber.js, only Solidity
Advancing the NFT standard: ERC721-Permit
And how to avoid the two step approve + transferFrom with ERC721-Permit (EIP-4494)
Moonbeam: The EVM of Polkadot
Deploying and onboarding users to Moonbeam or Moonriver
Advanced MultiSwap: How to better arbitrage with Solidity
Making multiple swaps across different decentralized exchanges in a single transaction
Deploying Solidity Smart Contracts to Solana
What is Solana and how can you deploy Solidity smart contracts to it?
Smock 2: The powerful mocking tool for Hardhat
Features of smock v2 and how to use them with examples
How to deploy on Evmos: The first EVM chain on Cosmos
Deploying and onboarding users to Evmos
EIP-2535: A standard for organizing and upgrading a modular smart contract system.
Multi-Facet Proxies for full control over your upgrades
MultiSwap: How to arbitrage with Solidity
Making multiple swaps across different decentralized exchanges in a single transaction
The latest tech for scaling your contracts: Optimism
How the blockchain on a blockchain works and how to use it
Ultimate Performance: The Aurora Layer2 Network
Deploying and onboarding users to the Aurora Network powered by NEAR Protocol
What is ecrecover in Solidity?
A dive into the waters of signatures for smart contracts
How to use Binance Smart Chain in your Dapp
Deploying and onboarding users to the Binance Smart Chain (BSC)
Using the new Uniswap v3 in your contracts
What's new in Uniswap v3 and how to integrate Uniswap v3
What's coming in the London Hardfork?
Looking at all the details of the upcoming fork
Welcome to the Matrix of blockchain
How to get alerted *before* getting hacked and prevent it
The Ultimate Ethereum Mainnet Deployment Guide
All you need to know to deploy to the Ethereum mainnet
SushiSwap Explained!
Looking at the implementation details of SushiSwap
Solidity Fast Track 2: Continue Learning Solidity Fast
Continuing to learn Solidity fast with the advanced basics
What's coming in the Berlin Hardfork?
Looking at all the details of the upcoming fork
Using 1inch ChiGas tokens to reduce transaction costs
What are gas tokens and example usage for Uniswap v2
Openzeppelin Contracts v4 in Review
Taking a look at the new Openzeppelin v4 Release
EIP-3156: Creating a standard for Flash Loans
A new standard for flash loans unifying the interface + wrappers for existing ecosystems
Tornado.cash: A story of anonymity and zk-SNARKs
What is Tornado.cash, how to use it and the future
High Stakes Roulette on Ethereum
Learn by Example: Building a secure High Stakes Roulette
How to implement generalized meta transactions
We'll explore a powerful design for meta transactions based on 0x
Utilizing Bitmaps to dramatically save on Gas
A simple pattern which can save you a lot of money
Using the new Uniswap v2 as oracle in your contracts
How does the Uniswap v2 oracle function and how to integrate with it
Smock: The powerful mocking tool for Hardhat
Features of smock and how to use them with examples
How to build and use ERC-721 tokens in 2021
An intro for devs to the uniquely identifying token standard and its future
Trustless token management with Set Protocol
How to integrate token sets in your contracts
Exploring the new Solidity 0.8 Release
And how to upgrade your contracts to Solidity 0.8
How to build and use ERC-1155 tokens
An intro to the new standard for having many tokens in one
Leveraging the power of Bitcoins with RSK
Learn how RSK works and how to deploy your smart contracts to it
Solidity Fast Track: Learn Solidity Fast
'Learn X in Y minutes' this time with X = Solidity 0.7 and Y = 20
Sourcify: The future of a Decentralized Etherscan
Learn how to use the new Sourcify infrastructure today
Integrating the 0x API into your contracts
How to automatically get the best prices via 0x
How to build and use ERC-777 tokens
An intro to the new upgraded standard for ERC-20 tokens
COMP Governance Explained
How Compound's Decentralized Governance is working under the hood
How to prevent stuck tokens in contracts
And other use cases for the popular EIP-165
Understanding the World of Automated Smart Contract Analyzers
What are the best tools today and how can you use them?
A Long Way To Go: On Gasless Tokens and ERC20-Permit
And how to avoid the two step approve + transferFrom with ERC20-Permit (EIP-2612)!
Smart Contract Testing with Waffle 3
What are the features of Waffle and how to use them.
How to use xDai in your Dapp
Deploying and onboarding users to xDai to avoid the high gas costs
Stack Too Deep
Three words of horror
Integrating the new Chainlink contracts
How to use the new price feeder oracles
TheGraph: Fixing the Web3 data querying
Why we need TheGraph and how to use it
Adding Typescript to Truffle and Buidler
How to use TypeChain to utilize the powers of Typescript in your project
Integrating Balancer in your contracts
What is Balancer and how to use it
Navigating the pitfalls of securely interacting with ERC20 tokens
Figuring out how to securely interact might be harder than you think
Why you should automatically generate interests from user funds
How to integrate Aave and similar systems in your contracts
How to use Polygon (Matic) in your Dapp
Deploying and onboarding users to Polygon to avoid the high gas costs
Migrating from Truffle to Buidler
And why you should probably keep both.
Contract factories and clones
How to deploy contracts within contracts as easily and gas-efficient as possible
How to use IPFS in your Dapp?
Using the interplanetary file system in your frontend and contracts
Downsizing contracts to fight the contract size limit
What can you do to prevent your contracts from getting too large?
Using EXTCODEHASH to secure your systems
How to safely integrate anyone's smart contract
Using the new Uniswap v2 in your contracts
What's new in Uniswap v2 and how to integrate Uniswap v2
Solidity and Truffle Continuous Integration Setup
How to setup Travis or Circle CI for Truffle testing along with useful plugins.
Upcoming Devcon 2021 and other events
The Ethereum Foundation just announced the next Devcon in 2021 in Colombia
The Year of the 20: Creating an ERC20 in 2020
How to use the latest and best tools to create an ERC-20 token contract
How to get a Solidity developer job?
There are many ways to get a Solidity job and it might be easier than you think!
Design Pattern Solidity: Mock contracts for testing
Why you should make fun of your contracts
Kickstart your Dapp frontend development with create-eth-app
An overview on how to use the app and its features
The big picture of Solidity and Blockchain development in 2020
Overview of the most important technologies, services and tools that you need to know
Design Pattern Solidity: Free up unused storage
Why you should clean up after yourself
How to setup Solidity Developer Environment on Windows
What you need to know about developing on Windows
Avoiding out of gas for Truffle tests
How you do not have to worry about gas in tests anymore
Design Pattern Solidity: Stages
How you can design stages in your contract
Web3 1.2.5: Revert reason strings
How to use the new feature
Gaining back control of the internet
How Ocelot is decentralizing cloud computing
Devcon 5 - Review
Impressions from the conference
Devcon 5 - Information, Events, Links, Telegram
What you need to know
Design Pattern Solidity: Off-chain beats on-chain
Why you should do as much as possible off-chain
Design Pattern Solidity: Initialize Contract after Deployment
How to use the Initializable pattern
Consensys Blockchain Jobs Report
What the current blockchain job market looks like
Provable — Randomness Oracle
How the Oraclize random number generator works
Solidity Design Patterns: Multiply before Dividing
Why the correct order matters!
Devcon 5 Applications closing in one week
Devcon 5 Applications closing
Randomness and the Blockchain
How to achieve secure randomness for Solidity smart contracts?