diff --git a/contracts/liquidity_hub/bonding-manager/src/contract.rs b/contracts/liquidity_hub/bonding-manager/src/contract.rs index 3003bbd3..25622813 100644 --- a/contracts/liquidity_hub/bonding-manager/src/contract.rs +++ b/contracts/liquidity_hub/bonding-manager/src/contract.rs @@ -253,6 +253,8 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult { } QueryMsg::TotalBonded {} => to_json_binary(&queries::query_total_bonded(deps)?), QueryMsg::GlobalIndex {} => to_json_binary(&queries::query_global_index(deps)?), + QueryMsg::Claimable { addr } => to_json_binary(&queries::query_claimable(deps, &deps.api.addr_validate(&addr)?)?), + QueryMsg::ClaimableEpochs {} => to_json_binary(&queries::get_claimable_epochs(deps)?), } } diff --git a/contracts/liquidity_hub/bonding-manager/src/queries.rs b/contracts/liquidity_hub/bonding-manager/src/queries.rs index 99242e66..4cb64cd9 100644 --- a/contracts/liquidity_hub/bonding-manager/src/queries.rs +++ b/contracts/liquidity_hub/bonding-manager/src/queries.rs @@ -295,7 +295,7 @@ pub fn get_claimable_epochs(deps: Deps) -> StdResult { pub fn query_claimable(deps: Deps, address: &Addr) -> StdResult { let mut claimable_epochs = get_claimable_epochs(deps)?.epochs; let last_claimed_epoch = LAST_CLAIMED_EPOCH.may_load(deps.storage, address)?; - + // filter out epochs that have already been claimed by the user if let Some(last_claimed_epoch) = last_claimed_epoch { claimable_epochs.retain(|epoch| epoch.id > last_claimed_epoch); diff --git a/contracts/liquidity_hub/bonding-manager/src/tests/claim.rs b/contracts/liquidity_hub/bonding-manager/src/tests/claim.rs index 2ffdec35..174789f3 100644 --- a/contracts/liquidity_hub/bonding-manager/src/tests/claim.rs +++ b/contracts/liquidity_hub/bonding-manager/src/tests/claim.rs @@ -1,31 +1,31 @@ -// use cosmwasm_std::testing::{mock_dependencies, mock_env}; -// use cosmwasm_std::Uint64; +use cosmwasm_std::testing::{mock_dependencies, mock_env}; +use cosmwasm_std::Uint64; -// use crate::tests::robot::TestingRobot; -// use crate::tests::test_helpers; +use crate::tests::robot::TestingRobot; +use crate::tests::test_helpers; -// #[test] -// fn test_claimable_epochs() { -// let mut robot = TestingRobot::default(); -// let grace_period = Uint64::new(2); +#[test] +fn test_claimable_epochs() { + let mut robot = TestingRobot::default(); + let grace_period = Uint64::new(21); -// let epochs = test_helpers::get_epochs(); -// let binding = epochs.clone(); -// let claimable_epochs = binding -// .iter() -// .rev() -// .take(grace_period.u64() as usize) -// .collect::>(); + let epochs = test_helpers::get_epochs(); + let binding = epochs.clone(); + let claimable_epochs = binding + .iter() + .rev() + .take(grace_period.u64() as usize) + .collect::>(); -// robot -// .instantiate_default() -// .add_epochs_to_state(epochs) -// .query_claimable_epochs(None, |res| { -// let (_, epochs) = res.unwrap(); + robot + .instantiate_default() + .add_epochs_to_state(epochs) + .query_claimable_epochs(None, |res| { + let (_, epochs) = res.unwrap(); -// assert_eq!(epochs.len(), claimable_epochs.len()); -// for (e, a) in epochs.iter().zip(claimable_epochs.iter()) { -// assert_eq!(e, *a); -// } -// }); -// } + assert_eq!(epochs.len(), claimable_epochs.len()); + for (e, a) in epochs.iter().zip(claimable_epochs.iter()) { + assert_eq!(e, *a); + } + }); +} diff --git a/contracts/liquidity_hub/bonding-manager/src/tests/robot.rs b/contracts/liquidity_hub/bonding-manager/src/tests/robot.rs index 72237561..3cdd60f1 100644 --- a/contracts/liquidity_hub/bonding-manager/src/tests/robot.rs +++ b/contracts/liquidity_hub/bonding-manager/src/tests/robot.rs @@ -1,11 +1,12 @@ use anyhow::Error; use cosmwasm_std::testing::{mock_dependencies, mock_env, MockApi, MockQuerier, MockStorage}; -use cosmwasm_std::{coin, Addr, Coin, Decimal, Empty, OwnedDeps, StdResult, Timestamp, Uint64}; +use cosmwasm_std::{coin, from_json, Addr, Coin, Decimal, Empty, OwnedDeps, StdResult, Timestamp, Uint64}; use cw_multi_test::{App, AppResponse, Executor}; +use crate::contract::query; use crate::state::{EPOCHS, LAST_CLAIMED_EPOCH}; use cw_multi_test::{Contract, ContractWrapper}; -use white_whale_std::bonding_manager::Epoch; +use white_whale_std::bonding_manager::{ClaimableEpochsResponse, Epoch}; use white_whale_std::bonding_manager::{ BondedResponse, BondingWeightResponse, Config, ExecuteMsg, InstantiateMsg, QueryMsg, UnbondingResponse, WithdrawableResponse, @@ -357,35 +358,35 @@ impl TestingRobot { self } - // pub(crate) fn query_claimable_epochs( - // &mut self, - // address: Option, - // response: impl Fn(StdResult<(&mut Self, Vec)>), - // ) -> &mut Self { - // let query_res = if let Some(address) = address { - // query( - // self.owned_deps.as_ref(), - // self.env.clone(), - // QueryMsg::Claimable { - // address: address.to_string(), - // }, - // ) - // .unwrap() - // } else { - // query( - // self.owned_deps.as_ref(), - // self.env.clone(), - // QueryMsg::ClaimableEpochs {}, - // ) - // .unwrap() - // }; - - // let res: ClaimableEpochsResponse = from_json(query_res).unwrap(); - - // response(Ok((self, res.epochs))); - - // self - // } + pub(crate) fn query_claimable_epochs( + &mut self, + address: Option, + response: impl Fn(StdResult<(&mut Self, Vec)>), + ) -> &mut Self { + let query_res = if let Some(address) = address { + query( + self.owned_deps.as_ref(), + self.env.clone(), + QueryMsg::Claimable { + addr: address.to_string(), + }, + ) + .unwrap() + } else { + query( + self.owned_deps.as_ref(), + self.env.clone(), + QueryMsg::ClaimableEpochs {}, + ) + .unwrap() + }; + + let res: ClaimableEpochsResponse = from_json(query_res).unwrap(); + + response(Ok((self, res.epochs))); + + self + } pub(crate) fn query_bonded( &mut self, diff --git a/packages/white-whale-std/src/bonding_manager.rs b/packages/white-whale-std/src/bonding_manager.rs index 6f8f82b1..f2d02b1a 100644 --- a/packages/white-whale-std/src/bonding_manager.rs +++ b/packages/white-whale-std/src/bonding_manager.rs @@ -167,6 +167,14 @@ pub enum QueryMsg { /// Returns the global index of the contract. #[returns(GlobalIndex)] GlobalIndex {}, + + /// Returns the [Epoch]s that can be claimed. + #[returns(ClaimableEpochsResponse)] + ClaimableEpochs {}, + + /// Returns the [Epoch]s that can be claimed by an address. + #[returns(ClaimableEpochsResponse)] + Claimable { addr: String }, } #[cw_serde]