Skip to content

Commit

Permalink
Merge pull request #25 from Phoenix-Protocol-Group/24-deployer-change…
Browse files Browse the repository at this point in the history
…-return-type-of-queries

Deployer: update return type of query
  • Loading branch information
gangov authored Sep 18, 2024
2 parents 4fa8001 + a63d829 commit caad7ef
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
27 changes: 22 additions & 5 deletions contracts/deployer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl CollectionsDeployer {
let _: Val = env.invoke_contract(&deployed_collection, &init_fn, init_fn_args);

save_collection_with_generic_key(&env, name.clone());
save_collection_with_admin_address_as_key(&env, name, admin);
save_collection_with_admin_address_as_key(&env, admin, deployed_collection.clone(), name);

deployed_collection
}
Expand Down Expand Up @@ -92,7 +92,7 @@ impl CollectionsDeployer {
pub fn query_collection_by_creator(
env: &Env,
creator: Address,
) -> Result<Vec<String>, ContractError> {
) -> Result<Vec<CollectionByCreatorResponse>, ContractError> {
let data_key = DataKey::Creator(creator);
let maybe_collections = env
.storage()
Expand All @@ -112,6 +112,13 @@ impl CollectionsDeployer {

// ---------- Storage types ----------

#[contracttype]
#[derive(Clone, Debug)]
pub struct CollectionByCreatorResponse {
collection: Address,
name: String,
}

#[contracttype]
#[derive(Clone)]
pub enum DataKey {
Expand Down Expand Up @@ -197,16 +204,26 @@ pub fn save_collection_with_generic_key(env: &Env, name: String) {
);
}

pub fn save_collection_with_admin_address_as_key(env: &Env, name: String, creator: Address) {
pub fn save_collection_with_admin_address_as_key(
env: &Env,
creator: Address,
collection_addr: Address,
name: String,
) {
let data_key = DataKey::Creator(creator);

let mut existent_collection: Vec<String> = env
let mut existent_collection: Vec<CollectionByCreatorResponse> = env
.storage()
.persistent()
.get(&data_key)
.unwrap_or(vec![&env]);

existent_collection.push_back(name);
let new_collection = CollectionByCreatorResponse {
collection: collection_addr,
name: name.clone(),
};

existent_collection.push_back(new_collection);

env.storage()
.persistent()
Expand Down
32 changes: 24 additions & 8 deletions contracts/deployer/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{CollectionsDeployer, CollectionsDeployerClient};
use crate::{CollectionByCreatorResponse, CollectionsDeployer, CollectionsDeployerClient};
#[cfg(test)]
use soroban_sdk::{testutils::Address as _, vec, Address, BytesN, Env, String};

Expand Down Expand Up @@ -33,12 +33,12 @@ fn test_deploy_collection_from_contract() {
let name = String::from_str(&env, "Stellar Kitties");
let symbol = String::from_str(&env, "STK");

client.deploy_new_collection(&salt, &creator, &name, &symbol);
let collection = client.deploy_new_collection(&salt, &creator, &name, &symbol);

assert_eq!(client.query_all_collections(), vec![&env, name.clone()]);
assert_eq!(
client.query_collection_by_creator(&creator),
vec![&env, name]
vec![&env, CollectionByCreatorResponse { collection, name }]
);
}

Expand Down Expand Up @@ -71,19 +71,19 @@ fn test_deploy_multiple_collections() {
let third_collection_name = String::from_str(&env, "Horror of Cthulhu");
let third_collection_symbol = String::from_str(&env, "HoC");

client.deploy_new_collection(
let first = client.deploy_new_collection(
&first_salt,
&creator,
&first_collection_name,
&first_collection_symbol,
);
client.deploy_new_collection(
let second = client.deploy_new_collection(
&second_salt,
&creator,
&second_collection_name,
&second_collection_symbol,
);
client.deploy_new_collection(
let third = client.deploy_new_collection(
&third_salt,
&bob,
&third_collection_name,
Expand All @@ -102,12 +102,28 @@ fn test_deploy_multiple_collections() {

assert_eq!(
client.query_collection_by_creator(&creator),
vec![&env, first_collection_name, second_collection_name]
vec![
&env,
CollectionByCreatorResponse {
collection: first,
name: first_collection_name,
},
CollectionByCreatorResponse {
collection: second,
name: second_collection_name,
},
]
);

assert_eq!(
client.query_collection_by_creator(&bob),
vec![&env, third_collection_name]
vec![
&env,
CollectionByCreatorResponse {
collection: third,
name: third_collection_name
}
]
);
}

Expand Down

0 comments on commit caad7ef

Please sign in to comment.