From caa5449b8aa6f070c269d6f0abb2ea6362cd37f7 Mon Sep 17 00:00:00 2001 From: PFC <81114960+PFC-developer@users.noreply.github.com> Date: Sun, 22 Sep 2024 11:02:21 -0500 Subject: [PATCH] fix: item 5 - return_dust only tickets are below a hard-coded threshold (2) --- .../treasurechest-contract/src/executions.rs | 23 +++++++++++++++---- packages/treasurechest/src/errors.rs | 3 +++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/contracts/treasurechest-contract/src/executions.rs b/contracts/treasurechest-contract/src/executions.rs index 131d66d..f939d3d 100644 --- a/contracts/treasurechest-contract/src/executions.rs +++ b/contracts/treasurechest-contract/src/executions.rs @@ -1,13 +1,12 @@ use std::{ops::Mul, str::FromStr}; use std::collections::HashMap; +use std::ops::Sub; const DEFAULT_SIZE:u32 = 20; +const MIN_TICKETS:u128 = 2; -use cosmwasm_std::{ - Addr, BankMsg, Coin, CosmosMsg, DepsMut, Env, Event, MessageInfo, Order, Response, StdResult, - Uint128, -}; - +use cosmwasm_std::{Addr, BalanceResponse, BankMsg, BankQuery, Coin, CosmosMsg, DepsMut, Env, Event, MessageInfo, Order, Response, StdResult, Uint128}; +use cosmwasm_std::QueryRequest::Bank; use treasurechest::{errors::ContractError, tf::tokenfactory::TokenFactoryType}; use crate::state::{CONFIG, TOTAL_REWARDS}; @@ -83,6 +82,20 @@ pub fn change_token_factory( pub fn return_dust(deps: DepsMut, env: Env, sender: Addr, limit: Option) -> Result { cw_ownable::assert_owner(deps.storage, &sender)?; let config = CONFIG.load(deps.storage)?; + + let denom_total = deps.querier.query::(&Bank(BankQuery::Supply {denom: config.denom.clone()}))?; + if config.burn_it { + if denom_total .amount.amount.u128()> MIN_TICKETS { + return Err(ContractError::TicketsOutstanding(denom_total.amount.amount.u128(),MIN_TICKETS)) + } + } else { + let ticket_balance = deps.querier.query_balance(env.contract.address.clone(),config.denom.clone())?; + let outstanding = denom_total.amount.amount.sub(ticket_balance.amount); + if outstanding.u128() > MIN_TICKETS { + return Err(ContractError::TicketsOutstanding(outstanding.u128(),MIN_TICKETS)) + } + } + let balances = deps .querier .query_all_balances(env.contract.address)? diff --git a/packages/treasurechest/src/errors.rs b/packages/treasurechest/src/errors.rs index 8811e1f..9cd8edf 100644 --- a/packages/treasurechest/src/errors.rs +++ b/packages/treasurechest/src/errors.rs @@ -26,6 +26,9 @@ pub enum ContractError { #[error("invalid token factory type {0}")] TokenFactoryTypeInvalid(String), + #[error("Too many outstanding redemption tokens {0} - min = {1}")] + TicketsOutstanding(u128,u128), + #[error("Unauthorized")] Unauthorized {},