diff --git a/src/utils/constants.cairo b/src/utils/constants.cairo index cac46a8..bad2e0f 100644 --- a/src/utils/constants.cairo +++ b/src/utils/constants.cairo @@ -4,6 +4,10 @@ pub fn OWNER() -> ContractAddress { contract_address_const::<'OWNER'>() } +pub fn NEW_OWNER() -> ContractAddress { + contract_address_const::<'NEW_OWNER'>() +} + pub fn MINTER() -> ContractAddress { contract_address_const::<'MINTER'>() } diff --git a/tests/lib.cairo b/tests/lib.cairo index f8d9182..860a8ff 100644 --- a/tests/lib.cairo +++ b/tests/lib.cairo @@ -1,6 +1,10 @@ mod utils; use starknet::class_hash::class_hash_const; use tokenized_bond::{TokenizedBond, ITokenizedBondDispatcher, ITokenizedBondDispatcherTrait}; +use openzeppelin_access::ownable::OwnableComponent; +use openzeppelin_access::ownable::interface::{ + IOwnableTwoStepDispatcher, IOwnableTwoStepDispatcherTrait, +}; use openzeppelin_upgrades::upgradeable::UpgradeableComponent; use openzeppelin_security::pausable::PausableComponent; use openzeppelin_token::erc1155::ERC1155Component; @@ -8,7 +12,7 @@ use openzeppelin_token::erc1155::interface::{IERC1155Dispatcher, IERC1155Dispatc use tokenized_bond::utils::constants::{ OWNER, MINTER, ZERO_ADDRESS, INTEREST_RATE, INTEREST_RATE_ZERO, MINT_AMOUNT, TOKEN_NAME, TOKEN_ID, TIME_IN_THE_FUTURE, CUSTODIAL_FALSE, NOT_MINTER, NEW_MINTER, AMOUNT_TRANSFERRED, - TRANSFER_AMOUNT, + TRANSFER_AMOUNT, NEW_OWNER, }; use snforge_std::{ EventSpyAssertionsTrait, spy_events, start_cheat_caller_address, stop_cheat_caller_address, @@ -956,3 +960,36 @@ fn test_upgrade_not_owner() { let (tokenized_bond, _minter) = setup_contract_with_minter(); tokenized_bond.upgrade(class_hash_const::<'UPGRADE'>()); } + +#[test] +fn test_tokenized_bond_transfer_ownership() { + let mut spy = spy_events(); + let (tokenized_bond, _minter) = setup_contract_with_minter(); + let ownable = IOwnableTwoStepDispatcher { contract_address: tokenized_bond.contract_address }; + + assert(ownable.owner() == OWNER(), 'Initial wrong owner'); + + start_cheat_caller_address(tokenized_bond.contract_address, OWNER()); + ownable.transfer_ownership(NEW_OWNER()); + start_cheat_block_timestamp_global(get_block_timestamp() + 99999); + + start_cheat_caller_address(tokenized_bond.contract_address, NEW_OWNER()); + ownable.accept_ownership(); + + assert(ownable.owner() == NEW_OWNER(), 'transfer owner failed'); + + let expected_event = OwnableComponent::Event::OwnershipTransferred( + OwnableComponent::OwnershipTransferred { previous_owner: OWNER(), new_owner: NEW_OWNER() }, + ); + spy.assert_emitted(@array![(ownable.contract_address, expected_event)]); +} + +#[test] +#[should_panic(expected: 'Caller is not the owner')] +fn test_tokenized_bond_transfer_ownership_not_owner() { + let (tokenized_bond, _minter) = setup_contract_with_minter(); + let ownable = IOwnableTwoStepDispatcher { contract_address: tokenized_bond.contract_address }; + + ownable.transfer_ownership(NEW_OWNER()); + assert(ownable.owner() == NEW_OWNER().into(), 'transfer owner failed'); +}