Giter Club home page Giter Club logo

erc4626-contracts's Introduction

ERC4626 Ecosystem Utilities

This repository contains open-source ERC4626 infrastructure that can be used by solidity developers using EIP-4626, including ERC4626Router (the canonical ERC-4626 multicall router) and xERC4626. Powered by forge and solmate.

About ERC-4626

EIP-4626: The Tokenized Vault Standard is an ethereum application developer interface for building token vaults and strategies. It is meant to consolidate development efforts around "single token strategies" such as lending, yield aggregators, and single-sided staking.

ERC4626Router and Base

ERC-4626 standardizes the interface around depositing and withdrawing tokens from strategies.

The ERC4626 Router is an ecosystem utility contract (like WETH) which can route tokens in and out of multiple ERC-4626 strategies in a single call. Its architecture was inspired by the Uniswap V3 multicall router.

Basic supported features include:

  • withdrawing from some Vault A and redepositing to Vault B
  • wrapping and unwrapping WETH
  • managing token approvals/transfers
  • slippage protection

Ultimately the ERC4626 router can support an arbitrary number of withdrawals, deposits, and even distinct token types in a single call, subject to the block gas limit.

The router is split between the base ERC4626RouterBase which only handles the ERC4626 mutable methods (deposit/withdraw/mint/redeem) and the main router ERC4626Router which includes support for common routing flows and max logic.

Using the Router

The router is a multicall-style router, meaning it can atomically perform any number of supported actions on behalf of the message sender.

Some example user flows are listed below.

Vault deposit (requires ERC-20 approval of vault underlying asset before calling OR use a self-permit):

  • PeripheryPayments.approve(asset, vault, amount) approves the vault to spend asset of the router
  • ERC4626Router.depositToVault

WETH vault redeem (requires the router to have ERC-20 approval of the vault shares before calling OR use a self-permit):

  • ERC4626Router.redeem to the router
  • PeripheryPayments.unwrapWETH9 to the user destination

2 to 1 vault consolidation (requires ERC-20 approval of both source vault underlying assets OR self-permit):

  • ERC4626RouterBase.withdraw (or redeem) on vault A to the router
  • ERC4626RouterBase.withdraw (or redeem) on vault B to the router
  • PeripheryPayments.approve(asset, vault C, amount) approves the vault to spend asset of the router
  • ERC4626RouterBase.deposit on destination vault C

It is REQUIRED to use multicall to interact across multi-step user flows. The router is stateless other than holding token approvals for vaults it interacts with. Any tokens that end a transaction in the router can be permissionlessly withdrawn by any address, likely an MEV searcher, so make sure to complete all multicalls with token withdrawals to an end user address.

It is recommended to max approve vaults, and check whether a vault is already approved before interacting with the vault. This can save user gas. In cases where the number of required steps in a user flow is reduced to 1, a direct call can be used instead of multicall.


The router will be immutably deployed to router.4626.eth as soon as the contracts are frozen and reviewed.

ERC4626RouterBase - basic ERC4626 methods

ERC4626Router - combined ERC4626 methods

PeripheryPayments - WETH and ERC-20 utility methods

Multicall - multicall utility

SelfPermit - user approvals to the router with EIP-712 and EIP-2612

Extending the Router

The router can be imported and extended. Many ERC-4626 use cases include additional methods which may want to be included in a multicall router.

Importing via npm: coming soon Importing via forge: forge install Fei-Protocol/ERC4626

Examples:

xERC4626

An "xToken" popularized by SushiSwap with xSUSHI is a single-sided autocompounding token rewards module.

xTokens were improved upon by Zephram Lou with xERC20 to include manipulation resistant reward distributions.

Because xTokens are a perfect use case of ERC-4626, a base utility called xERC4626 is included in this repo.

xERC4626 improvements:

  • 4626 complete interface compatibility
  • completely internal accounting to prevent all forms of exchange rate manipulation

xERC4626 examples:

erc4626-contracts's People

Contributors

eswak avatar jetjadeja avatar joeysantoro avatar sriyantra avatar thomas-waite avatar vpepo avatar

Stargazers

Ahmed Ihsan Tawfeeq avatar Yorke Rhodes avatar Cowboy avatar Avi Atkin avatar  avatar Nick avatar  avatar Chia Yong Kang avatar Asmee Dhungana avatar tanliwei avatar Gokul Alex avatar Victor Cañada Ojeda avatar ALFREDO J LOPEZ H (alfredolopez80.eth) avatar Ahmet avatar  avatar Beni Ben zikry avatar  avatar Bakuchi avatar Aleksey Bykhun avatar Frontier avatar maguro avatar 0xhaz avatar Egor Dergunov avatar Mohammad Mahdi Keshavarz avatar daweth avatar  avatar Cami Cast avatar Jake avatar Darpit Rangari avatar Hemendra sharma avatar Joe avatar  avatar Sergio Luciano de Souza Joselli avatar Matt Stam avatar @askCasmir  avatar Jiaji Wei avatar Mariia Synelnyk avatar  avatar Gilgameš avatar Edward Kim avatar Robert Leifke avatar  avatar Alfred Gaillard avatar vanbeethoven.eth avatar 禅 avatar Yuanye Ge avatar John C avatar porco avatar DJICKO avatar Temirzhan Yussupov avatar UnbanksyTV avatar Bookland avatar sfter avatar 0x4E33 avatar Tamara avatar Lisandro Fernández avatar Security.eth avatar John Lynch avatar  avatar  avatar Brando avatar Phy avatar Mayckon Barbosa da Silva avatar Max Mohammadi avatar AgusDuha avatar Christos avatar nael avatar clandestine.eth avatar zeez avatar indigo avatar Ahmed Ali avatar Merlin Egalite avatar Care Water avatar 0xhatsume avatar Matt (AG) avatar Tiago avatar supernoveau avatar AE_0h avatar Bayo Sodimu avatar  avatar jackgale.eth avatar  avatar Hritwik Tripathi avatar Shun Kakinoki avatar  avatar  avatar Picodes avatar Pablo Veyrat avatar Brian Le avatar Mick Hagen avatar Brendon Rofe avatar regohiro avatar Pedro Maia avatar Tadej Fius avatar Arjun Nemani avatar Arijit Das avatar Timur Badretdinov avatar wp-lai avatar K.Bapireddy avatar Mourad Kejji avatar

Watchers

danilo neves cruz avatar Adam Fraser avatar  avatar Caleb Ditchfield avatar  avatar ALFREDO J LOPEZ H (alfredolopez80.eth) avatar DJICKO avatar ZBR33ZY avatar  avatar

erc4626-contracts's Issues

Bug: Underflow error when redeeming to 0 after minting some rewards

👋 Love xERC4626 but I think I've hit a bug and I'm not sure of the best solution.

I wrote a test that shows the error case here: emersoncloud@bb26d16

The bug occurs when one tries to redeem shares for more assets than are in storedTotalAssets. This can happen after rewards have been minted, time advanced somewhere in the rewards cycle and syncRewards has not been called yet.

At that point the rewards show up in totalAssets [1], but haven't yet been moved to storedTotalAssets (which happens in syncRewards) [2].

So when beforeWithdraw is hit from the redeem call, we attempt to subtract the amount of underlying + rewards the caller is owed. But the rewards aren't reflected in storedTotalAssets yet and the subtraction underflows [3].

[1] https://github.com/fei-protocol/ERC4626/blob/643cd044fac34bcbf64e1c3790a5126fec0dbec1/src/xERC4626.sol#L45-L62

[2] https://github.com/fei-protocol/ERC4626/blob/643cd044fac34bcbf64e1c3790a5126fec0dbec1/src/xERC4626.sol#L87

[3] https://github.com/fei-protocol/ERC4626/blob/643cd044fac34bcbf64e1c3790a5126fec0dbec1/src/xERC4626.sol#L65-L68

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.