-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85701cd
commit 5a88088
Showing
13 changed files
with
811 additions
and
885 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}, | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.