From 4be1329186c02024b6a53ea60029e3d7b8f04eff Mon Sep 17 00:00:00 2001 From: Daniel Bigos Date: Thu, 23 Jan 2025 15:48:31 +0100 Subject: [PATCH] feat: add decimals --- .../src/token/erc20/extensions/erc4626.rs | 29 ++++++++++++++----- examples/erc4626/src/lib.rs | 6 +++- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/contracts/src/token/erc20/extensions/erc4626.rs b/contracts/src/token/erc20/extensions/erc4626.rs index 1d276adb..746873a7 100644 --- a/contracts/src/token/erc20/extensions/erc4626.rs +++ b/contracts/src/token/erc20/extensions/erc4626.rs @@ -7,7 +7,7 @@ //! would affect the "shares" token represented by this contract and not the //! "assets" token which is an independent contract. -use alloy_primitives::{Address, U256}; +use alloy_primitives::{Address, U256, U8}; pub use sol::*; use stylus_sdk::{ call::Call, @@ -728,6 +728,22 @@ impl Erc4626 { } impl Erc4626 { + /// TODO: Rust docs + /// + /// # Arguments + /// + /// * &self - + /// + /// # Panics + /// + /// * If ... + pub fn decimals(&self) -> U8 { + self.underlying_decimals + .get() + .checked_add(Self::_decimals_offset()) + .expect("Decimals should not be grater than U8::MAX") + } + /// TODO: Rust docs fn _convert_to_shares( &mut self, @@ -834,17 +850,16 @@ impl Erc4626 { } /// TODO: Rust docs - fn _decimals_offset() -> u8 { - 0 + fn _decimals_offset() -> U8 { + U8::ZERO } } #[cfg(all(test, feature = "std"))] mod tests { - use alloy_primitives::{address, U256}; use stylus_sdk::prelude::storage; - use super::{Erc4626, IErc4626}; + use super::Erc4626; use crate::token::erc20::Erc20; // const ALICE: Address = @@ -856,8 +871,8 @@ mod tests { #[storage] struct Erc4626TestExample { - pub erc4626: Erc4626, - pub erc20: Erc20, + _erc4626: Erc4626, + _erc20: Erc20, } /* diff --git a/examples/erc4626/src/lib.rs b/examples/erc4626/src/lib.rs index 1a1e760f..228bdf3d 100644 --- a/examples/erc4626/src/lib.rs +++ b/examples/erc4626/src/lib.rs @@ -3,7 +3,7 @@ extern crate alloc; use alloc::vec::Vec; -use alloy_primitives::{Address, U256}; +use alloy_primitives::{Address, U256, U8}; use openzeppelin_stylus::token::erc20::{ extensions::{Erc4626, IErc4626}, Erc20, @@ -22,6 +22,10 @@ struct Erc4626Example { #[public] #[inherit(Erc20)] impl Erc4626Example { + fn decimals(&self) -> U8 { + self.erc4626.decimals() + } + fn asset(&self) -> Address { self.erc4626.asset() }