-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.cairo
97 lines (84 loc) · 3.67 KB
/
factory.cairo
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
use core::serde::Serde;
use core::traits::Into;
#[contract]
mod Factory {
use starknet::ContractAddress;
use starknet::get_caller_address;
use starknet::deploy_syscall;
use starknet::class_hash::ClassHash;
use starknet::get_block_timestamp;
use array::ArrayTrait;
use traits::Into;
// Storage variable used to store the anchored value
struct Storage {
deployed_length: u128, // length of the deployed contracts array
deployed: LegacyMap<u128, ContractAddress>, // anchored_contract_address, user_account_contract_address
admin: ContractAddress, // account wallet authorized to push new contracts
class_hash: ClassHash, // class hash of the anchoring contract
}
// Function used to initialize the contract
#[constructor]
fn constructor(_admin: ContractAddress, _class_hash: ClassHash) {
admin::write(_admin);
class_hash::write(_class_hash);
}
#[external]
fn change_admin(new_admin: ContractAddress) {
assert(get_caller_address() == admin::read() , 'not_whitelisted_caller');
admin::write(new_admin);
}
// Function used to deploy and add a new contract to the whitelist
#[external]
fn deploy(_admin: ContractAddress, contract_label: felt252) -> ContractAddress { //, _description: Array::<felt252>
assert(get_caller_address() == admin::read() , 'not_whitelisted_caller');
// Creating the call data for the deploy syscall
let mut calldata_array = ArrayTrait::new();
calldata_array.append(_admin.into());
calldata_array.append(contract_label.into());
//calldata_array.append(_description.into().);
// Deploying the contract
let result = deploy_syscall(
class_hash::read(),
get_block_timestamp().into(),
// contract_address_salt: felt252, value used in order to calculate the futur contract address,
// in the futur we need to add some randomness here otherwise we might be able to predict the
// contract address and deploy a contract with the same address as an existing one.
calldata_array.span(),// calldata: Span<felt252>, // Should contain whitelisted value
false,
);
// Adding the contract to the whitelist mapping
let (deployed_addr, _) = result.unwrap_syscall();
deployed::write(deployed_length::read(), deployed_addr);
deployed_length::write(deployed_length::read() + 1);
// Returning the deployed contract address
deployed_addr
}
#[view]
fn get_admin() -> ContractAddress {
admin::read()
}
#[view]
// Get metadatas about the contract
fn get_metadatas() -> Array::<felt252> {
let mut metadatas = ArrayTrait::new();
metadatas.append('name: Smart-chain / Secure Fact');
metadatas.append('ory | author: smart-chain <cont');
metadatas.append('[email protected]> | version: ');
metadatas.append('1.0.0 | license: MIT | homepage');
metadatas.append(': https://secure.smart-chain.fr');
metadatas.append(' | description: Factory for Sec');
metadatas.append('ure product |');
metadatas
}
#[view]
fn get_deployed_address_array() -> Array::<felt252> {
let mut deployed_addr = ArrayTrait::new();
construct_deployed_address_array(deployed_addr, 0_u128)
}
fn construct_deployed_address_array(mut values: Array::<felt252>, index: u128) -> Array::<felt252> {
if index < deployed_length::read() {
values.append(deployed::read(index).into());
construct_deployed_address_array(values, index + 1)
} else { values }
}
}