Skip to content

Commit

Permalink
refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
gangov committed Aug 22, 2024
1 parent 3f9e514 commit df30bda
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 77 deletions.
4 changes: 2 additions & 2 deletions contracts/auctions/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 0 additions & 1 deletion contracts/auctions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
mod contract;
mod error;
mod storage;
mod utils;

#[cfg(test)]
mod test;
Expand Down
71 changes: 70 additions & 1 deletion contracts/auctions/src/storage.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -42,3 +44,70 @@ pub enum AuctionStatus {
Cancelled,
Paused,
}

pub fn generate_auction_id(env: &Env) -> Result<u64, ContractError> {
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<Auction, ContractError> {
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(())
}
73 changes: 0 additions & 73 deletions contracts/auctions/src/utils.rs

This file was deleted.

0 comments on commit df30bda

Please sign in to comment.