Skip to content

Commit

Permalink
fix: reward asset and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 committed Jan 23, 2024
1 parent 2b50554 commit 4dc5d45
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 82 deletions.
5 changes: 4 additions & 1 deletion contracts/alliance-hub/src/tests/stake_unstake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ fn test_stake_invalid() {
let msg = ExecuteMsg::Stake {};
let info = mock_info("user1", &[coin(100, "asset2")]);
let err = execute(deps.as_mut(), mock_env(), info, msg).unwrap_err();
assert_eq!(err, ContractError::AssetNotWhitelisted("native:asset2".to_string()));
assert_eq!(
err,
ContractError::AssetNotWhitelisted("native:asset2".to_string())
);

// Stake multiple assets in a single call
let msg = ExecuteMsg::Stake {};
Expand Down
9 changes: 3 additions & 6 deletions contracts/alliance-lp-hub/src/astro_models.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use cosmwasm_schema::{QueryResponses, cw_serde};
use cosmwasm_std::{Decimal, Addr, Uint128};
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Addr, Decimal, Uint128};
use cw20::Cw20ReceiveMsg;
use cw_asset::AssetInfo;



#[cw_serde]
pub enum ExecuteAstroMsg {
/// Receives a message of type [`Cw20ReceiveMsg`]. Handles cw20 LP token deposits.
Expand All @@ -29,7 +27,6 @@ pub enum Cw20Msg {
DepositFor(String),
}


#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryAstroMsg {
Expand Down Expand Up @@ -85,4 +82,4 @@ pub enum AstroAssetInfo {
Token { contract_addr: Addr },
/// Native token
NativeToken { denom: String },
}
}
24 changes: 17 additions & 7 deletions contracts/alliance-lp-hub/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ fn stake(
deps.storage,
sender.clone(),
received_asset.info.clone(),
reward_token,
reward_token.clone(),
)?;
if !rewards.is_zero() {
UNCLAIMED_REWARDS.update(
Expand Down Expand Up @@ -285,6 +285,15 @@ fn stake(
Ok(balance.unwrap_or(Uint128::zero()) + received_asset.amount)
},
)?;

let asset_reward_rate = ASSET_REWARD_RATE
.load(deps.storage, (asset_key.clone(), reward_token.clone()))
.unwrap_or(Decimal::zero());
USER_ASSET_REWARD_RATE.save(
deps.storage,
(sender, asset_key, reward_token),
&asset_reward_rate,
)?;
Ok(res)
}

Expand Down Expand Up @@ -360,7 +369,7 @@ fn claim_rewards(
) -> Result<Response, ContractError> {
let user = info.sender;
let config = CONFIG.load(deps.storage)?;
let reward_token = AssetInfoKey::from(AssetInfo::Native(config.alliance_token_denom));
let reward_token = AssetInfoKey::from(AssetInfo::Native(config.alliance_reward_denom.clone()));
let rewards = _claim_alliance_rewards(deps.storage, user.clone(), asset.clone(), reward_token)?;
let unclaimed_rewards = UNCLAIMED_REWARDS
.load(
Expand Down Expand Up @@ -411,14 +420,14 @@ fn _claim_alliance_rewards(
let rewards = ((asset_reward_rate - user_reward_rate) * user_staked).to_uint_floor();

if rewards.is_zero() {
return Ok(Uint128::zero());
Ok(Uint128::zero())
} else {
USER_ASSET_REWARD_RATE.save(storage, state_key, &asset_reward_rate)?;
return Ok(rewards);
Ok(rewards)
}
} else {
USER_ASSET_REWARD_RATE.save(storage, state_key, &asset_reward_rate)?;
return Ok(Uint128::zero());
Ok(Uint128::zero())
}
}

Expand Down Expand Up @@ -711,7 +720,7 @@ fn update_alliance_reward_callback(
(asset_key.clone(), reward_asset_info_key.clone()),
|rate| -> StdResult<_> {
let mut reward_rate = rate.unwrap_or_default();
reward_rate = reward_rate + rate_to_update;
reward_rate += rate_to_update;
Ok(reward_rate)
},
)?;
Expand Down Expand Up @@ -878,5 +887,6 @@ fn reply_claim_astro_rewards(
)?;
}
}
return Ok(res.add_attribute("action", "claim_astro_rewards_success"));

Ok(res.add_attribute("action", "claim_astro_rewards_success"))
}
1 change: 0 additions & 1 deletion contracts/alliance-lp-hub/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use cosmwasm_std::MessageInfo;

use crate::models::Config;


// Controller is used to perform administrative operations that deals with delegating the virtual
// tokens to the expected validators
pub fn is_controller(info: &MessageInfo, config: &Config) -> Result<(), ContractError> {
Expand Down
4 changes: 2 additions & 2 deletions contracts/alliance-lp-hub/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub mod astro_models;
pub mod contract;
pub mod helpers;
pub mod models;
pub mod query;
pub mod state;
pub mod astro_models;
pub mod helpers;

#[cfg(test)]
mod tests;
4 changes: 1 addition & 3 deletions contracts/alliance-lp-hub/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Addr, Coins, Uint128};
use cw20::Cw20ReceiveMsg;
use cw_asset::{Asset, AssetInfo, AssetInfoKey};
use std::{
collections::{HashMap, HashSet},
};
use std::collections::{HashMap, HashSet};

pub type AssetDenom = String;

Expand Down
7 changes: 4 additions & 3 deletions contracts/alliance-lp-hub/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ fn get_all_pending_rewards(deps: Deps, query: AllPendingRewardsQuery) -> StdResu
let deposit_asset = AssetInfoKey::from(assets.0.check(deps.api, None)?);
let reward_asset = AssetInfoKey::from(assets.1.check(deps.api, None)?);

let asset_reward_rate =
ASSET_REWARD_RATE.load(deps.storage, (deposit_asset.clone(), reward_asset.clone()))?;
let user_balance = BALANCES.load(deps.storage, (addr.clone(), deposit_asset.clone()))?;
let asset_reward_rate = ASSET_REWARD_RATE
.load(deps.storage, (deposit_asset.clone(), reward_asset.clone()))?;
let user_balance =
BALANCES.load(deps.storage, (addr.clone(), deposit_asset.clone()))?;
let unclaimed_rewards = UNCLAIMED_REWARDS
.load(deps.storage, (addr.clone(), deposit_asset))
.unwrap_or_default();
Expand Down
2 changes: 1 addition & 1 deletion contracts/alliance-lp-hub/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ pub const TEMP_BALANCE: Map<AssetInfoKey, Uint128> = Map::new("temp_balance");
// Temporary variable used to store the user address
// so we can access it on reply_claim_astro_rewards
// callback function and account for the rewards
pub const TEMP_USR_ADDR: Item<Addr> = Item::new("temp_addr_stake");
pub const TEMP_USR_ADDR: Item<Addr> = Item::new("temp_addr_stake");
19 changes: 11 additions & 8 deletions contracts/alliance-lp-hub/src/tests/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::contract::{execute, instantiate};
use crate::models::{
AllPendingRewardsQuery, AssetQuery, Config, ExecuteMsg, InstantiateMsg, PendingRewardsRes,
QueryMsg, StakedBalanceRes, ModifyAssetPair,
AllPendingRewardsQuery, AssetQuery, Config, ExecuteMsg, InstantiateMsg, ModifyAssetPair,
PendingRewardsRes, QueryMsg, StakedBalanceRes,
};
use crate::query::query;
use crate::state::CONFIG;
Expand All @@ -11,7 +11,7 @@ use alliance_protocol::alliance_protocol::{
};
use alliance_protocol::token_factory::CustomExecuteMsg;
use cosmwasm_std::testing::{mock_env, mock_info};
use cosmwasm_std::{coin, from_json, Deps, DepsMut, Response, StdResult, Uint128, Binary, Addr};
use cosmwasm_std::{coin, from_json, Addr, Binary, Deps, DepsMut, Response, StdResult, Uint128};
use cw20::Cw20ReceiveMsg;
use cw_asset::{Asset, AssetInfo};

Expand All @@ -24,7 +24,7 @@ pub fn setup_contract(deps: DepsMut) -> Response<CustomExecuteMsg> {
let init_msg = InstantiateMsg {
governance: "gov".to_string(),
fee_collector_address: "collector_address".to_string(),
astro_incentives_address : "astro_incentives".to_string(),
astro_incentives_address: "astro_incentives".to_string(),
controller: "controller".to_string(),
reward_denom: "uluna".to_string(),
};
Expand All @@ -51,19 +51,17 @@ pub fn modify_asset(deps: DepsMut, assets: Vec<ModifyAssetPair>) -> Response {
execute(deps, env, info, msg).unwrap()
}


pub fn stake(deps: DepsMut, user: &str, amount: u128, denom: &str) -> Response {
let info = mock_info(user, &[coin(amount, denom)]);
let env = mock_env();
let msg = ExecuteMsg::Stake {};
execute(deps, env, info, msg).unwrap()
}


pub fn stake_cw20(deps: DepsMut, user: &str, amount: u128, denom: &str) -> Response {
let mut info = mock_info(user, &[]);
let env = mock_env();
let msg = ExecuteMsg::Receive(Cw20ReceiveMsg{
let msg = ExecuteMsg::Receive(Cw20ReceiveMsg {
sender: String::from(user),
amount: Uint128::new(amount),
msg: Binary::default(),
Expand Down Expand Up @@ -133,7 +131,12 @@ pub fn claim_rewards(deps: DepsMut, user: &str, denom: &str) -> Response {
execute(deps, env, info, msg).unwrap()
}

pub fn query_rewards(deps: Deps, user: &str, deposit_asset: &str, reward_asset: &str ) -> PendingRewardsRes {
pub fn query_rewards(
deps: Deps,
user: &str,
deposit_asset: &str,
reward_asset: &str,
) -> PendingRewardsRes {
from_json(
query(
deps,
Expand Down
6 changes: 3 additions & 3 deletions contracts/alliance-lp-hub/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod alliance;
mod helpers;
mod instantiate;
mod stake_unstake;
mod mock_querier;
mod rewards;
mod alliance;
mod mock_querier;
mod stake_unstake;
49 changes: 29 additions & 20 deletions contracts/alliance-lp-hub/src/tests/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,26 +636,35 @@ fn claim_rewards_after_staking_and_unstaking() {
stake(deps.as_mut(), "user1", 1000000, "aWHALE");

// User 1 should not have any rewards
let res = query_rewards(deps.as_ref(), "user1", "aWHALE","uluna");
assert_eq!(res, PendingRewardsRes {
rewards: Uint128::zero(),
deposit_asset: AssetInfo::Native("aWHALE".to_string()),
reward_asset: AssetInfo::Native("uluna".to_string()),
});
let res = query_rewards(deps.as_ref(), "user1", "aWHALE", "uluna");
assert_eq!(
res,
PendingRewardsRes {
rewards: Uint128::zero(),
deposit_asset: AssetInfo::Native("aWHALE".to_string()),
reward_asset: AssetInfo::Native("uluna".to_string()),
}
);

// User 2 should receive all the rewards in the contract
let res = query_rewards(deps.as_ref(), "user2", "aWHALE","uluna");
assert_eq!(res, PendingRewardsRes {
rewards: Uint128::new(900000),
deposit_asset: AssetInfo::Native("aWHALE".to_string()),
reward_asset: AssetInfo::Native("uluna".to_string()),
});
let res = query_rewards(deps.as_ref(), "user2", "bWHALE","uluna");
assert_eq!(res, PendingRewardsRes {
rewards: Uint128::new(1000000),
deposit_asset: AssetInfo::Native("aWHALE".to_string()),
reward_asset: AssetInfo::Native("uluna".to_string()),
});
let res = query_rewards(deps.as_ref(), "user2", "aWHALE", "uluna");
assert_eq!(
res,
PendingRewardsRes {
rewards: Uint128::new(900000),
deposit_asset: AssetInfo::Native("aWHALE".to_string()),
reward_asset: AssetInfo::Native("uluna".to_string()),
}
);
let res = query_rewards(deps.as_ref(), "user2", "bWHALE", "uluna");
assert_eq!(
res,
PendingRewardsRes {
rewards: Uint128::new(1000000),
deposit_asset: AssetInfo::Native("bWHALE".to_string()),
reward_asset: AssetInfo::Native("uluna".to_string()),
}
);
}

#[test]
Expand Down Expand Up @@ -741,9 +750,9 @@ fn claim_rewards_after_rebalancing_emissions() {
)
.unwrap();

let rewards = query_rewards(deps.as_ref(), "user1", "aWHALE","uluna");
let rewards = query_rewards(deps.as_ref(), "user1", "aWHALE", "uluna");
assert_eq!(rewards.rewards, Uint128::new(1500000));
// User 2 should receive all the rewards in the contract
let rewards = query_rewards(deps.as_ref(), "user2", "bWHALE","uluna");
let rewards = query_rewards(deps.as_ref(), "user2", "bWHALE", "uluna");
assert_eq!(rewards.rewards, Uint128::new(500000));
}
51 changes: 25 additions & 26 deletions contracts/alliance-lp-hub/src/tests/stake_unstake.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::astro_models::{ExecuteAstroMsg, Cw20Msg};
use crate::astro_models::{Cw20Msg, ExecuteAstroMsg};
use crate::contract::execute;
use crate::models::{ExecuteMsg, ModifyAssetPair, StakedBalanceRes};
use crate::state::{BALANCES, TOTAL_BALANCES};
Expand All @@ -11,7 +11,7 @@ use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{
coin, to_json_binary, Addr, BankMsg, Coin, CosmosMsg, Response, Uint128, WasmMsg,
};
use cw20::{Cw20ReceiveMsg, Cw20ExecuteMsg};
use cw20::{Cw20ExecuteMsg, Cw20ReceiveMsg};
use cw_asset::{Asset, AssetInfo, AssetInfoKey};

#[test]
Expand Down Expand Up @@ -133,7 +133,6 @@ fn test_stake_astro_token() {
)
.unwrap();
assert_eq!(balance, Uint128::new(100));

}

#[test]
Expand Down Expand Up @@ -227,31 +226,31 @@ fn test_stake_astro_token_cw20() {
let res = stake_cw20(deps.as_mut(), "user1", 100, "astro_existent_cw20");
assert_eq!(
res,
Response::default().add_attributes(vec![
("action", "stake"),
("user", "user1"),
("asset", "cw20:astro_existent_cw20"),
("amount", "100"),
])
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: "astro_existent_cw20".to_string(),
msg: to_json_binary(&Cw20ExecuteMsg::Send {
contract: "astro_incentives".to_string(),
amount: Uint128::new(100),
msg: to_json_binary(&Cw20ReceiveMsg {
sender: "cosmos2contract".to_string(),
Response::default()
.add_attributes(vec![
("action", "stake"),
("user", "user1"),
("asset", "cw20:astro_existent_cw20"),
("amount", "100"),
])
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: "astro_existent_cw20".to_string(),
msg: to_json_binary(&Cw20ExecuteMsg::Send {
contract: "astro_incentives".to_string(),
amount: Uint128::new(100),
msg: to_json_binary(&Cw20Msg::Deposit {
recipient: None,
}).unwrap(),
}).unwrap(),
}).unwrap(),
funds: vec![],
}))
msg: to_json_binary(&Cw20ReceiveMsg {
sender: "cosmos2contract".to_string(),
amount: Uint128::new(100),
msg: to_json_binary(&Cw20Msg::Deposit { recipient: None }).unwrap(),
})
.unwrap(),
})
.unwrap(),
funds: vec![],
}))
);
}


#[test]
fn test_unstake() {
let mut deps = mock_dependencies();
Expand All @@ -266,9 +265,9 @@ fn test_unstake() {
}],
);
stake(deps.as_mut(), "user1", 100, "native_asset");

let asset_info = Asset::native(Addr::unchecked("native_asset"), 50u128);
let res = unstake(deps.as_mut(), "user1",asset_info);
let res = unstake(deps.as_mut(), "user1", asset_info);
assert_eq!(
res,
Response::default()
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 @@ -50,7 +50,7 @@ pub enum ContractError {

#[error("Invalid contract callback with key: {0} and type: {1}")]
InvalidContractCallback(String, String),

#[error("Invalid denom: {0}")]
InvalidDenom(String),

Expand Down

0 comments on commit 4dc5d45

Please sign in to comment.