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

All: migrate admin datakey #33

Merged
merged 2 commits into from
Dec 5, 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
15 changes: 8 additions & 7 deletions contracts/auctions/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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);
Expand Down Expand Up @@ -424,7 +425,7 @@ impl MarketplaceContract {

#[allow(dead_code)]
pub fn update_admin(env: Env, new_admin: Address) -> Result<Address, ContractError> {
let old_admin = get_admin(&env)?;
let old_admin = get_admin_old(&env)?;
old_admin.require_auth();

env.events()
Expand All @@ -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);
Expand Down
11 changes: 7 additions & 4 deletions contracts/auctions/src/storage.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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)]
Expand Down Expand Up @@ -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<Address, ContractError> {
pub fn get_admin_old(env: &Env) -> Result<Address, ContractError> {
let admin = env
.storage()
.persistent()
Expand Down Expand Up @@ -237,7 +240,7 @@ pub fn get_highest_bid(env: &Env, auction_id: u64) -> Result<HighestBid, Contrac
.unwrap_or(HighestBid {
bid: 0,
// I know
bidder: get_admin(env)?,
bidder: get_admin_old(env)?,
});

env.storage()
Expand Down
28 changes: 20 additions & 8 deletions contracts/auctions/src/test/setup.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
use soroban_sdk::{testutils::Address as _, xdr::ToXdr, Address, Bytes, Env, String};
use soroban_sdk::{
testutils::Address as _, token::TokenClient, xdr::ToXdr, Address, Bytes, Env, FromVal, String,
};

use crate::{
collection::{self, Client},
contract::{MarketplaceContract, MarketplaceContractClient},
storage::ItemInfo,
token,
};

pub const WEEKLY: u64 = 604_800u64;
pub const DAY: u64 = 86_400u64;
pub const FOUR_HOURS: u64 = 14_400u64;
const TOKEN_WASM: &[u8] =
include_bytes!("../../../../target/wasm32-unknown-unknown/release/soroban_token_contract.wasm");

pub fn deploy_token_contract<'a>(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 {
Expand All @@ -28,8 +41,7 @@ pub fn generate_marketplace_and_collection_client<'a>(
name: Option<String>,
symbol: Option<String>,
) -> (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);

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
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);
ueco-jb marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading