-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
174 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,21 @@ | ||
use soroban_sdk::{testutils::Address as _, Address, Bytes, Env, String}; | ||
use soroban_sdk::{testutils::Address as _, Address, Env, String}; | ||
|
||
use crate::{ | ||
contract::{Collections, CollectionsClient}, | ||
storage::URIValue, | ||
}; | ||
use crate::contract::{Collections, CollectionsClient}; | ||
|
||
pub fn initialize_collection_contract<'a>( | ||
env: &Env, | ||
admin: Option<&Address>, | ||
name: Option<&String>, | ||
image: Option<&URIValue>, | ||
) -> CollectionsClient<'a> { | ||
let collections = CollectionsClient::new(env, &env.register_contract(None, Collections {})); | ||
|
||
let alt_admin = &Address::generate(env); | ||
let alt_name = &String::from_str(env, "Stellar kitties"); | ||
let alt_image = URIValue { | ||
uri: Bytes::from_slice(env, &[64]), | ||
}; | ||
|
||
let admin = admin.unwrap_or(alt_admin); | ||
let name = name.unwrap_or(alt_name); | ||
let image = image.unwrap_or(&alt_image); | ||
|
||
collections.initialize(admin, name, image); | ||
collections.initialize(admin, name); | ||
|
||
collections | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "phoenix-multisig-deployer" | ||
version = { workspace = true } | ||
authors = ["Jakub <[email protected]", "Kaloyan <[email protected]"] | ||
repository = { workspace = true } | ||
edition = { workspace = true } | ||
license = { workspace = true } | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[features] | ||
testutils = ["soroban-sdk/testutils"] | ||
|
||
[dependencies] | ||
soroban-sdk = { workspace = true } | ||
|
||
[dev_dependencies] | ||
soroban-sdk = { workspace = true, features = ["testutils"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
default: all | ||
|
||
all: lint build test | ||
|
||
test: build | ||
$(MAKE) -C ../multisig build || break; | ||
cargo test | ||
|
||
build: | ||
cargo build --target wasm32-unknown-unknown --release | ||
|
||
lint: fmt clippy | ||
|
||
fmt: | ||
cargo fmt --all | ||
|
||
clippy: build | ||
cargo clippy --all-targets -- -D warnings | ||
|
||
clean: | ||
cargo clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#![no_std] | ||
|
||
use soroban_sdk::{ | ||
contract, contractimpl, contractmeta, contracttype, log, vec, Address, BytesN, Env, IntoVal, | ||
String, Symbol, Val, Vec, | ||
}; | ||
|
||
// Metadata that is added on to the WASM custom section | ||
contractmeta!( | ||
key = "Description", | ||
val = "Phoenix Multisig Deployer Contract" | ||
); | ||
|
||
#[contract] | ||
pub struct CollectionsDeployer; | ||
|
||
#[contractimpl] | ||
impl CollectionsDeployer { | ||
#[allow(dead_code)] | ||
pub fn initialize(env: Env, collections_wasm_hash: BytesN<32>) { | ||
if is_initialized(&env) { | ||
log!( | ||
&env, | ||
"Multisig Deployer: Initialize: initializing the contract twice is not allowed" | ||
); | ||
panic!("Multisig Deployer: Initialize: initializing the contract twice is not allowed"); | ||
} | ||
set_initialized(&env); | ||
|
||
set_wasm_hash(&env, &collections_wasm_hash); | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn deploy_new_collection( | ||
env: Env, | ||
salt: BytesN<32>, | ||
admin: Address, | ||
name: String, | ||
) -> Address { | ||
admin.require_auth(); | ||
let collections_wasm_hash = get_wasm_hash(&env); | ||
|
||
let deployed_multisig = env | ||
.deployer() | ||
.with_address(admin.clone(), salt) | ||
.deploy(collections_wasm_hash); | ||
|
||
let init_fn = Symbol::new(&env, "initialize"); | ||
let init_fn_args: Vec<Val> = vec![&env, admin.into_val(&env), name.into_val(&env)]; | ||
let _: Val = env.invoke_contract(&deployed_multisig, &init_fn, init_fn_args); | ||
|
||
save_collection_with_generic_key(&env, name.clone()); | ||
save_collection_with_admin_key(&env, name, admin); | ||
|
||
deployed_multisig | ||
} | ||
} | ||
|
||
// ---------- Storage types ---------- | ||
|
||
#[contracttype] | ||
#[derive(Clone)] | ||
pub enum DataKey { | ||
IsInitialized, | ||
CollectionsWasmHash, | ||
AllCollections, | ||
AdminId(Address), | ||
} | ||
|
||
pub fn set_initialized(env: &Env) { | ||
env.storage().instance().set(&DataKey::IsInitialized, &()); | ||
} | ||
|
||
pub fn is_initialized(env: &Env) -> bool { | ||
env.storage() | ||
.instance() | ||
.get::<_, ()>(&DataKey::IsInitialized) | ||
.is_some() | ||
} | ||
|
||
pub fn set_wasm_hash(env: &Env, hash: &BytesN<32>) { | ||
env.storage() | ||
.instance() | ||
.set(&DataKey::CollectionsWasmHash, hash); | ||
} | ||
|
||
pub fn get_wasm_hash(env: &Env) -> BytesN<32> { | ||
env.storage() | ||
.instance() | ||
.get(&DataKey::CollectionsWasmHash) | ||
.unwrap() | ||
} | ||
|
||
pub fn save_collection_with_generic_key(env: &Env, name: String) { | ||
let mut existent_collection: Vec<String> = env | ||
.storage() | ||
.persistent() | ||
.get(&DataKey::AllCollections) | ||
.unwrap_or(vec![&env]); | ||
|
||
existent_collection.push_back(name); | ||
|
||
env.storage() | ||
.persistent() | ||
.set(&DataKey::AllCollections, &existent_collection); | ||
} | ||
|
||
pub fn save_collection_with_admin_key(env: &Env, name: String, admin: Address) { | ||
let mut existent_collection: Vec<String> = env | ||
.storage() | ||
.persistent() | ||
.get(&DataKey::AdminId(admin.clone())) | ||
.unwrap_or(vec![&env]); | ||
|
||
existent_collection.push_back(name); | ||
|
||
env.storage() | ||
.persistent() | ||
.set(&DataKey::AdminId(admin), &existent_collection); | ||
} | ||
|