diff --git a/contracts/auctions/src/contract.rs b/contracts/auctions/src/contract.rs index 03fbce3..8403372 100644 --- a/contracts/auctions/src/contract.rs +++ b/contracts/auctions/src/contract.rs @@ -4,10 +4,11 @@ use crate::{ collection, error::ContractError, storage::{ - generate_auction_id, get_admin, get_auction_by_id, get_auction_token, get_auctions, - get_auctions_by_seller_id, get_highest_bid, is_initialized, save_admin, save_auction_by_id, - save_auction_by_seller, save_auction_token, set_highest_bid, set_initialized, update_admin, - validate_input_params, Auction, AuctionStatus, HighestBid, ItemInfo, + generate_auction_id, get_admin_old, get_auction_by_id, get_auction_token, get_auctions, + get_auctions_by_seller_id, get_highest_bid, is_initialized, save_admin_old, + save_auction_by_id, save_auction_by_seller, save_auction_token, set_highest_bid, + set_initialized, update_admin, validate_input_params, Auction, AuctionStatus, HighestBid, + ItemInfo, }, token, }; @@ -30,7 +31,7 @@ impl MarketplaceContract { return Err(ContractError::AlreadyInitialized); } - save_admin(&env, &admin); + save_admin_old(&env, &admin); save_auction_token(&env, auction_token); set_initialized(&env); @@ -424,7 +425,7 @@ impl MarketplaceContract { #[allow(dead_code)] pub fn update_admin(env: Env, new_admin: Address) -> Result { - let old_admin = get_admin(&env)?; + let old_admin = get_admin_old(&env)?; old_admin.require_auth(); env.events() @@ -437,7 +438,7 @@ impl MarketplaceContract { #[allow(dead_code)] pub fn upgrade(env: Env, new_wasm_hash: BytesN<32>) -> Result<(), ContractError> { - let admin: Address = get_admin(&env)?; + let admin: Address = get_admin_old(&env)?; admin.require_auth(); env.deployer().update_current_contract_wasm(new_wasm_hash); diff --git a/contracts/auctions/src/storage.rs b/contracts/auctions/src/storage.rs index e633510..8aef45b 100644 --- a/contracts/auctions/src/storage.rs +++ b/contracts/auctions/src/storage.rs @@ -1,4 +1,6 @@ -use soroban_sdk::{contracttype, log, panic_with_error, vec, Address, Env, Vec}; +use soroban_sdk::{ + contracttype, log, panic_with_error, symbol_short, vec, Address, Env, Symbol, Vec, +}; use crate::error::ContractError; @@ -11,6 +13,7 @@ pub const LIFETIME_THRESHOLD: u32 = BUMP_AMOUNT - DAY_IN_LEDGERS; // since we start counting from 1, default would be 1 as well pub const DEFAULT_INDEX: u64 = 1; pub const DEFAULT_LIMIT: u64 = 10; +pub const ADMIN: Symbol = symbol_short!("ADMIN"); #[contracttype] #[derive(Clone)] @@ -198,14 +201,14 @@ pub fn set_initialized(env: &Env) { .set(&DataKey::IsInitialized, &true); } -pub fn save_admin(env: &Env, admin: &Address) { +pub fn save_admin_old(env: &Env, admin: &Address) { env.storage().persistent().set(&DataKey::Admin, &admin); env.storage() .persistent() .extend_ttl(&DataKey::Admin, LIFETIME_THRESHOLD, BUMP_AMOUNT); } -pub fn get_admin(env: &Env) -> Result { +pub fn get_admin_old(env: &Env) -> Result { let admin = env .storage() .persistent() @@ -237,7 +240,7 @@ pub fn get_highest_bid(env: &Env, auction_id: u64) -> Result(env: &Env, admin: &Address) -> TokenClient<'a> { + let token_contract = env.register( + TOKEN_WASM, + ( + admin, + 7_u32, + String::from_val(env, &"name"), + String::from_val(env, &"symbol"), + ), + ); -pub fn deploy_token_contract<'a>(env: &Env, admin: &Address) -> token::Client<'a> { - token::Client::new(env, &env.register_stellar_asset_contract(admin.clone())) + TokenClient::new(env, &token_contract) } pub mod auctions_wasm { @@ -28,8 +41,7 @@ pub fn generate_marketplace_and_collection_client<'a>( name: Option, symbol: Option, ) -> (MarketplaceContractClient<'a>, collection::Client<'a>) { - let mp_client = - MarketplaceContractClient::new(env, &env.register_contract(None, MarketplaceContract {})); + let mp_client = MarketplaceContractClient::new(env, &env.register(MarketplaceContract, ())); mp_client.initialize(admin, auction_token); @@ -38,7 +50,7 @@ pub fn generate_marketplace_and_collection_client<'a>( let name = name.unwrap_or(alt_name); let symbol = symbol.unwrap_or(alt_symbol); - let collection_addr = env.register_contract_wasm(None, collection::WASM); + let collection_addr = env.register(collection::WASM, ()); let collection_client = collection::Client::new(env, &collection_addr); collection_client.initialize(admin, &name, &symbol); @@ -83,7 +95,7 @@ pub fn create_and_initialize_collection<'a>( let collection_addr = env .deployer() .with_address(Address::generate(env), salt) - .deploy(env.deployer().upload_contract_wasm(collection::WASM)); + .deploy_v2(env.deployer().upload_contract_wasm(collection::WASM), ()); let collection_client = collection::Client::new(env, &collection_addr); collection_client.initialize(seller, &collection_name, &collection_symbol); diff --git a/contracts/collections/src/contract.rs b/contracts/collections/src/contract.rs index 6d0ccb9..1da18cd 100644 --- a/contracts/collections/src/contract.rs +++ b/contracts/collections/src/contract.rs @@ -4,10 +4,10 @@ use crate::{ error::ContractError, storage::{ utils::{ - get_admin, get_balance_of, is_initialized, save_admin, save_config, set_initialized, - update_balance_of, + get_admin_old, get_balance_of, is_initialized, save_admin_old, save_config, + set_initialized, update_balance_of, }, - Config, DataKey, OperatorApprovalKey, TransferApprovalKey, URIValue, + Config, DataKey, OperatorApprovalKey, TransferApprovalKey, URIValue, ADMIN, }, ttl::{BUMP_AMOUNT, LIFETIME_THRESHOLD}, }; @@ -36,7 +36,7 @@ impl Collections { } save_config(&env, config)?; - save_admin(&env, &admin)?; + save_admin_old(&env, &admin)?; set_initialized(&env); @@ -95,7 +95,7 @@ impl Collections { operator: Address, approved: bool, ) -> Result<(), ContractError> { - let admin = get_admin(&env)?; + let admin = get_admin_old(&env)?; admin.require_auth(); if admin == operator { @@ -136,7 +136,7 @@ impl Collections { nft_id: u64, approved: bool, ) -> Result<(), ContractError> { - let admin = get_admin(&env)?; + let admin = get_admin_old(&env)?; admin.require_auth(); if admin == operator { @@ -590,7 +590,7 @@ impl Collections { #[allow(dead_code)] pub fn upgrade(env: Env, new_wasm_hash: BytesN<32>) -> Result<(), ContractError> { - let admin: Address = get_admin(&env)?; + let admin: Address = get_admin_old(&env)?; admin.require_auth(); env.deployer().update_current_contract_wasm(new_wasm_hash); @@ -598,8 +598,18 @@ impl Collections { Ok(()) } + #[allow(dead_code)] + pub fn migrate_admin(env: Env) -> Result<(), ContractError> { + let admin: Address = get_admin_old(&env)?; + admin.require_auth(); + + env.storage().instance().set(&ADMIN, &admin); + + Ok(()) + } + pub fn show_admin(env: &Env) -> Result { - let maybe_admin = crate::storage::utils::get_admin(env)?; + let maybe_admin = crate::storage::utils::get_admin_old(env)?; Ok(maybe_admin) } @@ -609,7 +619,7 @@ impl Collections { } fn is_authorized_for_transfer(env: &Env, sender: &Address, nft_id: u64) -> bool { - let admin = get_admin(env).expect("no admin found"); + let admin = get_admin_old(env).expect("no admin found"); admin == sender.clone() || Self::is_approved_for_all(env.clone(), admin.clone(), sender.clone()) @@ -617,7 +627,7 @@ impl Collections { } fn is_authorized_for_all(env: &Env, sender: &Address) -> bool { - let admin = get_admin(env).expect("no admin found"); + let admin = get_admin_old(env).expect("no admin found"); admin == sender.clone() || Self::is_approved_for_all(env.clone(), admin, sender.clone()) } diff --git a/contracts/collections/src/storage.rs b/contracts/collections/src/storage.rs index a3e23c2..8622f6b 100644 --- a/contracts/collections/src/storage.rs +++ b/contracts/collections/src/storage.rs @@ -1,9 +1,11 @@ -use soroban_sdk::{contracttype, Address, Bytes, String}; +use soroban_sdk::{contracttype, symbol_short, Address, Bytes, String, Symbol}; type NftId = u64; type TokenId = u64; type Balance = u64; +pub const ADMIN: Symbol = symbol_short!("ADMIN"); + // Struct to represent the operator approval status #[derive(Clone)] #[contracttype] @@ -60,7 +62,7 @@ pub mod utils { use crate::error::ContractError; - use super::{Balance, Config, DataKey, TokenId}; + use super::{Balance, Config, DataKey, TokenId, ADMIN}; pub fn get_balance_of(env: &Env, owner: &Address, id: u64) -> Result { let balance_map: Map = env @@ -114,13 +116,17 @@ pub mod utils { Ok(config) } - pub fn save_admin(env: &Env, admin: &Address) -> Result<(), ContractError> { + pub fn save_admin_old(env: &Env, admin: &Address) -> Result<(), ContractError> { env.storage().persistent().set(&DataKey::Admin, &admin); Ok(()) } - pub fn get_admin(env: &Env) -> Result { + pub fn _save_admin(env: &Env, admin: &Address) { + env.storage().instance().set(&ADMIN, admin); + } + + pub fn get_admin_old(env: &Env) -> Result { let admin = env .storage() .persistent() @@ -129,6 +135,17 @@ pub mod utils { Ok(admin) } + + pub fn _get_admin(env: &Env) -> Result { + let admin = env + .storage() + .instance() + .get(&ADMIN) + .ok_or(ContractError::AdminNotSet)?; + + Ok(admin) + } + pub fn is_initialized(env: &Env) -> bool { env.storage() .persistent()