Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Min Sale Amount Calculation Assertion Updates #6

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 65 additions & 22 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,58 @@ use cw_storage_plus::Bound;
use cw_utils::{NativeBalance, PaymentError};
use fuzion_flows::{FlowCreate, FlowSchedule, FlowType};
use fuzion_utilities::{Asset, AssetList, DenomUnit, LogoURIs};
use kujira::{DenomMsg, KujiraMsg, KujiraQuery, Precision};
use kujira::{DenomMsg, KujiraMsg, KujiraQuerier, KujiraQuery, Precision};
use kujira_orca::BidPoolsResponse;
use kujira_pilot::Status;
use kujira_pilot::{CreateOrca, CreateSale, Status};

use crate::launch::Launch;
use crate::msg::{
Bow, CallbackType, CategoryTypes, Config, Fin, LaunchStatus, Pilot, ReplyInfo, ReplyTypes,
Token, Tokenomics,
BidDenoms, Bow, CallbackType, CategoryTypes, Config, Fin, LaunchStatus, Pilot, ReplyInfo,
ReplyTypes, Token, Tokenomics, TokenomicsCategories,
};
use crate::state::{launch, CONFIG, REPLY};
use crate::{ContractError, ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};

const CONTRACT_NAME: &str = "fuzion-kujira-keiko";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

pub fn assert_min_sale_amount(
kujira_querier: &KujiraQuerier,
config: &Config,
bid_denom: &BidDenoms,
sale_category: &TokenomicsCategories,
sale: &CreateSale,
orca: &CreateOrca,
) -> Result<(), ContractError> {
let oracle_price = kujira_querier.query_exchange_rate(
bid_denom
.oracle_symbol
.clone()
.unwrap_or(bid_denom.symbol.to_string()),
)?;
let normalized_usd_price = oracle_price.normalize(bid_denom.decimals);
let launch_min_raise_amount = sale_category.recipients[0]
.amount
.mul(sale.price)
.mul(
Decimal::one().sub(
Decimal::from_str(&orca.max_slot.to_string())
.unwrap()
.mul(orca.premium_rate_per_slot),
),
)
.mul(normalized_usd_price);

ensure!(
launch_min_raise_amount > config.pilot.min_raise_amount,
ContractError::InvalidRaiseAmount(
launch_min_raise_amount.to_string(),
config.pilot.min_raise_amount.to_string()
)
);
Ok(())
}

#[entry_point]
pub fn migrate(deps: DepsMut<KujiraQuery>, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
Expand Down Expand Up @@ -389,7 +426,10 @@ pub fn execute(
.iter()
.find(|d| d.denom == orca.bid_denom.clone());

ensure!(bid_denom.is_some(), ContractError::InvalidBidDenom {});
ensure!(
bid_denom.clone().is_some(),
ContractError::InvalidBidDenom {}
);

let categories = launch.clone().tokenomics.unwrap().categories;

Expand All @@ -398,19 +438,14 @@ pub fn execute(
.find(|c| c.category_type == CategoryTypes::Sale)
.unwrap();

let launch_min_raise_amount = sale_category.recipients[0].amount.mul(sale.price).mul(
Decimal::from_str(&orca.max_slot.to_string())
.unwrap()
.mul(orca.premium_rate_per_slot),
);

ensure!(
launch_min_raise_amount > config.pilot.min_raise_amount,
ContractError::InvalidRaiseAmount(
launch_min_raise_amount.to_string(),
config.pilot.min_raise_amount.to_string()
)
);
assert_min_sale_amount(
&KujiraQuerier::new(&deps.querier),
&config,
bid_denom.unwrap(),
sale_category,
&sale,
&orca,
)?;

let mut pilot = Pilot {
idx: None,
Expand Down Expand Up @@ -501,13 +536,21 @@ pub fn execute(
launch.pilot.clone().unwrap().sale
};

let orca = launch.pilot.clone().unwrap().orca;

assert_min_sale_amount(
&KujiraQuerier::new(&deps.querier),
&config,
bid_denom.unwrap(),
sale_category,
&sale,
&orca,
)?;

messages.push(SubMsg::reply_on_success(
CosmosMsg::Wasm(wasm_execute(
config.pilot.pilot_contract,
&kujira_pilot::ExecuteMsg::Create {
sale,
orca: launch.pilot.clone().unwrap().orca,
},
&kujira_pilot::ExecuteMsg::Create { sale, orca },
vec![pilot_config.deposit, sale_funds],
)?),
ReplyTypes::Create as u64,
Expand Down
1 change: 1 addition & 0 deletions src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ impl fmt::Display for LaunchStatus {
pub struct BidDenoms {
pub denom: Denom,
pub symbol: String,
pub oracle_symbol: Option<String>,
pub decimals: u8,
}

Expand Down
3 changes: 3 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ fn launch_new_token() {
allowed_bid_denoms: vec![BidDenoms {
denom: Denom::from("bid"),
symbol: "bid".to_string(),
oracle_symbol: None,
decimals: 6,
}],
min_raise_amount: Uint128::from(100_000_000_000u128),
Expand Down Expand Up @@ -749,6 +750,7 @@ fn launch_own_token() {
allowed_bid_denoms: vec![BidDenoms {
denom: Denom::from("bid"),
symbol: "bid".to_string(),
oracle_symbol: None,
decimals: 6,
}],
min_raise_amount: Uint128::from(100_000_000_000u128),
Expand Down Expand Up @@ -1249,6 +1251,7 @@ fn launch_own_token_18_decimals() {
allowed_bid_denoms: vec![BidDenoms {
denom: Denom::from("bid"),
symbol: "bid".to_string(),
oracle_symbol: None,
decimals: 6,
}],
min_raise_amount: Uint128::from(100_000_000_000u128),
Expand Down