Skip to content

Commit

Permalink
feat: ERC1155 (#275)
Browse files Browse the repository at this point in the history
Resolves #261 

#### PR Checklist

- [x] ERC1155
- [x] Tests

---------

Co-authored-by: Alisander Qoshqosh <[email protected]>
Co-authored-by: Daniel Bigos <[email protected]>
Co-authored-by: Nenad <[email protected]>
  • Loading branch information
4 people authored Nov 7, 2024
1 parent ce244e9 commit 10690f1
Show file tree
Hide file tree
Showing 20 changed files with 4,239 additions and 2 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

- ERC-1155 Multi Token Standard. #275
- `SafeErc20` Utility. #289
- Finite Fields arithmetics. #376

Expand Down
14 changes: 14 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ members = [
"examples/erc721",
"examples/erc721-consecutive",
"examples/erc721-metadata",
"examples/erc1155",
"examples/merkle-proofs",
"examples/ownable",
"examples/access-control",
Expand All @@ -33,6 +34,7 @@ default-members = [
"examples/erc721",
"examples/erc721-consecutive",
"examples/erc721-metadata",
"examples/erc1155",
"examples/safe-erc20",
"examples/merkle-proofs",
"examples/ownable",
Expand Down
104 changes: 104 additions & 0 deletions benches/src/erc1155.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use alloy::{
network::{AnyNetwork, EthereumWallet},
primitives::Address,
providers::ProviderBuilder,
sol,
sol_types::SolCall,
uint,
};
use e2e::{receipt, Account};

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

sol!(
#[sol(rpc)]
contract Erc1155 {
function balanceOf(address account, uint256 id) external view returns (uint256 balance);
function balanceOfBatch(address[] accounts, uint256[] ids) external view returns (uint256[] memory balances);
function isApprovedForAll(address account, address operator) external view returns (bool approved);
function setApprovalForAll(address operator, bool approved) external;
function safeTransferFrom(address from, address to, uint256 id, uint256 value, bytes memory data) external;
function safeBatchTransferFrom(address from, address to, uint256[] memory ids, uint256[] memory values, bytes memory data) external;
function mint(address to, uint256 id, uint256 amount, bytes memory data) external;
function mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) external;
}
);

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

pub async fn bench() -> eyre::Result<ContractReport> {
let reports = run_with(CacheOpt::None).await?;
let report = reports
.into_iter()
.try_fold(ContractReport::new("Erc1155"), 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_addr = alice.address();
let alice_wallet = ProviderBuilder::new()
.network::<AnyNetwork>()
.with_recommended_fillers()
.wallet(EthereumWallet::from(alice.signer.clone()))
.on_http(alice.url().parse()?);

let bob = Account::new().await?;
let bob_addr = bob.address();

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

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

let token_1 = uint!(1_U256);
let token_2 = uint!(2_U256);
let token_3 = uint!(3_U256);
let token_4 = uint!(4_U256);

let value_1 = uint!(100_U256);
let value_2 = uint!(200_U256);
let value_3 = uint!(300_U256);
let value_4 = uint!(400_U256);

let ids = vec![token_1, token_2, token_3, token_4];
let values = vec![value_1, value_2, value_3, value_4];

let data: alloy_primitives::Bytes = vec![].into();

// IMPORTANT: Order matters!
use Erc1155::*;
#[rustfmt::skip]
let receipts = vec![
(mintCall::SIGNATURE, receipt!(contract.mint(alice_addr, token_1, value_1, data.clone()))?),
(mintBatchCall::SIGNATURE, receipt!(contract.mintBatch(alice_addr, ids.clone(), values.clone(), data.clone()))?),
(balanceOfCall::SIGNATURE, receipt!(contract.balanceOf(alice_addr, token_1))?),
(balanceOfBatchCall::SIGNATURE, receipt!(contract.balanceOfBatch(vec![alice_addr, bob_addr], vec![token_1, token_2]))?),
(setApprovalForAllCall::SIGNATURE, receipt!(contract.setApprovalForAll(bob_addr, true))?),
(isApprovedForAllCall::SIGNATURE, receipt!(contract.isApprovedForAll(alice_addr, bob_addr))?),
(safeTransferFromCall::SIGNATURE, receipt!(contract.safeTransferFrom(alice_addr, bob_addr, token_1, value_1, data.clone()))?),
(safeBatchTransferFromCall::SIGNATURE, receipt!(contract.safeBatchTransferFrom(alice_addr, bob_addr, ids, values, data.clone()))?)
];

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

async fn deploy(
account: &Account,
cache_opt: CacheOpt,
) -> eyre::Result<Address> {
crate::deploy(account, "erc1155", None, 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 @@ -14,6 +14,7 @@ use koba::config::{Deploy, Generate, PrivateKey};
use serde::Deserialize;

pub mod access_control;
pub mod erc1155;
pub mod erc20;
pub mod erc721;
pub mod merkle_proofs;
Expand Down
6 changes: 5 additions & 1 deletion contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ impl MyContract { }
```
*/

#![allow(clippy::pub_underscore_fields, clippy::module_name_repetitions)]
#![allow(
clippy::pub_underscore_fields,
clippy::module_name_repetitions,
clippy::used_underscore_items
)]
#![cfg_attr(not(feature = "std"), no_std, no_main)]
#![deny(rustdoc::broken_intra_doc_links)]
extern crate alloc;
Expand Down
Loading

0 comments on commit 10690f1

Please sign in to comment.