Skip to content

Commit

Permalink
chore: final refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kerber0x committed May 17, 2024
1 parent ca3db75 commit 23586f6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
30 changes: 15 additions & 15 deletions contracts/liquidity_hub/bonding-manager/src/bonding/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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))?;

Expand All @@ -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

Check warning on line 63 in contracts/liquidity_hub/bonding-manager/src/bonding/commands.rs

View check run for this annotation

Codecov / codecov/patch

contracts/liquidity_hub/bonding-manager/src/bonding/commands.rs#L63

Added line #L63 was not covered by tests
);

//todo change this error
bonds_by_receiver
.pop()
.ok_or(ContractError::NothingToUnbond)?
bonds_by_receiver[0].clone()
};

// update bond values
Expand Down Expand Up @@ -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),
Expand All @@ -121,17 +119,19 @@ pub(crate) fn unbond(

ensure!(
bonds_by_receiver.len() <= 1usize,
//todo change this error
ContractError::NothingToUnbond
ContractError::AssetMismatch

Check warning on line 122 in contracts/liquidity_hub/bonding-manager/src/bonding/commands.rs

View check run for this annotation

Codecov / codecov/patch

contracts/liquidity_hub/bonding-manager/src/bonding/commands.rs#L122

Added line #L122 was not covered by tests
);

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

Check warning on line 131 in contracts/liquidity_hub/bonding-manager/src/bonding/commands.rs

View check run for this annotation

Codecov / codecov/patch

contracts/liquidity_hub/bonding-manager/src/bonding/commands.rs#L131

Added line #L131 was not covered by tests
);

let mut unbond = bonds_by_receiver[0].clone();

// check if the address has enough bond
ensure!(
Expand Down
2 changes: 1 addition & 1 deletion contracts/liquidity_hub/bonding-manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down
1 change: 0 additions & 1 deletion contracts/liquidity_hub/bonding-manager/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
2 changes: 0 additions & 2 deletions contracts/liquidity_hub/bonding-manager/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ pub fn get_bonds_by_receiver(
})
.collect::<StdResult<Vec<Bond>>>()?;

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);
}
Expand Down
34 changes: 33 additions & 1 deletion contracts/liquidity_hub/bonding-manager/src/tests/bond.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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")]
);
});
}

0 comments on commit 23586f6

Please sign in to comment.