Skip to content

Commit

Permalink
🐛 Fix storage clash
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Aug 29, 2023
1 parent f225f8e commit 6d2be2c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
24 changes: 12 additions & 12 deletions src/extensions/slotenumerable/module.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ mod ERC3525SlotEnumerable {

#[storage]
struct Storage {
_slots_len: u256,
_slots: LegacyMap<u256, u256>,
_slots_index: LegacyMap<u256, u256>,
enumerable_slots_len: u256,
enumerable_slots: LegacyMap<u256, u256>,
enumerable_slots_index: LegacyMap<u256, u256>,
_slot_tokens_len: LegacyMap<u256, u256>,
_slot_tokens: LegacyMap<(u256, u256), u256>,
_slot_tokens_index: LegacyMap<(u256, u256), u256>,
Expand All @@ -25,14 +25,14 @@ mod ERC3525SlotEnumerable {
#[external(v0)]
impl ERC3525SlotEnumerableImpl of IERC3525SlotEnumerable<ContractState> {
fn slot_count(self: @ContractState) -> u256 {
self._slots_len.read()
self.enumerable_slots_len.read()
}

fn slot_by_index(self: @ContractState, index: u256) -> u256 {
// [Check] Index is in range
let count = self._slots_len.read();
let count = self.enumerable_slots_len.read();
assert(index < count, 'ERC3525: index out of bounds');
self._slots.read(index)
self.enumerable_slots.read(index)
}
fn token_supply_in_slot(self: @ContractState, slot: u256) -> u256 {
self._slot_tokens_len.read(slot)
Expand All @@ -54,8 +54,8 @@ mod ERC3525SlotEnumerable {
}

fn _slot_exists(self: @ContractState, slot: u256) -> bool {
let index = self._slots_index.read(slot);
self._slots.read(index) == slot && slot != 0
let index = self.enumerable_slots_index.read(slot);
self.enumerable_slots.read(index) == slot && slot != 0
}

fn _token_exists(self: @ContractState, slot: u256, token_id: u256) -> bool {
Expand Down Expand Up @@ -122,10 +122,10 @@ mod ERC3525SlotEnumerable {

fn _add_slot_to_slots_enumeration(ref self: ContractState, slot: u256) {
// [Effect] Store new slot
let index = self._slots_len.read();
self._slots_len.write(index + 1);
self._slots.write(index, slot);
self._slots_index.write(slot, index);
let index = self.enumerable_slots_len.read();
self.enumerable_slots_len.write(index + 1);
self.enumerable_slots.write(index, slot);
self.enumerable_slots_index.write(slot, index);
}

fn _add_token_to_slot_enumeration(ref self: ContractState, slot: u256, token_id: u256) {
Expand Down
27 changes: 25 additions & 2 deletions src/tests/integration/test_slot_enumerable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use result::ResultTrait;
use option::OptionTrait;
use array::ArrayTrait;
use traits::{Into, TryInto};

use starknet::ContractAddress;
use starknet::testing;

Expand All @@ -19,6 +20,8 @@ use cairo_erc_3525::extensions::slotenumerable::interface::{
use cairo_erc_3525::tests::integration::constants;
use cairo_erc_3525::tests::mocks::account::Account;

use debug::PrintTrait;

#[derive(Drop)]
struct Signers {
owner: ContractAddress,
Expand Down Expand Up @@ -87,7 +90,7 @@ fn test_integration_slot_enumerable_scenario() {
let four = external.mint(signers.anyone, constants::SLOT_1, constants::VALUE);
let five = external.mint(signers.operator, constants::SLOT_1, constants::VALUE);

// Assert enuerable
// Assert enumerable
assert(erc3525_se.slot_count() == 1, 'Wrong slot count');
assert(erc3525_se.slot_by_index(0) == constants::SLOT_1, 'Wrong slot at index');
assert(erc3525_se.token_supply_in_slot(constants::SLOT_1) == 5, 'Wrong toke supply in slot');
Expand Down Expand Up @@ -118,7 +121,7 @@ fn test_integration_slot_enumerable_scenario() {
let height = external.mint(signers.someone, constants::SLOT_2, constants::VALUE);
let nine = external.mint(signers.anyone, constants::SLOT_2, constants::VALUE);

// Assert enuerable
// Assert enumerable
assert(erc3525_se.slot_count() == 2, 'Wrong slot count');
assert(erc3525_se.slot_by_index(1) == constants::SLOT_2, 'Wrong slot at index');
assert(erc3525_se.token_supply_in_slot(constants::SLOT_2) == 4, 'Wrong toke supply in slot');
Expand All @@ -138,4 +141,24 @@ fn test_integration_slot_enumerable_scenario() {
erc3525_se.token_in_slot_by_index(constants::SLOT_2, 3) == nine,
'Wrong token in slot at index'
);

// Approve
testing::set_contract_address(signers.owner);
erc3525.approve_value(one, signers.anyone, constants::VALUE / 2);
erc3525.approve_value(two, signers.anyone, constants::VALUE / 2);
erc3525.approve_value(six, signers.anyone, constants::VALUE / 2);

// Transfers to token id
testing::set_contract_address(signers.anyone);
erc3525.transfer_value_from(one, three, constants::ZERO(), 1);
erc3525.transfer_value_from(two, three, constants::ZERO(), 1);
erc3525.transfer_value_from(six, seven, constants::ZERO(), 1);

// Transfer to
let ten = erc3525.transfer_value_from(one, 0, signers.operator, 1);
assert(ten == 10, 'Wrong id');
assert(erc3525.allowance(one, signers.anyone) == constants::VALUE / 2 - 2, 'Wrong allowance');
assert(erc3525.value_of(one) == constants::VALUE - 2, 'Wrong value');
assert(external.total_value(constants::SLOT_1) == 5 * constants::VALUE, 'Wrong total value');
assert(external.total_value(constants::SLOT_2) == 4 * constants::VALUE, 'Wrong total value');
}

0 comments on commit 6d2be2c

Please sign in to comment.