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: SafeErc20 utility #289

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

Conversation

0xNeshi
Copy link

@0xNeshi 0xNeshi commented Sep 17, 2024

Resolves #279

I think I did everything that is required. Let me know if I missed something.

If you prefer a different code style, naming convention and/or anything similar, let me know and I'll update (e.g. check out how tests for this are organized).

Used Solidity version SafeERC20 as inspiration for both implementation and tests.

PR Checklist

  • Tests
  • Documentation

Copy link

netlify bot commented Sep 17, 2024

Deploy Preview for contracts-stylus canceled.

Name Link
🔨 Latest commit f006d42
🔍 Latest deploy log https://app.netlify.com/sites/contracts-stylus/deploys/670e8a1186b9430008f5b474

@0xNeshi 0xNeshi mentioned this pull request Sep 17, 2024
1 task
@0xNeshi 0xNeshi marked this pull request as draft September 17, 2024 14:38
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.

Hey @0xNeshi and thanks for your contribution!
Appreciate it!

Hard to tell for me right now what kind of design should be the best for SafeErc20 with current state of the stylus-sdk.

IMHO we can try to build a working example first with end-to-end tests. Then later dive into optimization if it will make sense.
There is no good interoperability between rust struct, that defines a contract in stylus and an external call to that contract (deployed at some address) as it is in solidity.
So we can try to add Erc20 solidity abi into sol_interface! macro like IERC721Receiver example.
And use call contract's type generated by sol_interface! as a first argument like it is in solidity SafeErc20 example.
But we should process boolean responses from underlying Erc20 contract differently as it is specified in the solidity counterpart.

Then we can use IErc20 trait to create wrapped Erc20 contract. It should route most calls externally using the Call::new(..) api from the stylus-sdk (like Erc721Receiver example)

How do you feel about this plan?

@0xNeshi 0xNeshi marked this pull request as ready for review October 2, 2024 11:40
data: &[u8],
) -> Result<(), Error> {
match RawCall::new()
.gas(gas_left())
Copy link
Author

Choose a reason for hiding this comment

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

It's not really clear to me whether explicitly setting the gas remaining is necessary, so I set it just in case.

token: Address,
data: &[u8],
) -> Result<(), Error> {
match RawCall::new()
Copy link
Author

Choose a reason for hiding this comment

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

As far as I understand, RawCall is unsafe and should be called with flush_storage_cache when reentrancy is enabled. Let me know how you'd like to do this.

token: Address,
data: &[u8],
) -> Result<(), Error> {
match RawCall::new()
Copy link
Author

Choose a reason for hiding this comment

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

The reason I opted for RawCall here is that it enables us to set the return data size to just 32 bytes, which assumes the underlying ERC20 returns a boolean. It also does not make assumptions about the return value of the underlying ERC20.

@qalisander
Copy link
Member

@0xNeshi we're quite busy currently with security audit fixes and another huge pr review.
We will come and give a thorough review of your pr soon, once we finalize other things!
cc @bidzyyys

@0xNeshi
Copy link
Author

0xNeshi commented Oct 7, 2024

No problem!

@bidzyyys
Copy link
Collaborator

bidzyyys commented Oct 7, 2024

No problem!

Thanks @0xNeshi for your patience, we should have more time later this week!

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]: SafeERC20 utility
3 participants