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

feat: add ERC20FlashMint extension #407

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

Conversation

Ifechukwudaniel
Copy link

@Ifechukwudaniel Ifechukwudaniel commented Nov 14, 2024

NOTE: It is impossible to write any unit tests until it is possible to mock contract::address() in tests (see OpenZeppelin/stylus-test-helpers#5).

Resolves #355

PR Checklist

  • Tests
  • Documentation
  • Changelog

Copy link

netlify bot commented Nov 14, 2024

Deploy Preview for contracts-stylus canceled.

Name Link
🔨 Latest commit a7c0160
🔍 Latest deploy log https://app.netlify.com/sites/contracts-stylus/deploys/67357c1f782e3e00081ecd3f

Copy link

netlify bot commented Nov 14, 2024

Deploy Preview for contracts-stylus canceled.

Name Link
🔨 Latest commit 1ba51fb
🔍 Latest deploy log https://app.netlify.com/sites/contracts-stylus/deploys/6769107b888750000807398f

@Ifechukwudaniel
Copy link
Author

This is my initial draft

Copy link
Collaborator

@0xNeshi 0xNeshi left a comment

Choose a reason for hiding this comment

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

Hi @Ifechukwudaniel!
Thanks for the contribution!

The PR overall looks good, let's polish it a bit

contracts/src/flashloan/borrower.rs Outdated Show resolved Hide resolved
contracts/src/flashloan/borrower.rs Outdated Show resolved Hide resolved
contracts/src/token/erc20/extensions/flashmint.rs Outdated Show resolved Hide resolved
contracts/src/token/erc20/extensions/flashmint.rs Outdated Show resolved Hide resolved
contracts/src/token/erc20/extensions/flashmint.rs Outdated Show resolved Hide resolved
contracts/src/token/erc20/extensions/flashmint.rs Outdated Show resolved Hide resolved
pub fn _flash_fee_receiver(&self) -> Address {
Address::ZERO
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing unit tests

pub fn _flash_fee_receiver(&self) -> Address {
Address::ZERO
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing integration tests

contracts/src/token/erc20/extensions/flashmint.rs Outdated Show resolved Hide resolved
contracts/src/token/erc20/extensions/flashmint.rs Outdated Show resolved Hide resolved
@Ifechukwudaniel
Copy link
Author

@0xNeshi what do you think about the new implementation for the extension

@0xNeshi 0xNeshi marked this pull request as draft December 20, 2024 08:05
fn flash_fee(
&self,
token: Address,
amount: U256,
Copy link
Collaborator

@0xNeshi 0xNeshi Dec 20, 2024

Choose a reason for hiding this comment

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

It is not clear which terminology to use here:

  • ERC20 uses value (see transfer def)
  • ERC3156 uses amount (see EIP)

The Solidity team had the same problem. They opted for keeping things consistent with ERC20. Our implementation does not; instead it is consistent with the EIP.

Wdyt?

Copy link
Member

Choose a reason for hiding this comment

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

Isn't it in solidity value is used?

Copy link
Collaborator

@0xNeshi 0xNeshi Dec 20, 2024

Choose a reason for hiding this comment

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

Yea, I mentioned that above:

They opted for keeping things consistent with ERC20

Should we align with the Solidity team or with official EIP, wdyt?

@0xNeshi 0xNeshi marked this pull request as ready for review December 20, 2024 08:52
Copy link
Member

@qalisander qalisander left a comment

Choose a reason for hiding this comment

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

Good job!
I have a few comments and I'm also concerned about solidity files, that we copied from solidity repo.
Can we use a url instead of copying?

fn flash_fee(
&self,
token: Address,
amount: U256,
Copy link
Member

Choose a reason for hiding this comment

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

Isn't it in solidity value is used?

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "../../solidity-contracts/contracts/interfaces/IERC3156FlashBorrower.sol";
Copy link
Member

Choose a reason for hiding this comment

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

Can't we import them from by url like here

Copy link
Collaborator

@0xNeshi 0xNeshi Dec 20, 2024

Choose a reason for hiding this comment

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

solc doesn't work with urls. The reason we can do this in other Solidity files is that those files are, to be frank, useless - they're never used anywhere, so no builds are crashed.

I'm not even sure why we have them in the repo tbh 🤔

/**
* @dev Interface of the ERC-20 standard as defined in the ERC.
*/
interface IERC20 {
Copy link
Member

Choose a reason for hiding this comment

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

I hope we should avoid to copy sol files from solidity library and just use url references

Copy link
Collaborator

Choose a reason for hiding this comment

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

The problem is having to deploy ERC3156FlashBorrowerMock contract to be able to run in bench for flash_loan (see bench); and to be able to implement this contract locally, it seems necessary to have all the dependencies locally as well...

Do you if there's a way to import existing examples/erc20-flash-mint/tests/mock/borrower.rs implementation into benches and reuse it? 🤔 This would make the duplicate here redundant.

address!("dce82b5f92c98f27f116f70491a487effdb6a2aa");

#[motsu::test]
#[ignore]
Copy link
Member

Choose a reason for hiding this comment

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

I see that we can check these test cases with approach similar to Erc721 Metadata. Declare a contract that contains Erc20 and Erc20FlashMint inside. Similar to what you have in FlashMintExample.

Then test cases should work

Copy link
Member

@qalisander qalisander Dec 20, 2024

Choose a reason for hiding this comment

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

Otherwise we cannot use two contract's storages in motsu test case

Copy link
Member

Choose a reason for hiding this comment

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

Since FlashMint relies upon contract::address() make sense to wait a bit, when motsu can support it

Comment on lines +60 to +68
# The `erc20-flash-mint-example` package enables the `reentrant` feature
# on `openzeppelin-stylus`, which combined with `--all-targets` seems to
# cause a linker issue "error: undefined symbol: storage_flush_cache".
#
# This is a known problem with workspace feature unification:
# https://github.com/rust-lang/cargo/issues/4463
#
# To deal with this, we simply ignore `erc20-flash-mint-example` from the compilation.
run: cargo nextest run --locked --features std --all-targets --workspace --exclude erc20-flash-mint-example
Copy link
Collaborator

@0xNeshi 0xNeshi Dec 23, 2024

Choose a reason for hiding this comment

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

I think we could omit the whole of examples/* directory for this step, as we're only interested in unit tests, it would speed up the step's execution

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]: ERC20FlashMint extension
4 participants