Skip to content

Commit

Permalink
ref: remove rand Dev Dependency from ./contracts/* (#490)
Browse files Browse the repository at this point in the history
<!--
Thank you for your interest in contributing to OpenZeppelin!

Consider opening an issue for discussion prior to submitting a PR. New
features will be merged faster if they were first discussed and designed
with the team.

Describe the changes introduced in this pull request. Include any
context necessary for understanding the PR's purpose.
-->

<!-- Fill in with issue number -->
Resolves #435 

#### PR Checklist

<!--
Before merging the pull request all of the following must be completed.
Feel free to submit a PR or Draft PR even if some items are pending.
Some of the items may not apply.
-->

- [ ] Tests
- [ ] Documentation
- [ ] Changelog

---------

Co-authored-by: Daniel Bigos <[email protected]>
  • Loading branch information
0xNeshi and bidzyyys authored Jan 14, 2025
1 parent badee40 commit 4b5cedb
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 419 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ openzeppelin-stylus-proc.workspace = true
[dev-dependencies]
alloy-primitives = { workspace = true, features = ["arbitrary"] }
motsu.workspace = true
rand.workspace = true

[features]
# Enables using the standard library. This is not included in the default
Expand Down
11 changes: 3 additions & 8 deletions contracts/src/token/erc1155/extensions/metadata_uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,19 @@ impl IErc165 for Erc1155MetadataUri {

#[cfg(all(test, feature = "std"))]
mod tests {
use alloy_primitives::U256;
use stylus_sdk::alloy_primitives::uint;

use super::{Erc1155MetadataUri, IErc1155MetadataUri, IErc165};

fn random_token_id() -> U256 {
let num: u32 = rand::random();
U256::from(num)
}

#[motsu::test]
fn uri_ignores_token_id(contract: Erc1155MetadataUri) {
let uri = String::from("https://token-cdn-domain/\\{id\\}.json");
contract._uri.set_str(uri.clone());

let token_id = random_token_id();
let token_id = uint!(1_U256);
assert_eq!(uri, contract.uri(token_id));

let token_id = random_token_id();
let token_id = uint!(2_U256);
assert_eq!(uri, contract.uri(token_id));
}

Expand Down
40 changes: 16 additions & 24 deletions contracts/src/token/erc1155/extensions/uri_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,128 +92,120 @@ impl Erc1155UriStorage {

#[cfg(all(test, feature = "std"))]
mod tests {
use alloy_primitives::U256;
use stylus_sdk::prelude::storage;
use stylus_sdk::{
alloy_primitives::{uint, U256},
prelude::storage,
};

use super::Erc1155UriStorage;
use crate::token::erc1155::extensions::Erc1155MetadataUri;

fn random_token_id() -> U256 {
let num: u32 = rand::random();
U256::from(num)
}

#[storage]
struct Erc1155MetadataExample {
pub metadata_uri: Erc1155MetadataUri,
pub uri_storage: Erc1155UriStorage,
}

const TOKEN_ID: U256 = uint!(1_U256);

#[motsu::test]
fn uri_returns_metadata_uri_when_token_uri_is_not_set(
contract: Erc1155MetadataExample,
) {
let token_id = random_token_id();
let uri = "https://some.metadata/token/uri";

contract.metadata_uri._uri.set_str(uri.to_owned());

assert_eq!(
uri,
contract.uri_storage.uri(token_id, &contract.metadata_uri)
contract.uri_storage.uri(TOKEN_ID, &contract.metadata_uri)
);
}

#[motsu::test]
fn uri_returns_empty_string_when_no_uri_is_set(
contract: Erc1155MetadataExample,
) {
let token_id = random_token_id();

assert!(contract
.uri_storage
.uri(token_id, &contract.metadata_uri)
.uri(TOKEN_ID, &contract.metadata_uri)
.is_empty());
}

#[motsu::test]
fn uri_returns_token_uri_when_base_uri_is_empty(
contract: Erc1155MetadataExample,
) {
let token_id = random_token_id();
let token_uri = "https://some.short/token/uri";

contract
.uri_storage
._token_uris
.setter(token_id)
.setter(TOKEN_ID)
.set_str(token_uri.to_owned());

assert_eq!(
token_uri,
contract.uri_storage.uri(token_id, &contract.metadata_uri)
contract.uri_storage.uri(TOKEN_ID, &contract.metadata_uri)
);
}

#[motsu::test]
fn uri_returns_concatenated_base_uri_and_token_uri(
contract: Erc1155MetadataExample,
) {
let token_id = random_token_id();
let base_uri = "https://some.base.uri";
let token_uri = "/some/token/uri";

contract.uri_storage._base_uri.set_str(base_uri.to_owned());
contract
.uri_storage
._token_uris
.setter(token_id)
.setter(TOKEN_ID)
.set_str(token_uri.to_owned());

assert_eq!(
base_uri.to_string() + token_uri,
contract.uri_storage.uri(token_id, &contract.metadata_uri)
contract.uri_storage.uri(TOKEN_ID, &contract.metadata_uri)
);
}

#[motsu::test]
fn uri_ignores_metadata_uri_when_token_uri_is_set(
contract: Erc1155MetadataExample,
) {
let token_id = random_token_id();
let uri = "https://some.metadata/token/uri";
let token_uri = "https://some.short/token/uri";

contract.metadata_uri._uri.set_str(uri.to_owned());
contract
.uri_storage
._token_uris
.setter(token_id)
.setter(TOKEN_ID)
.set_str(token_uri.to_owned());

assert_eq!(
token_uri,
contract.uri_storage.uri(token_id, &contract.metadata_uri)
contract.uri_storage.uri(TOKEN_ID, &contract.metadata_uri)
);
}

#[motsu::test]
fn test_set_uri(contract: Erc1155MetadataExample) {
let token_id = random_token_id();
let uri = "https://some.metadata/token/uri";
let token_uri = "https://some.short/token/uri".to_string();

contract.metadata_uri._uri.set_str(uri.to_owned());

contract.uri_storage.set_token_uri(
token_id,
TOKEN_ID,
token_uri.clone(),
&contract.metadata_uri,
);

assert_eq!(
token_uri,
contract.uri_storage.uri(token_id, &contract.metadata_uri)
contract.uri_storage.uri(TOKEN_ID, &contract.metadata_uri)
);
}

Expand Down
62 changes: 28 additions & 34 deletions contracts/src/token/erc721/extensions/burnable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,31 @@ impl IErc721Burnable for Erc721 {

#[cfg(all(test, feature = "std"))]
mod tests {
use alloy_primitives::{address, uint, Address};
use alloy_primitives::{address, uint, Address, U256};
use stylus_sdk::msg;

use super::IErc721Burnable;
use crate::token::erc721::{
tests::random_token_id, ERC721InsufficientApproval,
ERC721NonexistentToken, Erc721, Error, IErc721,
ERC721InsufficientApproval, ERC721NonexistentToken, Erc721, Error,
IErc721,
};

const BOB: Address = address!("F4EaCDAbEf3c8f1EdE91b6f2A6840bc2E4DD3526");

const TOKEN_ID: U256 = uint!(1_U256);

#[motsu::test]
fn burns(contract: Erc721) {
let alice = msg::sender();
let one = uint!(1_U256);
let token_id = random_token_id();

contract._mint(alice, token_id).expect("should mint a token for Alice");
contract._mint(alice, TOKEN_ID).expect("should mint a token for Alice");

let initial_balance = contract
.balance_of(alice)
.expect("should return the balance of Alice");

let result = contract.burn(token_id);
let result = contract.burn(TOKEN_ID);
assert!(result.is_ok());

let balance = contract
Expand All @@ -87,41 +88,40 @@ mod tests {
assert_eq!(initial_balance - one, balance);

let err = contract
.owner_of(token_id)
.owner_of(TOKEN_ID)
.expect_err("should return Error::NonexistentToken");

assert!(matches!(
err,
Error::NonexistentToken (ERC721NonexistentToken{
token_id: t_id
}) if t_id == token_id
}) if t_id == TOKEN_ID
));
}

#[motsu::test]
fn burns_with_approval(contract: Erc721) {
let alice = msg::sender();
let token_id = random_token_id();

contract._mint(BOB, token_id).expect("should mint a token for Bob");
contract._mint(BOB, TOKEN_ID).expect("should mint a token for Bob");

let initial_balance =
contract.balance_of(BOB).expect("should return the balance of Bob");

contract._token_approvals.setter(token_id).set(alice);
contract._token_approvals.setter(TOKEN_ID).set(alice);

let result = contract.burn(token_id);
let result = contract.burn(TOKEN_ID);
assert!(result.is_ok());

let err = contract
.owner_of(token_id)
.owner_of(TOKEN_ID)
.expect_err("should return Error::NonexistentToken");

assert!(matches!(
err,
Error::NonexistentToken (ERC721NonexistentToken{
token_id: t_id
}) if t_id == token_id
}) if t_id == TOKEN_ID
));

let balance =
Expand All @@ -133,29 +133,28 @@ mod tests {
#[motsu::test]
fn burns_with_approval_for_all(contract: Erc721) {
let alice = msg::sender();
let token_id = random_token_id();

contract._mint(BOB, token_id).expect("should mint a token for Bob");
contract._mint(BOB, TOKEN_ID).expect("should mint a token for Bob");

let initial_balance =
contract.balance_of(BOB).expect("should return the balance of Bob");

// As we cannot change `msg::sender()`, we need to use this workaround.
contract._operator_approvals.setter(BOB).setter(alice).set(true);

let result = contract.burn(token_id);
let result = contract.burn(TOKEN_ID);

assert!(result.is_ok());

let err = contract
.owner_of(token_id)
.owner_of(TOKEN_ID)
.expect_err("should return Error::NonexistentToken");

assert!(matches!(
err,
Error::NonexistentToken (ERC721NonexistentToken{
token_id: t_id
}) if t_id == token_id
}) if t_id == TOKEN_ID
));

let balance =
Expand All @@ -167,59 +166,54 @@ mod tests {
#[motsu::test]
fn error_when_get_approved_of_previous_approval_burned(contract: Erc721) {
let alice = msg::sender();
let token_id = random_token_id();

contract._mint(alice, token_id).expect("should mint a token for Alice");
contract._mint(alice, TOKEN_ID).expect("should mint a token for Alice");
contract
.approve(BOB, token_id)
.approve(BOB, TOKEN_ID)
.expect("should approve a token for Bob");

contract.burn(token_id).expect("should burn previously minted token");
contract.burn(TOKEN_ID).expect("should burn previously minted token");

let err = contract
.get_approved(token_id)
.get_approved(TOKEN_ID)
.expect_err("should return Error::NonexistentToken");

assert!(matches!(
err,
Error::NonexistentToken (ERC721NonexistentToken{
token_id: t_id
}) if t_id == token_id
}) if t_id == TOKEN_ID
));
}

#[motsu::test]
fn error_when_burn_without_approval(contract: Erc721) {
let token_id = random_token_id();

contract._mint(BOB, token_id).expect("should mint a token for Bob");
contract._mint(BOB, TOKEN_ID).expect("should mint a token for Bob");

let err = contract
.burn(token_id)
.burn(TOKEN_ID)
.expect_err("should not burn unapproved token");

assert!(matches!(
err,
Error::InsufficientApproval(ERC721InsufficientApproval {
operator,
token_id: t_id,
}) if operator == msg::sender() && t_id == token_id
}) if operator == msg::sender() && t_id == TOKEN_ID
));
}

#[motsu::test]
fn error_when_burn_nonexistent_token(contract: Erc721) {
let token_id = random_token_id();

let err = contract
.burn(token_id)
.burn(TOKEN_ID)
.expect_err("should return Error::NonexistentToken");

assert!(matches!(
err,
Error::NonexistentToken (ERC721NonexistentToken{
token_id: t_id
}) if t_id == token_id
}) if t_id == TOKEN_ID
));
}
}
Loading

0 comments on commit 4b5cedb

Please sign in to comment.