Skip to content

Commit

Permalink
potLimitReached rule
Browse files Browse the repository at this point in the history
  • Loading branch information
magiodev committed Apr 24, 2024
1 parent 2986feb commit 35e82f0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
3 changes: 3 additions & 0 deletions contracts/prudent-pots/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}")]
Expand Down
21 changes: 8 additions & 13 deletions contracts/prudent-pots/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -37,15 +37,12 @@ pub fn allocate_tokens(
info: MessageInfo,
pot_id: u8,
) -> Result<Response, ContractError> {
// 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)?;
Expand Down Expand Up @@ -78,19 +75,15 @@ pub fn reallocate_tokens(
from_pot_id: u8,
to_pot_id: u8,
) -> Result<Response, ContractError> {
// 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())?;

Expand All @@ -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();

Expand Down
29 changes: 29 additions & 0 deletions contracts/prudent-pots/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}

0 comments on commit 35e82f0

Please sign in to comment.