Skip to content

feat: update SafeErc20 to newer Solidity version #652

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

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

Conversation

simon0x1800
Copy link

This PR updates the SafeERC20 implementation to match the latest Solidity version, adding support for new features and improving error handling. The changes include:

  • Addition of try_safe_transfer and try_safe_transfer_from functions that return bool instead of reverting
  • Implementation of force_approve function for handling tokens requiring zero-reset approval
  • Addition of ERC1363 support with transfer_and_call_relaxed, transfer_from_and_call_relaxed, and approve_and_call_relaxed functions
  • Implementation of safe_increase_allowance and safe_decrease_allowance functions
  • Enhanced error handling with detailed Error enum types
  • Added Rust-specific safety improvements and idiomatic error handling

Resolves #620

PR Checklist

  • Tests
  • Documentation
  • Changelog

simon0x1800 and others added 17 commits May 2, 2025 14:05
…xed method for clarity and consistency in ISafeErc20 trait
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
…g with calldata for improved error handling and clarity
Copy link

netlify bot commented May 8, 2025

Deploy Preview for contracts-stylus canceled.

Name Link
🔨 Latest commit 155e686
🔍 Latest deploy log https://app.netlify.com/projects/contracts-stylus/deploys/685ebef8252c200008159c6b

@bidzyyys bidzyyys changed the title Feature/620 update safe erc20 feat: update SafeErc20 to newer Solidity version May 27, 2025
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.

Thanks for contributing, left some comments

/// * `to` - Account to transfer tokens to.
/// * `value` - Number of tokens to transfer.
///
/// # Returns
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove # Returns sections, we generally don't include this

Copy link
Collaborator

@0xNeshi 0xNeshi Jun 27, 2025

Choose a reason for hiding this comment

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

Please refrain from resolving if the change request was not applied

Comment on lines 7 to 9
#[entrypoint]
#[storage]
struct SafeErc20Example {
#[borrow]
#[derive(Clone)]
pub struct SafeErc20Example {
Copy link
Collaborator

Choose a reason for hiding this comment

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

After fixing merge conflicts, make sure to use the new code (you'll see it after merging)

@@ -23,3 +23,57 @@ sol!(
event Approval(address indexed owner, address indexed spender, uint256 value);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Expand the sol! definition with the new functions

@@ -51,4 +51,4 @@ extern crate alloc;
pub mod access;
pub mod finance;
pub mod token;
pub mod utils;
pub mod utils;
Copy link
Collaborator

Choose a reason for hiding this comment

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

format all code with cargo +nightly fmt --all

@0xNeshi
Copy link
Collaborator

0xNeshi commented Jun 18, 2025

Hey @simon0x1800 , just checking in, have you had time to review the change requests?

@simon0x1800
Copy link
Author

@0xNeshi sorry, I'll was a bit occupied with another project. Thanks for reviewing the pr. I'll apply the changes asap.

@simon0x1800
Copy link
Author

@0xNeshi can you check now?

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.

Looks better.
For future reference, please address all of the change requests from previous code reviews before requesting a new review.
Thanks!

Comment on lines -275 to -279
#[public]
#[implements(IErc20<Error = Error>)]
impl Erc20 {}

#[public]
Copy link
Collaborator

Choose a reason for hiding this comment

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

revert this

Comment on lines 39 to 40
#[derive(Debug)]
#[allow(missing_docs)]
error SafeErc20FailedOperation(address token);
Copy link
Collaborator

Choose a reason for hiding this comment

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

why remove this?

Comment on lines -227 to -229
#[public]
#[implements(ISafeErc20<Error = Error>)]
impl SafeErc20 {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

why remove this?

/// * `to` - Account to transfer tokens to.
/// * `value` - Number of tokens to transfer.
///
/// # Returns
Copy link
Collaborator

@0xNeshi 0xNeshi Jun 27, 2025

Choose a reason for hiding this comment

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

Please refrain from resolving if the change request was not applied

Comment on lines -12 to +19
#[entrypoint]
#[storage]
struct SafeErc20Example {
use alloy_primitives::{Address, U256};
use openzeppelin_stylus::token::erc20::utils::safe_erc20::SafeErc20;
use stylus_sdk::prelude::*;

#[derive(Clone)]
pub struct SafeErc20Example {
safe_erc20: SafeErc20,
}

#[public]
#[implements(ISafeErc20<Error = safe_erc20::Error>)]
#[inherit(SafeErc20)]
impl SafeErc20Example {}

#[public]
impl ISafeErc20 for SafeErc20Example {
type Error = safe_erc20::Error;
#[external]
impl SafeErc20Example {
pub fn transfer_and_call(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please revert this and use the new inheritance model

&mut self,
token: Address,
to: Address,
value: U256,
) -> Result<(), Self::Error> {
self.safe_erc20.safe_transfer(token, to, value)
data: Vec<u8>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

data should be stylus_sdk::abi::Bytes

Comment on lines +28 to +36

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SafeErc20;

impl SafeErc20 {
pub fn new(address: Address, wallet: &Wallet) -> Self {
Self
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this necessary?

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]: Update SafeErc20 to match the new Solidity version of the Contract
3 participants