From b005fba78ba840c35d8e6347ce1f908cea901274 Mon Sep 17 00:00:00 2001 From: emidev98 Date: Mon, 5 Feb 2024 09:14:27 +0200 Subject: [PATCH] fix: lint and fmt for alliance lp hub --- contracts/alliance-lp-hub/src/contract.rs | 51 ++++++++++--------- contracts/alliance-lp-hub/src/models.rs | 2 +- contracts/alliance-lp-hub/src/query.rs | 2 +- contracts/alliance-lp-hub/src/state.rs | 3 +- .../alliance-lp-hub/src/tests/helpers.rs | 28 ++++++++-- .../alliance-lp-hub/src/tests/mock_querier.rs | 21 ++++---- contracts/alliance-lp-hub/src/tests/mod.rs | 2 +- .../alliance-lp-hub/src/tests/modify_asset.rs | 2 +- .../src/tests/stake_unstake.rs | 10 ++-- 9 files changed, 69 insertions(+), 52 deletions(-) diff --git a/contracts/alliance-lp-hub/src/contract.rs b/contracts/alliance-lp-hub/src/contract.rs index 70f7c5c..299f087 100644 --- a/contracts/alliance-lp-hub/src/contract.rs +++ b/contracts/alliance-lp-hub/src/contract.rs @@ -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(), @@ -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) } @@ -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, @@ -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, @@ -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()), ]); @@ -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(), )?; @@ -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![], @@ -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) => { @@ -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 { @@ -518,7 +517,7 @@ fn claim_rewards( ( user.clone(), AssetInfoKey::from(deposit_asset.clone()), - alliance_reward_token_key.clone(), + alliance_reward_token_key, ), ); @@ -561,11 +560,11 @@ 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), @@ -573,7 +572,7 @@ fn claim_rewards( }; let rewards_asset = Asset { - info: info, + info, amount: final_astro_rewards, }; res = res.add_message(rewards_asset.transfer_msg(&user)?) @@ -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(); @@ -620,15 +619,15 @@ fn _claim_astro_rewards( reward_denom: AssetInfoKey, ) -> Result { 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(); @@ -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::>()[1].to_string(); + let splitter = char::from_str(":").unwrap(); + let asset_denom = asset_string.split(splitter).collect::>()[1].to_string(); whitelist.push(asset_denom); } @@ -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) @@ -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, )); } diff --git a/contracts/alliance-lp-hub/src/models.rs b/contracts/alliance-lp-hub/src/models.rs index 3cfe77c..2e822cc 100644 --- a/contracts/alliance-lp-hub/src/models.rs +++ b/contracts/alliance-lp-hub/src/models.rs @@ -34,7 +34,7 @@ pub struct InstantiateMsg { pub astro_reward_denom: String, pub astro_incentives_addr: String, - + pub alliance_reward_denom: String, } diff --git a/contracts/alliance-lp-hub/src/query.rs b/contracts/alliance-lp-hub/src/query.rs index acb0b4d..b4963c8 100644 --- a/contracts/alliance-lp-hub/src/query.rs +++ b/contracts/alliance-lp-hub/src/query.rs @@ -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 = diff --git a/contracts/alliance-lp-hub/src/state.rs b/contracts/alliance-lp-hub/src/state.rs index 8040a28..01d18e4 100644 --- a/contracts/alliance-lp-hub/src/state.rs +++ b/contracts/alliance-lp-hub/src/state.rs @@ -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 = Map::new("temp_balance"); diff --git a/contracts/alliance-lp-hub/src/tests/helpers.rs b/contracts/alliance-lp-hub/src/tests/helpers.rs index 6acdcc5..1b86c01 100644 --- a/contracts/alliance-lp-hub/src/tests/helpers.rs +++ b/contracts/alliance-lp-hub/src/tests/helpers.rs @@ -45,7 +45,10 @@ pub fn set_alliance_asset(deps: DepsMut) { .unwrap(); } -pub fn modify_asset(deps: DepsMut, assets: Vec) -> Result { +pub fn modify_asset( + deps: DepsMut, + assets: Vec, +) -> Result { let info = mock_info("gov", &[]); let env = mock_env(); @@ -53,14 +56,24 @@ pub fn modify_asset(deps: DepsMut, assets: Vec) -> Result Result { +pub fn stake( + deps: DepsMut, + user: &str, + amount: u128, + denom: &str, +) -> Result { 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 { +pub fn stake_cw20( + deps: DepsMut, + user: &str, + amount: u128, + denom: &str, +) -> Result { let mut info = mock_info(user, &[]); let env = mock_env(); let msg = ExecuteMsg::Receive(Cw20ReceiveMsg { @@ -79,10 +92,15 @@ pub fn unstake(deps: DepsMut, user: &str, asset: Asset) -> Result Result { +pub fn unstake_callback( + deps: DepsMut, + sender: &str, + user: &str, + asset: Asset, +) -> Result { 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) } diff --git a/contracts/alliance-lp-hub/src/tests/mock_querier.rs b/contracts/alliance-lp-hub/src/tests/mock_querier.rs index 4ff4879..8460d82 100644 --- a/contracts/alliance-lp-hub/src/tests/mock_querier.rs +++ b/contracts/alliance-lp-hub/src/tests/mock_querier.rs @@ -16,13 +16,10 @@ pub fn mock_dependencies( ) -> OwnedDeps { 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() @@ -74,28 +71,28 @@ impl WasmMockQuerier { return SystemResult::Ok(to_json_binary(&msg).into()); } let msg: Vec = 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" { @@ -103,7 +100,7 @@ impl WasmMockQuerier { } 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), diff --git a/contracts/alliance-lp-hub/src/tests/mod.rs b/contracts/alliance-lp-hub/src/tests/mod.rs index eeddb74..bece28e 100644 --- a/contracts/alliance-lp-hub/src/tests/mod.rs +++ b/contracts/alliance-lp-hub/src/tests/mod.rs @@ -2,6 +2,6 @@ mod alliance; mod helpers; mod instantiate; mod mock_querier; +mod modify_asset; mod rewards; mod stake_unstake; -mod modify_asset; \ No newline at end of file diff --git a/contracts/alliance-lp-hub/src/tests/modify_asset.rs b/contracts/alliance-lp-hub/src/tests/modify_asset.rs index f5d9018..3307d7d 100644 --- a/contracts/alliance-lp-hub/src/tests/modify_asset.rs +++ b/contracts/alliance-lp-hub/src/tests/modify_asset.rs @@ -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] diff --git a/contracts/alliance-lp-hub/src/tests/stake_unstake.rs b/contracts/alliance-lp-hub/src/tests/stake_unstake.rs index e2bac17..552934c 100644 --- a/contracts/alliance-lp-hub/src/tests/stake_unstake.rs +++ b/contracts/alliance-lp-hub/src/tests/stake_unstake.rs @@ -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}; @@ -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( @@ -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