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

ref: add associated error types for contract traits #251

Merged
merged 7 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions contracts/src/token/erc20/extensions/burnable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use crate::token::erc20::{Erc20, Error};
/// their own tokens and those that they have an allowance for,
/// in a way that can be recognized off-chain (via event analysis).
pub trait IErc20Burnable {
/// The error type associated with ERC-20 burnable contract.
type Error: Into<alloc::vec::Vec<u8>>;

/// Destroys a `value` amount of tokens from the caller. lowering the total
/// supply.
///
Expand All @@ -26,7 +29,7 @@ pub trait IErc20Burnable {
/// # Events
///
/// Emits a [`super::super::Transfer`] event.
fn burn(&mut self, value: U256) -> Result<(), Error>;
fn burn(&mut self, value: U256) -> Result<(), Self::Error>;

/// Destroys a `value` amount of tokens from `account`, lowering the total
/// supply.
Expand All @@ -50,20 +53,25 @@ pub trait IErc20Burnable {
/// # Events
///
/// Emits a [`super::super::Transfer`] event.
fn burn_from(&mut self, account: Address, value: U256)
-> Result<(), Error>;
fn burn_from(
&mut self,
account: Address,
value: U256,
) -> Result<(), Self::Error>;
}

impl IErc20Burnable for Erc20 {
fn burn(&mut self, value: U256) -> Result<(), Error> {
type Error = Error;

fn burn(&mut self, value: U256) -> Result<(), Self::Error> {
self._burn(msg::sender(), value)
}

fn burn_from(
&mut self,
account: Address,
value: U256,
) -> Result<(), Error> {
) -> Result<(), Self::Error> {
self._spend_allowance(account, msg::sender(), value)?;
self._burn(account, value)
}
Expand Down
12 changes: 0 additions & 12 deletions contracts/src/token/erc20/extensions/permit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,6 @@ pub enum Error {
ECDSA(ecdsa::Error),
}

impl MethodError for erc20::Error {
Copy link
Member Author

Choose a reason for hiding this comment

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

MethodError implementation is moved to the corresponding contract

Copy link
Contributor

Choose a reason for hiding this comment

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

This means that this import can be removed from this file, right?

fn encode(self) -> alloc::vec::Vec<u8> {
self.into()
}
}

impl MethodError for ecdsa::Error {
fn encode(self) -> alloc::vec::Vec<u8> {
self.into()
}
}

sol_storage! {
/// State of a Permit Contract.
pub struct Erc20Permit<T: IEip712 + StorageType>{
Expand Down
37 changes: 30 additions & 7 deletions contracts/src/token/erc20/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use alloy_primitives::{Address, U256};
use alloy_sol_types::sol;
use stylus_proc::SolidityError;
use stylus_sdk::{
call::MethodError,
evm, msg,
stylus_proc::{external, sol_storage},
};
Expand Down Expand Up @@ -91,6 +92,12 @@ pub enum Error {
InvalidSpender(ERC20InvalidSpender),
}

impl MethodError for Error {
fn encode(self) -> alloc::vec::Vec<u8> {
self.into()
}
}

sol_storage! {
/// State of an `Erc20` token.
pub struct Erc20 {
Expand All @@ -105,6 +112,9 @@ sol_storage! {

/// Required interface of an [`Erc20`] compliant contract.
pub trait IErc20 {
/// The error type associated with ERC-20 contract.
type Error: Into<alloc::vec::Vec<u8>>;

/// Returns the number of tokens in existence.
///
/// # Arguments
Expand Down Expand Up @@ -140,7 +150,11 @@ pub trait IErc20 {
/// # Events
///
/// Emits a [`Transfer`] event.
fn transfer(&mut self, to: Address, value: U256) -> Result<bool, Error>;
fn transfer(
&mut self,
to: Address,
value: U256,
) -> Result<bool, Self::Error>;

/// Returns the remaining number of tokens that `spender` will be allowed
/// to spend on behalf of `owner` through `transfer_from`. This is zero by
Expand Down Expand Up @@ -181,8 +195,11 @@ pub trait IErc20 {
/// # Events
///
/// Emits an [`Approval`] event.
fn approve(&mut self, spender: Address, value: U256)
-> Result<bool, Error>;
fn approve(
&mut self,
spender: Address,
value: U256,
) -> Result<bool, Self::Error>;

/// Moves a `value` number of tokens from `from` to `to` using the
/// allowance mechanism. `value` is then deducted from the caller's
Expand Down Expand Up @@ -218,11 +235,13 @@ pub trait IErc20 {
from: Address,
to: Address,
value: U256,
) -> Result<bool, Error>;
) -> Result<bool, Self::Error>;
}

#[external]
impl IErc20 for Erc20 {
type Error = Error;

fn total_supply(&self) -> U256 {
self._total_supply.get()
}
Expand All @@ -231,7 +250,11 @@ impl IErc20 for Erc20 {
self._balances.get(account)
}

fn transfer(&mut self, to: Address, value: U256) -> Result<bool, Error> {
fn transfer(
&mut self,
to: Address,
value: U256,
) -> Result<bool, Self::Error> {
let from = msg::sender();
self._transfer(from, to, value)?;
Ok(true)
Expand All @@ -245,7 +268,7 @@ impl IErc20 for Erc20 {
&mut self,
spender: Address,
value: U256,
) -> Result<bool, Error> {
) -> Result<bool, Self::Error> {
let owner = msg::sender();
self._approve(owner, spender, value)
}
Expand All @@ -255,7 +278,7 @@ impl IErc20 for Erc20 {
from: Address,
to: Address,
value: U256,
) -> Result<bool, Error> {
) -> Result<bool, Self::Error> {
let spender = msg::sender();
self._spend_allowance(from, spender, value)?;
self._transfer(from, to, value)?;
Expand Down
9 changes: 7 additions & 2 deletions contracts/src/token/erc721/extensions/burnable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use crate::token::erc721::{Erc721, Error};

/// An [`Erc721`] token that can be burned (destroyed).
pub trait IErc721Burnable {
/// The error type associated with ERC-721 burnable contract.
type Error: Into<alloc::vec::Vec<u8>>;

/// Burns `token_id`.
///
/// The approval is cleared when the token is burned. Relies on the `_burn`
Expand All @@ -31,11 +34,13 @@ pub trait IErc721Burnable {
/// # Events
///
/// Emits a [`super::super::Transfer`] event.
fn burn(&mut self, token_id: U256) -> Result<(), Error>;
fn burn(&mut self, token_id: U256) -> Result<(), Self::Error>;
}

impl IErc721Burnable for Erc721 {
fn burn(&mut self, token_id: U256) -> Result<(), Error> {
type Error = Error;

fn burn(&mut self, token_id: U256) -> Result<(), Self::Error> {
// Setting an "auth" arguments enables the
// [`super::super::Erc721::_is_authorized`] check which verifies that
// the token exists (from != `Address::ZERO`).
Expand Down
12 changes: 0 additions & 12 deletions contracts/src/token/erc721/extensions/consecutive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,6 @@ pub enum Error {

unsafe impl TopLevelStorage for Erc721Consecutive {}

impl MethodError for erc721::Error {
qalisander marked this conversation as resolved.
Show resolved Hide resolved
fn encode(self) -> alloc::vec::Vec<u8> {
self.into()
}
}

impl MethodError for checkpoints::Error {
fn encode(self) -> alloc::vec::Vec<u8> {
self.into()
}
}

// ************** ERC-721 External **************

#[external]
Expand Down
13 changes: 9 additions & 4 deletions contracts/src/token/erc721/extensions/enumerable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ sol_storage! {
/// This is the interface of the optional `Enumerable` extension
/// of the ERC-721 standard.
pub trait IErc721Enumerable {
/// The error type associated with ERC-721 enumerable contract.
type Error: Into<alloc::vec::Vec<u8>>;

// TODO: fn supports_interface (#33)

/// Returns a token ID owned by `owner` at a given `index` of its token
Expand All @@ -84,7 +87,7 @@ pub trait IErc721Enumerable {
&self,
owner: Address,
index: U256,
) -> Result<U256, Error>;
) -> Result<U256, Self::Error>;

/// Returns the total amount of tokens stored by the contract.
///
Expand All @@ -107,16 +110,18 @@ pub trait IErc721Enumerable {
///
/// * If an `owner`'s token query is out of bounds for `index`,
/// then the error [`Error::OutOfBoundsIndex`] is returned.
fn token_by_index(&self, index: U256) -> Result<U256, Error>;
fn token_by_index(&self, index: U256) -> Result<U256, Self::Error>;
}

#[external]
impl IErc721Enumerable for Erc721Enumerable {
type Error = Error;

fn token_of_owner_by_index(
&self,
owner: Address,
index: U256,
) -> Result<U256, Error> {
) -> Result<U256, Self::Error> {
let token = self._owned_tokens.getter(owner).get(index);

if token.is_zero() {
Expand All @@ -131,7 +136,7 @@ impl IErc721Enumerable for Erc721Enumerable {
U256::from(tokens_length)
}

fn token_by_index(&self, index: U256) -> Result<U256, Error> {
fn token_by_index(&self, index: U256) -> Result<U256, Self::Error> {
self._all_tokens.get(index).ok_or(
ERC721OutOfBoundsIndex { owner: Address::ZERO, index }.into(),
)
Expand Down
11 changes: 9 additions & 2 deletions contracts/src/token/erc721/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloy_primitives::{fixed_bytes, uint, Address, FixedBytes, U128, U256};
use stylus_sdk::{
abi::Bytes,
alloy_sol_types::sol,
call::{self, Call},
call::{self, Call, MethodError},
evm, msg,
prelude::*,
};
Expand Down Expand Up @@ -150,6 +150,12 @@ pub enum Error {
InvalidOperator(ERC721InvalidOperator),
}

impl MethodError for Error {
fn encode(self) -> alloc::vec::Vec<u8> {
self.into()
}
}

sol_interface! {
/// [`Erc721`] token receiver interface.
///
Expand Down Expand Up @@ -193,8 +199,9 @@ unsafe impl TopLevelStorage for Erc721 {}

/// Required interface of an [`Erc721`] compliant contract.
pub trait IErc721 {
/// The error type associated to this ERC-721 trait implementation.
/// The error type associated with ERC-721 contract.
qalisander marked this conversation as resolved.
Show resolved Hide resolved
type Error: Into<alloc::vec::Vec<u8>>;

/// Returns the number of tokens in `owner`'s account.
///
/// # Arguments
Expand Down
10 changes: 9 additions & 1 deletion contracts/src/utils/cryptography/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ use alloy_primitives::{address, uint, Address, B256, U256};
use alloy_sol_types::{sol, SolType};
use stylus_proc::SolidityError;
use stylus_sdk::{
call::{self, Call},
call::{self, Call, MethodError},
storage::TopLevelStorage,
};

use crate::utils::cryptography::ecdsa;

/// Address of the `ecrecover` EVM precompile.
pub const ECRECOVER_ADDR: Address =
address!("0000000000000000000000000000000000000001");
Expand Down Expand Up @@ -54,6 +56,12 @@ pub enum Error {
InvalidSignatureS(ECDSAInvalidSignatureS),
}

impl MethodError for ecdsa::Error {
fn encode(self) -> alloc::vec::Vec<u8> {
self.into()
}
}

sol! {
/// Struct with callable data to the `ecrecover` precompile.
#[allow(missing_docs)]
Expand Down
11 changes: 10 additions & 1 deletion contracts/src/utils/structs/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use alloy_primitives::{uint, Uint, U256, U32};
use alloy_sol_types::sol;
use stylus_proc::{sol_storage, SolidityError};
use stylus_sdk::storage::{StorageGuard, StorageGuardMut};
use stylus_sdk::{
call::MethodError,
storage::{StorageGuard, StorageGuardMut},
};

use crate::utils::math::alloy::Math;

Expand All @@ -29,6 +32,12 @@ pub enum Error {
CheckpointUnorderedInsertion(CheckpointUnorderedInsertion),
}

impl MethodError for Error {
qalisander marked this conversation as resolved.
Show resolved Hide resolved
fn encode(self) -> alloc::vec::Vec<u8> {
self.into()
}
}

sol_storage! {
/// State of the checkpoint library contract.
pub struct Trace160 {
Expand Down
Loading