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/Sub Assignment Operations + e2e-tests.sh #467

Merged
merged 24 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
34e8883
docs: add warning to pause/unpause in examples
0xNeshi Dec 26, 2024
cce990b
test: update e2e-tests script
0xNeshi Dec 26, 2024
3054d65
docs: add warning to pause/unpause in erc1155 example
0xNeshi Dec 26, 2024
fff4210
docs: add missing eyre module import in README example
0xNeshi Dec 26, 2024
5fbb8aa
ref: explicitly import eyre::Result in e2e accounts.rs
0xNeshi Dec 26, 2024
3406681
ref: use assign unchecked traits in erc20::update
0xNeshi Dec 26, 2024
75d3e1b
docs: update docs for nonces contract
0xNeshi Dec 26, 2024
a264c90
ref: reuse logic in use_checked_nonce
0xNeshi Dec 26, 2024
84fceef
ref: update AddAssignUnchecked and SubAssignUnchecked to cast values …
0xNeshi Dec 26, 2024
627fd17
chore: add missing PR ids to changelog
0xNeshi Dec 26, 2024
dbfc6cb
docs: add links explaining undefined behavior
0xNeshi Dec 26, 2024
a3d5470
ref: address 'clippy::used_underscore_binding' warnings
0xNeshi Dec 26, 2024
f51bc08
test: simplify e2e-tests.sh
0xNeshi Dec 26, 2024
da89ce4
test: update comments in e2e-tests.sh
0xNeshi Dec 26, 2024
c858b28
docs: Apply comment updates from code review
0xNeshi Dec 27, 2024
fa3956a
docs: change F link to plain code
0xNeshi Dec 27, 2024
33f834e
docs: format in nonces
0xNeshi Dec 27, 2024
59c1ead
Merge branch 'main' into return-res-refactor
bidzyyys Dec 27, 2024
b5e7bec
feat: now panics on exceeding
0xNeshi Dec 30, 2024
ad9732d
docs: align Nonce docs to existing convention
0xNeshi Dec 30, 2024
26f7c59
ci: remove std feature from e2e-tests.sh
0xNeshi Dec 30, 2024
07ebad3
test: remove dir navigation from e2e-tests.sh
0xNeshi Dec 31, 2024
c3a1599
revert: update AddAssignUnchecked and SubAssignUnchecked to cast valu…
0xNeshi Dec 31, 2024
c5c7be5
revert: remove dir navigation from e2e-tests.sh
0xNeshi Dec 31, 2024
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: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

-

### Changed

- Refactor how `AddAssignUnchecked` and `SubAssignUnchecked` traits are used in `Erc20`, `Erc721`, and `Erc721Consecutive`. #467

### Changed (Breaking)

-

### Fixed

-

## [v0.2.0-alpha.2] - 2024-12-18

### Added
Expand Down
1 change: 1 addition & 0 deletions contracts/src/access/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,11 @@
#[storage]
pub struct AccessControl {
/// Role identifier -> Role information.
#[allow(clippy::used_underscore_binding)]
0xNeshi marked this conversation as resolved.
Show resolved Hide resolved
pub _roles: StorageMap<FixedBytes<32>, RoleData>,
}

#[public]

Check warning on line 126 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / nightly / doc

unexpected `cfg` condition value: `export-abi`

Check warning on line 126 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 126 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 126 in contracts/src/access/control.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

unexpected `cfg` condition value: `export-abi`
impl AccessControl {
/// The default admin role. `[0; 32]` by default.
pub const DEFAULT_ADMIN_ROLE: [u8; 32] = [0; 32];
Expand Down
1 change: 1 addition & 0 deletions contracts/src/access/ownable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#[storage]
pub struct Ownable {
/// The current owner of this contract.
#[allow(clippy::used_underscore_binding)]
pub _owner: StorageAddress,
}

Expand Down Expand Up @@ -126,7 +127,7 @@
fn renounce_ownership(&mut self) -> Result<(), Self::Error>;
}

#[public]

Check warning on line 130 in contracts/src/access/ownable.rs

View workflow job for this annotation

GitHub Actions / nightly / doc

unexpected `cfg` condition value: `export-abi`

Check warning on line 130 in contracts/src/access/ownable.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 130 in contracts/src/access/ownable.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 130 in contracts/src/access/ownable.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

unexpected `cfg` condition value: `export-abi`
impl IOwnable for Ownable {
type Error = Error;

Expand Down
2 changes: 2 additions & 0 deletions contracts/src/access/ownable_two_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@
#[storage]
pub struct Ownable2Step {
/// [`Ownable`] contract.
#[allow(clippy::used_underscore_binding)]
pub _ownable: Ownable,
/// Pending owner of the contract.
#[allow(clippy::used_underscore_binding)]
pub _pending_owner: StorageAddress,
}

Expand Down Expand Up @@ -146,7 +148,7 @@
fn renounce_ownership(&mut self) -> Result<(), Self::Error>;
}

#[public]

Check warning on line 151 in contracts/src/access/ownable_two_step.rs

View workflow job for this annotation

GitHub Actions / nightly / doc

unexpected `cfg` condition value: `export-abi`

Check warning on line 151 in contracts/src/access/ownable_two_step.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 151 in contracts/src/access/ownable_two_step.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 151 in contracts/src/access/ownable_two_step.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

unexpected `cfg` condition value: `export-abi`
impl IOwnable2Step for Ownable2Step {
type Error = Error;

Expand Down
4 changes: 4 additions & 0 deletions contracts/src/finance/vesting_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,16 @@
/// [`Ownable`] contract.
pub ownable: Ownable,
/// Amount of Ether already released.
#[allow(clippy::used_underscore_binding)]
0xNeshi marked this conversation as resolved.
Show resolved Hide resolved
pub _released: StorageU256,
/// Amount of ERC-20 tokens already released.
#[allow(clippy::used_underscore_binding)]
pub _erc20_released: StorageMap<Address, StorageU256>,
/// Start timestamp.
#[allow(clippy::used_underscore_binding)]
pub _start: StorageU64,
/// Vesting duration.
#[allow(clippy::used_underscore_binding)]
pub _duration: StorageU64,
/// [`SafeErc20`] contract.
pub safe_erc20: SafeErc20,
Expand Down Expand Up @@ -352,7 +356,7 @@
) -> Result<U256, Self::Error>;
}

#[public]

Check warning on line 359 in contracts/src/finance/vesting_wallet.rs

View workflow job for this annotation

GitHub Actions / nightly / doc

unexpected `cfg` condition value: `export-abi`

Check warning on line 359 in contracts/src/finance/vesting_wallet.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 359 in contracts/src/finance/vesting_wallet.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 359 in contracts/src/finance/vesting_wallet.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

unexpected `cfg` condition value: `export-abi`
impl IVestingWallet for VestingWallet {
type Error = Error;

Expand Down
1 change: 1 addition & 0 deletions contracts/src/token/erc1155/extensions/metadata_uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
pub struct Erc1155MetadataUri {
/// Used as the URI for all token types by relying on ID substitution,
/// e.g. https://token-cdn-domain/{id}.json.
#[allow(clippy::used_underscore_binding)]
pub _uri: StorageString,
}

Expand All @@ -54,7 +55,7 @@
fn uri(&self, id: U256) -> String;
}

#[public]

Check warning on line 58 in contracts/src/token/erc1155/extensions/metadata_uri.rs

View workflow job for this annotation

GitHub Actions / nightly / doc

unexpected `cfg` condition value: `export-abi`

Check warning on line 58 in contracts/src/token/erc1155/extensions/metadata_uri.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 58 in contracts/src/token/erc1155/extensions/metadata_uri.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 58 in contracts/src/token/erc1155/extensions/metadata_uri.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

unexpected `cfg` condition value: `export-abi`
impl IErc1155MetadataUri for Erc1155MetadataUri {
/// This implementation returns the same URI for all token types.
/// Clients calling this function must replace the `id` substring with
Expand Down
2 changes: 2 additions & 0 deletions contracts/src/token/erc1155/extensions/supply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
/// ERC-1155 contract storage.
pub erc1155: Erc1155,
/// Mapping from token id to total supply.
#[allow(clippy::used_underscore_binding)]
pub _total_supply: StorageMap<U256, StorageU256>,
/// Total supply of all token ids.
#[allow(clippy::used_underscore_binding)]
pub _total_supply_all: StorageU256,
}

Expand Down Expand Up @@ -80,7 +82,7 @@
}
}

#[public]

Check warning on line 85 in contracts/src/token/erc1155/extensions/supply.rs

View workflow job for this annotation

GitHub Actions / nightly / doc

unexpected `cfg` condition value: `export-abi`

Check warning on line 85 in contracts/src/token/erc1155/extensions/supply.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 85 in contracts/src/token/erc1155/extensions/supply.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 85 in contracts/src/token/erc1155/extensions/supply.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

unexpected `cfg` condition value: `export-abi`
impl IErc1155 for Erc1155Supply {
type Error = erc1155::Error;

Expand Down
2 changes: 2 additions & 0 deletions contracts/src/token/erc1155/extensions/uri_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#[storage]
pub struct Erc1155UriStorage {
/// Optional base URI.
#[allow(clippy::used_underscore_binding)]
pub _base_uri: StorageString,
/// Optional mapping for token URIs.
#[allow(clippy::used_underscore_binding)]
pub _token_uris: StorageMap<U256, StorageString>,
}

Expand Down Expand Up @@ -94,7 +96,7 @@
use stylus_sdk::prelude::storage;

use super::Erc1155UriStorage;
use crate::token::erc1155::{extensions::Erc1155MetadataUri, Erc1155};

Check warning on line 99 in contracts/src/token/erc1155/extensions/uri_storage.rs

View workflow job for this annotation

GitHub Actions / ubuntu / stable

unused import: `Erc1155`

Check warning on line 99 in contracts/src/token/erc1155/extensions/uri_storage.rs

View workflow job for this annotation

GitHub Actions / macos-latest / stable

unused import: `Erc1155`

fn random_token_id() -> U256 {
let num: u32 = rand::random();
Expand Down
2 changes: 2 additions & 0 deletions contracts/src/token/erc1155/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@
#[storage]
pub struct Erc1155 {
/// Maps users to balances.
#[allow(clippy::used_underscore_binding)]
pub _balances: StorageMap<U256, StorageMap<Address, StorageU256>>,
/// Maps owners to a mapping of operator approvals.
#[allow(clippy::used_underscore_binding)]
pub _operator_approvals:
StorageMap<Address, StorageMap<Address, StorageBool>>,
}
Expand Down Expand Up @@ -389,7 +391,7 @@
) -> Result<(), Self::Error>;
}

#[public]

Check warning on line 394 in contracts/src/token/erc1155/mod.rs

View workflow job for this annotation

GitHub Actions / nightly / doc

unexpected `cfg` condition value: `export-abi`

Check warning on line 394 in contracts/src/token/erc1155/mod.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 394 in contracts/src/token/erc1155/mod.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 394 in contracts/src/token/erc1155/mod.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

unexpected `cfg` condition value: `export-abi`
impl IErc1155 for Erc1155 {
type Error = Error;

Expand Down
1 change: 1 addition & 0 deletions contracts/src/token/erc20/extensions/capped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@
#[storage]
pub struct Capped {
/// A cap to the supply of tokens.
#[allow(clippy::used_underscore_binding)]
pub _cap: StorageU256,
}

#[public]

Check warning on line 54 in contracts/src/token/erc20/extensions/capped.rs

View workflow job for this annotation

GitHub Actions / nightly / doc

unexpected `cfg` condition value: `export-abi`

Check warning on line 54 in contracts/src/token/erc20/extensions/capped.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 54 in contracts/src/token/erc20/extensions/capped.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 54 in contracts/src/token/erc20/extensions/capped.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

unexpected `cfg` condition value: `export-abi`
impl Capped {
/// Returns the cap on the token's total supply.
pub fn cap(&self) -> U256 {
Expand Down
1 change: 1 addition & 0 deletions contracts/src/token/erc20/extensions/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#[storage]
pub struct Erc20Metadata {
/// Common Metadata.
#[allow(clippy::used_underscore_binding)]
pub _metadata: Metadata,
}

Expand Down Expand Up @@ -60,7 +61,7 @@
fn decimals(&self) -> u8;
}

#[public]

Check warning on line 64 in contracts/src/token/erc20/extensions/metadata.rs

View workflow job for this annotation

GitHub Actions / nightly / doc

unexpected `cfg` condition value: `export-abi`

Check warning on line 64 in contracts/src/token/erc20/extensions/metadata.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 64 in contracts/src/token/erc20/extensions/metadata.rs

View workflow job for this annotation

GitHub Actions / ubuntu / beta

unexpected `cfg` condition value: `export-abi`

Check warning on line 64 in contracts/src/token/erc20/extensions/metadata.rs

View workflow job for this annotation

GitHub Actions / ubuntu / nightly / coverage

unexpected `cfg` condition value: `export-abi`
impl IErc20Metadata for Erc20Metadata {
fn name(&self) -> String {
self._metadata.name()
Expand Down
14 changes: 9 additions & 5 deletions contracts/src/token/erc20/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use stylus_sdk::{
stylus_proc::{public, SolidityError},
};

use crate::utils::introspection::erc165::{Erc165, IErc165};
use crate::utils::{
introspection::erc165::{Erc165, IErc165},
math::storage::{AddAssignUnchecked, SubAssignUnchecked},
};

pub mod extensions;
pub mod utils;
Expand Down Expand Up @@ -123,10 +126,13 @@ impl MethodError for Error {
#[storage]
pub struct Erc20 {
/// Maps users to balances.
#[allow(clippy::used_underscore_binding)]
pub _balances: StorageMap<Address, StorageU256>,
/// Maps users to a mapping of each spender's allowance.
#[allow(clippy::used_underscore_binding)]
pub _allowances: StorageMap<Address, StorageMap<Address, StorageU256>>,
/// The total supply of the token.
#[allow(clippy::used_underscore_binding)]
pub _total_supply: StorageU256,
}

Expand Down Expand Up @@ -489,17 +495,15 @@ impl Erc20 {
}

if to.is_zero() {
let total_supply = self.total_supply();
// Overflow not possible:
// `value` <= `_total_supply` or
// `value` <= `from_balance` <= `_total_supply`.
self._total_supply.set(total_supply - value);
self._total_supply.sub_assign_unchecked(value);
} else {
let balance_to = self._balances.get(to);
// Overflow not possible:
// `balance_to` + `value` is at most `total_supply`,
// which fits into a `U256`.
self._balances.setter(to).set(balance_to + value);
self._balances.setter(to).add_assign_unchecked(value);
}

evm::log(Transfer { from, to, value });
Expand Down
14 changes: 6 additions & 8 deletions contracts/src/token/erc721/extensions/consecutive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,20 @@ pub struct Erc721Consecutive {
/// Erc721 contract storage.
pub erc721: Erc721,
/// Checkpoint library contract for sequential ownership.
#[allow(clippy::used_underscore_binding)]
pub _sequential_ownership: Trace<S160>,
/// BitMap library contract for sequential burn of tokens.
#[allow(clippy::used_underscore_binding)]
pub _sequential_burn: BitMap,
/// Used to offset the first token id in
/// [`Erc721Consecutive::_next_consecutive_id`].
#[allow(clippy::used_underscore_binding)]
pub _first_consecutive_id: StorageU96,
/// Maximum size of a batch of consecutive tokens. This is designed to
/// limit stress on off-chain indexing services that have to record one
/// entry per token, and have protections against "unreasonably large"
/// batches of tokens.
#[allow(clippy::used_underscore_binding)]
pub _max_batch_size: StorageU96,
}

Expand Down Expand Up @@ -452,17 +456,11 @@ impl Erc721Consecutive {
// Clear approval. No need to re-authorize or emit the `Approval`
// event.
self._approve(Address::ZERO, token_id, Address::ZERO, false)?;
self.erc721
._balances
.setter(from)
.sub_assign_unchecked(uint!(1_U256));
self.erc721._balances.setter(from).sub_assign_unchecked(1);
}

if !to.is_zero() {
self.erc721
._balances
.setter(to)
.add_assign_unchecked(uint!(1_U256));
self.erc721._balances.setter(to).add_assign_unchecked(1);
}

self.erc721._owners.setter(token_id).set(to);
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/token/erc721/extensions/enumerable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,16 @@ pub enum Error {
#[storage]
pub struct Erc721Enumerable {
/// Maps owners to a mapping of indices to tokens ids.
#[allow(clippy::used_underscore_binding)]
pub _owned_tokens: StorageMap<Address, StorageMap<U256, StorageU256>>,
/// Maps tokens ids to indices in `_owned_tokens`.
#[allow(clippy::used_underscore_binding)]
pub _owned_tokens_index: StorageMap<U256, StorageU256>,
/// Stores all tokens ids.
#[allow(clippy::used_underscore_binding)]
pub _all_tokens: StorageVec<StorageU256>,
/// Maps indices at `_all_tokens` to tokens ids.
#[allow(clippy::used_underscore_binding)]
pub _all_tokens_index: StorageMap<U256, StorageU256>,
}

Expand Down
2 changes: 2 additions & 0 deletions contracts/src/token/erc721/extensions/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ use crate::{
#[storage]
pub struct Erc721Metadata {
/// Common Metadata.
#[allow(clippy::used_underscore_binding)]
pub _metadata: Metadata,
/// Base URI for tokens.
#[allow(clippy::used_underscore_binding)]
pub _base_uri: StorageString,
}

Expand Down
1 change: 1 addition & 0 deletions contracts/src/token/erc721/extensions/uri_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mod sol {
#[storage]
pub struct Erc721UriStorage {
/// Optional mapping for token URIs.
#[allow(clippy::used_underscore_binding)]
pub _token_uris: StorageMap<U256, StorageString>,
}

Expand Down
12 changes: 8 additions & 4 deletions contracts/src/token/erc721/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Implementation of the [`Erc721`] token standard.
use alloc::vec;

use alloy_primitives::{uint, Address, FixedBytes, U128, U256};
use alloy_primitives::{Address, FixedBytes, U128, U256};
use openzeppelin_stylus_proc::interface_id;
use stylus_sdk::{
abi::Bytes,
Expand Down Expand Up @@ -197,12 +197,16 @@ mod receiver {
#[storage]
pub struct Erc721 {
/// Maps tokens to owners.
#[allow(clippy::used_underscore_binding)]
pub _owners: StorageMap<U256, StorageAddress>,
/// Maps users to balances.
#[allow(clippy::used_underscore_binding)]
pub _balances: StorageMap<Address, StorageU256>,
/// Maps tokens to approvals.
#[allow(clippy::used_underscore_binding)]
pub _token_approvals: StorageMap<U256, StorageAddress>,
/// Maps owners to a mapping of operator approvals.
#[allow(clippy::used_underscore_binding)]
pub _operator_approvals:
StorageMap<Address, StorageMap<Address, StorageBool>>,
}
Expand Down Expand Up @@ -688,7 +692,7 @@ impl Erc721 {
/// * `account` - Account to increase balance.
/// * `value` - The number of tokens to increase balance.
pub fn _increase_balance(&mut self, account: Address, value: U128) {
self._balances.setter(account).add_assign_unchecked(U256::from(value));
self._balances.setter(account).add_assign_unchecked(value);
}

/// Transfers `token_id` from its current owner to `to`, or alternatively
Expand Down Expand Up @@ -738,11 +742,11 @@ impl Erc721 {
// Clear approval. No need to re-authorize or emit the `Approval`
// event.
self._approve(Address::ZERO, token_id, Address::ZERO, false)?;
self._balances.setter(from).sub_assign_unchecked(uint!(1_U256));
self._balances.setter(from).sub_assign_unchecked(1);
}

if !to.is_zero() {
self._balances.setter(to).add_assign_unchecked(uint!(1_U256));
self._balances.setter(to).add_assign_unchecked(1);
}

self._owners.setter(token_id).set(to);
Expand Down
18 changes: 11 additions & 7 deletions contracts/src/utils/math/storage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Simple math operations missing in `stylus_sdk::storage`.
use alloy_primitives::Uint;
use alloy_primitives::{ruint::UintTryFrom, Uint};
use stylus_sdk::storage::StorageUint;

/// Adds value and assign the result to `self`, ignoring overflow.
Expand All @@ -8,11 +8,13 @@ pub(crate) trait AddAssignUnchecked<T> {
fn add_assign_unchecked(&mut self, rhs: T);
}

impl<const B: usize, const L: usize> AddAssignUnchecked<Uint<B, L>>
impl<T, const B: usize, const L: usize> AddAssignUnchecked<T>
for StorageUint<B, L>
where
Uint<B, L>: UintTryFrom<T>,
{
fn add_assign_unchecked(&mut self, rhs: Uint<B, L>) {
let new_balance = self.get() + rhs;
fn add_assign_unchecked(&mut self, rhs: T) {
let new_balance = self.get() + Uint::<B, L>::from(rhs);
0xNeshi marked this conversation as resolved.
Show resolved Hide resolved
self.set(new_balance);
}
}
Expand All @@ -23,11 +25,13 @@ pub(crate) trait SubAssignUnchecked<T> {
fn sub_assign_unchecked(&mut self, rhs: T);
}

impl<const B: usize, const L: usize> SubAssignUnchecked<Uint<B, L>>
impl<T, const B: usize, const L: usize> SubAssignUnchecked<T>
for StorageUint<B, L>
where
Uint<B, L>: UintTryFrom<T>,
{
fn sub_assign_unchecked(&mut self, rhs: Uint<B, L>) {
let new_balance = self.get() - rhs;
fn sub_assign_unchecked(&mut self, rhs: T) {
let new_balance = self.get() - Uint::<B, L>::from(rhs);
self.set(new_balance);
}
}
2 changes: 2 additions & 0 deletions contracts/src/utils/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ use stylus_sdk::{
#[storage]
pub struct Metadata {
/// Token name.
#[allow(clippy::used_underscore_binding)]
pub _name: StorageString,
/// Token symbol.
#[allow(clippy::used_underscore_binding)]
pub _symbol: StorageString,
}

Expand Down
Loading
Loading