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
65 lines (53 loc) · 1.64 KB
/
lib.rs
File metadata and controls
65 lines (53 loc) · 1.64 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
#![no_std]
use soroban_sdk::{Address, BytesN, Env, contract, contractimpl, contracttype};
pub(crate) mod asset;
pub(crate) mod errors;
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) {
admin.require_auth();
if env.storage().persistent().has(&DataKey::Admin) {
panic!("Contract is already initialized");
}
env.storage().persistent().set(&DataKey::Admin, &admin);
}
pub fn get_admin(env: Env) -> Address {
env.storage().persistent().get(&DataKey::Admin).unwrap()
}
// Asset functions
pub fn register_asset(env: Env, asset: asset::Asset) -> Result<(), errors::ContractError> {
// Access control
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(errors::ContractError::AssetAlreadyExists);
}
store.set(&key, &asset);
Ok(())
}
pub fn get_asset(
env: Env,
asset_id: BytesN<32>,
) -> Result<asset::Asset, errors::ContractError> {
let key = asset::DataKey::Asset(asset_id);
let store = env.storage().persistent();
match store.get::<_, asset::Asset>(&key) {
Some(a) => Ok(a),
None => Err(errors::ContractError::AssetNotFound),
}
}
}
mod tests;