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

changes storage #32

Merged
merged 3 commits into from
Feb 6, 2025
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
29 changes: 22 additions & 7 deletions src/tokenized_bond.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod TokenizedBond {
use openzeppelin_token::erc1155::ERC1155Component;
use openzeppelin_upgrades::UpgradeableComponent;
use starknet::{ClassHash, ContractAddress, get_block_timestamp, get_caller_address};
use starknet::storage::{StoragePointerWriteAccess, StoragePathEntry, Map, Vec, MutableVecTrait};
use starknet::storage::{StoragePointerWriteAccess, StoragePathEntry, Map};

component!(path: ERC1155Component, storage: erc1155, event: ERC1155Event);
component!(path: SRC5Component, storage: src5, event: SRC5Event);
Expand Down Expand Up @@ -45,7 +45,8 @@ pub mod TokenizedBond {
upgradeable: UpgradeableComponent::Storage,
minters: Map<ContractAddress, u8>,
tokens: Map<u256, Token>,
minter_tokens: Map<ContractAddress, Vec<u256>>,
minter_tokens: Map<ContractAddress, Map<u256, u256>>,
minter_tokens_len: Map<ContractAddress, u256>,
minter_is_operator: Map<u256, bool>,
}

Expand Down Expand Up @@ -156,6 +157,7 @@ pub mod TokenizedBond {
pub const INSUFFICIENT_BALANCE: felt252 = 'Insufficient balance';
pub const MINTER_IS_TOKEN_OPERATOR: felt252 = 'Minter is already operator';
pub const MINTER_NOT_TOKEN_OPERATOR: felt252 = 'Minter is not operator';
pub const MINTER_IS_ACTIVE: felt252 = 'Minter is active';
}

#[constructor]
Expand Down Expand Up @@ -277,6 +279,8 @@ pub mod TokenizedBond {

fn remove_minter(ref self: ContractState, minter: ContractAddress) {
self.ownable.assert_only_owner();
assert(self.minters.entry(minter).read() == 1, Errors::MINTER_DOES_NOT_EXIST);
assert(self.minter_tokens_len.entry(minter).read() == 0, Errors::MINTER_IS_ACTIVE);
self.minters.entry(minter).write(0);
self.emit(MinterRemoved { minter });
}
Expand All @@ -287,13 +291,13 @@ pub mod TokenizedBond {
self.ownable.assert_only_owner();
assert(self.minters.entry(old_minter).read() == 1, Errors::OLD_MINTER_DOES_NOT_EXIST);
assert(self.minters.entry(new_minter).read() == 0, Errors::NEW_MINTER_ALREADY_EXISTS);
let number_of_tokens_to_replace = self.minter_tokens.entry(old_minter).len();
let number_of_tokens_to_replace = self.minter_tokens_len.entry(old_minter).read();

// replace old minter with new minter in all minted tokens
for element in 0..number_of_tokens_to_replace {
let token_id = self.minter_tokens.entry(old_minter).at(element).read();
let token_id = self.minter_tokens.entry(old_minter).entry(element).read();

self.minter_tokens.entry(old_minter).at(element).write(0);
self.minter_tokens.entry(old_minter).entry(element).write(0);
let old_minter_balance = self.erc1155.balance_of(old_minter, token_id);

// replace old minter with new minter in all minted tokens
Expand All @@ -305,13 +309,15 @@ pub mod TokenizedBond {
self.erc1155.burn(old_minter, token_id, old_minter_balance);

//add new minter with respective tokens
self.minter_tokens.entry(new_minter).append().write(token_id);
self.add_token_to_minters_tokens(new_minter, token_id);

let mut token = self.tokens.entry(token_id).read();
token.minter = new_minter;
self.tokens.entry(token_id).write(token);

self.emit(MinterReplaced { token_id, old_minter, new_minter });
};
self.minter_tokens_len.entry(old_minter).write(0);
self.minters.entry(old_minter).write(0);
self.minters.entry(new_minter).write(1);
self.emit(MinterRemoved { minter: old_minter });
Expand Down Expand Up @@ -375,7 +381,8 @@ pub mod TokenizedBond {
name,
},
);
self.minter_tokens.entry(minter).append().write(token_id);
self.add_token_to_minters_tokens(minter, token_id);

self.erc1155.mint_with_acceptance_check(minter, token_id, amount, array![].span());
}

Expand Down Expand Up @@ -507,5 +514,13 @@ pub mod TokenizedBond {
}
return false;
}

fn add_token_to_minters_tokens(
ref self: ContractState, minter: ContractAddress, token_id: u256,
) {
let index = self.minter_tokens_len.entry(minter).read();
self.minter_tokens.entry(minter).entry(index).write(token_id);
self.minter_tokens_len.entry(minter).write(index + 1);
}
}
}
20 changes: 20 additions & 0 deletions tests/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,26 @@ fn test_remove_minter_not_owner() {
tokenized_bond.remove_minter(MINTER());
}

#[test]
#[should_panic(expected: 'Minter does not exist')]
fn test_remove_minter_does_not_exist() {
let mut tokenized_bond = ITokenizedBondDispatcher { contract_address: setup() };

start_cheat_caller_address(tokenized_bond.contract_address, OWNER());

tokenized_bond.remove_minter(MINTER());
}

#[test]
#[should_panic(expected: 'Minter is active')]
fn test_remove_minter_with_active_tokens() {
let (tokenized_bond, minter) = setup_contract_with_minter();

start_cheat_caller_address(tokenized_bond.contract_address, OWNER());

tokenized_bond.remove_minter(minter);
}

#[test]
fn test_mint_success() {
let mut tokenized_bond = ITokenizedBondDispatcher { contract_address: setup() };
Expand Down
Loading