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

Add initializer_no_metadata to erc1155 component #1287

Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- ERC1155Component `initializer_no_metadata` (#1287)
- SRC9 (Outside Execution) integration to account presets (#1201)
- `SNIP12HashSpanImpl` to `openzeppelin_utils::cryptography::snip12` (#1180)
- GovernorComponent with the following extensions: (#1180)
Expand Down
16 changes: 16 additions & 0 deletions docs/modules/ROOT/pages/api/erc1155.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ NOTE: See xref:#ERC1155Component-Hooks[Hooks] to understand how are hooks used.
--
.InternalImpl
* xref:#ERC1155Component-initializer[`++initializer(self, base_uri)++`]
* xref:#ERC1155Component-initializer_no_metadata[`++initializer_no_metadata(self)++`]
* xref:#ERC1155Component-mint_with_acceptance_check[`++mint_with_acceptance_check(self, to, token_id, value, data)++`]
* xref:#ERC1155Component-batch_mint_with_acceptance_check[`++batch_mint_with_acceptance_check(self, to, token_ids, values, data)++`]
* xref:#ERC1155Component-burn[`++burn(self, from, token_id, value)++`]
Expand Down Expand Up @@ -401,6 +402,21 @@ See <<ERC1155Component-is_approved_for_all,ERC1155Component::is_approved_for_all
Initializes the contract by setting the token's base URI as `base_uri`, and registering the supported interfaces.
This should only be used inside the contract's constructor.

WARNING: Most ERC1155 contracts expose the <<IERC1155MetadataURI,IERC1155MetadataURI>> interface which is what this initializer is meant to support.
If the contract DOES NOT expose the <<IERC1155MetadataURI,IERC1155MetadataURI>> interface, meaning tokens do not have a URI,
the contract must instead use <<ERC1155Component-initializer_no_metadata,initializer_no_metadata>> in the constructor.
Failure to abide by these instructions can lead to unexpected issues especially with UIs.

[.contract-item]
[[ERC1155Component-initializer_no_metadata]]
==== `[.contract-item-name]#++initializer_no_metadata++#++(ref self: ContractState)++` [.item-kind]#internal#

Initializes the contract with no metadata by registering only the IERC1155 interface.

WARNING: This initializer should ONLY be used during construction in the very
specific instance when the contract does NOT expose the <<IERC1155MetadataURI,IERC1155MetadataURI>> interface.
Initializing a contract with this initializer means that tokens will not have a URI.

[.contract-item]
[[ERC1155Component-mint_with_acceptance_check]]
==== `[.contract-item-name]#++mint_with_acceptance_check++#++(ref self: ContractState, to: ContractAddress, token_id: u256, value: u256, data: Span<felt252>)++` [.item-kind]#internal#
Expand Down
19 changes: 19 additions & 0 deletions packages/token/src/erc1155/erc1155.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@ pub mod ERC1155Component {
/// Initializes the contract by setting the `base_uri` for all tokens,
/// and registering the supported interfaces.
/// This should only be used inside the contract's constructor.
///
/// WARNING: Most ERC1155 contracts expose the `IERC1155MetadataURI` interface which
/// is what this initializer is meant to support.
/// If the contract DOES NOT expose the `IERC1155MetadataURI` interface,
/// meaning the token does not have a URI, the contract must instead use
/// `initializer_no_metadata` in the constructor.
/// Failure to abide by these instructions can lead to unexpected issues especially with
/// UIs.
fn initializer(ref self: ComponentState<TContractState>, base_uri: ByteArray) {
self._set_base_uri(base_uri);

Expand All @@ -478,6 +486,17 @@ pub mod ERC1155Component {
src5_component.register_interface(interface::IERC1155_METADATA_URI_ID);
}

/// Initializes the contract with no metadata by registering only the IERC1155 interface.
///
/// WARNING: This initializer should ONLY be used during construction in the very
/// specific instance when the contract does NOT expose the `IERC1155MetadataURI` interface.
/// Initializing a contract with this initializer means that tokens will not
/// have a URI.
fn initializer_no_metadata(ref self: ComponentState<TContractState>) {
let mut src5_component = get_dep_component_mut!(ref self, SRC5);
src5_component.register_interface(interface::IERC1155_ID);
}

/// Creates a `value` amount of tokens of type `token_id`, and assigns them to `to`.
///
/// Requirements:
Expand Down
23 changes: 23 additions & 0 deletions packages/token/src/tests/erc1155/test_erc1155.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ fn test_initialize() {
assert!(supports_isrc5);
}

#[test]
fn test_initialize_no_metadata() {
let mut state = COMPONENT_STATE();
let mock_state = CONTRACT_STATE();

state.initializer_no_metadata();

let empty_str = "";
assert_eq!(state.ERC1155_uri.read(), empty_str);
assert!(state.balance_of(OWNER(), TOKEN_ID).is_zero());

let supports_ierc1155 = mock_state.supports_interface(erc1155::interface::IERC1155_ID);
assert!(supports_ierc1155);

let does_not_support_ierc1155_metadata_uri = !mock_state
.supports_interface(erc1155::interface::IERC1155_METADATA_URI_ID);
assert!(does_not_support_ierc1155_metadata_uri);

let supports_isrc5 = mock_state
.supports_interface(openzeppelin_introspection::interface::ISRC5_ID);
assert!(supports_isrc5);
}

//
// balance_of & balanceOf
//
Expand Down
Loading