Skip to content

Commit

Permalink
Touch up PR
Browse files Browse the repository at this point in the history
  • Loading branch information
tasiov committed Jun 13, 2023
1 parent fa84d9d commit be72bee
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
28 changes: 11 additions & 17 deletions contracts/fair-burn/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{
ensure, to_binary, Addr, BankMsg, Binary, Coin, Decimal, Deps, DepsMut, Env, Event,
MessageInfo, StdResult, Uint128,
ensure, to_binary, Addr, BankMsg, Binary, Coin, Deps, DepsMut, Env, Event, MessageInfo,
StdResult,
};
use cw2::{get_contract_version, set_contract_version};
use cw_utils::{maybe_addr, NativeBalance};
Expand All @@ -9,7 +9,7 @@ use std::collections::BTreeMap;

use crate::{
error::ContractError,
helpers::calculate_payouts,
helpers::{bps_to_decimal, calculate_payouts},
msg::{ExecuteMsg, InstantiateMsg, QueryMsg, SudoMsg},
state::{Config, CONFIG},
};
Expand All @@ -31,7 +31,7 @@ pub fn instantiate(
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

let config = Config {
fee_percent: Decimal::percent(msg.fee_bps) / Uint128::from(100u64),
fee_percent: bps_to_decimal(msg.fee_bps),
};
config.save(deps.storage)?;

Expand Down Expand Up @@ -65,14 +65,11 @@ pub fn execute_fair_burn(
info: MessageInfo,
recipient: Option<Addr>,
) -> Result<Response, ContractError> {
ensure!(
!info.funds.is_empty(),
ContractError::InvalidInput("must send some coins".to_string())
);

let mut funds_normalized = NativeBalance(info.funds);
funds_normalized.normalize();

ensure!(!funds_normalized.is_empty(), ContractError::ZeroFunds);

let mut response = Response::new();

let config = CONFIG.load(deps.storage)?;
Expand Down Expand Up @@ -164,20 +161,17 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn sudo(deps: DepsMut, _env: Env, msg: SudoMsg) -> Result<Response, ContractError> {
match msg {
SudoMsg::UpdateConfig { fair_burn_bps } => sudo_update_config(deps, fair_burn_bps),
SudoMsg::UpdateConfig { fee_bps } => sudo_update_config(deps, fee_bps),
}
}

pub fn sudo_update_config(
deps: DepsMut,
fair_burn_bps: Option<u64>,
) -> Result<Response, ContractError> {
pub fn sudo_update_config(deps: DepsMut, fee_bps: Option<u64>) -> Result<Response, ContractError> {
let mut config = CONFIG.load(deps.storage)?;

let mut event = Event::new("sudo-update-config");

if let Some(fair_burn_bps) = fair_burn_bps {
config.fee_percent = Decimal::percent(fair_burn_bps) / Uint128::from(100u128);
if let Some(fee_bps) = fee_bps {
config.fee_percent = bps_to_decimal(fee_bps);

This comment has been minimized.

Copy link
@yubrew

yubrew Jun 13, 2023

Contributor

Can this be used instead of the helper? CosmWasm/cosmwasm#1715

event = event.add_attribute("fee_percent", config.fee_percent.to_string());
}

Expand Down Expand Up @@ -280,7 +274,7 @@ mod tests {

let new_fee_bps = 4000;
let sudo_msg = SudoMsg::UpdateConfig {
fair_burn_bps: Some(new_fee_bps),
fee_bps: Some(new_fee_bps),
};
let response = app.sudo(CwSudoMsg::Wasm(WasmSudo {
contract_addr: fair_burn.clone(),
Expand Down
7 changes: 5 additions & 2 deletions contracts/fair-burn/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),

#[error("Invalid input: {0}")]
InvalidInput(String),
#[error("Zero funds: must send non-zero funds to this contract")]
ZeroFunds,

#[error("Invalid config: {0}")]
InvalidConfig(String),
}
6 changes: 5 additions & 1 deletion contracts/fair-burn/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{coin, to_binary, Addr, Coin, Uint128, WasmMsg};
use cosmwasm_std::{coin, to_binary, Addr, Coin, Decimal, Uint128, WasmMsg};
use sg_std::Response;

use crate::{msg::ExecuteMsg, state::Config};
Expand Down Expand Up @@ -32,3 +32,7 @@ pub fn append_fair_burn_msg(
funds,
})
}

pub fn bps_to_decimal(bps: u64) -> Decimal {
Decimal::percent(bps) / Uint128::from(100u64)
}
2 changes: 1 addition & 1 deletion contracts/fair-burn/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ pub enum QueryMsg {

#[cw_serde]
pub enum SudoMsg {
UpdateConfig { fair_burn_bps: Option<u64> },
UpdateConfig { fee_bps: Option<u64> },
}
2 changes: 1 addition & 1 deletion contracts/fair-burn/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Config {
fn validate(&self) -> Result<(), ContractError> {
ensure!(
self.fee_percent > Decimal::zero(),
ContractError::InvalidInput("fee_percent must be positive".to_string())
ContractError::InvalidConfig("fee_percent must be positive".to_string())

This comment has been minimized.

Copy link
@yubrew

yubrew Jun 13, 2023

Contributor

Avoid catch alls and magic string values which can change over time.

#[error("Fee percent must be positive")]
ZeroFeePercent{}
);
Ok(())
}
Expand Down

0 comments on commit be72bee

Please sign in to comment.