Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjatosba committed Dec 4, 2024
1 parent 85701cd commit 5a88088
Show file tree
Hide file tree
Showing 13 changed files with 811 additions and 885 deletions.
5 changes: 5 additions & 0 deletions contracts/stream/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ pub fn instantiate(
if in_denom == out_asset.denom {
return Err(ContractError::SameDenomOnEachSide {});
}
if let Some(threshold) = threshold {
if threshold.is_zero() {
return Err(ContractError::InvalidThreshold {});
}
}
let stream_admin = deps.api.addr_validate(&stream_admin)?;
let treasury = deps.api.addr_validate(&treasury)?;

Expand Down
8 changes: 3 additions & 5 deletions contracts/stream/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use std::convert::Infallible;
use streamswap_utils::payment_checker::CustomPaymentError;
use thiserror::Error;

use streamswap_types::stream::ThresholdError;

#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
#[error("{0}")]
Expand All @@ -26,9 +24,6 @@ pub enum ContractError {
#[error("{0}")]
DivideByZeroError(#[from] DivideByZeroError),

#[error("{0}")]
ThresholdError(#[from] ThresholdError),

#[error("{0}")]
CustomPayment(#[from] CustomPaymentError),

Expand Down Expand Up @@ -156,4 +151,7 @@ pub enum ContractError {

#[error("Invalid pool config")]
InvalidPoolConfig {},

#[error("Threshold must be greater than zero")]
InvalidThreshold {},
}
30 changes: 15 additions & 15 deletions packages/types/src/stream/error.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use cosmwasm_std::StdError;
use thiserror::Error;
// use cosmwasm_std::StdError;
// use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
pub enum ThresholdError {
#[error(transparent)]
Std(#[from] StdError),
// #[derive(Error, Debug, PartialEq)]
// pub enum ThresholdError {
// #[error(transparent)]
// Std(#[from] StdError),

#[error("Threshold not reached")]
ThresholdNotReached {},
// #[error("Threshold not reached")]
// ThresholdNotReached {},

#[error("Threshold reached")]
ThresholdReached {},
// #[error("Threshold reached")]
// ThresholdReached {},

#[error("Threshold not set")]
ThresholdNotSet {},
// #[error("Threshold not set")]
// ThresholdNotSet {},

#[error("Min price can't be zero")]
ThresholdZero {},
}
// #[error("Min price can't be zero")]
// ThresholdZero {},
// }
148 changes: 74 additions & 74 deletions packages/types/src/stream/threshold.rs
Original file line number Diff line number Diff line change
@@ -1,81 +1,81 @@
use crate::stream::{StreamState, ThresholdError};
use cosmwasm_std::{StdError, Storage, Uint256};
use cw_storage_plus::Item;
// use crate::stream::{StreamState, ThresholdError};
// use cosmwasm_std::{StdError, Storage, Uint256};
// use cw_storage_plus::Item;

pub type Threshold = Uint256;
// pub type Threshold = Uint256;

pub const THRESHOLDS_STATE_KEY: &str = "thresholds";
// pub const THRESHOLDS_STATE_KEY: &str = "thresholds";

pub struct ThresholdState<'a>(Item<'a, Threshold>);
// pub struct ThresholdState<'a>(Item<'a, Threshold>);

impl<'a> ThresholdState<'a> {
pub fn new() -> Self {
ThresholdState(Item::new(THRESHOLDS_STATE_KEY))
}
// impl<'a> ThresholdState<'a> {
// pub fn new() -> Self {
// ThresholdState(Item::new(THRESHOLDS_STATE_KEY))
// }

pub fn set_threshold_if_any(
&self,
threshold: Option<Uint256>,
storage: &mut dyn Storage,
) -> Result<(), ThresholdError> {
match threshold {
Some(threshold) => {
if threshold.is_zero() {
return Err(ThresholdError::ThresholdZero {});
}
self.0.save(storage, &threshold)?;
Ok(())
}
None => Ok(()),
}
}
pub fn error_if_not_reached(
&self,
storage: &dyn Storage,
state: &StreamState,
) -> Result<(), ThresholdError> {
// If threshold is not set, It returns ok
// If threshold is set, It returns error if threshold is not reached
let threshold = self.0.may_load(storage)?;
if let Some(threshold) = threshold {
if state.spent_in < threshold {
Err(ThresholdError::ThresholdNotReached {})
} else {
Ok(())
}
} else {
Ok(())
}
}
// pub fn set_threshold_if_any(
// &self,
// threshold: Option<Uint256>,
// storage: &mut dyn Storage,
// ) -> Result<(), ThresholdError> {
// match threshold {
// Some(threshold) => {
// if threshold.is_zero() {
// return Err(ThresholdError::ThresholdZero {});
// }
// self.0.save(storage, &threshold)?;
// Ok(())
// }
// None => Ok(()),
// }
// }
// pub fn error_if_not_reached(
// &self,
// storage: &dyn Storage,
// state: &StreamState,
// ) -> Result<(), ThresholdError> {
// // If threshold is not set, It returns ok
// // If threshold is set, It returns error if threshold is not reached
// let threshold = self.0.may_load(storage)?;
// if let Some(threshold) = threshold {
// if state.spent_in < threshold {
// Err(ThresholdError::ThresholdNotReached {})
// } else {
// Ok(())
// }
// } else {
// Ok(())
// }
// }

pub fn error_if_reached(
&self,
storage: &dyn Storage,
state: &StreamState,
) -> Result<(), ThresholdError> {
let threshold = self.0.may_load(storage)?;
if let Some(threshold) = threshold {
if state.spent_in >= threshold {
Err(ThresholdError::ThresholdReached {})
} else {
Ok(())
}
} else {
Ok(())
}
}
pub fn check_if_threshold_set(&self, storage: &dyn Storage) -> Result<bool, ThresholdError> {
let threshold = self.0.may_load(storage)?;
Ok(threshold.is_some())
}
pub fn get_threshold(&self, storage: &dyn Storage) -> Result<Option<Threshold>, StdError> {
let threshold = self.0.may_load(storage)?;
Ok(threshold)
}
}
// pub fn error_if_reached(
// &self,
// storage: &dyn Storage,
// state: &StreamState,
// ) -> Result<(), ThresholdError> {
// let threshold = self.0.may_load(storage)?;
// if let Some(threshold) = threshold {
// if state.spent_in >= threshold {
// Err(ThresholdError::ThresholdReached {})
// } else {
// Ok(())
// }
// } else {
// Ok(())
// }
// }
// pub fn check_if_threshold_set(&self, storage: &dyn Storage) -> Result<bool, ThresholdError> {
// let threshold = self.0.may_load(storage)?;
// Ok(threshold.is_some())
// }
// pub fn get_threshold(&self, storage: &dyn Storage) -> Result<Option<Threshold>, StdError> {
// let threshold = self.0.may_load(storage)?;
// Ok(threshold)
// }
// }

impl<'a> Default for ThresholdState<'a> {
fn default() -> Self {
ThresholdState::new()
}
}
// impl<'a> Default for ThresholdState<'a> {
// fn default() -> Self {
// ThresholdState::new()
// }
// }
6 changes: 1 addition & 5 deletions tests/src/tests/streamswap_tests/create_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod create_stream_tests {
use streamswap_stream::ContractError as StreamSwapError;
use streamswap_types::controller::Params as ControllerParams;
use streamswap_types::controller::QueryMsg;
use streamswap_types::stream::ThresholdError;
use streamswap_utils::payment_checker::CustomPaymentError;

#[test]
Expand Down Expand Up @@ -396,10 +395,7 @@ mod create_stream_tests {

let err = res.source().unwrap().source().unwrap();
let error = err.downcast_ref::<StreamSwapError>().unwrap();
assert_eq!(
*error,
StreamSwapError::ThresholdError(ThresholdError::ThresholdZero {})
);
assert_eq!(*error, StreamSwapError::InvalidThreshold {});
}

#[test]
Expand Down
Loading

0 comments on commit 5a88088

Please sign in to comment.