diff --git a/contracts/assetsup/src/error.rs b/contracts/assetsup/src/error.rs
new file mode 100644
index 0000000..42fa4e3
--- /dev/null
+++ b/contracts/assetsup/src/error.rs
@@ -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(()));
+ }
+}
diff --git a/contracts/assetsup/src/errors.rs b/contracts/assetsup/src/errors.rs
deleted file mode 100644
index c872c70..0000000
--- a/contracts/assetsup/src/errors.rs
+++ /dev/null
@@ -1,8 +0,0 @@
-use soroban_sdk::contracterror;
-
-#[contracterror]
-#[derive(Copy, Clone, Debug, Eq, PartialEq)]
-pub enum ContractError {
- AssetAlreadyExists = 1,
- AssetNotFound = 2,
-}
diff --git a/contracts/assetsup/src/lib.rs b/contracts/assetsup/src/lib.rs
index 2c9b33a..7957963 100644
--- a/contracts/assetsup/src/lib.rs
+++ b/contracts/assetsup/src/lib.rs
@@ -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::*;
@@ -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
{
+ 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();
@@ -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 {
+ pub fn get_asset(env: Env, asset_id: BytesN<32>) -> Result {
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),
}
}
}
diff --git a/contracts/assetsup/src/tests/initialize.rs b/contracts/assetsup/src/tests/initialize.rs
index a1b0a21..05e596d 100644
--- a/contracts/assetsup/src/tests/initialize.rs
+++ b/contracts/assetsup/src/tests/initialize.rs
@@ -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();
+}
diff --git a/contracts/assetsup/test_snapshots/tests/asset/test_register_and_get_asset_success.1.json b/contracts/assetsup/test_snapshots/tests/asset/test_register_and_get_asset_success.1.json
index 1d4890f..b10000c 100644
--- a/contracts/assetsup/test_snapshots/tests/asset/test_register_and_get_asset_success.1.json
+++ b/contracts/assetsup/test_snapshots/tests/asset/test_register_and_get_asset_success.1.json
@@ -23,7 +23,7 @@
"val": {
"vec": [
{
- "symbol": "IT"
+ "symbol": "Digital"
}
]
}
@@ -199,7 +199,7 @@
"val": {
"vec": [
{
- "symbol": "IT"
+ "symbol": "Digital"
}
]
}
diff --git a/contracts/assetsup/test_snapshots/tests/asset/test_register_asset_duplicate.1.json b/contracts/assetsup/test_snapshots/tests/asset/test_register_asset_duplicate.1.json
index 591423c..bf7f439 100644
--- a/contracts/assetsup/test_snapshots/tests/asset/test_register_asset_duplicate.1.json
+++ b/contracts/assetsup/test_snapshots/tests/asset/test_register_asset_duplicate.1.json
@@ -23,7 +23,7 @@
"val": {
"vec": [
{
- "symbol": "Furniture"
+ "symbol": "Physical"
}
]
}
@@ -199,7 +199,7 @@
"val": {
"vec": [
{
- "symbol": "Furniture"
+ "symbol": "Physical"
}
]
}
diff --git a/contracts/assetsup/test_snapshots/tests/initialize/test_admin_doesnt_exist.1.json b/contracts/assetsup/test_snapshots/tests/initialize/test_admin_doesnt_exist.1.json
new file mode 100644
index 0000000..5655749
--- /dev/null
+++ b/contracts/assetsup/test_snapshots/tests/initialize/test_admin_doesnt_exist.1.json
@@ -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": []
+}
\ No newline at end of file