From df30bdae7ea2b374cbe09bb4a88db63318ba7975 Mon Sep 17 00:00:00 2001 From: Kaloyan Gangov <6922910+gangov@users.noreply.github.com> Date: Thu, 22 Aug 2024 10:20:09 +0300 Subject: [PATCH] refactors --- contracts/auctions/src/contract.rs | 4 +- contracts/auctions/src/lib.rs | 1 - contracts/auctions/src/storage.rs | 71 ++++++++++++++++++++++++++++- contracts/auctions/src/utils.rs | 73 ------------------------------ 4 files changed, 72 insertions(+), 77 deletions(-) delete mode 100644 contracts/auctions/src/utils.rs diff --git a/contracts/auctions/src/contract.rs b/contracts/auctions/src/contract.rs index a1eaa6ad..58a56077 100644 --- a/contracts/auctions/src/contract.rs +++ b/contracts/auctions/src/contract.rs @@ -3,11 +3,11 @@ use soroban_sdk::{contract, contractimpl, log, token, Address, Env, Vec}; use crate::{ collection, error::ContractError, - storage::{Auction, AuctionStatus, ItemInfo, BUMP_AMOUNT, LIFETIME_THRESHOLD}, - utils::{ + storage::{ distribute_funds, generate_auction_id, get_auction_by_id, update_auction, validate_input_params, }, + storage::{Auction, AuctionStatus, ItemInfo, BUMP_AMOUNT, LIFETIME_THRESHOLD}, }; #[contract] diff --git a/contracts/auctions/src/lib.rs b/contracts/auctions/src/lib.rs index 85f53b67..c8621566 100644 --- a/contracts/auctions/src/lib.rs +++ b/contracts/auctions/src/lib.rs @@ -3,7 +3,6 @@ mod contract; mod error; mod storage; -mod utils; #[cfg(test)] mod test; diff --git a/contracts/auctions/src/storage.rs b/contracts/auctions/src/storage.rs index fc77884f..131ec69a 100644 --- a/contracts/auctions/src/storage.rs +++ b/contracts/auctions/src/storage.rs @@ -1,4 +1,6 @@ -use soroban_sdk::{contracttype, Address}; +use soroban_sdk::{contracttype, log, panic_with_error, token, Address, Env}; + +use crate::error::ContractError; // Values used to extend the TTL of storage pub const DAY_IN_LEDGERS: u32 = 17280; @@ -42,3 +44,70 @@ pub enum AuctionStatus { Cancelled, Paused, } + +pub fn generate_auction_id(env: &Env) -> Result { + let id = env + .storage() + .instance() + .get::<_, u64>(&DataKey::AuctionId) + .unwrap_or_default() + + 1u64; + env.storage().instance().set(&DataKey::AuctionId, &id); + env.storage() + .instance() + .extend_ttl(LIFETIME_THRESHOLD, BUMP_AMOUNT); + + Ok(id) +} + +pub fn distribute_funds(env: &Env, auction: &Auction) -> Result<(), ContractError> { + let highest_bidder = auction.highest_bidder.clone(); + let amount_due = auction.highest_bid.unwrap_or_else(|| { + log!( + env, + "Auction: Distribute Funds: Missing value for highest bid." + ); + panic_with_error!(env, ContractError::MissingHighestBid); + }); + + let rcpt = auction.seller.clone(); + + let token = token::Client::new(env, &auction.currency); + token.transfer(&highest_bidder, &rcpt, &(amount_due as i128)); + + Ok(()) +} + +pub fn get_auction_by_id(env: &Env, auction_id: u64) -> Result { + env.storage() + .instance() + .get(&auction_id) + .unwrap_or_else(|| { + log!(env, "Auction: Get auction by id: Auction not present"); + panic_with_error!(&env, ContractError::AuctionIdNotFound); + }) +} + +pub fn update_auction(env: &Env, id: u64, auction: Auction) -> Result<(), ContractError> { + if id != auction.id { + log!(&env, "Auction update auction: Id missmatch"); + panic_with_error!(&env, ContractError::IDMissmatch); + } + env.storage().instance().set(&auction.id, &auction); + env.storage() + .instance() + .extend_ttl(LIFETIME_THRESHOLD, BUMP_AMOUNT); + + Ok(()) +} + +pub fn validate_input_params(env: &Env, values_to_check: &[&u64]) -> Result<(), ContractError> { + values_to_check.iter().for_each(|i| { + if i < &&1 { + log!(&env, "Auction: Validate input: Invalid inputs used"); + panic_with_error!(&env, ContractError::InvalidInputs); + } + }); + + Ok(()) +} diff --git a/contracts/auctions/src/utils.rs b/contracts/auctions/src/utils.rs deleted file mode 100644 index 916ddfc8..00000000 --- a/contracts/auctions/src/utils.rs +++ /dev/null @@ -1,73 +0,0 @@ -use soroban_sdk::{log, panic_with_error, token, Env}; - -use crate::{ - error::ContractError, - storage::{Auction, DataKey, BUMP_AMOUNT, LIFETIME_THRESHOLD}, -}; - -pub fn generate_auction_id(env: &Env) -> Result { - let id = env - .storage() - .instance() - .get::<_, u64>(&DataKey::AuctionId) - .unwrap_or_default() - + 1u64; - env.storage().instance().set(&DataKey::AuctionId, &id); - env.storage() - .instance() - .extend_ttl(LIFETIME_THRESHOLD, BUMP_AMOUNT); - - Ok(id) -} - -pub fn distribute_funds(env: &Env, auction: &Auction) -> Result<(), ContractError> { - let highest_bidder = auction.highest_bidder.clone(); - let amount_due = auction.highest_bid.unwrap_or_else(|| { - log!( - env, - "Auction: Distribute Funds: Missing value for highest bid." - ); - panic_with_error!(env, ContractError::MissingHighestBid); - }); - - let rcpt = auction.seller.clone(); - - let token = token::Client::new(env, &auction.currency); - token.transfer(&highest_bidder, &rcpt, &(amount_due as i128)); - - Ok(()) -} - -pub fn get_auction_by_id(env: &Env, auction_id: u64) -> Result { - env.storage() - .instance() - .get(&auction_id) - .unwrap_or_else(|| { - log!(env, "Auction: Get auction by id: Auction not present"); - panic_with_error!(&env, ContractError::AuctionIdNotFound); - }) -} - -pub fn update_auction(env: &Env, id: u64, auction: Auction) -> Result<(), ContractError> { - if id != auction.id { - log!(&env, "Auction update auction: Id missmatch"); - panic_with_error!(&env, ContractError::IDMissmatch); - } - env.storage().instance().set(&auction.id, &auction); - env.storage() - .instance() - .extend_ttl(LIFETIME_THRESHOLD, BUMP_AMOUNT); - - Ok(()) -} - -pub fn validate_input_params(env: &Env, values_to_check: &[&u64]) -> Result<(), ContractError> { - values_to_check.iter().for_each(|i| { - if i < &&1 { - log!(&env, "Auction: Validate input: Invalid inputs used"); - panic_with_error!(&env, ContractError::InvalidInputs); - } - }); - - Ok(()) -}