Skip to content

Commit

Permalink
fix: lint and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 committed Feb 12, 2024
1 parent c1ad4c8 commit b0cbc68
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 85 deletions.
18 changes: 10 additions & 8 deletions contracts/alliance-hub/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ fn get_pending_rewards(deps: Deps, asset_query: AssetQuery) -> StdResult<Binary>
let key = (addr, AssetInfoKey::from(asset_query.asset.clone()));
let asset_reward_rate =
ASSET_REWARD_RATE.load(deps.storage, AssetInfoKey::from(asset_query.asset.clone()))?;
let user_reward_rate = USER_ASSET_REWARD_RATE.load(deps.storage, key.clone()).unwrap_or(asset_reward_rate);
let user_reward_rate = USER_ASSET_REWARD_RATE
.load(deps.storage, key.clone())
.unwrap_or(asset_reward_rate);
let user_balance = BALANCES.load(deps.storage, key.clone()).unwrap_or_default();
let unclaimed_rewards = UNCLAIMED_REWARDS
.load(deps.storage, key)
Expand All @@ -103,9 +105,7 @@ fn get_all_staked_balances(deps: Deps, asset_query: AllStakedBalancesQuery) -> S
let checked_asset_info = asset_key.check(deps.api, None)?;
let asset_info_key = AssetInfoKey::from(checked_asset_info.clone());
let stake_key = (addr.clone(), asset_info_key);
let balance = BALANCES
.load(deps.storage, stake_key)
.unwrap_or_default();
let balance = BALANCES.load(deps.storage, stake_key).unwrap_or_default();

// Append the request
res.push(StakedBalanceRes {
Expand All @@ -128,10 +128,12 @@ fn get_all_pending_rewards(deps: Deps, query: AllPendingRewardsQuery) -> StdResu
let asset = asset.check(deps.api, None)?;
let asset_reward_rate =
ASSET_REWARD_RATE.load(deps.storage, AssetInfoKey::from(asset.clone()))?;
let user_balance = BALANCES.load(
deps.storage,
(addr.clone(), AssetInfoKey::from(asset.clone())),
).unwrap_or_default();
let user_balance = BALANCES
.load(
deps.storage,
(addr.clone(), AssetInfoKey::from(asset.clone())),
)
.unwrap_or_default();
let unclaimed_rewards = UNCLAIMED_REWARDS
.load(
deps.storage,
Expand Down
21 changes: 8 additions & 13 deletions contracts/alliance-hub/src/tests/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,18 +372,17 @@ fn claim_user_rewards_without_stake() {
mock_info("cosmos2contract", &[]),
ExecuteMsg::UpdateRewardsCallback {},
)
.unwrap();
.unwrap();

let res = claim_rewards(deps.as_mut(), "user2", "aWHALE");
assert_eq!(
res,
Response::new()
.add_attributes(vec![
("action", "claim_rewards"),
("user", "user2"),
("asset", "native:aWHALE"),
("reward_amount", "0"),
])
Response::new().add_attributes(vec![
("action", "claim_rewards"),
("user", "user2"),
("asset", "native:aWHALE"),
("reward_amount", "0"),
])
);

USER_ASSET_REWARD_RATE
Expand All @@ -407,13 +406,9 @@ fn claim_user_rewards_without_stake() {
);

let all_rewards = query_all_rewards(deps.as_ref(), "user2");
assert_eq!(
all_rewards,
vec![]
);
assert_eq!(all_rewards, vec![]);
}


#[test]
fn claim_user_rewards_after_staking() {
let mut deps = mock_dependencies_with_balance(&[coin(2000000, "uluna")]);
Expand Down
14 changes: 3 additions & 11 deletions contracts/alliance-lp-hub/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct InstantiateMsg {

pub astro_incentives_addr: String,
pub alliance_reward_denom: AssetInfo,

pub alliance_token_subdenom: String,
}

Expand Down Expand Up @@ -124,19 +124,11 @@ pub struct PendingRewards {
}

impl PendingRewards {
pub fn default() -> Self {
PendingRewards {
deposit_asset : None,
reward_asset: None,
rewards: Uint128::zero(),
}
}

pub fn new(deposit_asset: AssetInfo, reward_asset: AssetInfo, rewards: Uint128) -> Self {
PendingRewards {
deposit_asset : Some(deposit_asset),
deposit_asset: Some(deposit_asset),
reward_asset: Some(reward_asset),
rewards: rewards,
rewards,
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions contracts/alliance-lp-hub/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use cosmwasm_std::{to_json_binary, Binary, Decimal, Deps, Env, Order, StdResult,
use cw_asset::{AssetInfo, AssetInfoKey, AssetInfoUnchecked};

use crate::state::{
TOTAL_ASSET_REWARD_RATE, USER_BALANCES, CONFIG, TOTAL_BALANCES, UNCLAIMED_REWARDS, USER_ASSET_REWARD_RATE,
VALIDATORS, WHITELIST,
CONFIG, TOTAL_ASSET_REWARD_RATE, TOTAL_BALANCES, UNCLAIMED_REWARDS, USER_ASSET_REWARD_RATE,
USER_BALANCES, VALIDATORS, WHITELIST,
};

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down Expand Up @@ -130,7 +130,9 @@ fn get_address_staked_balances(
let asset_info_key = AssetInfoKey::from(checked_asset_info.clone());
let stake_key = (addr.clone(), asset_info_key);

let balance = USER_BALANCES.load(deps.storage, stake_key).unwrap_or_default();
let balance = USER_BALANCES
.load(deps.storage, stake_key)
.unwrap_or_default();

// Append the request
res.push(StakedBalanceRes {
Expand Down Expand Up @@ -209,7 +211,7 @@ fn get_address_pending_rewards(deps: Deps, query: AddressPendingRewardsQuery) ->
.unwrap();
let reward_asset_key = AssetInfoKey::from(reward_asset_info.clone());

let user_balance = user_balances.get(&deposit_asset).unwrap().clone();
let user_balance = *user_balances.get(&deposit_asset).unwrap();
let key = (
addr.clone(),
deposit_asset_key.clone(),
Expand Down
4 changes: 2 additions & 2 deletions contracts/alliance-lp-hub/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub const WHITELIST: Map<AssetInfoKey, Decimal> = Map::new("whitelist");
pub const TOTAL_BALANCES: Map<AssetInfoKey, Uint128> = Map::new("total_balances");
pub const USER_BALANCES: Map<(Addr, AssetInfoKey), Uint128> = Map::new("user_balances");

// Keeps track of the alliance validators where the
// alliance virtual token is being staked that way
// Keeps track of the alliance validators where the
// alliance virtual token is being staked that way
// it can easily operate the operations like unstake.
pub const VALIDATORS: Item<HashSet<String>> = Item::new("validators");

Expand Down
3 changes: 2 additions & 1 deletion contracts/alliance-lp-hub/src/tests/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::contract::{execute, instantiate};
use crate::models::{
AddressPendingRewardsQuery, AssetQuery, Config, ExecuteMsg, InstantiateMsg, ModifyAssetPair, PendingRewardsRes, QueryMsg, StakedBalanceRes
AddressPendingRewardsQuery, AssetQuery, Config, ExecuteMsg, InstantiateMsg, ModifyAssetPair,
PendingRewardsRes, QueryMsg, StakedBalanceRes,
};
use crate::query::query;
use crate::state::CONFIG;
Expand Down
4 changes: 3 additions & 1 deletion contracts/alliance-lp-hub/src/tests/query_rewards.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::models::{AddressPendingRewardsQuery, AssetQuery, PendingRewardsRes, QueryMsg};

use crate::query::query;
use crate::state::{TOTAL_ASSET_REWARD_RATE, USER_BALANCES, UNCLAIMED_REWARDS, USER_ASSET_REWARD_RATE};
use crate::state::{
TOTAL_ASSET_REWARD_RATE, UNCLAIMED_REWARDS, USER_ASSET_REWARD_RATE, USER_BALANCES,
};
use crate::tests::helpers::{set_alliance_asset, setup_contract};
use crate::tests::mock_querier::mock_dependencies;
use cosmwasm_std::testing::mock_env;
Expand Down
75 changes: 32 additions & 43 deletions contracts/alliance-lp-hub/src/tests/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::helpers::from_string_to_asset_info;
use crate::models::{AssetQuery, ExecuteMsg, ModifyAssetPair, PendingRewardsRes, QueryMsg};
use crate::query::query;
use crate::state::{
TOTAL_ASSET_REWARD_RATE, TEMP_BALANCE, TOTAL_BALANCES, USER_ASSET_REWARD_RATE, VALIDATORS, WHITELIST,
TEMP_BALANCE, TOTAL_ASSET_REWARD_RATE, TOTAL_BALANCES, USER_ASSET_REWARD_RATE, VALIDATORS,
WHITELIST,
};
use crate::tests::helpers::{
claim_rewards, modify_asset, query_all_rewards, query_rewards, set_alliance_asset,
Expand Down Expand Up @@ -325,13 +326,13 @@ fn claim_user_rewards() {
Vec::from([
ModifyAssetPair {
asset_info: AssetInfo::Native("aWHALE".to_string()),
asset_distribution: Decimal::percent(50).to_uint_floor(),
asset_distribution: Uint128::new(500000000000000000),
reward_asset_info: Some(AssetInfo::Native("uluna".to_string())),
delete: false,
},
ModifyAssetPair {
asset_info: AssetInfo::Native("bWHALE".to_string()),
asset_distribution: Decimal::percent(50).to_uint_floor(),
asset_distribution: Uint128::new(500000000000000000),
reward_asset_info: Some(AssetInfo::Native("uluna".to_string())),
delete: false,
},
Expand Down Expand Up @@ -427,29 +428,19 @@ fn claim_user_rewards() {
let all_rewards = query_all_rewards(deps.as_ref(), "user1");
assert_eq!(
all_rewards,
vec![
PendingRewardsRes {
rewards: Uint128::zero(),
deposit_asset: AssetInfo::Native("aWHALE".to_string()),
reward_asset: AssetInfo::Cw20(Addr::unchecked("astro_reward_denom".to_string())),
},
PendingRewardsRes {
rewards: Uint128::zero(),
deposit_asset: AssetInfo::Native("aWHALE".to_string()),
reward_asset: AssetInfo::Native("uluna".to_string()),
}
]
vec![PendingRewardsRes {
rewards: Uint128::zero(),
deposit_asset: AssetInfo::Native("aWHALE".to_string()),
reward_asset: AssetInfo::Native("uluna".to_string()),
}]
);

let res = claim_rewards(deps.as_mut(), "user1", "aWHALE");
assert_eq!(
res,
Response::new().add_attributes(vec![
("action", "claim_alliance_lp_rewards"),
("user", "user1"),
("asset", "native:aWHALE"),
("alliance_reward_amount", "0"),
("astro_reward_amount", "0"),
("sender", "user1")
])
);

Expand Down Expand Up @@ -477,10 +468,10 @@ fn claim_user_rewards() {
Response::new()
.add_attributes(vec![
("action", "claim_alliance_lp_rewards"),
("user", "user1"),
("asset", "native:aWHALE"),
("alliance_reward_amount", "10000"),
("astro_reward_amount", "0"),
("sender", "user1"),
("deposit_asset", "native:aWHALE"),
("reward_asset", "native:uluna"),
("rewards_amount", "10000"),
])
.add_message(CosmosMsg::Bank(BankMsg::Send {
to_address: "user1".to_string(),
Expand All @@ -495,30 +486,31 @@ fn claim_user_rewards_after_staking() {
setup_contract(deps.as_mut());
set_alliance_asset(deps.as_mut());

// Whitelist two assets with 50% of asset distribution
// from the alliance staking to each asset
let res = modify_asset(
deps.as_mut(),
Vec::from([
ModifyAssetPair {
asset_info: AssetInfo::Native("aWHALE".to_string()),
asset_distribution: Decimal::percent(50).to_uint_floor(),
asset_distribution: Uint128::new(100000000000000000),
reward_asset_info: Some(AssetInfo::Native("uluna".to_string())),
delete: false,
},
ModifyAssetPair {
asset_info: AssetInfo::Native("bWHALE".to_string()),
asset_distribution: Decimal::percent(50).to_uint_floor(),
asset_distribution: Uint128::new(100000000000000000),
reward_asset_info: Some(AssetInfo::Native("uluna".to_string())),
delete: false,
},
]),
)
.unwrap();
assert_eq!(res, Response::new()
.add_attribute("action", "modify_asset")
.add_attribute("asset", "native:aWHALE")
.add_attribute("asset", "native:bWHALE"));
assert_eq!(
res,
Response::new()
.add_attribute("action", "modify_asset")
.add_attribute("asset", "native:aWHALE")
.add_attribute("asset", "native:bWHALE")
);

let res = stake(deps.as_mut(), "user1", 1000000, "aWHALE").unwrap();
assert_eq!(
Expand Down Expand Up @@ -562,7 +554,7 @@ fn claim_user_rewards_after_staking() {
.add_attribute("action", "update_alliance_rewards_callback")
.add_message(CosmosMsg::Bank(BankMsg::Send {
to_address: "controller".to_string(),
amount: coins(1000000, "uluna"),
amount: coins(800000, "uluna"),
}))
);

Expand All @@ -574,14 +566,14 @@ fn claim_user_rewards_after_staking() {
Response::new()
.add_attributes(vec![
("action", "claim_alliance_lp_rewards"),
("user", "user1"),
("sender", "user1"),
("deposit_asset", "native:aWHALE"),
("reward_asset", "100000"),
("rewards_amount", "0"),
("reward_asset", "native:uluna"),
("rewards_amount", "60000"),
])
.add_message(CosmosMsg::Bank(BankMsg::Send {
to_address: "user1".to_string(),
amount: coins(100000, "uluna"),
amount: coins(60000, "uluna"),
}))
);

Expand All @@ -591,10 +583,7 @@ fn claim_user_rewards_after_staking() {
res,
Response::new().add_attributes(vec![
("action", "claim_alliance_lp_rewards"),
("user", "user1"),
("deposit_asset", "native:aWHALE"),
("reward_asset", "0"),
("rewards_amount", "0"),
("sender", "user1")
])
);
}
Expand All @@ -609,13 +598,13 @@ fn claim_rewards_after_staking_and_unstaking() {
Vec::from([
ModifyAssetPair {
asset_info: AssetInfo::Native("aWHALE".to_string()),
asset_distribution: Uint128::new(1),
asset_distribution: Uint128::new(500000000000000000),
reward_asset_info: Some(AssetInfo::Native("uluna".to_string())),
delete: false,
},
ModifyAssetPair {
asset_info: AssetInfo::Native("bWHALE".to_string()),
asset_distribution: Uint128::new(4),
asset_distribution: Uint128::new(400000000000000000),
reward_asset_info: Some(AssetInfo::Native("uluna".to_string())),
delete: false,
},
Expand Down Expand Up @@ -712,7 +701,7 @@ fn claim_rewards_after_staking_and_unstaking() {
assert_eq!(
res,
PendingRewardsRes {
rewards: Uint128::new(1000000),
rewards: Uint128::new(800000),
deposit_asset: AssetInfo::Native("bWHALE".to_string()),
reward_asset: AssetInfo::Native("uluna".to_string()),
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/alliance-lp-hub/src/tests/stake_unstake.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::astro_models::{Cw20Msg, ExecuteAstroMsg};
use crate::contract::execute;
use crate::models::{ExecuteMsg, ModifyAssetPair, StakedBalanceRes};
use crate::state::{USER_BALANCES, TOTAL_BALANCES};
use crate::state::{TOTAL_BALANCES, USER_BALANCES};
use crate::tests::helpers::{
modify_asset, query_contract_balances, setup_contract, stake, stake_cw20, unstake,
unstake_callback,
Expand Down
2 changes: 1 addition & 1 deletion packages/alliance-protocol/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub enum ContractError {

#[error("Missing reward asset info for asset {0}")]
MissingRewardAsset(String),

#[error("Asset not staked")]
AssetNotStaked {},
}

0 comments on commit b0cbc68

Please sign in to comment.