Skip to content

Commit

Permalink
chore: modify emitted event for upgradeable comp.
Browse files Browse the repository at this point in the history
  • Loading branch information
Darlington02 committed Aug 18, 2024
1 parent 59e2ae6 commit 20a4173
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
16 changes: 12 additions & 4 deletions src/components/upgradeable/upgradeable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ pub mod UpgradeableComponent {
// *************************************************************************
// IMPORTS
// *************************************************************************
use starknet::{ClassHash, SyscallResultTrait, get_caller_address};
use starknet::{
ClassHash, SyscallResultTrait, get_caller_address, get_contract_address, ContractAddress
};
use core::num::traits::zero::Zero;
use token_bound_accounts::components::account::account::AccountComponent;
use token_bound_accounts::components::account::account::AccountComponent::InternalImpl;
Expand All @@ -23,13 +25,14 @@ pub mod UpgradeableComponent {
#[event]
#[derive(Drop, starknet::Event)]
pub enum Event {
Upgraded: Upgraded
TBAUpgraded: TBAUpgraded
}

/// @notice Emitted when the contract is upgraded.
/// @param class_hash implementation hash to be upgraded to
#[derive(Drop, starknet::Event)]
pub struct Upgraded {
pub struct TBAUpgraded {
pub account_address: ContractAddress,
pub class_hash: ClassHash
}

Expand Down Expand Up @@ -68,7 +71,12 @@ pub mod UpgradeableComponent {

// upgrade account
starknet::syscalls::replace_class_syscall(new_class_hash).unwrap_syscall();
self.emit(Upgraded { class_hash: new_class_hash });
self
.emit(
TBAUpgraded {
account_address: get_contract_address(), class_hash: new_class_hash
}
);
}
}
}
37 changes: 34 additions & 3 deletions tests/test_upgradeable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use token_bound_accounts::interfaces::IUpgradeable::{
IUpgradeableDispatcher, IUpgradeableDispatcherTrait
};
use token_bound_accounts::components::presets::account_preset::AccountPreset;
use token_bound_accounts::components::account::account::AccountComponent;
use token_bound_accounts::components::upgradeable::upgradeable::UpgradeableComponent;

use token_bound_accounts::test_helper::{
erc721_helper::{IERC721Dispatcher, IERC721DispatcherTrait, ERC721},
Expand Down Expand Up @@ -66,7 +66,6 @@ fn __setup__() -> (ContractAddress, ContractAddress) {
#[test]
fn test_upgrade() {
let (contract_address, erc721_contract_address) = __setup__();

let new_class_hash = declare("UpgradedAccount").unwrap().class_hash;

// get token owner
Expand All @@ -89,11 +88,43 @@ fn test_upgrade() {
#[should_panic(expected: ('Account: unauthorized',))]
fn test_upgrade_with_unauthorized() {
let (contract_address, _) = __setup__();

let new_class_hash = declare("UpgradedAccount").unwrap().class_hash;

// call upgrade function with an unauthorized address
start_cheat_caller_address(contract_address, ACCOUNT2.try_into().unwrap());
let safe_upgrade_dispatcher = IUpgradeableDispatcher { contract_address };
safe_upgrade_dispatcher.upgrade(new_class_hash);
}

#[test]
fn test_upgrade_emits_event() {
let (contract_address, erc721_contract_address) = __setup__();
let new_class_hash = declare("UpgradedAccount").unwrap().class_hash;

// get token owner
let token_dispatcher = IERC721Dispatcher { contract_address: erc721_contract_address };
let token_owner = token_dispatcher.ownerOf(1.try_into().unwrap());

// spy on emitted events
let mut spy = spy_events();

// call the upgrade function
let dispatcher = IUpgradeableDispatcher { contract_address };
start_cheat_caller_address(contract_address, token_owner);
dispatcher.upgrade(new_class_hash);

// check events are emitted
spy
.assert_emitted(
@array![
(
contract_address,
UpgradeableComponent::Event::TBAUpgraded(
UpgradeableComponent::TBAUpgraded {
account_address: contract_address, class_hash: new_class_hash
}
)
)
]
);
}

0 comments on commit 20a4173

Please sign in to comment.