Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deployer: update return type of query #25

Merged
merged 2 commits into from
Sep 18, 2024
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
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
Loading