Skip to content

Commit

Permalink
fix: proper Erc4626Example contract
Browse files Browse the repository at this point in the history
  • Loading branch information
bidzyyys committed Jan 23, 2025
1 parent f7fd8f8 commit 090ffac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 136 deletions.
7 changes: 6 additions & 1 deletion contracts/src/token/erc20/extensions/erc4626.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use alloy_sol_macro::sol;
use stylus_sdk::{
call::Call,
contract, evm, msg,
prelude::storage,
prelude::{public, storage},
storage::{StorageAddress, StorageU8, TopLevelStorage},
stylus_proc::SolidityError,
};
Expand Down Expand Up @@ -283,6 +283,10 @@ pub trait IErc4626 {
/// * `&mut self` - Write access to the contract's state.
/// * `assets` - Amount of the underlying asset to deposit.
/// * `receiver` - The address receiving the shares.
///
/// # Errors
///
/// *
fn deposit(
&mut self,
assets: U256,
Expand Down Expand Up @@ -522,6 +526,7 @@ pub trait IErc4626 {
/// BorrowMut<Self>)`. Should be fixed in the future by the Stylus team.
unsafe impl TopLevelStorage for Erc4626 {}

#[public]

Check failure on line 529 in contracts/src/token/erc20/extensions/erc4626.rs

View workflow job for this annotation

GitHub Actions / tests

failed to resolve: use of undeclared type `Vec`

Check failure on line 529 in contracts/src/token/erc20/extensions/erc4626.rs

View workflow job for this annotation

GitHub Actions / tests

cannot find type `Vec` in this scope

Check failure on line 529 in contracts/src/token/erc20/extensions/erc4626.rs

View workflow job for this annotation

GitHub Actions / Check WASM binary

failed to resolve: use of undeclared type `Vec`

Check failure on line 529 in contracts/src/token/erc20/extensions/erc4626.rs

View workflow job for this annotation

GitHub Actions / Check WASM binary

cannot find type `Vec` in this scope

Check failure on line 529 in contracts/src/token/erc20/extensions/erc4626.rs

View workflow job for this annotation

GitHub Actions / wasm32-unknown-unknown

failed to resolve: use of undeclared type `Vec`

Check failure on line 529 in contracts/src/token/erc20/extensions/erc4626.rs

View workflow job for this annotation

GitHub Actions / wasm32-unknown-unknown

cannot find type `Vec` in this scope

Check failure on line 529 in contracts/src/token/erc20/extensions/erc4626.rs

View workflow job for this annotation

GitHub Actions / stable / clippy

[clippy] reported by reviewdog 🐶 error[E0433]: failed to resolve: use of undeclared type `Vec` --> contracts/src/token/erc20/extensions/erc4626.rs:529:1 | 529 | #[public] | ^^^^^^^^^ use of undeclared type `Vec` | = note: this error originates in the attribute macro `public` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider importing one of these structs | 10 + use crate::token::erc20::Vec; | 10 + use alloc::vec::Vec; | Raw Output: contracts/src/token/erc20/extensions/erc4626.rs:529:1:e:error[E0433]: failed to resolve: use of undeclared type `Vec` --> contracts/src/token/erc20/extensions/erc4626.rs:529:1 | 529 | #[public] | ^^^^^^^^^ use of undeclared type `Vec` | = note: this error originates in the attribute macro `public` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider importing one of these structs | 10 + use crate::token::erc20::Vec; | 10 + use alloc::vec::Vec; | __END__

Check failure on line 529 in contracts/src/token/erc20/extensions/erc4626.rs

View workflow job for this annotation

GitHub Actions / stable / clippy

[clippy] reported by reviewdog 🐶 error[E0412]: cannot find type `Vec` in this scope --> contracts/src/token/erc20/extensions/erc4626.rs:529:1 | 529 | #[public] | ^^^^^^^^^ not found in this scope | = note: this error originates in the attribute macro `public` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider importing one of these structs | 10 + use crate::token::erc20::Vec; | 10 + use alloc::vec::Vec; | Raw Output: contracts/src/token/erc20/extensions/erc4626.rs:529:1:e:error[E0412]: cannot find type `Vec` in this scope --> contracts/src/token/erc20/extensions/erc4626.rs:529:1 | 529 | #[public] | ^^^^^^^^^ not found in this scope | = note: this error originates in the attribute macro `public` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider importing one of these structs | 10 + use crate::token::erc20::Vec; | 10 + use alloc::vec::Vec; | __END__

Check failure on line 529 in contracts/src/token/erc20/extensions/erc4626.rs

View workflow job for this annotation

GitHub Actions / Gas usage report

failed to resolve: use of undeclared type `Vec`

Check failure on line 529 in contracts/src/token/erc20/extensions/erc4626.rs

View workflow job for this annotation

GitHub Actions / Gas usage report

cannot find type `Vec` in this scope

Check failure on line 529 in contracts/src/token/erc20/extensions/erc4626.rs

View workflow job for this annotation

GitHub Actions / beta / clippy

[clippy] reported by reviewdog 🐶 error[E0433]: failed to resolve: use of undeclared type `Vec` --> contracts/src/token/erc20/extensions/erc4626.rs:529:1 | 529 | #[public] | ^^^^^^^^^ use of undeclared type `Vec` | = note: this error originates in the attribute macro `public` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider importing one of these structs | 10 + use crate::token::erc20::Vec; | 10 + use alloc::vec::Vec; | Raw Output: contracts/src/token/erc20/extensions/erc4626.rs:529:1:e:error[E0433]: failed to resolve: use of undeclared type `Vec` --> contracts/src/token/erc20/extensions/erc4626.rs:529:1 | 529 | #[public] | ^^^^^^^^^ use of undeclared type `Vec` | = note: this error originates in the attribute macro `public` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider importing one of these structs | 10 + use crate::token::erc20::Vec; | 10 + use alloc::vec::Vec; | __END__

Check failure on line 529 in contracts/src/token/erc20/extensions/erc4626.rs

View workflow job for this annotation

GitHub Actions / beta / clippy

[clippy] reported by reviewdog 🐶 error[E0412]: cannot find type `Vec` in this scope --> contracts/src/token/erc20/extensions/erc4626.rs:529:1 | 529 | #[public] | ^^^^^^^^^ not found in this scope | = note: this error originates in the attribute macro `public` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider importing one of these structs | 10 + use crate::token::erc20::Vec; | 10 + use alloc::vec::Vec; | Raw Output: contracts/src/token/erc20/extensions/erc4626.rs:529:1:e:error[E0412]: cannot find type `Vec` in this scope --> contracts/src/token/erc20/extensions/erc4626.rs:529:1 | 529 | #[public] | ^^^^^^^^^ not found in this scope | = note: this error originates in the attribute macro `public` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider importing one of these structs | 10 + use crate::token::erc20::Vec; | 10 + use alloc::vec::Vec; | __END__

Check failure on line 529 in contracts/src/token/erc20/extensions/erc4626.rs

View workflow job for this annotation

GitHub Actions / ubuntu / stable / features

failed to resolve: use of undeclared type `Vec`

Check failure on line 529 in contracts/src/token/erc20/extensions/erc4626.rs

View workflow job for this annotation

GitHub Actions / ubuntu / stable / features

cannot find type `Vec` in this scope
impl IErc4626 for Erc4626 {
type Error = Error;

Expand Down
37 changes: 21 additions & 16 deletions contracts/src/utils/math/alloy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub trait Math {
/// following the selected `rounding` direction. Throws if result
/// overflows a `U256` or `denominator` is zero.
///
/// Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv) with further edits by
/// Uniswap Labs also under MIT license.
/// Original credit to Remco Bloemen under MIT license (<https://xn--2-umb.com/21/muldiv>)
/// with further edits by Uniswap Labs also under MIT license.
///
/// # Arguments
///
Expand All @@ -40,6 +40,13 @@ pub trait Math {
fn mul_div(self, y: Self, denominator: Self, rounding: Rounding) -> Self;

#[must_use]
/// Multiplies `self` * `y` and calculates modulo `m`.
///
/// # Arguments
///
/// * `self` -
/// * `y` -
/// * `m` -
fn mul_mod(self, y: Self, m: Self) -> Self;
}

Expand Down Expand Up @@ -187,9 +194,7 @@ impl Math for U256 {
fn mul_mod(self, y: Self, m: Self) -> Self {
let x = self;
// Ensure m is not zero to avoid division by zero
if m.is_zero() {
panic!("Modulus cannot be zero");
}
assert!(!m.is_zero(), "Modulus cannot be zero in `Math::mul_mod`");

let mut result = U256::ZERO;

Expand Down Expand Up @@ -228,9 +233,10 @@ impl Math for U256 {
let two = U256::from(2);
let three = U256::from(3);

if denominator.is_zero() {
panic!("Division by U256::ZERO in `Math::mul_div`")
}
assert!(
!denominator.is_zero(),
"Division by U256::ZERO in `Math::mul_div`"
);

let x = self;

Expand All @@ -244,7 +250,8 @@ impl Math for U256 {
// Most significant 256 bits of the product.
let mut prod1: U256 = {
let mm: U256 = x.wrapping_mul(y);
mm.wrapping_sub(prod0).wrapping_sub(U256::from((mm < prod0) as u8))
mm.wrapping_sub(prod0)
.wrapping_sub(U256::from(u8::from(mm < prod0)))
};
// Handle non-overflow cases, 256 by 256 division.
if prod1.is_zero() {
Expand All @@ -253,9 +260,7 @@ impl Math for U256 {
}

// Make sure the result is less than 2²⁵⁶.
if denominator <= prod1 {
panic!("Under overflow in `Math::mul_div`");
}
assert!((denominator > prod1), "Under overflow in `Math::mul_div`");

///////////////////////////////////////////////
// 512 by 256 division.
Expand All @@ -267,17 +272,17 @@ impl Math for U256 {

// Subtract 256 bit number from 512 bit number.
if remainder > prod0 {
prod1 = prod1 - one;
prod1 -= one;
}
prod0 = prod0 - remainder;
prod0 -= remainder;

// Factor powers of two out of denominator and compute largest power of
// two divisor of denominator. Always >= 1. See https://cs.stackexchange.com/q/138556/92363.
let mut twos: U256 = denominator & (!denominator + one);
// Divide denominator by twos.
let denominator = denominator / twos;
// Divide [prod1 prod0] by twos.
prod0 = prod0 / twos;
prod0 /= twos;
// Flip twos such that it is 2²⁵⁶ / twos. If twos is zero, then it
// becomes one.
twos =
Expand Down Expand Up @@ -313,7 +318,7 @@ impl Math for U256 {
if rounding.unsigned_rounds_up()
&& x.mul_mod(y, denominator) > U256::ZERO
{
result = result + one;
result += one;
}

result
Expand Down
122 changes: 3 additions & 119 deletions examples/erc4626/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,132 +3,16 @@ extern crate alloc;

use alloc::vec::Vec;

use openzeppelin_stylus::token::erc20::{
extensions::{Erc20Metadata, Erc4626},
utils::SafeErc20,
Erc20,
};
use openzeppelin_stylus::token::erc20::extensions::Erc4626;
use stylus_sdk::prelude::{entrypoint, public, storage};

#[entrypoint]
#[storage]
struct Erc4626Example {
#[borrow]
pub erc20: Erc20,
#[borrow]
pub erc4626: Erc4626,
#[borrow]
pub metadata: Erc20Metadata,
#[borrow]
pub safe_erc20: SafeErc20,
}

#[public]
#[inherit(Erc20)]
impl Erc4626Example {
/*
fn name(&self) -> String {
self.metadata.name()
}
fn symbol(&self) -> String {
self.metadata.symbol()
}
fn decimals(&self) -> u8 {
self.erc4626._underlying_decimals.get().to()
}
fn max_mint(&self, _receiver: Address) -> U256 {
self.erc4626.max_mint(_receiver)
}
fn asset(&self) -> Address {
self.erc4626.asset()
}
fn total_assets(&self) -> U256 {
self.erc4626.total_assets(&self.erc20)
}
fn convert_to_shares(&mut self, assets: U256) -> U256 {
self.erc4626.convert_to_shares(assets, &mut self.erc20)
}
fn convert_to_assets(&mut self, shares: U256) -> U256 {
self.erc4626.convert_to_assets(shares, &mut self.erc20)
}
fn preview_deposit(&mut self, assets: U256) -> U256 {
self.erc4626.preview_deposit(assets, &mut self.erc20)
}
fn deposit(
&mut self,
assets: U256,
receiver: Address,
) -> Result<U256, Vec<u8>> {
Ok(self.erc4626.deposit(assets, receiver, &mut self.erc20)?)
}
fn preview_mint(&mut self, shares: U256) -> U256 {
self.erc4626.preview_mint(shares, &mut self.erc20)
}
fn mint(
&mut self,
shares: U256,
receiver: Address,
) -> Result<U256, Vec<u8>> {
Ok(self.erc4626.mint(shares, receiver, &mut self.erc20)?)
}
fn max_withdraw(&mut self, owner: Address) -> U256 {
self.erc4626.max_withdraw(owner, &mut self.erc20)
}
fn preview_withdraw(&mut self, assets: U256) -> U256 {
self.erc4626.preview_withdraw(assets, &mut self.erc20)
}
fn withdraw(
&mut self,
_assets: U256,
_receiver: Address,
_owner: Address,
) -> Result<U256, Vec<u8>> {
// Ok(self.erc4626.withdraw(
// assets,
// receiver,
// owner,
// &mut self.erc20,
// &mut self.safe_erc20,
// )?)
Ok(U256::from(100))
}
fn max_redeem(&mut self, owner: Address) -> U256 {
self.erc4626.max_redeem(owner, &mut self.erc20)
}
fn preview_redeem(&mut self, shares: U256) -> U256 {
self.erc4626.preview_redeem(shares, &mut self.erc20)
}
fn redeem(
&mut self,
_shares: U256,
_receiver: Address,
_owner: Address,
) -> Result<U256, Vec<u8>> {
// Ok(self.erc4626.redeem(
// shares,
// receiver,
// owner,
// &mut self.erc20,
// &mut self.safe_erc20,
// )?)
Ok(U256::from(100))
}
*/
}
#[inherit(Erc4626)]
impl Erc4626Example {}

0 comments on commit 090ffac

Please sign in to comment.