forked from DistinctCodes/AssetsUp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
324 lines (264 loc) · 9.48 KB
/
lib.rs
File metadata and controls
324 lines (264 loc) · 9.48 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
#![no_std]
use crate::error::{Error, handle_error};
use soroban_sdk::{Address, BytesN, Env, String, Vec, contract, contractimpl, contracttype};
pub(crate) mod asset;
pub(crate) mod audit;
pub(crate) mod branch;
pub(crate) mod error;
pub(crate) mod types;
pub use types::*;
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DataKey {
Admin,
}
#[contract]
pub struct AssetUpContract;
#[contractimpl]
impl AssetUpContract {
pub fn initialize(env: Env, admin: Address) -> Result<(), Error> {
admin.require_auth();
if env.storage().persistent().has(&DataKey::Admin) {
handle_error(&env, Error::AlreadyInitialized)
}
env.storage().persistent().set(&DataKey::Admin, &admin);
Ok(())
}
pub fn get_admin(env: Env) -> Result<Address, Error> {
let key = DataKey::Admin;
if !env.storage().persistent().has(&key) {
handle_error(&env, Error::AdminNotFound)
}
let admin = env.storage().persistent().get(&key).unwrap();
Ok(admin)
}
// Asset functions
pub fn register_asset(env: Env, asset: asset::Asset) -> Result<(), Error> {
asset.owner.require_auth();
if asset.name.is_empty() {
panic!("Name cannot be empty");
}
let key = asset::DataKey::Asset(asset.id.clone());
let store = env.storage().persistent();
if store.has(&key) {
return Err(Error::AssetAlreadyExists);
}
store.set(&key, &asset);
// Log the procurement action
audit::log_action(
&env,
&asset.id,
asset.owner,
ActionType::Procured,
String::from_str(&env, "Asset registered"),
);
Ok(())
}
pub fn update_asset_status(
env: Env,
asset_id: BytesN<32>,
new_status: AssetStatus,
) -> Result<(), Error> {
let key = asset::DataKey::Asset(asset_id.clone());
let store = env.storage().persistent();
let mut asset = match store.get::<_, asset::Asset>(&key) {
Some(a) => a,
None => return Err(Error::AssetNotFound),
};
// Only asset owner can update status
asset.owner.require_auth();
// Update status
asset.status = new_status.clone();
store.set(&key, &asset);
// Log appropriate audit action based on status
let (details, action_type) = match new_status {
AssetStatus::InMaintenance => (
String::from_str(&env, "Asset in maintenance"),
ActionType::Maintained,
),
AssetStatus::Disposed => (
String::from_str(&env, "Asset disposed"),
ActionType::Disposed,
),
_ => return Ok(()), // Don't log other status changes
};
audit::log_action(&env, &asset_id, asset.owner, action_type, details);
Ok(())
}
pub fn get_asset(env: Env, asset_id: BytesN<32>) -> Result<asset::Asset, Error> {
let key = asset::DataKey::Asset(asset_id);
let store = env.storage().persistent();
match store.get::<_, asset::Asset>(&key) {
Some(a) => Ok(a),
None => Err(Error::AssetNotFound),
}
}
// Branch functions
pub fn create_branch(
env: Env,
id: BytesN<32>,
name: String,
location: String,
admin: Address,
) -> Result<(), Error> {
let contract_admin = Self::get_admin(env.clone())?;
contract_admin.require_auth();
if name.is_empty() {
panic!("Branch name cannot be empty");
}
let key = branch::DataKey::Branch(id.clone());
let store = env.storage().persistent();
if store.has(&key) {
return Err(Error::BranchAlreadyExists);
}
let branch = branch::Branch {
id: id.clone(),
name,
location,
admin,
};
store.set(&key, &branch);
let asset_list_key = branch::DataKey::AssetList(id);
let empty_asset_list: Vec<BytesN<32>> = Vec::new(&env);
store.set(&asset_list_key, &empty_asset_list);
Ok(())
}
pub fn add_asset_to_branch(
env: Env,
branch_id: BytesN<32>,
asset_id: BytesN<32>,
) -> Result<(), Error> {
let store = env.storage().persistent();
let branch_key = branch::DataKey::Branch(branch_id.clone());
if !store.has(&branch_key) {
return Err(Error::BranchNotFound);
}
let asset_key = asset::DataKey::Asset(asset_id.clone());
if !store.has(&asset_key) {
return Err(Error::AssetNotFound);
}
let asset_list_key = branch::DataKey::AssetList(branch_id);
let mut asset_list: Vec<BytesN<32>> =
store.get(&asset_list_key).unwrap_or_else(|| Vec::new(&env));
for existing_asset_id in asset_list.iter() {
if existing_asset_id == asset_id {
return Ok(());
}
}
asset_list.push_back(asset_id);
store.set(&asset_list_key, &asset_list);
Ok(())
}
pub fn get_branch_assets(env: Env, branch_id: BytesN<32>) -> Result<Vec<BytesN<32>>, Error> {
let store = env.storage().persistent();
let branch_key = branch::DataKey::Branch(branch_id.clone());
if !store.has(&branch_key) {
return Err(Error::BranchNotFound);
}
let asset_list_key = branch::DataKey::AssetList(branch_id);
match store.get(&asset_list_key) {
Some(asset_list) => Ok(asset_list),
None => Ok(Vec::new(&env)),
}
}
pub fn get_branch(env: Env, branch_id: BytesN<32>) -> Result<branch::Branch, Error> {
let key = branch::DataKey::Branch(branch_id);
let store = env.storage().persistent();
match store.get::<_, branch::Branch>(&key) {
Some(branch) => Ok(branch),
None => Err(Error::BranchNotFound),
}
}
pub fn tokenize_asset(
env: Env,
asset_id: BytesN<32>,
token_id: BytesN<32>,
) -> Result<(), Error> {
let admin_key = DataKey::Admin;
if !env.storage().persistent().has(&admin_key) {
handle_error(&env, Error::AdminNotFound)
}
let admin: Address = env.storage().persistent().get(&admin_key).unwrap();
admin.require_auth();
let key = asset::DataKey::Asset(asset_id.clone());
let store = env.storage().persistent();
let mut a: asset::Asset = match store.get(&key) {
Some(v) => v,
None => return Err(Error::AssetNotFound),
};
a.stellar_token_id = token_id;
store.set(&key, &a);
Ok(())
}
pub fn transfer_asset(
env: Env,
actor: Address,
asset_id: BytesN<32>,
new_branch_id: BytesN<32>,
) -> Result<(), Error> {
actor.require_auth();
let store = env.storage().persistent();
let asset_key = asset::DataKey::Asset(asset_id.clone());
let mut asset: asset::Asset = match store.get(&asset_key) {
Some(a) => a,
None => return Err(Error::AssetNotFound),
};
let contract_admin = Self::get_admin(env.clone())?;
if actor != contract_admin && actor != asset.owner {
return Err(Error::Unauthorized);
}
let old_branch_id = asset.branch_id.clone();
if old_branch_id == new_branch_id {
return Ok(());
}
let new_branch_key = branch::DataKey::Branch(new_branch_id.clone());
if !store.has(&new_branch_key) {
return Err(Error::BranchNotFound);
}
let old_asset_list_key = branch::DataKey::AssetList(old_branch_id);
let mut old_asset_list: Vec<BytesN<32>> = store.get(&old_asset_list_key).unwrap();
if let Some(index) = old_asset_list.iter().position(|x| x == asset_id) {
old_asset_list.remove(index as u32);
}
store.set(&old_asset_list_key, &old_asset_list);
let new_asset_list_key = branch::DataKey::AssetList(new_branch_id.clone());
let mut new_asset_list: Vec<BytesN<32>> = store
.get(&new_asset_list_key)
.unwrap_or_else(|| Vec::new(&env));
new_asset_list.push_back(asset_id.clone());
store.set(&new_asset_list_key, &new_asset_list);
asset.branch_id = new_branch_id;
store.set(&asset_key, &asset);
let note = String::from_str(&env, "Asset transferred");
audit::log_action(&env, &asset_id, actor, ActionType::Transferred, note);
Ok(())
}
pub fn log_action(
env: Env,
actor: Address,
asset_id: BytesN<32>,
action: ActionType,
note: String,
) -> Result<(), Error> {
actor.require_auth();
let store = env.storage().persistent();
let asset_key = asset::DataKey::Asset(asset_id.clone());
let asset: asset::Asset = match store.get(&asset_key) {
Some(a) => a,
None => return Err(Error::AssetNotFound),
};
let contract_admin = Self::get_admin(env.clone())?;
if actor != contract_admin && actor != asset.owner {
return Err(Error::Unauthorized);
}
audit::log_action(&env, &asset_id, actor, action, note);
Ok(())
}
pub fn get_asset_audit_logs(
env: Env,
asset_id: BytesN<32>,
) -> Result<Vec<audit::AuditEntry>, Error> {
Ok(audit::get_asset_log(&env, &asset_id))
}
}
mod tests;