Skip to content

Commit

Permalink
🔒️ Rename storage to avoid clashes
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Sep 19, 2023
1 parent f734e3f commit 58904d4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 81 deletions.
12 changes: 6 additions & 6 deletions src/extensions/metadata/module.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ mod ERC3525Metadata {

#[storage]
struct Storage {
_contract_uri: felt252,
_slot_uri: LegacyMap<u256, felt252>,
_erc3525_contract_uri: felt252,
_erc3525_slot_uri: LegacyMap<u256, felt252>,
}

#[external(v0)]
impl ERC3525MetadataImpl of IERC3525Metadata<ContractState> {
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)
}
}

Expand All @@ -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);
}
}
}
6 changes: 3 additions & 3 deletions src/extensions/slotapprovable/module.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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 });
Expand All @@ -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))
}
}

Expand Down
66 changes: 33 additions & 33 deletions src/extensions/slotenumerable/module.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ mod ERC3525SlotEnumerable {

#[storage]
struct Storage {
_slot_enumerables_len: u256,
_slot_enumerables: LegacyMap<u256, u256>,
_slot_enumerables_index: LegacyMap<u256, u256>,
_slot_tokens_len: LegacyMap<u256, u256>,
_slot_tokens: LegacyMap<(u256, u256), u256>,
_slot_tokens_index: LegacyMap<(u256, u256), u256>,
_erc3525_slot_enumerables_len: u256,
_erc3525_slot_enumerables: LegacyMap<u256, u256>,
_erc3525_slot_enumerables_index: LegacyMap<u256, u256>,
_erc3525_slot_tokens_len: LegacyMap<u256, u256>,
_erc3525_slot_tokens: LegacyMap<(u256, u256), u256>,
_erc3525_slot_tokens_index: LegacyMap<(u256, u256), u256>,
}

mod Errors {
Expand All @@ -37,23 +37,23 @@ mod ERC3525SlotEnumerable {
#[external(v0)]
impl ERC3525SlotEnumerableImpl of IERC3525SlotEnumerable<ContractState> {
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))
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}

Expand Down
76 changes: 37 additions & 39 deletions src/module.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ mod ERC3525 {

#[storage]
struct Storage {
_value_decimals: u8,
_values: LegacyMap::<u256, u256>,
_approved_values: LegacyMap::<(ContractAddress, u256, ContractAddress), u256>,
_slots: LegacyMap::<u256, u256>,
_slot_uri: LegacyMap::<u256, felt252>,
_contract_uri: felt252,
_total_minted: u256,
_total_value: LegacyMap::<u256, u256>,
_erc3525_value_decimals: u8,
_erc3525_values: LegacyMap::<u256, u256>,
_erc3525_approved_values: LegacyMap::<(ContractAddress, u256, ContractAddress), u256>,
_erc3525_slots: LegacyMap::<u256, u256>,
_erc3525_total_minted: u256,
_erc3525_total_value: LegacyMap::<u256, u256>,
}

#[event]
Expand Down Expand Up @@ -91,21 +89,21 @@ mod ERC3525 {
impl ERC3525Impl of IERC3525<ContractState> {
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 {
// [Check] Token exists
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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -178,19 +176,19 @@ 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();
SRC5::InternalImpl::register_interface(ref unsafe_state, IERC3525_ID);
}

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(
Expand All @@ -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 });
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 });
Expand All @@ -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 });
Expand All @@ -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();
Expand All @@ -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 });
Expand All @@ -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 });
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 58904d4

Please sign in to comment.