Skip to content

Commit

Permalink
save auction by seller
Browse files Browse the repository at this point in the history
  • Loading branch information
gangov committed Aug 22, 2024
1 parent df30bda commit a1a2b02
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
23 changes: 13 additions & 10 deletions contracts/auctions/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use crate::{
collection,
error::ContractError,
storage::{
distribute_funds, generate_auction_id, get_auction_by_id, update_auction,
validate_input_params,
distribute_funds, generate_auction_id, get_auction_by_id, get_auction_by_seller,
get_auctions_by_seller_id, save_auction_by_id, save_auction_by_seller, update_auction,
validate_input_params, Auction, AuctionStatus, ItemInfo,
},
storage::{Auction, AuctionStatus, ItemInfo, BUMP_AMOUNT, LIFETIME_THRESHOLD},
};

#[contract]
Expand Down Expand Up @@ -54,16 +54,14 @@ impl MarketplaceContract {
seller: seller.clone(),
highest_bid: None,
// we use the seller's address as we cannot add `Option<Address>` in the struct
highest_bidder: seller,
highest_bidder: seller.clone(),
end_time,
status: AuctionStatus::Active,
currency,
};

env.storage().instance().set(&id, &auction);
env.storage()
.instance()
.extend_ttl(LIFETIME_THRESHOLD, BUMP_AMOUNT);
save_auction_by_id(&env, id, &auction)?;
save_auction_by_seller(&env, &seller, &auction)?;

Ok(auction)
}
Expand Down Expand Up @@ -237,8 +235,13 @@ impl MarketplaceContract {
}

#[allow(dead_code)]
pub fn get_auctions_by_seller(env: Env, seller: Address) -> Vec<Auction> {
todo!()
pub fn get_auctions_by_seller(
env: Env,
seller: Address,
) -> Result<Vec<Auction>, ContractError> {
let seller_auction_list = get_auctions_by_seller_id(&env, &seller)?;

Ok(seller_auction_list)
}

#[allow(dead_code)]
Expand Down
2 changes: 1 addition & 1 deletion contracts/auctions/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use soroban_sdk::contracterror;
#[repr(u32)]
pub enum ContractError {
Unauthorized = 0,
AuctionIdNotFound = 1,
AuctionNotFound = 1,
IDMissmatch = 2,
BidNotEnough = 3,
AuctionNotFinished = 4,
Expand Down
61 changes: 57 additions & 4 deletions contracts/auctions/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use soroban_sdk::{contracttype, log, panic_with_error, token, Address, Env};
use soroban_sdk::{contracttype, log, panic_with_error, token, vec, Address, Env, Vec};

use crate::error::ContractError;

Expand Down Expand Up @@ -78,14 +78,67 @@ pub fn distribute_funds(env: &Env, auction: &Auction) -> Result<(), ContractErro
Ok(())
}

pub fn get_auction_by_id(env: &Env, auction_id: u64) -> Result<Auction, ContractError> {
pub fn save_auction_by_id(
env: &Env,
auction_id: u64,
auction: &Auction,
) -> Result<(), ContractError> {
env.storage().instance().set(&auction_id, auction);
env.storage()
.instance()
.extend_ttl(LIFETIME_THRESHOLD, BUMP_AMOUNT);

Ok(())
}

pub fn save_auction_by_seller(
env: &Env,
seller: &Address,
auction: &Auction,
) -> Result<(), ContractError> {
let mut seller_auctions_list: Vec<Auction> =
env.storage().instance().get(seller).unwrap_or(vec![&env]);

seller_auctions_list.push_back(auction.clone());

env.storage().instance().set(seller, &seller_auctions_list);

env.storage()
.instance()
.extend_ttl(LIFETIME_THRESHOLD, BUMP_AMOUNT);

Ok(())
}

pub fn get_auction_by_id(env: &Env, auction_id: u64) -> Result<Auction, ContractError> {
let auction = 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);
})
panic_with_error!(&env, ContractError::AuctionNotFound);
});
env.storage()
.instance()
.extend_ttl(LIFETIME_THRESHOLD, BUMP_AMOUNT);

auction
}

pub fn get_auctions_by_seller_id(
env: &Env,
seller: &Address,
) -> Result<Vec<Auction>, ContractError> {
let seller_auctions_list = env.storage().instance().get(seller).unwrap_or_else(|| {
log!(env, "Auction: Get auction by seller: No auctions found");
panic_with_error!(&env, ContractError::AuctionNotFound);
});
env.storage()
.instance()
.extend_ttl(LIFETIME_THRESHOLD, BUMP_AMOUNT);

Ok(seller_auctions_list)
}

pub fn update_auction(env: &Env, id: u64, auction: Auction) -> Result<(), ContractError> {
Expand Down

0 comments on commit a1a2b02

Please sign in to comment.