Skip to content

Commit

Permalink
collections - new key for admin prep
Browse files Browse the repository at this point in the history
  • Loading branch information
gangov committed Dec 4, 2024
1 parent b5ceaf1 commit 49ea703
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
30 changes: 20 additions & 10 deletions contracts/collections/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -36,7 +36,7 @@ impl Collections {
}

save_config(&env, config)?;
save_admin(&env, &admin)?;
save_admin_old(&env, &admin)?;

set_initialized(&env);

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -590,16 +590,26 @@ 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);

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<Address, ContractError> {
let maybe_admin = crate::storage::utils::get_admin(env)?;
let maybe_admin = crate::storage::utils::get_admin_old(env)?;
Ok(maybe_admin)
}

Expand All @@ -609,15 +619,15 @@ 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())
|| Self::is_approved_for_transfer(env.clone(), admin.clone(), sender.clone(), nft_id)
}

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())
}
Expand Down
25 changes: 21 additions & 4 deletions contracts/collections/src/storage.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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<u64, ContractError> {
let balance_map: Map<TokenId, Balance> = env
Expand Down Expand Up @@ -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<Address, ContractError> {
pub fn _save_admin(env: &Env, admin: &Address) {
env.storage().instance().set(&ADMIN, admin);
}

pub fn get_admin_old(env: &Env) -> Result<Address, ContractError> {
let admin = env
.storage()
.persistent()
Expand All @@ -129,6 +135,17 @@ pub mod utils {

Ok(admin)
}

pub fn _get_admin(env: &Env) -> Result<Address, ContractError> {
let admin = env
.storage()
.instance()
.get(&ADMIN)
.ok_or(ContractError::AdminNotSet)?;

Ok(admin)
}

pub fn is_initialized(env: &Env) -> bool {
env.storage()
.persistent()
Expand Down

0 comments on commit 49ea703

Please sign in to comment.