From a1a2b02259c4a0cd5015c5cbd90fa709a26ec766 Mon Sep 17 00:00:00 2001 From: Kaloyan Gangov <6922910+gangov@users.noreply.github.com> Date: Thu, 22 Aug 2024 11:00:28 +0300 Subject: [PATCH] save auction by seller --- contracts/auctions/src/contract.rs | 23 ++++++----- contracts/auctions/src/error.rs | 2 +- contracts/auctions/src/storage.rs | 61 ++++++++++++++++++++++++++++-- 3 files changed, 71 insertions(+), 15 deletions(-) diff --git a/contracts/auctions/src/contract.rs b/contracts/auctions/src/contract.rs index 58a56077..eee16d5b 100644 --- a/contracts/auctions/src/contract.rs +++ b/contracts/auctions/src/contract.rs @@ -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] @@ -54,16 +54,14 @@ impl MarketplaceContract { seller: seller.clone(), highest_bid: None, // we use the seller's address as we cannot add `Option
` 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) } @@ -237,8 +235,13 @@ impl MarketplaceContract { } #[allow(dead_code)] - pub fn get_auctions_by_seller(env: Env, seller: Address) -> Vec { - todo!() + pub fn get_auctions_by_seller( + env: Env, + seller: Address, + ) -> Result, ContractError> { + let seller_auction_list = get_auctions_by_seller_id(&env, &seller)?; + + Ok(seller_auction_list) } #[allow(dead_code)] diff --git a/contracts/auctions/src/error.rs b/contracts/auctions/src/error.rs index d2d10230..efca2fc4 100644 --- a/contracts/auctions/src/error.rs +++ b/contracts/auctions/src/error.rs @@ -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, diff --git a/contracts/auctions/src/storage.rs b/contracts/auctions/src/storage.rs index 131ec69a..f2f4e921 100644 --- a/contracts/auctions/src/storage.rs +++ b/contracts/auctions/src/storage.rs @@ -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; @@ -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 { +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 = + 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 { + 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, 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> {