Skip to content

Commit

Permalink
Return result in to_curve_fn
Browse files Browse the repository at this point in the history
  • Loading branch information
ismellike committed Sep 17, 2024
1 parent 9dd313e commit b84971c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
27 changes: 13 additions & 14 deletions contracts/external/cw-abc/src/abc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{ensure, Decimal, Uint128};
use cosmwasm_std::{ensure, Decimal, StdResult, Uint128};
use cw_curves::{
curves::{Constant, Linear, SquareRoot},
utils::decimal,
Expand Down Expand Up @@ -194,25 +194,24 @@ pub enum CurveType {
}

impl CurveType {
pub fn to_curve_fn(&self) -> CurveFn {
pub fn to_curve_fn(&self) -> StdResult<CurveFn> {
match self.clone() {
CurveType::Constant { value, scale } => {
let calc = move |places| -> Box<dyn Curve> {
Box::new(Constant::new(decimal(value, scale).unwrap(), places))
};
Box::new(calc)
let value = decimal(value, scale)?;
let calc =
move |places| -> Box<dyn Curve> { Box::new(Constant::new(value, places)) };
Ok(Box::new(calc))
}
CurveType::Linear { slope, scale } => {
let calc = move |places| -> Box<dyn Curve> {
Box::new(Linear::new(decimal(slope, scale).unwrap(), places))
};
Box::new(calc)
let slope = decimal(slope, scale)?;
let calc = move |places| -> Box<dyn Curve> { Box::new(Linear::new(slope, places)) };
Ok(Box::new(calc))
}
CurveType::SquareRoot { slope, scale } => {
let calc = move |places| -> Box<dyn Curve> {
Box::new(SquareRoot::new(decimal(slope, scale).unwrap(), places))
};
Box::new(calc)
let slope = decimal(slope, scale)?;
let calc =
move |places| -> Box<dyn Curve> { Box::new(SquareRoot::new(slope, places)) };
Ok(Box::new(calc))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/external/cw-abc/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
// default implementation stores curve info as enum, you can do something else in a derived
// contract and just pass in your custom curve to do_execute
let curve_type = CURVE_TYPE.load(deps.storage)?;
let curve_fn = curve_type.to_curve_fn();
let curve_fn = curve_type.to_curve_fn()?;
do_query(deps, env, msg, curve_fn)
}

Expand Down
4 changes: 2 additions & 2 deletions contracts/external/cw-abc/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn calculate_buy_quote(
phase_config: &CommonsPhaseConfig,
) -> Result<QuoteResponse, ContractError> {
// Generate the bonding curve
let curve_fn = curve_type.to_curve_fn();
let curve_fn = curve_type.to_curve_fn()?;
let curve = curve_fn(curve_state.decimals);

// Calculate the reserved and funded amounts based on the Commons phase
Expand Down Expand Up @@ -50,7 +50,7 @@ pub fn calculate_sell_quote(
phase_config: &CommonsPhaseConfig,
) -> Result<QuoteResponse, ContractError> {
// Generate the bonding curve
let curve_fn = curve_type.to_curve_fn();
let curve_fn = curve_type.to_curve_fn()?;
let curve = curve_fn(curve_state.decimals);

// Reduce the supply by the amount being burned
Expand Down

0 comments on commit b84971c

Please sign in to comment.