diff --git a/contracts/auctions/src/contract.rs b/contracts/auctions/src/contract.rs index 0aa3267..30d31b9 100644 --- a/contracts/auctions/src/contract.rs +++ b/contracts/auctions/src/contract.rs @@ -4,10 +4,10 @@ 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, get_auction_by_id, get_auctions, get_auctions_by_seller_id, + get_config, get_highest_bid, is_initialized, save_admin, save_auction_by_id, + save_auction_by_seller, save_config, set_highest_bid, set_initialized, update_admin, + validate_input_params, Auction, AuctionStatus, Config, HighestBid, ItemInfo, }, token, }; @@ -22,6 +22,7 @@ impl MarketplaceContract { env: Env, admin: Address, auction_token: Address, + auction_creation_fee: u128, ) -> Result<(), ContractError> { admin.require_auth(); @@ -31,7 +32,13 @@ impl MarketplaceContract { } save_admin(&env, &admin); - save_auction_token(&env, auction_token); + + let config = Config { + auction_token, + auction_creation_fee, + }; + + save_config(&env, config); set_initialized(&env); @@ -59,7 +66,7 @@ impl MarketplaceContract { ]; validate_input_params(&env, &input_values[..])?; - let currency = get_auction_token(&env)?; + let auction_token = get_config(&env)?.auction_token; let nft_client = collection::Client::new(&env, &item_info.collection_addr); let item_balance = nft_client.balance_of(&seller, &item_info.item_id); @@ -88,7 +95,7 @@ impl MarketplaceContract { highest_bid: None, end_time, status: AuctionStatus::Active, - currency, + auction_token, }; save_auction(&env, &auction)?; @@ -130,7 +137,7 @@ impl MarketplaceContract { return Err(ContractError::InvalidBidder); } - let token_client = token::Client::new(&env, &auction.currency); + let token_client = token::Client::new(&env, &auction.auction_token); match auction.highest_bid { Some(current_highest_bid) if bid_amount > current_highest_bid => { @@ -192,7 +199,7 @@ impl MarketplaceContract { return Err(ContractError::AuctionNotFinished); } - let token_client = token::Client::new(&env, &auction.currency); + let token_client = token::Client::new(&env, &auction.auction_token); let highest_bid = get_highest_bid(&env, auction_id)?; // check if minimum price has been reached @@ -274,7 +281,7 @@ impl MarketplaceContract { let old_highest_bid = get_highest_bid(&env, auction_id)?; - let token = token::Client::new(&env, &auction.currency); + let token = token::Client::new(&env, &auction.auction_token); // refund only when there is some previous highest bid if old_highest_bid.bid > 0 { diff --git a/contracts/auctions/src/error.rs b/contracts/auctions/src/error.rs index 1915b21..c5ae3fe 100644 --- a/contracts/auctions/src/error.rs +++ b/contracts/auctions/src/error.rs @@ -21,5 +21,5 @@ pub enum ContractError { InvalidBidder = 14, AdminNotFound = 15, NoBidFound = 16, - CurrencyNotFound = 17, + ConfigNotFound = 17, } diff --git a/contracts/auctions/src/storage.rs b/contracts/auctions/src/storage.rs index c75dbce..6bdc2c3 100644 --- a/contracts/auctions/src/storage.rs +++ b/contracts/auctions/src/storage.rs @@ -1,6 +1,6 @@ use soroban_sdk::{contracttype, log, panic_with_error, vec, Address, Env, Vec}; -use crate::error::ContractError; +use crate::{collection::DataKey, error::ContractError}; // Values used to extend the TTL of storage pub const DAY_IN_LEDGERS: u32 = 17280; @@ -20,7 +20,7 @@ pub enum DataKey { AuctionId, AllAuctions, HighestBid(u64), - AuctionToken, + Config, } #[derive(Clone, Debug, PartialEq)] @@ -41,7 +41,7 @@ pub struct Auction { pub highest_bid: Option, pub end_time: u64, pub status: AuctionStatus, - pub currency: Address, + pub auction_token: Address, } #[derive(Clone, Debug, PartialEq)] @@ -60,6 +60,13 @@ pub enum AuctionStatus { Paused, } +#[derive(Clone, Debug)] +#[contracttype] +pub struct Config { + pub auction_token: Address, + pub auction_creation_fee: u128, +} + pub fn generate_auction_id(env: &Env) -> Result { let id = env .storage() @@ -269,37 +276,60 @@ pub fn set_highest_bid( Ok(()) } -pub fn save_auction_token(env: &Env, auction_token: Address) { - env.storage() - .persistent() - .set(&DataKey::AuctionToken, &auction_token); - +pub fn save_config(env: &Env, config: Config) { + env.storage().persistent().set(&DataKey::Config, &config); env.storage() .persistent() - .extend_ttl(&DataKey::AuctionToken, LIFETIME_THRESHOLD, BUMP_AMOUNT); + .extend_ttl(&DataKey::Config, LIFETIME_THRESHOLD, BUMP_AMOUNT); } -pub fn get_auction_token(env: &Env) -> Result { - let auction_token = env +pub fn get_config(env: &Env) -> Result { + let config = env .storage() .persistent() - .get(&DataKey::AuctionToken) - .ok_or(ContractError::CurrencyNotFound)?; + .get(&DataKey::Config) + .ok_or(ContractError::ConfigNotFound); - env.storage() - .persistent() - .has(&DataKey::AuctionToken) - .then(|| { - env.storage().persistent().extend_ttl( - &DataKey::AuctionToken, - LIFETIME_THRESHOLD, - BUMP_AMOUNT, - ); - }); + env.storage().persistent().has(&DataKey::Config).then(|| { + env.storage() + .persistent() + .extend_ttl(&DataKey::Config, LIFETIME_THRESHOLD, BUMP_AMOUNT) + }); - Ok(auction_token) + Ok(config)? } +//pub fn save_auction_token(env: &Env, auction_token: Address) { +// env.storage() +// .persistent() +// .set(&DataKey::AuctionToken, &auction_token); +// +// env.storage() +// .persistent() +// .extend_ttl(&DataKey::AuctionToken, LIFETIME_THRESHOLD, BUMP_AMOUNT); +//} +// +//pub fn get_auction_token(env: &Env) -> Result { +// let auction_token = env +// .storage() +// .persistent() +// .get(&DataKey::AuctionToken) +// .ok_or(ContractError::CurrencyNotFound)?; +// +// env.storage() +// .persistent() +// .has(&DataKey::AuctionToken) +// .then(|| { +// env.storage().persistent().extend_ttl( +// &DataKey::AuctionToken, +// LIFETIME_THRESHOLD, +// BUMP_AMOUNT, +// ); +// }); +// +// Ok(auction_token) +//} + #[cfg(test)] mod test { use soroban_sdk::Env;