Skip to content

Commit ff33b65

Browse files
committed
Merge remote-tracking branch 'origin/main' into itay/add_deploy_mock_erc20_to_utils
2 parents bd70b81 + 475ee83 commit ff33b65

File tree

6 files changed

+276
-3
lines changed

6 files changed

+276
-3
lines changed

packages/testing/src/constants.cairo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub const APP_ROLE_ADMIN: ContractAddress = 'APP_ROLE_ADMIN'.try_into().unwrap()
88
pub const APP_GOVERNOR: ContractAddress = 'APP_GOVERNOR'.try_into().unwrap();
99
pub const OPERATOR: ContractAddress = 'OPERATOR'.try_into().unwrap();
1010
pub const TOKEN_ADMIN: ContractAddress = 'TOKEN_ADMIN'.try_into().unwrap();
11+
pub const UPGRADE_AGENT: ContractAddress = 'UPGRADE_AGENT'.try_into().unwrap();
1112
pub const UPGRADE_GOVERNOR: ContractAddress = 'UPGRADE_GOVERNOR'.try_into().unwrap();
1213
pub const SECURITY_AGENT: ContractAddress = 'SECURITY_AGENT'.try_into().unwrap();
1314
pub const DUMMY_ADDRESS: ContractAddress = 'DUMMY_ADDRESS'.try_into().unwrap();

packages/utils/src/components/roles/errors.cairo

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ pub enum AccessErrors {
1212
ONLY_OPERATOR,
1313
ONLY_TOKEN_ADMIN,
1414
ONLY_UPGRADE_GOVERNOR,
15+
ONLY_UPGRADER,
1516
ONLY_SECURITY_ADMIN,
1617
ONLY_SECURITY_AGENT,
18+
ONLY_SECURITY_GOVERNOR,
1719
ONLY_MINTER,
1820
ONLY_SELF_CAN_RENOUNCE,
1921
GOV_ADMIN_CANNOT_RENOUNCE,
@@ -33,8 +35,10 @@ impl DescribableError of Describable<AccessErrors> {
3335
AccessErrors::ONLY_OPERATOR => "ONLY_OPERATOR",
3436
AccessErrors::ONLY_TOKEN_ADMIN => "ONLY_TOKEN_ADMIN",
3537
AccessErrors::ONLY_UPGRADE_GOVERNOR => "ONLY_UPGRADE_GOVERNOR",
38+
AccessErrors::ONLY_UPGRADER => "ONLY_UPGRADER",
3639
AccessErrors::ONLY_SECURITY_ADMIN => "ONLY_SECURITY_ADMIN",
3740
AccessErrors::ONLY_SECURITY_AGENT => "ONLY_SECURITY_AGENT",
41+
AccessErrors::ONLY_SECURITY_GOVERNOR => "ONLY_SECURITY_GOVERNOR",
3842
AccessErrors::ONLY_MINTER => "MINTER_ONLY",
3943
AccessErrors::ONLY_SELF_CAN_RENOUNCE => "ONLY_SELF_CAN_RENOUNCE",
4044
AccessErrors::GOV_ADMIN_CANNOT_RENOUNCE => "GOV_ADMIN_CANNOT_SELF_REMOVE",

packages/utils/src/components/roles/event_test_utils.cairo

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,50 @@ pub(crate) fn assert_upgrade_governor_added_event(
197197
}
198198
}
199199

200+
pub(crate) fn assert_upgrade_agent_added_event(
201+
spied_event: @(ContractAddress, Event),
202+
added_account: ContractAddress,
203+
added_by: ContractAddress,
204+
) {
205+
let expected_event = @MockContract::Event::RolesEvent(
206+
RolesEvent::UpgradeAgentAdded(
207+
RolesInterface::UpgradeAgentAdded { added_account, added_by },
208+
),
209+
);
210+
let (expected_emitted_by, raw_event) = spied_event;
211+
let wrapped_spied_event = Events { events: array![(*expected_emitted_by, raw_event.clone())] };
212+
let emitted = is_emitted(self: @wrapped_spied_event, :expected_emitted_by, :expected_event);
213+
if !emitted {
214+
let details = format!(
215+
"UpgradeAgentAdded{{added_account: {:?}, added_by: {:?}}}", added_account, added_by,
216+
);
217+
panic_with_event_details(:expected_emitted_by, :details);
218+
}
219+
}
220+
221+
pub(crate) fn assert_upgrade_agent_removed_event(
222+
spied_event: @(ContractAddress, Event),
223+
removed_account: ContractAddress,
224+
removed_by: ContractAddress,
225+
) {
226+
let expected_event = @MockContract::Event::RolesEvent(
227+
RolesEvent::UpgradeAgentRemoved(
228+
RolesInterface::UpgradeAgentRemoved { removed_account, removed_by },
229+
),
230+
);
231+
let (expected_emitted_by, raw_event) = spied_event;
232+
let wrapped_spied_event = Events { events: array![(*expected_emitted_by, raw_event.clone())] };
233+
let emitted = is_emitted(self: @wrapped_spied_event, :expected_emitted_by, :expected_event);
234+
if !emitted {
235+
let details = format!(
236+
"UpgradeAgentRemoved{{removed_account: {:?}, removed_by: {:?}}}",
237+
removed_account,
238+
removed_by,
239+
);
240+
panic_with_event_details(:expected_emitted_by, :details);
241+
}
242+
}
243+
200244
pub(crate) fn assert_upgrade_governor_removed_event(
201245
spied_event: @(ContractAddress, Event),
202246
removed_account: ContractAddress,

packages/utils/src/components/roles/interface.cairo

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ pub type RoleId = felt252;
55
// ----------------------------------------
66
// GOVERNANCE_ADMIN | GOVERNANCE_ADMIN
77
// UPGRADE_GOVERNOR | GOVERNANCE_ADMIN
8+
// UPGRADE_AGENT | APP_ROLE_ADMIN
89
// APP_ROLE_ADMIN | GOVERNANCE_ADMIN
910
// APP_GOVERNOR | APP_ROLE_ADMIN
1011
// OPERATOR | APP_ROLE_ADMIN
1112
// TOKEN_ADMIN | APP_ROLE_ADMIN
1213
// SECURITY_ADMIN | SECURITY_ADMIN
1314
// SECURITY_AGENT | SECURITY_ADMIN.
15+
// SECURITY_GOVERNOR | SECURITY_ADMIN.
1416

1517
// int.from_bytes(Web3.keccak(text="ROLE_APP_GOVERNOR"), "big") & MASK_250 .
1618
pub const APP_GOVERNOR: RoleId = 0xd2ead78c620e94b02d0a996e99298c59ddccfa1d8a0149080ac3a20de06068;
@@ -29,6 +31,9 @@ pub const OPERATOR: RoleId = 0x023edb77f7c8cc9e38e8afe78954f703aeeda7fffe014eeb6
2931
// int.from_bytes(Web3.keccak(text="ROLE_TOKEN_ADMIN"), "big") & MASK_250 .
3032
pub const TOKEN_ADMIN: RoleId = 0x0128d63adbf6b09002c26caf55c47e2f26635807e3ef1b027218aa74c8d61a3e;
3133

34+
// int.from_bytes(Web3.keccak(text="ROLE_UPGRADE_AGENT"), "big") & MASK_250 .
35+
pub const UPGRADE_AGENT: RoleId = 0x1d8034a6db21585e9d97ca912eb8113361e6858f64c45c9b321a4d01e949484;
36+
3237
// int.from_bytes(Web3.keccak(text="ROLE_UPGRADE_GOVERNOR"), "big") & MASK_250 .
3338
pub const UPGRADE_GOVERNOR: RoleId =
3439
0x251e864ca2a080f55bce5da2452e8cfcafdbc951a3e7fff5023d558452ec228;
@@ -41,16 +46,22 @@ pub const SECURITY_ADMIN: RoleId =
4146
pub const SECURITY_AGENT: RoleId =
4247
0x37693ba312785932d430dccf0f56ffedd0aa7c0f8b6da2cc4530c2717689b96;
4348

49+
// int.from_bytes(Web3.keccak(text="ROLE_SECURITY_GOVERNOR"), "big") & MASK_250 .
50+
pub const SECURITY_GOVERNOR: RoleId =
51+
0xa5a83e9807e87f281d865ab54b7b0ed2f7f4bbfef73888810ca16e95e734eb;
52+
4453
#[starknet::interface]
4554
pub trait IRoles<TContractState> {
4655
fn is_app_governor(self: @TContractState, account: ContractAddress) -> bool;
4756
fn is_app_role_admin(self: @TContractState, account: ContractAddress) -> bool;
4857
fn is_governance_admin(self: @TContractState, account: ContractAddress) -> bool;
4958
fn is_operator(self: @TContractState, account: ContractAddress) -> bool;
5059
fn is_token_admin(self: @TContractState, account: ContractAddress) -> bool;
60+
fn is_upgrade_agent(self: @TContractState, account: ContractAddress) -> bool;
5161
fn is_upgrade_governor(self: @TContractState, account: ContractAddress) -> bool;
5262
fn is_security_admin(self: @TContractState, account: ContractAddress) -> bool;
5363
fn is_security_agent(self: @TContractState, account: ContractAddress) -> bool;
64+
fn is_security_governor(self: @TContractState, account: ContractAddress) -> bool;
5465
fn register_app_governor(ref self: TContractState, account: ContractAddress);
5566
fn remove_app_governor(ref self: TContractState, account: ContractAddress);
5667
fn register_app_role_admin(ref self: TContractState, account: ContractAddress);
@@ -61,13 +72,17 @@ pub trait IRoles<TContractState> {
6172
fn remove_operator(ref self: TContractState, account: ContractAddress);
6273
fn register_token_admin(ref self: TContractState, account: ContractAddress);
6374
fn remove_token_admin(ref self: TContractState, account: ContractAddress);
75+
fn register_upgrade_agent(ref self: TContractState, account: ContractAddress);
76+
fn remove_upgrade_agent(ref self: TContractState, account: ContractAddress);
6477
fn register_upgrade_governor(ref self: TContractState, account: ContractAddress);
6578
fn remove_upgrade_governor(ref self: TContractState, account: ContractAddress);
6679
fn renounce(ref self: TContractState, role: RoleId);
6780
fn register_security_admin(ref self: TContractState, account: ContractAddress);
6881
fn remove_security_admin(ref self: TContractState, account: ContractAddress);
6982
fn register_security_agent(ref self: TContractState, account: ContractAddress);
7083
fn remove_security_agent(ref self: TContractState, account: ContractAddress);
84+
fn register_security_governor(ref self: TContractState, account: ContractAddress);
85+
fn remove_security_governor(ref self: TContractState, account: ContractAddress);
7186
fn has_legacy_role(self: @TContractState, account: ContractAddress, role: RoleId) -> bool;
7287
fn reclaim_legacy_roles(ref self: TContractState);
7388
}
@@ -76,10 +91,13 @@ pub trait IRoles<TContractState> {
7691
pub trait IMinimalRoles<TContractState> {
7792
fn is_governance_admin(self: @TContractState, account: ContractAddress) -> bool;
7893
fn is_upgrade_governor(self: @TContractState, account: ContractAddress) -> bool;
94+
fn is_upgrade_agent(self: @TContractState, account: ContractAddress) -> bool;
7995
fn register_governance_admin(ref self: TContractState, account: ContractAddress);
8096
fn remove_governance_admin(ref self: TContractState, account: ContractAddress);
8197
fn register_upgrade_governor(ref self: TContractState, account: ContractAddress);
8298
fn remove_upgrade_governor(ref self: TContractState, account: ContractAddress);
99+
fn register_upgrade_agent(ref self: TContractState, account: ContractAddress);
100+
fn remove_upgrade_agent(ref self: TContractState, account: ContractAddress);
83101
fn renounce(ref self: TContractState, role: RoleId);
84102
}
85103

@@ -136,6 +154,18 @@ pub(crate) struct SecurityAgentRemoved {
136154
pub removed_by: ContractAddress,
137155
}
138156

157+
#[derive(Copy, Drop, PartialEq, starknet::Event)]
158+
pub(crate) struct SecurityGovernorAdded {
159+
pub added_account: ContractAddress,
160+
pub added_by: ContractAddress,
161+
}
162+
163+
#[derive(Copy, Drop, PartialEq, starknet::Event)]
164+
pub(crate) struct SecurityGovernorRemoved {
165+
pub removed_account: ContractAddress,
166+
pub removed_by: ContractAddress,
167+
}
168+
139169
#[derive(Copy, Drop, PartialEq, starknet::Event)]
140170
pub(crate) struct SecurityAdminAdded {
141171
pub added_account: ContractAddress,
@@ -177,3 +207,15 @@ pub(crate) struct UpgradeGovernorRemoved {
177207
pub removed_account: ContractAddress,
178208
pub removed_by: ContractAddress,
179209
}
210+
211+
#[derive(Copy, Drop, PartialEq, starknet::Event)]
212+
pub(crate) struct UpgradeAgentAdded {
213+
pub added_account: ContractAddress,
214+
pub added_by: ContractAddress,
215+
}
216+
217+
#[derive(Copy, Drop, PartialEq, starknet::Event)]
218+
pub(crate) struct UpgradeAgentRemoved {
219+
pub removed_account: ContractAddress,
220+
pub removed_by: ContractAddress,
221+
}

packages/utils/src/components/roles/roles.cairo

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ pub(crate) mod RolesComponent {
44
APP_GOVERNOR, APP_ROLE_ADMIN, AppGovernorAdded, AppGovernorRemoved, AppRoleAdminAdded,
55
AppRoleAdminRemoved, GOVERNANCE_ADMIN, GovernanceAdminAdded, GovernanceAdminRemoved, IRoles,
66
OPERATOR, OperatorAdded, OperatorRemoved, RoleId, SECURITY_ADMIN, SECURITY_AGENT,
7-
SecurityAdminAdded, SecurityAdminRemoved, SecurityAgentAdded, SecurityAgentRemoved,
8-
TOKEN_ADMIN, TokenAdminAdded, TokenAdminRemoved, UPGRADE_GOVERNOR, UpgradeGovernorAdded,
9-
UpgradeGovernorRemoved,
7+
SECURITY_GOVERNOR, SecurityAdminAdded, SecurityAdminRemoved, SecurityAgentAdded,
8+
SecurityAgentRemoved, SecurityGovernorAdded, SecurityGovernorRemoved, TOKEN_ADMIN,
9+
TokenAdminAdded, TokenAdminRemoved, UPGRADE_AGENT, UPGRADE_GOVERNOR, UpgradeAgentAdded,
10+
UpgradeAgentRemoved, UpgradeGovernorAdded, UpgradeGovernorRemoved,
1011
};
1112
use core::num::traits::Zero;
1213
use starknet::storage::StorageMapReadAccess;
@@ -36,10 +37,14 @@ pub(crate) mod RolesComponent {
3637
SecurityAdminRemoved: SecurityAdminRemoved,
3738
SecurityAgentAdded: SecurityAgentAdded,
3839
SecurityAgentRemoved: SecurityAgentRemoved,
40+
SecurityGovernorAdded: SecurityGovernorAdded,
41+
SecurityGovernorRemoved: SecurityGovernorRemoved,
3942
TokenAdminAdded: TokenAdminAdded,
4043
TokenAdminRemoved: TokenAdminRemoved,
4144
UpgradeGovernorAdded: UpgradeGovernorAdded,
4245
UpgradeGovernorRemoved: UpgradeGovernorRemoved,
46+
UpgradeAgentAdded: UpgradeAgentAdded,
47+
UpgradeAgentRemoved: UpgradeAgentRemoved,
4348
}
4449
use openzeppelin::access::accesscontrol::AccessControlComponent;
4550
use openzeppelin::access::accesscontrol::AccessControlComponent::{
@@ -95,11 +100,25 @@ pub(crate) mod RolesComponent {
95100
access_comp.has_role(role: SECURITY_AGENT, :account)
96101
}
97102

103+
fn is_security_governor(
104+
self: @ComponentState<TContractState>, account: ContractAddress,
105+
) -> bool {
106+
let access_comp = get_dep_component!(self, Access);
107+
access_comp.has_role(role: SECURITY_GOVERNOR, :account)
108+
}
109+
98110
fn is_token_admin(self: @ComponentState<TContractState>, account: ContractAddress) -> bool {
99111
let access_comp = get_dep_component!(self, Access);
100112
access_comp.has_role(role: TOKEN_ADMIN, :account)
101113
}
102114

115+
fn is_upgrade_agent(
116+
self: @ComponentState<TContractState>, account: ContractAddress,
117+
) -> bool {
118+
let access_comp = get_dep_component!(self, Access);
119+
access_comp.has_role(role: UPGRADE_AGENT, :account)
120+
}
121+
103122
fn is_upgrade_governor(
104123
self: @ComponentState<TContractState>, account: ContractAddress,
105124
) -> bool {
@@ -177,6 +196,25 @@ pub(crate) mod RolesComponent {
177196
self._revoke_role_and_emit(role: SECURITY_AGENT, :account, :event);
178197
}
179198

199+
fn register_security_governor(
200+
ref self: ComponentState<TContractState>, account: ContractAddress,
201+
) {
202+
let event = Event::SecurityGovernorAdded(
203+
SecurityGovernorAdded { added_account: account, added_by: get_caller_address() },
204+
);
205+
self._grant_role_and_emit(role: SECURITY_GOVERNOR, :account, :event);
206+
}
207+
208+
fn remove_security_governor(
209+
ref self: ComponentState<TContractState>, account: ContractAddress,
210+
) {
211+
let event = Event::SecurityGovernorRemoved(
212+
SecurityGovernorRemoved {
213+
removed_account: account, removed_by: get_caller_address(),
214+
},
215+
);
216+
self._revoke_role_and_emit(role: SECURITY_GOVERNOR, :account, :event);
217+
}
180218

181219
fn register_governance_admin(
182220
ref self: ComponentState<TContractState>, account: ContractAddress,
@@ -230,6 +268,24 @@ pub(crate) mod RolesComponent {
230268
self._revoke_role_and_emit(role: TOKEN_ADMIN, :account, :event);
231269
}
232270

271+
fn register_upgrade_agent(
272+
ref self: ComponentState<TContractState>, account: ContractAddress,
273+
) {
274+
let event = Event::UpgradeAgentAdded(
275+
UpgradeAgentAdded { added_account: account, added_by: get_caller_address() },
276+
);
277+
self._grant_role_and_emit(role: UPGRADE_AGENT, :account, :event);
278+
}
279+
280+
fn remove_upgrade_agent(
281+
ref self: ComponentState<TContractState>, account: ContractAddress,
282+
) {
283+
let event = Event::UpgradeAgentRemoved(
284+
UpgradeAgentRemoved { removed_account: account, removed_by: get_caller_address() },
285+
);
286+
self._revoke_role_and_emit(role: UPGRADE_AGENT, :account, :event);
287+
}
288+
233289
fn register_upgrade_governor(
234290
ref self: ComponentState<TContractState>, account: ContractAddress,
235291
) {
@@ -349,26 +405,31 @@ pub(crate) mod RolesComponent {
349405
access_comp.set_role_admin(role: GOVERNANCE_ADMIN, admin_role: GOVERNANCE_ADMIN);
350406
access_comp.set_role_admin(role: OPERATOR, admin_role: APP_ROLE_ADMIN);
351407
access_comp.set_role_admin(role: TOKEN_ADMIN, admin_role: APP_ROLE_ADMIN);
408+
access_comp.set_role_admin(role: UPGRADE_AGENT, admin_role: APP_ROLE_ADMIN);
352409
access_comp.set_role_admin(role: UPGRADE_GOVERNOR, admin_role: GOVERNANCE_ADMIN);
353410

354411
access_comp._grant_role(role: SECURITY_ADMIN, account: governance_admin);
355412
access_comp.set_role_admin(role: SECURITY_ADMIN, admin_role: SECURITY_ADMIN);
356413
access_comp.set_role_admin(role: SECURITY_AGENT, admin_role: SECURITY_ADMIN);
414+
access_comp.set_role_admin(role: SECURITY_GOVERNOR, admin_role: SECURITY_ADMIN);
357415
}
358416

359417
fn only_app_governor(self: @ComponentState<TContractState>) {
360418
assert!(
361419
self.is_app_governor(get_caller_address()), "{}", AccessErrors::ONLY_APP_GOVERNOR,
362420
);
363421
}
422+
364423
fn only_operator(self: @ComponentState<TContractState>) {
365424
assert!(self.is_operator(get_caller_address()), "{}", AccessErrors::ONLY_OPERATOR);
366425
}
426+
367427
fn only_token_admin(self: @ComponentState<TContractState>) {
368428
assert!(
369429
self.is_token_admin(get_caller_address()), "{}", AccessErrors::ONLY_TOKEN_ADMIN,
370430
);
371431
}
432+
372433
fn only_upgrade_governor(self: @ComponentState<TContractState>) {
373434
assert!(
374435
self.is_upgrade_governor(get_caller_address()),
@@ -377,6 +438,15 @@ pub(crate) mod RolesComponent {
377438
);
378439
}
379440

441+
fn only_upgrader(self: @ComponentState<TContractState>) {
442+
assert!(
443+
self.is_upgrade_agent(get_caller_address())
444+
|| self.is_upgrade_governor(get_caller_address()),
445+
"{}",
446+
AccessErrors::ONLY_UPGRADER,
447+
);
448+
}
449+
380450
fn only_security_admin(self: @ComponentState<TContractState>) {
381451
assert!(
382452
self.is_security_admin(get_caller_address()),
@@ -392,5 +462,13 @@ pub(crate) mod RolesComponent {
392462
AccessErrors::ONLY_SECURITY_AGENT,
393463
);
394464
}
465+
466+
fn only_security_governor(self: @ComponentState<TContractState>) {
467+
assert!(
468+
self.is_security_governor(get_caller_address()),
469+
"{}",
470+
AccessErrors::ONLY_SECURITY_GOVERNOR,
471+
);
472+
}
395473
}
396474
}

0 commit comments

Comments
 (0)