Skip to content

Commit

Permalink
feat: add decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
bidzyyys committed Jan 23, 2025
1 parent 35af0d3 commit 4be1329
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
29 changes: 22 additions & 7 deletions contracts/src/token/erc20/extensions/erc4626.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 =
Expand All @@ -856,8 +871,8 @@ mod tests {

#[storage]
struct Erc4626TestExample {
pub erc4626: Erc4626,
pub erc20: Erc20,
_erc4626: Erc4626,
_erc20: Erc20,
}

/*
Expand Down
6 changes: 5 additions & 1 deletion examples/erc4626/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
}
Expand Down

0 comments on commit 4be1329

Please sign in to comment.