Skip to content

Commit 2444726

Browse files
Add initializer_no_metadata to erc1155 component (#1287)
* add ERC1155 initializer_no_metadata * add initializer_no_metadata to ERC1155 API * add changelog entry * fix doc fmt * update changelog entry
1 parent f5e6818 commit 2444726

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Added
1313

14+
- ERC1155Component `initializer_no_metadata` (#1287)
1415
- SRC9 (Outside Execution) integration to account presets (#1201)
1516
- `SNIP12HashSpanImpl` to `openzeppelin_utils::cryptography::snip12` (#1180)
1617
- GovernorComponent with the following extensions: (#1180)

docs/modules/ROOT/pages/api/erc1155.adoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ NOTE: See xref:#ERC1155Component-Hooks[Hooks] to understand how are hooks used.
220220
--
221221
.InternalImpl
222222
* xref:#ERC1155Component-initializer[`++initializer(self, base_uri)++`]
223+
* xref:#ERC1155Component-initializer_no_metadata[`++initializer_no_metadata(self)++`]
223224
* xref:#ERC1155Component-mint_with_acceptance_check[`++mint_with_acceptance_check(self, to, token_id, value, data)++`]
224225
* xref:#ERC1155Component-batch_mint_with_acceptance_check[`++batch_mint_with_acceptance_check(self, to, token_ids, values, data)++`]
225226
* xref:#ERC1155Component-burn[`++burn(self, from, token_id, value)++`]
@@ -401,6 +402,21 @@ See <<ERC1155Component-is_approved_for_all,ERC1155Component::is_approved_for_all
401402
Initializes the contract by setting the token's base URI as `base_uri`, and registering the supported interfaces.
402403
This should only be used inside the contract's constructor.
403404

405+
WARNING: Most ERC1155 contracts expose the <<IERC1155MetadataURI,IERC1155MetadataURI>> interface which is what this initializer is meant to support.
406+
If the contract DOES NOT expose the <<IERC1155MetadataURI,IERC1155MetadataURI>> interface, meaning tokens do not have a URI,
407+
the contract must instead use <<ERC1155Component-initializer_no_metadata,initializer_no_metadata>> in the constructor.
408+
Failure to abide by these instructions can lead to unexpected issues especially with UIs.
409+
410+
[.contract-item]
411+
[[ERC1155Component-initializer_no_metadata]]
412+
==== `[.contract-item-name]#++initializer_no_metadata++#++(ref self: ContractState)++` [.item-kind]#internal#
413+
414+
Initializes the contract with no metadata by registering only the IERC1155 interface.
415+
416+
WARNING: This initializer should ONLY be used during construction in the very
417+
specific instance when the contract does NOT expose the <<IERC1155MetadataURI,IERC1155MetadataURI>> interface.
418+
Initializing a contract with this initializer means that tokens will not have a URI.
419+
404420
[.contract-item]
405421
[[ERC1155Component-mint_with_acceptance_check]]
406422
==== `[.contract-item-name]#++mint_with_acceptance_check++#++(ref self: ContractState, to: ContractAddress, token_id: u256, value: u256, data: Span<felt252>)++` [.item-kind]#internal#

packages/token/src/erc1155/erc1155.cairo

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,14 @@ pub mod ERC1155Component {
470470
/// Initializes the contract by setting the `base_uri` for all tokens,
471471
/// and registering the supported interfaces.
472472
/// This should only be used inside the contract's constructor.
473+
///
474+
/// WARNING: Most ERC1155 contracts expose the `IERC1155MetadataURI` interface which
475+
/// is what this initializer is meant to support.
476+
/// If the contract DOES NOT expose the `IERC1155MetadataURI` interface,
477+
/// meaning the token does not have a URI, the contract must instead use
478+
/// `initializer_no_metadata` in the constructor.
479+
/// Failure to abide by these instructions can lead to unexpected issues especially with
480+
/// UIs.
473481
fn initializer(ref self: ComponentState<TContractState>, base_uri: ByteArray) {
474482
self._set_base_uri(base_uri);
475483

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

489+
/// Initializes the contract with no metadata by registering only the IERC1155 interface.
490+
///
491+
/// WARNING: This initializer should ONLY be used during construction in the very
492+
/// specific instance when the contract does NOT expose the `IERC1155MetadataURI` interface.
493+
/// Initializing a contract with this initializer means that tokens will not
494+
/// have a URI.
495+
fn initializer_no_metadata(ref self: ComponentState<TContractState>) {
496+
let mut src5_component = get_dep_component_mut!(ref self, SRC5);
497+
src5_component.register_interface(interface::IERC1155_ID);
498+
}
499+
481500
/// Creates a `value` amount of tokens of type `token_id`, and assigns them to `to`.
482501
///
483502
/// Requirements:

packages/token/src/tests/erc1155/test_erc1155.cairo

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ fn test_initialize() {
9191
assert!(supports_isrc5);
9292
}
9393

94+
#[test]
95+
fn test_initialize_no_metadata() {
96+
let mut state = COMPONENT_STATE();
97+
let mock_state = CONTRACT_STATE();
98+
99+
state.initializer_no_metadata();
100+
101+
let empty_str = "";
102+
assert_eq!(state.ERC1155_uri.read(), empty_str);
103+
assert!(state.balance_of(OWNER(), TOKEN_ID).is_zero());
104+
105+
let supports_ierc1155 = mock_state.supports_interface(erc1155::interface::IERC1155_ID);
106+
assert!(supports_ierc1155);
107+
108+
let does_not_support_ierc1155_metadata_uri = !mock_state
109+
.supports_interface(erc1155::interface::IERC1155_METADATA_URI_ID);
110+
assert!(does_not_support_ierc1155_metadata_uri);
111+
112+
let supports_isrc5 = mock_state
113+
.supports_interface(openzeppelin_introspection::interface::ISRC5_ID);
114+
assert!(supports_isrc5);
115+
}
116+
94117
//
95118
// balance_of & balanceOf
96119
//

0 commit comments

Comments
 (0)