Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions contracts/assetsup/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use soroban_sdk::{Env, contracterror, panic_with_error};

#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
pub enum Error {
AlreadyInitialized = 1,
AdminNotFound = 2,
// Asset exist
AssetAlreadyExists = 3,
//Asset not found
AssetNotFound = 4,
// Branch already exists
BranchAlreadyExists = 5,
// Branch not found
BranchNotFound = 6,
// Subscription already exist
SubscriptionAlreadyExists = 7,
// User not authorized
Unauthorized = 8,
// Payment is not valid
InvalidPayment = 9,
}

pub fn handle_error(env: &Env, error: Error) -> ! {
panic_with_error!(env, error);
}

#[allow(dead_code)]
pub fn dummy_function(_env: Env, asset_exists: bool) -> Result<(), Error> {
if asset_exists {
Err(Error::AssetAlreadyExists)
} else {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use soroban_sdk::Env;

#[test]
fn test_dummy_function_asset_exists() {
let env = Env::default();
let result = dummy_function(env.clone(), true);
assert_eq!(result, Err(Error::AssetAlreadyExists));
}

#[test]
fn test_dummy_function_asset_not_exists() {
let env = Env::default();
let result = dummy_function(env.clone(), false);
assert_eq!(result, Ok(()));
}
}
8 changes: 0 additions & 8 deletions contracts/assetsup/src/errors.rs

This file was deleted.

30 changes: 18 additions & 12 deletions contracts/assetsup/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#![no_std]

use crate::error::{Error, handle_error};
use soroban_sdk::{Address, BytesN, Env, contract, contractimpl, contracttype};

pub(crate) mod asset;
pub(crate) mod errors;
pub(crate) mod error;
pub(crate) mod types;

pub use types::*;
Expand All @@ -18,21 +20,28 @@ pub struct AssetUpContract;

#[contractimpl]
impl AssetUpContract {
pub fn initialize(env: Env, admin: Address) {
pub fn initialize(env: Env, admin: Address) -> Result<(), Error> {
admin.require_auth();

if env.storage().persistent().has(&DataKey::Admin) {
panic!("Contract is already initialized");
handle_error(&env, Error::AlreadyInitialized)
}
env.storage().persistent().set(&DataKey::Admin, &admin);
Ok(())
}

pub fn get_admin(env: Env) -> Address {
env.storage().persistent().get(&DataKey::Admin).unwrap()
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<(), errors::ContractError> {
pub fn register_asset(env: Env, asset: asset::Asset) -> Result<(), Error> {
// Access control
asset.owner.require_auth();

Expand All @@ -43,21 +52,18 @@ impl AssetUpContract {
let key = asset::DataKey::Asset(asset.id.clone());
let store = env.storage().persistent();
if store.has(&key) {
return Err(errors::ContractError::AssetAlreadyExists);
return Err(Error::AssetAlreadyExists);
}
store.set(&key, &asset);
Ok(())
}

pub fn get_asset(
env: Env,
asset_id: BytesN<32>,
) -> Result<asset::Asset, errors::ContractError> {
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(errors::ContractError::AssetNotFound),
None => Err(Error::AssetNotFound),
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion contracts/assetsup/src/tests/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ fn test_initialize() {
}

#[test]
#[should_panic]
#[should_panic(expected = "Error(Contract, #1)")]
fn test_initialize_panic() {
let (_env, client, admin) = setup_test_environment();
client.initialize(&admin);
client.initialize(&admin);
}

#[test]
#[should_panic(expected = "Error(Contract, #2)")]
fn test_admin_doesnt_exist() {
let (_env, client, _) = setup_test_environment();
client.get_admin();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"val": {
"vec": [
{
"symbol": "IT"
"symbol": "Digital"
}
]
}
Expand Down Expand Up @@ -199,7 +199,7 @@
"val": {
"vec": [
{
"symbol": "IT"
"symbol": "Digital"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"val": {
"vec": [
{
"symbol": "Furniture"
"symbol": "Physical"
}
]
}
Expand Down Expand Up @@ -199,7 +199,7 @@
"val": {
"vec": [
{
"symbol": "Furniture"
"symbol": "Physical"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"generators": {
"address": 2,
"nonce": 0
},
"auth": [
[],
[]
],
"ledger": {
"protocol_version": 22,
"sequence_number": 0,
"timestamp": 0,
"network_id": "0000000000000000000000000000000000000000000000000000000000000000",
"base_reserve": 0,
"min_persistent_entry_ttl": 4096,
"min_temp_entry_ttl": 16,
"max_entry_ttl": 6312000,
"ledger_entries": [
[
{
"contract_data": {
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
"key": "ledger_key_contract_instance",
"durability": "persistent"
}
},
[
{
"last_modified_ledger_seq": 0,
"data": {
"contract_data": {
"ext": "v0",
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
"key": "ledger_key_contract_instance",
"durability": "persistent",
"val": {
"contract_instance": {
"executable": {
"wasm": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
},
"storage": null
}
}
}
},
"ext": "v0"
},
4095
]
],
[
{
"contract_code": {
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
}
},
[
{
"last_modified_ledger_seq": 0,
"data": {
"contract_code": {
"ext": "v0",
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"code": ""
}
},
"ext": "v0"
},
4095
]
]
]
},
"events": []
}