-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathorganization.cairo
More file actions
269 lines (245 loc) · 10.7 KB
/
Copy pathorganization.cairo
File metadata and controls
269 lines (245 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/// ## A Starknet component responsible for managing the core details of an organization.
///
/// This component handles:
/// - Storing fundamental organization information (name, ID, owner, etc.).
/// - Managing a committee of privileged addresses.
/// - Handling organization-level configuration.
/// - Handling members' and interorganization business contracts
/// - Ownership transfers.
#[starknet::component]
pub mod OrganizationComponent {
use starknet::storage::{
Map, StoragePathEntry, StoragePointerReadAccess, StoragePointerWriteAccess,
};
use starknet::{ContractAddress, get_block_timestamp, get_caller_address};
// use core::ec::stark_curve;
// use core::ecdsa;
use crate::interfaces::iorganization::IOrganization;
use crate::structs::organization::{
Contract, ContractParties, ContractStatus, ContractType, OrganizationConfig,
OrganizationConfigNode, OrganizationInfo, OrganizationType,
};
use super::super::member_manager::MemberManagerComponent;
/// Defines the storage layout for the `OrganizationComponent`.
#[storage]
pub struct Storage {
/// The address of the contract deployer.
pub deployer: ContractAddress,
/// Maps a committee member's address to their power level or rank.
pub commitee: Map<ContractAddress, u16>, // address -> level of power
/// The configuration node for the organization.
pub config: OrganizationConfigNode, // refactor to OrganizationConfig
/// Struct containing the core information of the organization.
pub org_info: OrganizationInfo,
/// Maps an id to a contract
pub contracts: Map<u256, Contract>,
/// Contract counter
pub contract_counter: u64,
}
/// Events emitted by the `OrganizationComponent`.
#[event]
#[derive(Drop, starknet::Event)]
pub enum Event {}
/// # OrganizationManager
///
/// Public-facing implementation of the `IOrganization` interface.
#[embeddable_as(OrganizationManager)]
pub impl Organization<
TContractState,
+HasComponent<TContractState>,
+Drop<TContractState>,
impl Member: MemberManagerComponent::HasComponent<TContractState>,
> of IOrganization<ComponentState<TContractState>> {
/// Transfers the claim of the organization to a new address.
///
/// ### Note
/// This function is not yet implemented.
///
/// ### Parameters
/// - `to`: The address of the new owner.
fn transfer_organization_claim(
ref self: ComponentState<TContractState>, to: ContractAddress,
) {}
/// Adjusts the organization's committee by adding or removing members.
///
/// ### Note
/// This function is not yet implemented. Any subtracted member would have their power level
/// set to zero.
///
/// ### Parameters
/// - `add`: An array of addresses to add to the committee.
/// - `subtract`: An array of addresses to remove from the committee.
fn adjust_committee(
ref self: ComponentState<TContractState>,
add: Array<ContractAddress>,
subtract: Array<ContractAddress>,
) { // any one subtracted, power would be taken down to zero.
}
/// Updates the organization's configuration.
///
/// ### Note
/// This function is not yet implemented.
///
/// ### Parameters
/// - `config`: The new organization configuration.
fn update_organization_config(
ref self: ComponentState<TContractState>, config: OrganizationConfig,
) {}
/// Retrieves the core details of the organization.
///
/// ### Returns
/// - `OrganizationInfo`: A struct containing the organization's details.
fn get_organization_details(self: @ComponentState<TContractState>) -> OrganizationInfo {
self.org_info.read()
}
/// Creates an employee contract, to be given at hiring, or updated during employment
/// Show to employee at hiring
fn create_company_to_member_contract(
ref self: ComponentState<TContractState>,
contract_type: ContractType,
member_id: u256,
ipfs_hash: felt252,
expiry: Option<u64>,
) {
let member_component = get_dep_component!(@self, Member);
let caller = get_caller_address();
let is_admin = member_component.admin_ca.entry(caller).read();
assert(is_admin, 'Caller Not Permitted');
let contract = Contract {
id: self.contract_counter.read().into(),
hash: ipfs_hash,
version: 1,
signed_time: 0,
contract_parties: ContractParties::COMPANY_MEMBER(member_id),
status: ContractStatus::PROPOSED,
expiry_time: Option::None,
};
self.contracts.entry(self.contract_counter.read().into()).write(contract);
self.contract_counter.write(self.contract_counter.read() + 1);
}
/// Creates a contract between two companies using Littlefinger. Advanced features
/// Show to both companies
fn create_company_to_partner_contract(
ref self: ComponentState<TContractState>,
contract_type: ContractType,
partner_address: ContractAddress,
ipfs_hash: felt252,
expiry: Option<u64>,
) {
let member_component = get_dep_component!(@self, Member);
let caller = get_caller_address();
let is_admin = member_component.admin_ca.entry(caller).read();
assert(is_admin, 'Caller Not Permitted');
let contract = Contract {
id: self.contract_counter.read().into(),
hash: ipfs_hash,
version: 1,
signed_time: 0,
contract_parties: ContractParties::COMPANY_COMPANY(partner_address),
status: ContractStatus::PROPOSED,
expiry_time: Option::None,
};
self.contracts.entry(self.contract_counter.read().into()).write(contract);
self.contract_counter.write(self.contract_counter.read() + 1);
}
/// Used to accept a contract, by whoever is on the other side of the contract (recipeint)
/// Will implement Starknet message signing soon
fn sign_contract(
ref self: ComponentState<TContractState>, contract_id: u256, signature: Array<felt252>,
) {
// ecdsa::check_ecdsa_signature()
let mut contract = self.contracts.entry(contract_id).read();
contract.status = ContractStatus::ACTIVE;
contract.signed_time = get_block_timestamp();
self.contracts.entry(contract_id).write(contract);
}
/// Updates a contract ipfs hash and version
/// Show to employee at hiring
fn update_contract(
ref self: ComponentState<TContractState>,
contract_id: u256,
new_ipfs_hash: felt252,
expiry: Option<u64>,
) {
let mut contract = self.contracts.entry(contract_id).read();
contract.hash = new_ipfs_hash;
if expiry.is_some() {
contract.expiry_time = Option::Some(expiry.unwrap());
}
contract.version += 1;
self.contracts.entry(contract_id).write(contract);
}
/// Termminates a contract, can be used by employees or fellow companies
/// Mutual agreement between company and employee
fn terminate_contract(
ref self: ComponentState<TContractState>, contract_id: u256, signature: Array<felt252>,
) {
let mut contract = self.contracts.entry(contract_id).read();
contract.status = ContractStatus::TERMINATED;
}
/// Used to get a contract ipfs hash for access purpose
/// ### Returns
/// - Contract: all the important info of the contract suitable for storage onchain.
/// - The rest goes to IPFS
fn get_contract(self: @ComponentState<TContractState>, contract_id: u256) -> Contract {
self.contracts.entry(contract_id).read()
}
}
/// # InternalImpl
///
/// Internal functions for initialization and privileged operations.
#[generate_trait]
pub impl InternalImpl<
TContractState, +HasComponent<TContractState>,
// +Drop<TContractState>,
// impl Member: MemberManagerComponent::HasComponent<TContractState>,
> of OrganizationInternalTrait<TContractState> {
/// Initializes the organization component with its essential data.
///
/// ### Parameters
/// - `owner`: An optional address for the organization's owner. If `None`, the caller
/// becomes the owner.
/// - `name`: The name of the organization.
/// - `ipfs_url`: A URL pointing to more detailed organization metadata (e.g., on IPFS).
/// - `vault_address`: The address of the organization's treasury or vault contract.
/// - `org_id`: A unique identifier for the organization.
/// - `deployer`: The address of the account or contract that deployed this component.
/// - `organization_type`: A numeric code for the organization type (0 for Centralized, 1
/// for Decentralized).
fn _init(
ref self: ComponentState<TContractState>,
owner: Option<ContractAddress>,
name: ByteArray,
ipfs_url: ByteArray,
vault_address: ContractAddress,
org_id: u256,
// organization_info: OrganizationInfo,
deployer: ContractAddress,
organization_type: u8,
) {
let caller = get_caller_address();
let mut ascribed_owner = caller;
if owner.is_some() {
ascribed_owner = owner.unwrap();
}
let current_timestamp = get_block_timestamp();
let mut processed_org_type = OrganizationType::CENTRALIZED;
match organization_type {
1 => processed_org_type = OrganizationType::DECENTRALIZED,
0 | _ => processed_org_type = OrganizationType::CENTRALIZED,
}
let organization_info = OrganizationInfo {
org_id,
name,
deployer: caller,
owner: ascribed_owner,
ipfs_url,
vault_address,
created_at: current_timestamp,
organization_type: processed_org_type,
};
self.org_info.write(organization_info);
self.deployer.write(deployer);
}
}
}