diff --git a/contracts/liquidity_hub/bonding-manager/src/bonding/commands.rs b/contracts/liquidity_hub/bonding-manager/src/bonding/commands.rs index 62bed1e3..c93c7e1a 100644 --- a/contracts/liquidity_hub/bonding-manager/src/bonding/commands.rs +++ b/contracts/liquidity_hub/bonding-manager/src/bonding/commands.rs @@ -30,7 +30,7 @@ pub(crate) fn bond( &white_whale_std::epoch_manager::epoch_manager::QueryMsg::CurrentEpoch {}, )?; - let mut bonds_by_receiver = get_bonds_by_receiver( + let bonds_by_receiver = get_bonds_by_receiver( deps.storage, info.sender.to_string(), Some(true), @@ -40,7 +40,8 @@ pub(crate) fn bond( )?; let mut bond = if bonds_by_receiver.is_empty() { - // create bond id + // the user doesn't have any bonds of the given asset + let bond_id = BOND_COUNTER.update::<_, StdError>(deps.storage, |current_id| Ok(current_id + 1u64))?; @@ -56,16 +57,13 @@ pub(crate) fn bond( ..Bond::default() } } else { + // sanity check ensure!( bonds_by_receiver.len() == 1usize, - //todo change this error - ContractError::NothingToUnbond + ContractError::AssetMismatch ); - //todo change this error - bonds_by_receiver - .pop() - .ok_or(ContractError::NothingToUnbond)? + bonds_by_receiver[0].clone() }; // update bond values @@ -110,7 +108,7 @@ pub(crate) fn unbond( helpers::validate_claimed(&deps, &info)?; helpers::validate_bonding_for_current_epoch(&deps)?; - let mut bonds_by_receiver = get_bonds_by_receiver( + let bonds_by_receiver = get_bonds_by_receiver( deps.storage, info.sender.to_string(), Some(true), @@ -121,17 +119,19 @@ pub(crate) fn unbond( ensure!( bonds_by_receiver.len() <= 1usize, - //todo change this error - ContractError::NothingToUnbond + ContractError::AssetMismatch ); if bonds_by_receiver.is_empty() { Err(ContractError::NothingToUnbond) } else { - //todo change this error - let mut unbond: Bond = bonds_by_receiver - .pop() - .ok_or(ContractError::NothingToUnbond)?; + // sanity check + ensure!( + bonds_by_receiver.len() == 1usize, + ContractError::AssetMismatch + ); + + let mut unbond = bonds_by_receiver[0].clone(); // check if the address has enough bond ensure!( diff --git a/contracts/liquidity_hub/bonding-manager/src/contract.rs b/contracts/liquidity_hub/bonding-manager/src/contract.rs index f2b9a379..c282a13c 100644 --- a/contracts/liquidity_hub/bonding-manager/src/contract.rs +++ b/contracts/liquidity_hub/bonding-manager/src/contract.rs @@ -43,7 +43,7 @@ pub fn instantiate( CONFIG.save(deps.storage, &config)?; cw_ownable::initialize_owner(deps.storage, deps.api, Some(info.sender.as_str()))?; - // Initialize the upcoming reward bucket + // Initialize the upcoming reward bucket and bond counter UPCOMING_REWARD_BUCKET.save(deps.storage, &UpcomingRewardBucket::default())?; BOND_COUNTER.save(deps.storage, &0)?; diff --git a/contracts/liquidity_hub/bonding-manager/src/queries.rs b/contracts/liquidity_hub/bonding-manager/src/queries.rs index 5b490bf2..c8926db0 100644 --- a/contracts/liquidity_hub/bonding-manager/src/queries.rs +++ b/contracts/liquidity_hub/bonding-manager/src/queries.rs @@ -166,7 +166,6 @@ pub fn query_claimable( claimable_reward_buckets.retain(|bucket| !bucket.available.is_empty()); } - println!("here: {:?}", claimable_reward_buckets); Ok(ClaimableRewardBucketsResponse { reward_buckets: claimable_reward_buckets, }) diff --git a/contracts/liquidity_hub/bonding-manager/src/state.rs b/contracts/liquidity_hub/bonding-manager/src/state.rs index e9e16ee4..ab0af3e7 100644 --- a/contracts/liquidity_hub/bonding-manager/src/state.rs +++ b/contracts/liquidity_hub/bonding-manager/src/state.rs @@ -137,8 +137,6 @@ pub fn get_bonds_by_receiver( }) .collect::>>()?; - println!("bonds_by_receiver: {:?}", bonds_by_receiver); - if let Some(is_bonding) = is_bonding { bonds_by_receiver.retain(|bond| bond.unbonded_at.is_none() == is_bonding); } diff --git a/contracts/liquidity_hub/bonding-manager/src/tests/bond.rs b/contracts/liquidity_hub/bonding-manager/src/tests/bond.rs index 7d882e5c..11e825dd 100644 --- a/contracts/liquidity_hub/bonding-manager/src/tests/bond.rs +++ b/contracts/liquidity_hub/bonding-manager/src/tests/bond.rs @@ -1,6 +1,7 @@ +use cosmwasm_std::{coin, coins}; + use crate::tests::suite::TestingSuite; use crate::ContractError; -use cosmwasm_std::{coin, coins}; #[test] fn test_bond_unsuccessful() { @@ -48,3 +49,34 @@ fn test_bond_unsuccessful() { }, ); } + +#[test] +fn test_same_bond_multiple_times() { + let mut suite = TestingSuite::default(); + let creator = suite.senders[0].clone(); + + suite + .instantiate_default() + .add_one_day() + .create_new_epoch() + .bond( + creator.clone(), + &vec![coin(1_000u128, "bWHALE")], + |result| { + result.unwrap(); + }, + ) + .bond( + creator.clone(), + &vec![coin(2_000u128, "bWHALE")], + |result| { + result.unwrap(); + }, + ) + .query_bonded(Some(creator.clone().to_string()), |res| { + assert_eq!( + res.unwrap().1.bonded_assets, + vec![coin(3_000u128, "bWHALE")] + ); + }); +}