Skip to content

Commit

Permalink
fix: lint and fmt for alliance lp hub
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 committed Feb 5, 2024
1 parent 1b5ce68 commit b005fba
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 52 deletions.
51 changes: 26 additions & 25 deletions contracts/alliance-lp-hub/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ pub fn instantiate(
let config = Config {
governance: governance_address,
controller: controller_address,
fee_collector_addr: fee_collector_addr,
fee_collector_addr,

astro_incentives_addr: astro_incentives_addr,
astro_incentives_addr,
astro_reward_denom: msg.astro_reward_denom,

alliance_token_denom: "".to_string(),
Expand Down Expand Up @@ -100,7 +100,7 @@ pub fn execute(
// User interactions Stake, Unstake and ClaimRewards
ExecuteMsg::Receive(cw20_msg) => {
let sender = deps.api.addr_validate(&cw20_msg.sender)?;
let received_asset = Asset::cw20(info.sender.clone(), cw20_msg.amount);
let received_asset = Asset::cw20(info.sender, cw20_msg.amount);

stake(deps, env, sender, received_asset)
}
Expand Down Expand Up @@ -241,8 +241,7 @@ fn stake(
env,
)?;
res = res.add_message(msg);
let astro_reward_token =
AssetInfoKey::from(AssetInfo::Native(config.astro_reward_denom.clone()));
let astro_reward_token = AssetInfoKey::from(AssetInfo::Native(config.astro_reward_denom));
let received_asset_key = AssetInfoKey::from(received_asset.info.clone());
let astro_rewards = _claim_astro_rewards(
deps.storage,
Expand Down Expand Up @@ -303,7 +302,7 @@ fn _create_astro_deposit_msg(
// If the asset is native, we need to send it to the astro incentives contract
// using the ExecuteAstroMsg::Deposit message
CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: astro_incentives_addr.to_string(),
contract_addr: astro_incentives_addr,
msg: to_json_binary(&ExecuteAstroMsg::Deposit { recipient: None })?,
funds: vec![CwCoin {
denom: native_asset,
Expand Down Expand Up @@ -370,7 +369,7 @@ fn unstake(

let mut res = Response::new().add_attributes(vec![
("action", "unstake_alliance_lp"),
("user", sender.clone().as_ref()),
("user", sender.as_ref()),
("asset", &asset.info.to_string()),
("amount", &asset.amount.to_string()),
]);
Expand Down Expand Up @@ -398,7 +397,7 @@ fn unstake(
let astro_rewards = _claim_astro_rewards(
deps.storage,
sender.clone(),
AssetInfoKey::from(deposit_asset.clone()),
deposit_asset.clone(),
astro_reward_token.clone(),
)?;

Expand All @@ -414,7 +413,7 @@ fn unstake(
let withdraw_msg: CosmosMsg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: config.astro_incentives_addr.to_string(),
msg: to_json_binary(&ExecuteAstroMsg::Withdraw {
lp_token: lp_token,
lp_token,
amount: asset.amount,
})?,
funds: vec![],
Expand All @@ -424,7 +423,7 @@ fn unstake(

// Subtract the amount from the user balance and the total balance
// since these tokens will be send to the user on the callback function
let balance_key = (sender.clone(), deposit_asset.clone());
let balance_key = (sender, deposit_asset.clone());
BALANCES.update(deps.storage, balance_key, |b| -> Result<_, ContractError> {
match b {
Some(b) => {
Expand All @@ -439,7 +438,7 @@ fn unstake(
})?;
TOTAL_BALANCES.update(
deps.storage,
deposit_asset.clone(),
deposit_asset,
|b| -> Result<_, ContractError> {
let b = b.unwrap_or(Uint128::zero());
if b < asset.amount {
Expand Down Expand Up @@ -518,7 +517,7 @@ fn claim_rewards(
(
user.clone(),
AssetInfoKey::from(deposit_asset.clone()),
alliance_reward_token_key.clone(),
alliance_reward_token_key,
),
);

Expand Down Expand Up @@ -561,19 +560,19 @@ fn claim_rewards(
deps.storage,
(
user.clone(),
AssetInfoKey::from(deposit_asset.clone()),
AssetInfoKey::from(deposit_asset),
astro_reward_token,
),
);
res = res.add_attribute("astro_reward_amount", &final_astro_rewards.to_string());
res = res.add_attribute("astro_reward_amount", final_astro_rewards.to_string());
if !final_astro_rewards.is_zero() {
let info = match deps.api.addr_validate(&config.astro_reward_denom) {
Ok(addr) => AssetInfo::Cw20(addr),
Err(_) => AssetInfo::Native(config.astro_reward_denom.clone()),
};

let rewards_asset = Asset {
info: info,
info,
amount: final_astro_rewards,
};
res = res.add_message(rewards_asset.transfer_msg(&user)?)
Expand All @@ -591,12 +590,12 @@ fn _claim_alliance_rewards(
let state_key = (user.clone(), staked_asset.clone(), reward_denom.clone());
let user_reward_rate = USER_ASSET_REWARD_RATE.load(storage, state_key.clone());
let asset_reward_rate = ASSET_REWARD_RATE
.load(storage, (staked_asset.clone(), reward_denom.clone()))
.load(storage, (staked_asset.clone(), reward_denom))
.unwrap_or_default();

if let Ok(user_reward_rate) = user_reward_rate {
let user_staked = BALANCES
.load(storage, (user.clone(), staked_asset.clone()))
.load(storage, (user, staked_asset))
.unwrap_or_default();
let user_staked = Decimal::from_atomics(user_staked, 0)?;
let rewards = ((asset_reward_rate - user_reward_rate) * user_staked).to_uint_floor();
Expand All @@ -620,15 +619,15 @@ fn _claim_astro_rewards(
reward_denom: AssetInfoKey,
) -> Result<Uint128, ContractError> {
let state_key: (Addr, AssetInfoKey, AssetInfoKey) =
(user.clone(), staked_asset.clone(), reward_denom.clone());
(user, staked_asset.clone(), reward_denom.clone());
let user_reward_rate = USER_ASSET_REWARD_RATE.load(storage, state_key.clone());
let asset_reward_rate = ASSET_REWARD_RATE
.load(storage, (staked_asset.clone(), reward_denom.clone()))
.load(storage, (staked_asset.clone(), reward_denom))
.unwrap_or_default();

if let Ok(user_reward_rate) = user_reward_rate {
let total_staked = TOTAL_BALANCES
.load(storage, staked_asset.clone())
.load(storage, staked_asset)
.unwrap_or_default();
let user_staked = Decimal::from_atomics(total_staked, 0)?;
let rewards = ((asset_reward_rate - user_reward_rate) * user_staked).to_uint_floor();
Expand Down Expand Up @@ -760,7 +759,8 @@ fn _update_astro_rewards(
let (asset_info, _) = f?;
let asset_info = asset_info.check(deps.api, None)?;
let asset_string = asset_info.to_string();
let asset_denom = asset_string.split(":").collect::<Vec<&str>>()[1].to_string();
let splitter = char::from_str(":").unwrap();
let asset_denom = asset_string.split(splitter).collect::<Vec<&str>>()[1].to_string();

whitelist.push(asset_denom);
}
Expand Down Expand Up @@ -945,7 +945,7 @@ fn update_alliance_reward_callback(
}
TEMP_BALANCE.remove(
deps.storage,
AssetInfoKey::from(AssetInfo::Native(config.alliance_reward_denom.to_string())),
AssetInfoKey::from(AssetInfo::Native(config.alliance_reward_denom)),
);

Ok(res)
Expand Down Expand Up @@ -1051,10 +1051,11 @@ fn reply_claim_astro_rewards(
// Check if the callback comes from the correct contract
let first_attr = event.attributes[0].clone();
let event_key = first_attr.key.clone();
let even_value = first_attr.value.clone();
if event_key != "_contract_address" && even_value != config.astro_incentives_addr {
let event_value = first_attr.value;
if event_key != "_contract_address" && event_value != config.astro_incentives_addr {
return Err(ContractError::InvalidContractCallback(
event_key, even_value,
event_key,
event_value,
));
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/alliance-lp-hub/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct InstantiateMsg {

pub astro_reward_denom: String,
pub astro_incentives_addr: String,

pub alliance_reward_denom: String,
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/alliance-lp-hub/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn get_all_pending_rewards(deps: Deps, query: AllPendingRewardsQuery) -> StdResu
})
} else {
let total_balances = TOTAL_BALANCES
.load(deps.storage, deposit_asset.clone())
.load(deps.storage, deposit_asset)
.unwrap_or_default();

let alliance_pending_rewards =
Expand Down
3 changes: 2 additions & 1 deletion contracts/alliance-lp-hub/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ pub const USER_ASSET_REWARD_RATE: Map<(Addr, AssetInfoKey, AssetInfoKey), Decima
// - AssetInfoKey: is the asset that is being deposited,
// - AssetInfoKey: is the asset that is being rewarded,
// - Decimal: is the reward rate,
pub const UNCLAIMED_REWARDS: Map<(Addr, AssetInfoKey, AssetInfoKey), Uint128> = Map::new("unclaimed_rewards");
pub const UNCLAIMED_REWARDS: Map<(Addr, AssetInfoKey, AssetInfoKey), Uint128> =
Map::new("unclaimed_rewards");

pub const TEMP_BALANCE: Map<AssetInfoKey, Uint128> = Map::new("temp_balance");

Expand Down
28 changes: 23 additions & 5 deletions contracts/alliance-lp-hub/src/tests/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,35 @@ pub fn set_alliance_asset(deps: DepsMut) {
.unwrap();
}

pub fn modify_asset(deps: DepsMut, assets: Vec<ModifyAssetPair>) -> Result<Response, ContractError> {
pub fn modify_asset(
deps: DepsMut,
assets: Vec<ModifyAssetPair>,
) -> Result<Response, ContractError> {
let info = mock_info("gov", &[]);
let env = mock_env();

let msg = ExecuteMsg::ModifyAssetPairs(assets);
execute(deps, env, info, msg)
}

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

pub fn stake_cw20(deps: DepsMut, user: &str, amount: u128, denom: &str) -> Result<Response, ContractError> {
pub fn stake_cw20(
deps: DepsMut,
user: &str,
amount: u128,
denom: &str,
) -> Result<Response, ContractError> {
let mut info = mock_info(user, &[]);
let env = mock_env();
let msg = ExecuteMsg::Receive(Cw20ReceiveMsg {
Expand All @@ -79,10 +92,15 @@ pub fn unstake(deps: DepsMut, user: &str, asset: Asset) -> Result<Response, Cont
execute(deps, env, info, msg)
}

pub fn unstake_callback(deps: DepsMut, sender:&str, user: &str, asset: Asset) -> Result<Response, ContractError> {
pub fn unstake_callback(
deps: DepsMut,
sender: &str,
user: &str,
asset: Asset,
) -> Result<Response, ContractError> {
let info = mock_info(sender, &[]);
let env = mock_env();
let msg = ExecuteMsg::UnstakeCallback(asset,Addr::unchecked(user));
let msg = ExecuteMsg::UnstakeCallback(asset, Addr::unchecked(user));
execute(deps, env, info, msg)
}

Expand Down
21 changes: 9 additions & 12 deletions contracts/alliance-lp-hub/src/tests/mock_querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ pub fn mock_dependencies(
) -> OwnedDeps<MockStorage, MockApi, WasmMockQuerier> {
let custom_querier: WasmMockQuerier = match balance {
Some(b) => {
let balances = vec![
(ASTRO_MOCK_CONTRACT_ADDR, b),
(MOCK_CONTRACT_ADDR, b)
];

let balances = vec![(ASTRO_MOCK_CONTRACT_ADDR, b), (MOCK_CONTRACT_ADDR, b)];

WasmMockQuerier::new(MockQuerier::new(&balances))
},
}
None => WasmMockQuerier::new(MockQuerier::new(&[(ASTRO_MOCK_CONTRACT_ADDR, &[])])),
};
// MockQuerier::default()
Expand Down Expand Up @@ -74,36 +71,36 @@ impl WasmMockQuerier {
return SystemResult::Ok(to_json_binary(&msg).into());
}
let msg: Vec<RewardInfo> = vec![];
return SystemResult::Ok(to_json_binary(&msg).into());
SystemResult::Ok(to_json_binary(&msg).into())
}
QueryAstroMsg::PendingRewards { lp_token, user: _ } => {
if lp_token == "factory/astro_native" {
let msg = vec![Asset {
info: AssetInfoBase::native(lp_token.to_string()),
info: AssetInfoBase::native(lp_token),
amount: Uint128::one(),
}];
return SystemResult::Ok(to_json_binary(&msg).into());
} else if lp_token == "terra_astro_cw20" {
let msg = vec![Asset {
info: AssetInfoBase::cw20(Addr::unchecked(lp_token.to_string())),
info: AssetInfoBase::cw20(Addr::unchecked(lp_token)),
amount: Uint128::one(),
}];
return SystemResult::Ok(to_json_binary(&msg).into());
}

let msg = vec![Asset {
info: AssetInfoBase::cw20(Addr::unchecked(lp_token.to_string())),
info: AssetInfoBase::cw20(Addr::unchecked(lp_token)),
amount: Uint128::zero(),
}];
return SystemResult::Ok(to_json_binary(&msg).into());
SystemResult::Ok(to_json_binary(&msg).into())
}
QueryAstroMsg::Deposit { lp_token, user: _ } => {
if lp_token == "factory/astro_native" {
return SystemResult::Ok(to_json_binary(&Uint128::one()).into());
} else if lp_token == "terra_astro_cw20" {
return SystemResult::Ok(to_json_binary(&Uint128::new(50)).into());
}
return SystemResult::Ok(to_json_binary(&Uint128::zero()).into());
SystemResult::Ok(to_json_binary(&Uint128::zero()).into())
}
},
_ => self.base.handle_query(request),
Expand Down
2 changes: 1 addition & 1 deletion contracts/alliance-lp-hub/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ mod alliance;
mod helpers;
mod instantiate;
mod mock_querier;
mod modify_asset;
mod rewards;
mod stake_unstake;
mod modify_asset;
2 changes: 1 addition & 1 deletion contracts/alliance-lp-hub/src/tests/modify_asset.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::models::ModifyAssetPair;
use crate::tests::helpers::{modify_asset, setup_contract, stake};
use alliance_protocol::error::ContractError;
use cosmwasm_std::{Response, testing::mock_dependencies};
use cosmwasm_std::{testing::mock_dependencies, Response};
use cw_asset::AssetInfo;

#[test]
Expand Down
10 changes: 5 additions & 5 deletions contracts/alliance-lp-hub/src/tests/stake_unstake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use crate::tests::helpers::{
use crate::tests::mock_querier::mock_dependencies;
use alliance_protocol::error::ContractError;
use cosmwasm_std::testing::{mock_env, mock_info};
use cosmwasm_std::{coin, to_json_binary, Addr, Coin, CosmosMsg, Response, Uint128, WasmMsg};
use cosmwasm_std::{
coin, coins, to_json_binary, Addr, Coin, CosmosMsg, Response, Uint128, WasmMsg,
};
use cw20::{Cw20ExecuteMsg, Cw20ReceiveMsg};
use cw_asset::{Asset, AssetInfo, AssetInfoKey};

Expand Down Expand Up @@ -377,7 +379,7 @@ fn test_unstake() {

#[test]
fn test_unstake_cw20_from_astro() {
let mut deps = mock_dependencies(Some(&vec![coin(100, "terra_astro_cw20")]));
let mut deps = mock_dependencies(Some(&coins(100, "terra_astro_cw20")));
setup_contract(deps.as_mut());

modify_asset(
Expand Down Expand Up @@ -474,9 +476,7 @@ fn test_unstake_cw20_from_astro() {
res.unwrap(),
Response::new()
.add_message(asset_info.transfer_msg(Addr::unchecked("user1")).unwrap())
.add_attributes(vec![
("action", "unstake_alliance_lp_callback"),
])
.add_attributes(vec![("action", "unstake_alliance_lp_callback"),])
);

let balance = BALANCES
Expand Down

0 comments on commit b005fba

Please sign in to comment.