How to Use ERC-4626 with Your Smart Contract

·

Overview

If you've built protocols or tools that interact with multiple DeFi tokens, you've likely faced the challenge of inconsistent code logic across protocols. The ERC-4626 Tokenized Vault Standard solves this by unifying yield-bearing vault interactions under a single interface. Major protocols like Yearn V3 already use this standard.

This guide covers:


What You’ll Learn

Yield-bearing vault mechanics
ERC-4626 standard components
Deploying a vault contract

Prerequisites


Yield-Bearing Vaults Explained

Yield-bearing vaults are DeFi smart contracts that:

  1. Pool user deposits (e.g., USDC, ETH).
  2. Generate yield via strategies (lending, liquidity provision, etc.).
  3. Issue tokens (e.g., vUSDC) representing a user’s share.

Examples:

Benefits:


Challenges Without ERC-4626

Integrating multiple protocols requires:

👉 ERC-4626 standardizes this process.


ERC-4626: The Tokenized Vault Standard

Key Features:

Extends ERC-20 with:


Build an ERC-4626 Vault

Step 1: Create a Sepolia Endpoint

  1. Sign up for QuickNode.
  2. Deploy an Ethereum Sepolia endpoint.
  3. Copy the HTTP Provider URL.

Step 2: Configure MetaMask


Step 3: Deploy the Vault Contract

Code Highlights:

// TokenVault.sol  
contract TokenVault is ERC4626 {  
    mapping(address => uint256) public shareHolder;  

    constructor(ERC20 _asset, string memory _name, string memory _symbol)  
        ERC4626(_asset, _name, _symbol) {}  

    function _deposit(uint _assets) public {  
        deposit(_assets, msg.sender);  
        shareHolder[msg.sender] += _assets;  
    }  

    function _withdraw(uint _shares, address _receiver) public {  
        uint256 assets = _shares + (10 * _shares) / 100; // 10% yield  
        redeem(assets, _receiver, msg.sender);  
        shareHolder[msg.sender] -= _shares;  
    }  
}  

Deployment Steps:

  1. Deploy an ERC-20 asset token (e.g., USDC).
  2. Mint test tokens (USDC.mint()).
  3. Approve the vault to spend USDC (USDC.approve()).
  4. Deploy the TokenVault with:

    • Asset token address.
    • Vault name/symbol (e.g., "vUSDC").

Testing the Vault

  1. Deposit: Call _deposit(10000) → Receives 10000 vUSDC.
  2. Check Balances:

    • totalAssets(): Vault’s USDC balance.
    • totalAssetsOfUser(): User’s USDC balance.
  3. Withdraw: Call _withdraw(1000, user) → Receives 1100 USDC (10% yield).

FAQ

Q1: What’s the advantage of ERC-4626?

A: Standardizes yield vaults, reducing integration overhead.

Q2: Can I use existing ERC-20 tokens?

A: Yes! Pass the token address to the vault constructor.

Q3: How is yield calculated?

A: In this example, yield is fixed at 10%. Real vaults use dynamic strategies.


Conclusion

By adopting ERC-4626, developers can:

👉 Join QuickNode’s Discord for more!

We ❤️ Feedback! Let us know how we can improve this guide.


Ready to Build?

Start with a free QuickNode endpoint today!