From 35e82f0b1ed0c6414a9edadca1940018a8d64ad6 Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Wed, 24 Apr 2024 09:01:05 +0200 Subject: [PATCH] potLimitReached rule --- contracts/prudent-pots/src/error.rs | 3 +++ contracts/prudent-pots/src/execute.rs | 21 ++++++++----------- contracts/prudent-pots/src/helpers.rs | 29 +++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/contracts/prudent-pots/src/error.rs b/contracts/prudent-pots/src/error.rs index 767ac62..d7f3a2a 100644 --- a/contracts/prudent-pots/src/error.rs +++ b/contracts/prudent-pots/src/error.rs @@ -30,6 +30,9 @@ pub enum ContractError { #[error("GameStillActive")] GameStillActive {}, + #[error("PotLimitReached: A pot cannot contain more tokens than the sum of the others.")] + PotLimitReached {}, + #[error("BidOutOfRange. Min: {min:?}, Max: {max:?}")] BidOutOfRange { min: Uint128, max: Uint128 }, // #[error("Custom Error val: {val:?}")] diff --git a/contracts/prudent-pots/src/execute.rs b/contracts/prudent-pots/src/execute.rs index 351dc80..ea8cc75 100644 --- a/contracts/prudent-pots/src/execute.rs +++ b/contracts/prudent-pots/src/execute.rs @@ -5,7 +5,7 @@ use crate::{ calculate_max_bid, calculate_min_bid, calculate_total_losing_tokens, check_existing_allocation, get_distribute_bank_msgs, is_contract_admin, is_winning_pot, prepare_next_game, update_player_allocation, update_pot_state, - validate_and_extend_game_time, validate_and_sum_funds, + validate_and_extend_game_time, validate_and_sum_funds, validate_pot_limit_not_exceeded, }, state::{ GameConfig, TokenAllocation, GAME_CONFIG, GAME_STATE, PLAYER_ALLOCATIONS, POT_STATES, @@ -37,15 +37,12 @@ pub fn allocate_tokens( info: MessageInfo, pot_id: u8, ) -> Result { - // Validate the game's end time and extend it if necessary - validate_and_extend_game_time(deps.storage, &env)?; - - // Check if the player has already allocated to this pot - check_existing_allocation(deps.storage, &info.sender, pot_id)?; - let config = GAME_CONFIG.load(deps.storage)?; + validate_and_extend_game_time(deps.storage, &env)?; let amount = validate_and_sum_funds(&info.funds, &config.game_denom)?; + validate_pot_limit_not_exceeded(deps.storage, pot_id, amount)?; + check_existing_allocation(deps.storage, &info.sender, pot_id)?; // Implementing dynamic bid constraints let min_bid = calculate_min_bid(deps.storage)?; @@ -78,19 +75,15 @@ pub fn reallocate_tokens( from_pot_id: u8, to_pot_id: u8, ) -> Result { - // Validate the game's end time and extend it if necessary - validate_and_extend_game_time(deps.storage, &env)?; + let config = GAME_CONFIG.load(deps.storage)?; - // Ensure pots are different if from_pot_id == to_pot_id { return Err(ContractError::InvalidPot {}); } - // Check if the player has already allocated to the target pot + validate_and_extend_game_time(deps.storage, &env)?; check_existing_allocation(deps.storage, &info.sender, to_pot_id)?; - let config = GAME_CONFIG.load(deps.storage)?; - // Load the player's allocations let mut player_allocations = PLAYER_ALLOCATIONS.load(deps.storage, info.sender.clone())?; @@ -106,6 +99,8 @@ pub fn reallocate_tokens( return Err(ContractError::InsufficientFunds {}); } + validate_pot_limit_not_exceeded(deps.storage, to_pot_id, amount)?; + let fee = amount.multiply_ratio(config.fee_reallocation, 100u128); let net_amount = amount.checked_sub(fee).unwrap(); diff --git a/contracts/prudent-pots/src/helpers.rs b/contracts/prudent-pots/src/helpers.rs index 9607fe3..0042b83 100644 --- a/contracts/prudent-pots/src/helpers.rs +++ b/contracts/prudent-pots/src/helpers.rs @@ -492,3 +492,32 @@ pub fn create_fee_message( })]) } } + +pub fn validate_pot_limit_not_exceeded( + storage: &dyn Storage, + pot_id: u8, + amount: Uint128, +) -> Result<(), ContractError> { + let pots = POT_STATES.range(storage, None, None, cosmwasm_std::Order::Ascending); + let mut sum_of_other_pots = Uint128::zero(); + let mut current_pot_amount = Uint128::zero(); + + // Calculate the sum of all other pots and find the current pot amount + for item in pots { + let (id, token_allocation) = item?; + if id == pot_id { + current_pot_amount = token_allocation.amount; + } else { + sum_of_other_pots = sum_of_other_pots + .checked_add(token_allocation.amount) + .unwrap(); + } + } + + // Check if adding the amount to the current pot exceeds the sum of all other pots + if current_pot_amount.checked_add(amount).unwrap() > sum_of_other_pots { + Err(ContractError::PotLimitReached {}) + } else { + Ok(()) + } +} \ No newline at end of file