From 58904d47bd06bd71301d4322185469da30fca68d Mon Sep 17 00:00:00 2001 From: bal7hazar Date: Tue, 19 Sep 2023 09:33:12 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=92=EF=B8=8F=20Rename=20storage=20to?= =?UTF-8?q?=20avoid=20clashes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/extensions/metadata/module.cairo | 12 ++-- src/extensions/slotapprovable/module.cairo | 6 +- src/extensions/slotenumerable/module.cairo | 66 +++++++++---------- src/module.cairo | 76 +++++++++++----------- 4 files changed, 79 insertions(+), 81 deletions(-) diff --git a/src/extensions/metadata/module.cairo b/src/extensions/metadata/module.cairo index ab059a1..edac838 100644 --- a/src/extensions/metadata/module.cairo +++ b/src/extensions/metadata/module.cairo @@ -11,18 +11,18 @@ mod ERC3525Metadata { #[storage] struct Storage { - _contract_uri: felt252, - _slot_uri: LegacyMap, + _erc3525_contract_uri: felt252, + _erc3525_slot_uri: LegacyMap, } #[external(v0)] impl ERC3525MetadataImpl of IERC3525Metadata { fn contract_uri(self: @ContractState) -> felt252 { - self._contract_uri.read() + self._erc3525_contract_uri.read() } fn slot_uri(self: @ContractState, slot: u256) -> felt252 { - self._slot_uri.read(slot) + self._erc3525_slot_uri.read(slot) } } @@ -36,12 +36,12 @@ mod ERC3525Metadata { fn _set_contract_uri(ref self: ContractState, uri: felt252) { // [Effect] Store uri - self._contract_uri.write(uri); + self._erc3525_contract_uri.write(uri); } fn _set_slot_uri(ref self: ContractState, slot: u256, uri: felt252) { // [Effect] Store uri - self._slot_uri.write(slot, uri); + self._erc3525_slot_uri.write(slot, uri); } } } diff --git a/src/extensions/slotapprovable/module.cairo b/src/extensions/slotapprovable/module.cairo index 3bae647..96ef729 100644 --- a/src/extensions/slotapprovable/module.cairo +++ b/src/extensions/slotapprovable/module.cairo @@ -21,7 +21,7 @@ mod ERC3525SlotApprovable { #[storage] struct Storage { - _slot_approvals: LegacyMap::<(ContractAddress, u256, ContractAddress), bool>, + _erc3525_slot_approvals: LegacyMap::<(ContractAddress, u256, ContractAddress), bool>, } #[event] @@ -67,7 +67,7 @@ mod ERC3525SlotApprovable { assert(caller != operator, Errors::SELF_APPROVAL); // [Effect] Store approval - self._slot_approvals.write((owner, slot, operator), approved); + self._erc3525_slot_approvals.write((owner, slot, operator), approved); // [Event] Emit ApprovalForSlot self.emit(ApprovalForSlot { owner, slot, operator, approved }); @@ -76,7 +76,7 @@ mod ERC3525SlotApprovable { fn is_approved_for_slot( self: @ContractState, owner: ContractAddress, slot: u256, operator: ContractAddress ) -> bool { - self._slot_approvals.read((owner, slot, operator)) + self._erc3525_slot_approvals.read((owner, slot, operator)) } } diff --git a/src/extensions/slotenumerable/module.cairo b/src/extensions/slotenumerable/module.cairo index ff46395..169f6e7 100644 --- a/src/extensions/slotenumerable/module.cairo +++ b/src/extensions/slotenumerable/module.cairo @@ -20,12 +20,12 @@ mod ERC3525SlotEnumerable { #[storage] struct Storage { - _slot_enumerables_len: u256, - _slot_enumerables: LegacyMap, - _slot_enumerables_index: LegacyMap, - _slot_tokens_len: LegacyMap, - _slot_tokens: LegacyMap<(u256, u256), u256>, - _slot_tokens_index: LegacyMap<(u256, u256), u256>, + _erc3525_slot_enumerables_len: u256, + _erc3525_slot_enumerables: LegacyMap, + _erc3525_slot_enumerables_index: LegacyMap, + _erc3525_slot_tokens_len: LegacyMap, + _erc3525_slot_tokens: LegacyMap<(u256, u256), u256>, + _erc3525_slot_tokens_index: LegacyMap<(u256, u256), u256>, } mod Errors { @@ -37,23 +37,23 @@ mod ERC3525SlotEnumerable { #[external(v0)] impl ERC3525SlotEnumerableImpl of IERC3525SlotEnumerable { fn slot_count(self: @ContractState) -> u256 { - self._slot_enumerables_len.read() + self._erc3525_slot_enumerables_len.read() } fn slot_by_index(self: @ContractState, index: u256) -> u256 { // [Check] Index is in range - let count = self._slot_enumerables_len.read(); + let count = self._erc3525_slot_enumerables_len.read(); assert(index < count, Errors::INDEX_OUT_OF_BOUNDS); - self._slot_enumerables.read(index) + self._erc3525_slot_enumerables.read(index) } fn token_supply_in_slot(self: @ContractState, slot: u256) -> u256 { - self._slot_tokens_len.read(slot) + self._erc3525_slot_tokens_len.read(slot) } fn token_in_slot_by_index(self: @ContractState, slot: u256, index: u256) -> u256 { // [Check] Index is in range - let supply = self._slot_tokens_len.read(slot); + let supply = self._erc3525_slot_tokens_len.read(slot); assert(index < supply, Errors::INDEX_OUT_OF_BOUNDS); - self._slot_tokens.read((slot, index)) + self._erc3525_slot_tokens.read((slot, index)) } } @@ -66,13 +66,13 @@ mod ERC3525SlotEnumerable { } fn _slot_exists(self: @ContractState, slot: u256) -> bool { - let index = self._slot_enumerables_index.read(slot); - self._slot_enumerables.read(index) == slot && slot != 0 + let index = self._erc3525_slot_enumerables_index.read(slot); + self._erc3525_slot_enumerables.read(index) == slot && slot != 0 } fn _token_exists(self: @ContractState, slot: u256, token_id: u256) -> bool { - let index = self._slot_tokens_index.read((slot, token_id)); - self._slot_tokens.read((slot, index)) == token_id && token_id != 0 + let index = self._erc3525_slot_tokens_index.read((slot, token_id)); + self._erc3525_slot_tokens.read((slot, index)) == token_id && token_id != 0 } fn _after_transfer_value_from(ref self: ContractState, token_id: u256) { @@ -134,39 +134,39 @@ mod ERC3525SlotEnumerable { fn _add_slot_to_slots_enumeration(ref self: ContractState, slot: u256) { // [Effect] Store new slot - let index = self._slot_enumerables_len.read(); - self._slot_enumerables_len.write(index + 1); - self._slot_enumerables.write(index, slot); - self._slot_enumerables_index.write(slot, index); + let index = self._erc3525_slot_enumerables_len.read(); + self._erc3525_slot_enumerables_len.write(index + 1); + self._erc3525_slot_enumerables.write(index, slot); + self._erc3525_slot_enumerables_index.write(slot, index); } fn _add_token_to_slot_enumeration(ref self: ContractState, slot: u256, token_id: u256) { // [Effect] Store new token - let index = self._slot_tokens_len.read(slot); - self._slot_tokens_len.write(slot, index + 1); - self._slot_tokens.write((slot, index), token_id); - self._slot_tokens_index.write((slot, token_id), index); + let index = self._erc3525_slot_tokens_len.read(slot); + self._erc3525_slot_tokens_len.write(slot, index + 1); + self._erc3525_slot_tokens.write((slot, index), token_id); + self._erc3525_slot_tokens_index.write((slot, token_id), index); } fn _remove_token_from_slot_enumeration( ref self: ContractState, slot: u256, token_id: u256 ) { // [Compute] Read last token - let supply = self._slot_tokens_len.read(slot); - let last_token = self._slot_tokens.read((slot, supply - 1)); - let last_index = self._slot_tokens_index.read((slot, last_token)); + let supply = self._erc3525_slot_tokens_len.read(slot); + let last_token = self._erc3525_slot_tokens.read((slot, supply - 1)); + let last_index = self._erc3525_slot_tokens_index.read((slot, last_token)); // [Compute] Token index to remove - let token_index = self._slot_tokens_index.read((slot, token_id)); + let token_index = self._erc3525_slot_tokens_index.read((slot, token_id)); // [Effect] Replace token_id byt last token - self._slot_tokens.write((slot, token_index), last_token); - self._slot_tokens_index.write((slot, last_token), token_index); + self._erc3525_slot_tokens.write((slot, token_index), last_token); + self._erc3525_slot_tokens_index.write((slot, last_token), token_index); // [Effect] Remove last token and its index - self._slot_tokens_len.write(slot, supply - 1); - self._slot_tokens.write((slot, last_index), 0); - self._slot_tokens_index.write((slot, token_id), 0); + self._erc3525_slot_tokens_len.write(slot, supply - 1); + self._erc3525_slot_tokens.write((slot, last_index), 0); + self._erc3525_slot_tokens_index.write((slot, token_id), 0); } } diff --git a/src/module.cairo b/src/module.cairo index 4934bac..f2754b5 100644 --- a/src/module.cairo +++ b/src/module.cairo @@ -24,14 +24,12 @@ mod ERC3525 { #[storage] struct Storage { - _value_decimals: u8, - _values: LegacyMap::, - _approved_values: LegacyMap::<(ContractAddress, u256, ContractAddress), u256>, - _slots: LegacyMap::, - _slot_uri: LegacyMap::, - _contract_uri: felt252, - _total_minted: u256, - _total_value: LegacyMap::, + _erc3525_value_decimals: u8, + _erc3525_values: LegacyMap::, + _erc3525_approved_values: LegacyMap::<(ContractAddress, u256, ContractAddress), u256>, + _erc3525_slots: LegacyMap::, + _erc3525_total_minted: u256, + _erc3525_total_value: LegacyMap::, } #[event] @@ -91,7 +89,7 @@ mod ERC3525 { impl ERC3525Impl of IERC3525 { fn value_decimals(self: @ContractState) -> u8 { // [Compute] Value decimals - self._value_decimals.read() + self._erc3525_value_decimals.read() } fn value_of(self: @ContractState, token_id: u256) -> u256 { @@ -99,13 +97,13 @@ mod ERC3525 { self._assert_minted(token_id); // [Compute] Token value - self._values.read(token_id) + self._erc3525_values.read(token_id) } fn slot_of(self: @ContractState, token_id: u256) -> u256 { // [Check] Token exists self._assert_minted(token_id); - self._slots.read(token_id) + self._erc3525_slots.read(token_id) } fn approve_value( @@ -133,7 +131,7 @@ mod ERC3525 { // [Check] let unsafe_state = ERC721::unsafe_new_contract_state(); let owner = ERC721::ERC721Impl::owner_of(@unsafe_state, token_id); - self._approved_values.read((owner, token_id, operator)) + self._erc3525_approved_values.read((owner, token_id, operator)) } fn transfer_value_from( @@ -178,7 +176,7 @@ mod ERC3525 { impl InternalImpl of InternalTrait { fn initializer(ref self: ContractState, value_decimals: u8) { // [Effect] Store value decimals - self._value_decimals.write(value_decimals); + self._erc3525_value_decimals.write(value_decimals); // [Effect] Register interfaces let mut unsafe_state = SRC5::unsafe_new_contract_state(); @@ -186,11 +184,11 @@ mod ERC3525 { } fn _get_new_token_id(self: @ContractState) -> u256 { - self._total_minted.read() + 1 + self._erc3525_total_minted.read() + 1 } fn _total_value(self: @ContractState, slot: u256) -> u256 { - self._total_value.read(slot) + self._erc3525_total_value.read(slot) } fn _approve_value( @@ -199,7 +197,7 @@ mod ERC3525 { // [Effect] Store approved value let unsafe_state = ERC721::unsafe_new_contract_state(); let owner = ERC721::ERC721Impl::owner_of(@unsafe_state, token_id); - self._approved_values.write((owner, token_id, operator), value); + self._erc3525_approved_values.write((owner, token_id, operator), value); // [Event] Emit ApprovalValue self.emit(ApprovalValue { token_id, operator, value }); @@ -230,7 +228,7 @@ mod ERC3525 { // [Compute] Spender allowance let unsafe_state = ERC721::unsafe_new_contract_state(); let owner = ERC721::ERC721Impl::owner_of(@unsafe_state, token_id); - let current_allowance = self._approved_values.read((owner, token_id, spender)); + let current_allowance = self._erc3525_approved_values.read((owner, token_id, spender)); let infinity: u256 = BoundedInt::max(); let is_approved = ERC721::InternalImpl::_is_approved_or_owner( @unsafe_state, spender, token_id @@ -276,10 +274,10 @@ mod ERC3525 { ERC721::InternalImpl::_mint(ref unsafe_state, to, token_id); // [Effect] Store slot - self._slots.write(token_id, slot); + self._erc3525_slots.write(token_id, slot); // [Effect] Update new total minted - self._total_minted.write(self._total_minted.read() + 1); + self._erc3525_total_minted.write(self._erc3525_total_minted.read() + 1); // [Event] Emit SlotChanged self.emit(SlotChanged { token_id, old_slot: 0.into(), new_slot: slot }); @@ -290,12 +288,12 @@ mod ERC3525 { self._assert_minted(token_id); // [Effect] Update token value - self._values.write(token_id, self._values.read(token_id) + value); + self._erc3525_values.write(token_id, self._erc3525_values.read(token_id) + value); // [Effect] Update total value let slot = self.slot_of(token_id); - let total = self._total_value.read(slot); - self._total_value.write(slot, self._total_value.read(slot) + value); + let total = self._erc3525_total_value.read(slot); + self._erc3525_total_value.write(slot, self._erc3525_total_value.read(slot) + value); // [Event] Emit TransferValue self.emit(TransferValue { from_token_id: 0.into(), to_token_id: token_id, value }); @@ -316,13 +314,13 @@ mod ERC3525 { ); // [Check] Transfer amount does not exceed balance - let from_balance = self._values.read(from_token_id); + let from_balance = self._erc3525_values.read(from_token_id); assert(from_balance >= value, Errors::VALUE_EXCEEDS_BALANCE); // [Effect] Update tokens balance - self._values.write(from_token_id, from_balance - value); - let to_balance = self._values.read(to_token_id); - self._values.write(to_token_id, to_balance + value); + self._erc3525_values.write(from_token_id, from_balance - value); + let to_balance = self._erc3525_values.read(to_token_id); + self._erc3525_values.write(to_token_id, to_balance + value); // [Interaction] Receiver let unsafe_state = ERC721::unsafe_new_contract_state(); @@ -347,13 +345,13 @@ mod ERC3525 { ERC721::InternalImpl::_burn(ref unsafe_state, token_id); // [Effect] Update token and total value - let value = self._values.read(token_id); - let slot = self._slots.read(token_id); - self._values.write(token_id, 0.into()); - self._total_value.write(slot, self._total_value.read(slot) - value); + let value = self._erc3525_values.read(token_id); + let slot = self._erc3525_slots.read(token_id); + self._erc3525_values.write(token_id, 0.into()); + self._erc3525_total_value.write(slot, self._erc3525_total_value.read(slot) - value); // [Effect] Update slot - self._slots.write(token_id, 0.into()); + self._erc3525_slots.write(token_id, 0.into()); // [Event] Emit TransferValue and SlotChanged self.emit(TransferValue { from_token_id: token_id, to_token_id: 0.into(), value }); @@ -365,13 +363,13 @@ mod ERC3525 { self._assert_minted(token_id); // [Check] Burn value does not exceed balance - let balance = self._values.read(token_id); + let balance = self._erc3525_values.read(token_id); assert(balance >= value, Errors::VALUE_EXCEEDS_BALANCE); // [Effect] Update token and total value - let slot = self._slots.read(token_id); - self._values.write(token_id, balance - value); - self._total_value.write(slot, self._total_value.read(slot) - value); + let slot = self._erc3525_slots.read(token_id); + self._erc3525_values.write(token_id, balance - value); + self._erc3525_total_value.write(slot, self._erc3525_total_value.read(slot) - value); // [Event] Emit TransferValue self.emit(TransferValue { from_token_id: token_id, to_token_id: 0.into(), value }); @@ -421,14 +419,14 @@ mod ERC3525 { assert(total_amount != 0.into(), Errors::INVALID_AMOUNTS); // [Check] Amounts sum does not exceed balance - let balance = self._values.read(token_id); + let balance = self._erc3525_values.read(token_id); assert(balance >= total_amount, Errors::VALUE_EXCEEDS_BALANCE); // [Effect] Update token and total value - let slot = self._slots.read(token_id); + let slot = self._erc3525_slots.read(token_id); let unsafe_state = ERC721::unsafe_new_contract_state(); let owner = ERC721::ERC721Impl::owner_of(@unsafe_state, token_id); - self._values.write(token_id, balance - total_amount); + self._erc3525_values.write(token_id, balance - total_amount); // [Effect] Mint new tokens let mut new_token_ids = ArrayTrait::new(); @@ -473,7 +471,7 @@ mod ERC3525 { let to_owner = ERC721::ERC721Impl::owner_of(@unsafe_state, to_token_id); assert(from_owner == to_owner, Errors::OWNER_MISTMATCH); // [Effect] Merge tokens - let value = self._values.read(from_token_id); + let value = self._erc3525_values.read(from_token_id); self._transfer_value(from_token_id, to_token_id, value); // [Effect] Burn from token self._burn(from_token_id);