-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathaccount.cairo
More file actions
349 lines (318 loc) · 14.2 KB
/
account.cairo
File metadata and controls
349 lines (318 loc) · 14.2 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//! This module defines the Spherre Account contract, which is a multi-signature account
//! with various components for managing permissions, transactions, and account data.
//!
//! The comment documentation for public entrypoints can be found in the `IAccount` interface.
#[starknet::contract]
pub mod SpherreAccount {
use AccountData::InternalTrait;
use openzeppelin::introspection::src5::SRC5Component;
use openzeppelin::token::erc721::ERC721ReceiverComponent;
use openzeppelin_access::ownable::OwnableComponent;
use openzeppelin_security::pausable::PausableComponent;
use openzeppelin_upgrades::UpgradeableComponent;
use openzeppelin_upgrades::interface::IUpgradeable;
use spherre::{
account_data::AccountData,
actions::{
change_threshold_transaction::ChangeThresholdTransaction,
member_add_transaction::MemberAddTransaction,
member_permission_tx::MemberPermissionTransaction,
member_remove_transaction::MemberRemoveTransaction, nft_transaction::NFTTransaction,
smart_token_lock_transaction::SmartTokenLockTransactionComponent,
token_transaction::TokenTransaction,
},
components::{permission_control::PermissionControl, treasury_handler::TreasuryHandler},
interfaces::iaccount::IAccount, types::{AccountDetails, TransactionStatus, TransactionType},
{errors::Errors},
};
use starknet::{
{ClassHash, ContractAddress, contract_address_const, get_caller_address},
{storage::{StorableStoragePointerReadAccess, StoragePointerWriteAccess}},
};
component!(path: AccountData, storage: account_data, event: AccountDataEvent);
component!(path: PermissionControl, storage: permission_control, event: PermissionControlEvent);
component!(path: TreasuryHandler, storage: treasury_handler, event: TreasuryHandlerEvent);
component!(
path: ChangeThresholdTransaction,
storage: change_threshold_transaction,
event: ChangeThresholdEvent,
);
component!(
path: MemberAddTransaction,
storage: member_add_transaction,
event: MemberAddTransactionEvent,
);
component!(
path: MemberRemoveTransaction,
storage: member_remove_transaction,
event: MemberRemoveTransactionEvent,
);
component!(
path: MemberPermissionTransaction,
storage: member_permission_transaction,
event: MemberPermissionTransactionEvent,
);
component!(
path: SmartTokenLockTransactionComponent,
storage: smart_token_lock_transaction,
event: SmartTokenLockTransactionEvent,
);
component!(path: NFTTransaction, storage: nft_transaction, event: NFTTransactionEvent);
component!(path: TokenTransaction, storage: token_transaction, event: TokenTransactionEvent);
component!(path: PausableComponent, storage: pausable, event: PausableEvent);
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);
component!(path: ERC721ReceiverComponent, storage: erc721_receiver, event: ERC721ReceiverEvent);
component!(path: SRC5Component, storage: src5, event: SRC5Event);
// AccountData component implementation
#[abi(embed_v0)]
impl AccountDataImpl = AccountData::AccountData<ContractState>;
impl AccountDataInternalImpl = AccountData::InternalImpl<ContractState>;
// PermissionControl component implementation
#[abi(embed_v0)]
impl PermissionControlImpl =
PermissionControl::PermissionControl<ContractState>;
impl PermissionControlInternalImpl = PermissionControl::InternalImpl<ContractState>;
// TreasuryHandler component implementation
#[abi(embed_v0)]
impl TreasuryHandlerImpl = TreasuryHandler::TreasuryHandler<ContractState>;
impl TreasuryHandlerInternalImpl = TreasuryHandler::InternalImpl<ContractState>;
// Pausable component implementation
#[abi(embed_v0)]
pub impl PausableImpl = PausableComponent::PausableImpl<ContractState>;
pub impl PausableInternalImpl = PausableComponent::InternalImpl<ContractState>;
// Ownable Mixin
#[abi(embed_v0)]
impl OwnableMixinImpl = OwnableComponent::OwnableMixinImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;
// Upgradeable component implementation
pub impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;
// Implement SRC5 mixin
impl SRC5Impl = SRC5Component::SRC5Impl<ContractState>;
impl SRC5InternalImpl = SRC5Component::InternalImpl<ContractState>;
// Implement ERC721Receiver mixin
#[abi(embed_v0)]
impl ERC721ReceiverMixinImpl =
ERC721ReceiverComponent::ERC721ReceiverMixinImpl<ContractState>;
impl ERC721ReceiverInternalImpl = ERC721ReceiverComponent::InternalImpl<ContractState>;
// Integrate the actions components to the contract
// ChangeThresholdTransaction component implementation
#[abi(embed_v0)]
impl ChangeThresholdTransactionImpl =
ChangeThresholdTransaction::ChangeThresholdTransaction<ContractState>;
// MemberAddTransaction component implementation
#[abi(embed_v0)]
impl MemberAddTransactionImpl =
MemberAddTransaction::MemberAddTransaction<ContractState>;
// MemberRemoveTransaction component implementation
#[abi(embed_v0)]
impl MemberRemoveTransactionImpl =
MemberRemoveTransaction::MemberRemoveTransaction<ContractState>;
impl MemberRemoveInternalImpl = MemberRemoveTransaction::PrivateImpl<ContractState>;
// MemberPermissionTransaction component implementation
#[abi(embed_v0)]
impl MemberPermissionTransactionImpl =
MemberPermissionTransaction::MemberPermissionTransaction<ContractState>;
// SmartTokenLockTransaction component implementation
#[abi(embed_v0)]
impl SmartTokenLockTransactionImpl =
SmartTokenLockTransactionComponent::SmartTokenLockTransactionComponent<ContractState>;
// NFTTransaction component implementation
#[abi(embed_v0)]
impl NFTTransactionImpl = NFTTransaction::NFTTransaction<ContractState>;
// TokenTransaction component implementation
#[abi(embed_v0)]
impl TokenTransactionImpl = TokenTransaction::TokenTransaction<ContractState>;
impl TokenTransactionInternalImpl = TokenTransaction::PrivateImpl<ContractState>;
#[storage]
struct Storage {
pub deployer: ContractAddress,
pub contract_address: ContractAddress,
name: ByteArray,
description: ByteArray,
#[substorage(v0)]
account_data: AccountData::Storage,
#[substorage(v0)]
permission_control: PermissionControl::Storage,
#[substorage(v0)]
treasury_handler: TreasuryHandler::Storage,
#[substorage(v0)]
change_threshold_transaction: ChangeThresholdTransaction::Storage,
#[substorage(v0)]
member_add_transaction: MemberAddTransaction::Storage,
#[substorage(v0)]
member_remove_transaction: MemberRemoveTransaction::Storage,
#[substorage(v0)]
member_permission_transaction: MemberPermissionTransaction::Storage,
#[substorage(v0)]
smart_token_lock_transaction: SmartTokenLockTransactionComponent::Storage,
#[substorage(v0)]
nft_transaction: NFTTransaction::Storage,
#[substorage(v0)]
token_transaction: TokenTransaction::Storage,
#[substorage(v0)]
pausable: PausableComponent::Storage,
#[substorage(v0)]
ownable: OwnableComponent::Storage,
#[substorage(v0)]
upgradeable: UpgradeableComponent::Storage,
#[substorage(v0)]
pub erc721_receiver: ERC721ReceiverComponent::Storage,
#[substorage(v0)]
pub src5: SRC5Component::Storage,
}
#[event]
#[derive(Drop, starknet::Event)]
pub enum Event {
#[flat]
AccountDataEvent: AccountData::Event,
#[flat]
PermissionControlEvent: PermissionControl::Event,
#[flat]
TreasuryHandlerEvent: TreasuryHandler::Event,
#[flat]
ChangeThresholdEvent: ChangeThresholdTransaction::Event,
#[flat]
MemberAddTransactionEvent: MemberAddTransaction::Event,
#[flat]
MemberRemoveTransactionEvent: MemberRemoveTransaction::Event,
#[flat]
MemberPermissionTransactionEvent: MemberPermissionTransaction::Event,
#[flat]
SmartTokenLockTransactionEvent: SmartTokenLockTransactionComponent::Event,
#[flat]
NFTTransactionEvent: NFTTransaction::Event,
#[flat]
TokenTransactionEvent: TokenTransaction::Event,
#[flat]
PausableEvent: PausableComponent::Event,
#[flat]
OwnableEvent: OwnableComponent::Event,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event,
#[flat]
ERC721ReceiverEvent: ERC721ReceiverComponent::Event,
#[flat]
SRC5Event: SRC5Component::Event,
}
#[constructor]
pub fn constructor(
ref self: ContractState,
deployer: ContractAddress,
owner: ContractAddress,
name: ByteArray,
description: ByteArray,
members: Array<ContractAddress>,
threshold: u64,
) {
assert(deployer != contract_address_const::<0>(), Errors::ERR_DEPLOYER_ZERO);
assert(owner != contract_address_const::<0>(), Errors::ERR_OWNER_ZERO);
assert(members.len() > 0, Errors::NON_ZERO_MEMBER_LENGTH);
assert(threshold > 0, Errors::NON_ZERO_THRESHOLD);
assert((members.len()).into() >= threshold, Errors::ERR_INVALID_MEMBER_THRESHOLD);
//TODO: add assertion check to check that owner is in members array.
self.name.write(name);
self.description.write(description);
let len_member = members.len();
for index in 0
..len_member {
let member = *members.at(index);
self.account_data._add_member(member);
self.permission_control.assign_all_permissions(member);
};
self.account_data.set_threshold(threshold);
// Record deployer
self.deployer.write(deployer);
// Initialize Ownable component
self.ownable.initializer(deployer);
// Initialize ERC721Receiver
self.erc721_receiver.initializer();
}
#[abi(embed_v0)]
pub impl AccountImpl of IAccount<ContractState> {
fn get_name(self: @ContractState) -> ByteArray {
self.name.read()
}
fn get_description(self: @ContractState) -> ByteArray {
self.description.read()
}
fn get_account_details(self: @ContractState) -> AccountDetails {
AccountDetails { name: self.name.read(), description: self.description.read() }
}
fn get_deployer(self: @ContractState) -> ContractAddress {
self.deployer.read()
}
fn pause(ref self: ContractState) {
let caller = get_caller_address();
let deployer = self.deployer.read();
assert(caller == deployer, Errors::ERR_NOT_DEPLOYER);
// The Pausable component automatically emits events when the respective functions are
// called. These events are included in the PausableEvent variant.
self.pausable.pause();
}
fn unpause(ref self: ContractState) {
let caller = get_caller_address();
let deployer = self.deployer.read();
assert(caller == deployer, Errors::ERR_NOT_DEPLOYER);
self.pausable.unpause();
}
fn execute_transaction(ref self: ContractState, transaction_id: u256) {
let transaction = self.account_data.get_transaction(transaction_id);
// Check if the transaction is executable
assert(
transaction.tx_status == TransactionStatus::APPROVED,
Errors::ERR_TRANSACTION_NOT_EXECUTABLE,
);
match transaction.tx_type {
// Handle ChangeThresholdTransaction
TransactionType::THRESHOLD_CHANGE => {
self
.change_threshold_transaction
.execute_threshold_change_transaction(transaction_id);
},
// Handle MemberAddTransaction
TransactionType::MEMBER_ADD => {
self.member_add_transaction.execute_member_add_transaction(transaction_id);
},
// Handle MemberRemoveTransaction
TransactionType::MEMBER_REMOVE => {
self
.member_remove_transaction
.execute_remove_member_transaction(transaction_id);
},
// Handle MemberPermissionTransaction
TransactionType::MEMBER_PERMISSION_EDIT => {
self
.member_permission_transaction
.execute_edit_permission_transaction(transaction_id);
},
// Handle NFTTransaction
TransactionType::NFT_SEND => {
self.nft_transaction.execute_nft_transaction(transaction_id);
},
// Handle TokenTransaction
TransactionType::TOKEN_SEND => {
self.token_transaction.execute_token_transaction(transaction_id);
},
TransactionType::SMART_TOKEN_LOCK => {
self
.smart_token_lock_transaction
.execute_smart_token_lock_transaction(transaction_id);
},
_ => {
// If the transaction type is not recognized, raise an error
panic(array![Errors::ERR_INVALID_TRANSACTION_TYPE]);
},
}
}
}
#[abi(embed_v0)]
impl UpgradeableImpl of IUpgradeable<ContractState> {
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
// This function can only be called by the deployer
self.ownable.assert_only_owner();
// Replace the class hash, hence upgrading the contract
self.upgradeable.upgrade(new_class_hash);
}
}
}