Skip to content

Commit

Permalink
feat: ERC1155 URIStorage Extension (#431)
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.
-->

Since Erc1155-related benches and tests have become huge, decided to split them into separate files/directories.

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

\#### 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.
-->

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

---------

Co-authored-by: Alisander Qoshqosh <[email protected]>
  • Loading branch information
0xNeshi and qalisander committed Dec 9, 2024
1 parent 504b083 commit 36b4dc1
Show file tree
Hide file tree
Showing 20 changed files with 765 additions and 369 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `Erc1155UriStorage` extension. #431
- `VestingWallet` contract. #402
- `Erc1155Burnable` extension. #417
- `Erc1155MetadataUri` extension. #416
Expand Down
13 changes: 13 additions & 0 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ members = [
"examples/erc721-consecutive",
"examples/erc721-metadata",
"examples/erc1155",
"examples/erc1155-metadata-uri",
"examples/merkle-proofs",
"examples/ownable",
"examples/vesting-wallet",
Expand All @@ -37,6 +38,7 @@ default-members = [
"examples/erc721-consecutive",
"examples/erc721-metadata",
"examples/erc1155",
"examples/erc1155-metadata-uri",
"examples/safe-erc20",
"examples/merkle-proofs",
"examples/ownable",
Expand Down Expand Up @@ -102,7 +104,9 @@ tiny-keccak = { version = "2.0.2", features = ["keccak"] }
tokio = { version = "1.12.0", features = ["full"] }
futures = "0.3.30"
dashmap = "6.1.0"
crypto-bigint = { version = "0.5.5", default-features = false, features = ["zeroize"] }
crypto-bigint = { version = "0.5.5", default-features = false, features = [
"zeroize",
] }
num-traits = "0.2.14"
zeroize = { version = "1.8.1", features = ["derive"] }
proptest = "1"
Expand Down
12 changes: 2 additions & 10 deletions benches/src/erc1155.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use alloy::{
primitives::Address,
providers::ProviderBuilder,
sol,
sol_types::{SolCall, SolConstructor},
sol_types::SolCall,
uint,
};
use e2e::{receipt, Account};
Expand All @@ -26,14 +26,9 @@ sol!(
function mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) external;
function burn(address account, uint256 id, uint256 value) external;
function burnBatch(address account, uint256[] memory ids, uint256[] memory values) external;
function uri(uint256 id) external view returns (string memory uri);
}
);

sol!("../examples/erc1155/src/constructor.sol");

const URI: &str = "https://github.com/OpenZeppelin/rust-contracts-stylus";

pub async fn bench() -> eyre::Result<ContractReport> {
let reports = run_with(CacheOpt::None).await?;
let report = reports
Expand Down Expand Up @@ -102,7 +97,6 @@ pub async fn run_with(
// We should burn Bob's tokens on behalf of Bob, not Alice.
(burnCall::SIGNATURE, receipt!(contract_bob.burn(bob_addr, token_1, value_1))?),
(burnBatchCall::SIGNATURE, receipt!(contract_bob.burnBatch(bob_addr, ids, values))?),
(uriCall::SIGNATURE, receipt!(contract.uri(token_1))?),
];

receipts
Expand All @@ -115,7 +109,5 @@ async fn deploy(
account: &Account,
cache_opt: CacheOpt,
) -> eyre::Result<Address> {
let args = Erc1155Example::constructorCall { uri_: URI.to_owned() };
let args = alloy::hex::encode(args.abi_encode());
crate::deploy(account, "erc1155", Some(args), cache_opt).await
crate::deploy(account, "erc1155", None, cache_opt).await
}
85 changes: 85 additions & 0 deletions benches/src/erc1155_metadata_uri.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use alloy::{
network::{AnyNetwork, EthereumWallet},
primitives::Address,
providers::ProviderBuilder,
sol,
sol_types::{SolCall, SolConstructor},
uint,
};
use e2e::{receipt, Account};

use crate::{
report::{ContractReport, FunctionReport},
CacheOpt,
};

sol!(
#[sol(rpc)]
contract Erc1155MetadataUri {
function uri(uint256 id) external view returns (string memory uri);
function setTokenURI(uint256 tokenId, string memory tokenURI) external;
function setBaseURI(string memory tokenURI) external;
}
);

sol!("../examples/erc1155-metadata-uri/src/constructor.sol");

const URI: &str = "https://github.com/OpenZeppelin/rust-contracts-stylus";
const BASE_URI: &str = "https://github.com";
const TOKEN_URI: &str = "/some/token/uri";

pub async fn bench() -> eyre::Result<ContractReport> {
let reports = run_with(CacheOpt::None).await?;
let report = reports.into_iter().try_fold(
ContractReport::new("Erc1155MetadataUri"),
ContractReport::add,
)?;

let cached_reports = run_with(CacheOpt::Bid(0)).await?;
let report = cached_reports
.into_iter()
.try_fold(report, ContractReport::add_cached)?;

Ok(report)
}

pub async fn run_with(
cache_opt: CacheOpt,
) -> eyre::Result<Vec<FunctionReport>> {
let alice = Account::new().await?;
let alice_wallet = ProviderBuilder::new()
.network::<AnyNetwork>()
.with_recommended_fillers()
.wallet(EthereumWallet::from(alice.signer.clone()))
.on_http(alice.url().parse()?);

let contract_addr = deploy(&alice, cache_opt).await?;

let contract = Erc1155MetadataUri::new(contract_addr, &alice_wallet);

let token_id = uint!(1_U256);

// IMPORTANT: Order matters!
use Erc1155MetadataUri::*;
#[rustfmt::skip]
let receipts = vec![
(setTokenURICall::SIGNATURE, receipt!(contract.setTokenURI(token_id, TOKEN_URI.to_owned()))?),
(setBaseURICall::SIGNATURE, receipt!(contract.setBaseURI(BASE_URI.to_owned()))?),
(uriCall::SIGNATURE, receipt!(contract.uri(token_id))?),
];

receipts
.into_iter()
.map(FunctionReport::new)
.collect::<eyre::Result<Vec<_>>>()
}

async fn deploy(
account: &Account,
cache_opt: CacheOpt,
) -> eyre::Result<Address> {
let args =
Erc1155MetadataUriExample::constructorCall { uri_: URI.to_owned() };
let args = alloy::hex::encode(args.abi_encode());
crate::deploy(account, "erc1155-metadata-uri", Some(args), cache_opt).await
}
1 change: 1 addition & 0 deletions benches/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use serde::Deserialize;

pub mod access_control;
pub mod erc1155;
pub mod erc1155_metadata_uri;
pub mod erc20;
pub mod erc721;
pub mod merkle_proofs;
Expand Down
5 changes: 3 additions & 2 deletions benches/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use benches::{
access_control, erc1155, erc20, erc721, merkle_proofs, ownable,
report::BenchmarkReport,
access_control, erc1155, erc1155_metadata_uri, erc20, erc721,
merkle_proofs, ownable, report::BenchmarkReport,
};
use futures::FutureExt;
use itertools::Itertools;
Expand All @@ -14,6 +14,7 @@ async fn main() -> eyre::Result<()> {
merkle_proofs::bench().boxed(),
ownable::bench().boxed(),
erc1155::bench().boxed(),
erc1155_metadata_uri::bench().boxed(),
];

// Run benchmarks max 3 at the same time.
Expand Down
2 changes: 2 additions & 0 deletions contracts/src/token/erc1155/extensions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Common extensions to the ERC-1155 standard.
pub mod burnable;
pub mod metadata_uri;
pub mod uri_storage;

pub use burnable::IErc1155Burnable;
pub use metadata_uri::{Erc1155MetadataUri, IErc1155MetadataUri};
pub use uri_storage::Erc1155UriStorage;
Loading

0 comments on commit 36b4dc1

Please sign in to comment.