Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Erc4626 extension #465

Open
wants to merge 32 commits into
base: main
Choose a base branch
from

Conversation

Ifechukwudaniel
Copy link

@Ifechukwudaniel Ifechukwudaniel commented Dec 19, 2024

Resolves #356

PR Checklist

  • Tests
  • Documentation
  • Changelog

Copy link

netlify bot commented Dec 19, 2024

Deploy Preview for contracts-stylus canceled.

Name Link
🔨 Latest commit d7939ea
🔍 Latest deploy log https://app.netlify.com/sites/contracts-stylus/deploys/676a48d8b5d7950008653b5f

@Ifechukwudaniel
Copy link
Author

I just stared this

@0xNeshi
Copy link
Collaborator

0xNeshi commented Dec 19, 2024

I'll convert this to a draft, so that the team does not invest time reviewing a WIP

@0xNeshi 0xNeshi marked this pull request as draft December 19, 2024 07:38

fn _deposit(&mut self,caller:Address,receiver:Address, assets:U256, shares:U256) -> Result<(), Error> {
// _SafeERC20.safeTransferFrom(_asset, caller, address(this), assets);
self._asset._mint(receiver, shares)?;
Copy link
Collaborator

@0xNeshi 0xNeshi Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be careful, you should not be calling _mint on the asset, but on the contract itself (see Solidity version).

Maybe it would be helpful to take inspiration from the Erc20FlashMint extension PR, as it has a similar design problem as ERC4626, namely that you need to extend ERC20, but still leverage ERC20's functions (which is a problem in Stylus due to lack of proper inheritance).

One way to solve this is to make each ERC4626 function that needs to call base contract's functions (e.g. _mint) accept an erc20: &mut Erc20 parameter, which acts as a reference to the base/parent contract.

Note that you'll still need _asset: Erc20 as a storage field. But you can make it a generic IErc20 type, so that users of the library are free to define their own Erc20 implementation (as is done in Solidity) (@bidzyyys @qalisander wdyt about this approach?):

#[storage] // this is the new convention we use for defining storage
#[allow(clippy::pub_underscore_fields)]
pub struct ERC4626<T: IErc20 + StorageType> {
    pub _asset: T, 
    pub _underlying_decimals: StorageU8,
}

NOTE: although _asset and _underlying_decimals should be private, we're making them public for the purposes of testing. This will be changed at some point.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks you

@0xNeshi 0xNeshi self-requested a review December 19, 2024 09:26
@Ifechukwudaniel
Copy link
Author

@0xNeshi
The Solidity implementation uses safe transfer

SafeERC20.safeTransferFrom(_asset, caller, address(this), assets);

In the Solidity implementation, SafeERC20 is a utility library. However, in the Stylus implementation, SafeTransfer is a contract. What do you think

@0xNeshi
Copy link
Collaborator

0xNeshi commented Dec 19, 2024

@0xNeshi The Solidity implementation uses safe transfer

SafeERC20.safeTransferFrom(_asset, caller, address(this), assets);

In the Solidity implementation, SafeERC20 is a utility library. However, in the Stylus implementation, SafeTransfer is a contract. What do you think

See how VestingWallet handles this

@Ifechukwudaniel
Copy link
Author

Should I implement something like mulDiv for U256 MATH?

function _convertToShares(uint256 assets, Math.Rounding rounding) internal view virtual returns (uint256) {
        return assets.mulDiv(totalSupply() + 10 ** _decimalsOffset(), totalAssets() + 1, rounding);
    }

@0xNeshi

@Ifechukwudaniel Ifechukwudaniel marked this pull request as ready for review December 24, 2024 00:09
@Ifechukwudaniel
Copy link
Author

Should I add the security concerns like docs erc4626

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature]: ERC4626 extension
2 participants