From 374eea0f1314b5f43ab9394401fab05cb47e46f5 Mon Sep 17 00:00:00 2001 From: pr0n00gler Date: Thu, 21 Mar 2024 16:49:01 +0200 Subject: [PATCH 1/2] update pool info on LP deposit to reflect implicit rewards claim --- contracts/lockdrop-pcl/src/contract.rs | 68 ++++++++++++++++++++------ 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/contracts/lockdrop-pcl/src/contract.rs b/contracts/lockdrop-pcl/src/contract.rs index 4581dab1..16a6c21f 100644 --- a/contracts/lockdrop-pcl/src/contract.rs +++ b/contracts/lockdrop-pcl/src/contract.rs @@ -5,25 +5,25 @@ use std::str::FromStr; use astroport::asset::{Asset, AssetInfo}; use astroport::common::{claim_ownership, drop_ownership_proposal, propose_new_owner}; use astroport::cosmwasm_ext::IntegerToDecimal; +use astroport::DecimalCheckedOps; use astroport::incentives::{ExecuteMsg as IncentivesExecuteMsg, QueryMsg as IncentivesQueryMsg}; use astroport::pair::ExecuteMsg::ProvideLiquidity; use astroport::restricted_vector::RestrictedVector; -use astroport::DecimalCheckedOps; use cosmwasm_std::{ - attr, coins, entry_point, to_json_binary, Addr, BankMsg, Binary, CosmosMsg, Decimal, - Decimal256, Deps, DepsMut, Empty, Env, MessageInfo, Order, Response, StdError, StdResult, + Addr, attr, BankMsg, Binary, coins, CosmosMsg, Decimal, Decimal256, Deps, + DepsMut, Empty, entry_point, Env, MessageInfo, Order, Response, StdError, StdResult, to_json_binary, Uint128, Uint256, WasmMsg, }; -use cw2::set_contract_version; use cw20::{BalanceResponse, Cw20ExecuteMsg, Cw20QueryMsg, MinterResponse}; +use cw2::set_contract_version; use astroport_periphery::lockdrop::{ LockupInfoV2 as LockdropXYKLockupInfoV2, PoolType as LockdropXYKPoolType, UserInfo as LockdropXYKUserInfo, }; use astroport_periphery::lockdrop_pcl::{ - CallbackMsg, Config, ExecuteMsg, InstantiateMsg, LockUpInfoResponse, LockUpInfoSummary, - LockupInfo, MigrateMsg, PoolInfo, PoolType, QueryMsg, State, StateResponse, UpdateConfigMsg, + CallbackMsg, Config, ExecuteMsg, InstantiateMsg, LockupInfo, LockUpInfoResponse, + LockUpInfoSummary, MigrateMsg, PoolInfo, PoolType, QueryMsg, State, StateResponse, UpdateConfigMsg, UserInfo, UserInfoResponse, UserInfoWithListResponse, }; use astroport_periphery::utils::Decimal256CheckedOps; @@ -371,8 +371,9 @@ pub fn handle_migrate_xyk_liquidity( }, )?; + let mut cosmos_msgs: Vec> = vec![]; // provide the transferred liquidity to the PCL pool - let mut cosmos_msgs: Vec> = vec![CosmosMsg::Wasm(WasmMsg::Execute { + cosmos_msgs.push(CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: astroport_pool.to_string(), funds: info.funds.clone(), msg: to_json_binary(&ProvideLiquidity { @@ -390,7 +391,46 @@ pub fn handle_migrate_xyk_liquidity( auto_stake: Some(true), receiver: None, })?, - })]; + })); + + let incentives = &config.incentives; + + // QUERY :: Check if there are any pending staking rewards + let pending_rewards_response: StdResult> = deps.querier.query_wasm_smart( + incentives, + &IncentivesQueryMsg::PendingRewards { + lp_token: pool_info.lp_token.to_string(), + user: env.contract.address.to_string(), + }, + ); + + // the incentives contract claims rewards on LP Deposit: https://github.com/astroport-fi/astroport-core/blob/514d83331da3232111c5c590fd8086ef62025ca9/contracts/tokenomics/incentives/src/execute.rs#L190 + // thus we need update pool info after the deposit otherwise there will be unaccounted rewards on PCL lockdrop during users migration + if let Ok(pending_rewards) = pending_rewards_response { + let prev_pending_rewards_balances: Vec = pending_rewards + .iter() + .map(|asset| { + let balance = asset + .info + .query_pool(&deps.querier, env.contract.address.clone()) + .unwrap_or_default(); + + Asset { + info: asset.info.clone(), + amount: balance, + } + }) + .collect(); + + cosmos_msgs.push( + CallbackMsg::UpdatePoolOnDualRewardsClaim { + pool_type: pool_type.into(), + prev_reward_balances: prev_pending_rewards_balances, + } + .to_cosmos_msg(&env)?, + ); + } + // invoke callback that creates a lockup entry for the provided liquidity cosmos_msgs.push( CallbackMsg::FinishLockupMigrationCallback { @@ -402,7 +442,7 @@ pub fn handle_migrate_xyk_liquidity( user_info, lockup_info, } - .to_cosmos_msg(&env)?, + .to_cosmos_msg(&env)?, ); Ok(Response::default().add_messages(cosmos_msgs)) @@ -517,7 +557,7 @@ pub fn handle_claim_rewards_and_unlock_for_lockup( pool_type, prev_reward_balances: prev_pending_rewards_balances, } - .to_cosmos_msg(&env)?, + .to_cosmos_msg(&env)?, ); } @@ -528,7 +568,7 @@ pub fn handle_claim_rewards_and_unlock_for_lockup( duration, withdraw_lp_stake, } - .to_cosmos_msg(&env)?, + .to_cosmos_msg(&env)?, ); Ok(Response::new().add_messages(cosmos_msgs)) @@ -693,7 +733,7 @@ pub fn callback_withdraw_user_rewards_for_lockup_optional_withdraw( .lp_units_locked .full_mul(balance) .checked_div(Uint256::from(pool_info.amount_in_lockups))?) - .try_into()? + .try_into()? }; let mut pending_reward_assets: Vec = vec![]; @@ -1090,7 +1130,7 @@ pub fn query_lockup_info( .lp_units_locked .full_mul(pool_astroport_lp_units) .checked_div(Uint256::from(pool_info.amount_in_lockups))?) - .try_into()? + .try_into()? }; lockup_astroport_lp_units_opt = Some(lockup_astroport_lp_units); astroport_lp_token_opt = astroport_lp_token.clone(); @@ -1181,7 +1221,7 @@ pub fn calculate_astro_incentives_for_lockup( Uint256::from(pool_incentives_share).checked_mul(lockup_weighted_balance)?, Uint256::from(total_incentives_share).checked_mul(total_weighted_amount)?, ) - .checked_mul_uint256(total_lockdrop_incentives.into())?) + .checked_mul_uint256(total_lockdrop_incentives.into())?) } } From d44ee2f39e61ac3bd6a4f82b77c01f31d42ce072 Mon Sep 17 00:00:00 2001 From: pr0n00gler Date: Thu, 21 Mar 2024 16:50:34 +0200 Subject: [PATCH 2/2] formatting --- contracts/lockdrop-pcl/src/contract.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/contracts/lockdrop-pcl/src/contract.rs b/contracts/lockdrop-pcl/src/contract.rs index 16a6c21f..8663bcf8 100644 --- a/contracts/lockdrop-pcl/src/contract.rs +++ b/contracts/lockdrop-pcl/src/contract.rs @@ -5,25 +5,25 @@ use std::str::FromStr; use astroport::asset::{Asset, AssetInfo}; use astroport::common::{claim_ownership, drop_ownership_proposal, propose_new_owner}; use astroport::cosmwasm_ext::IntegerToDecimal; -use astroport::DecimalCheckedOps; use astroport::incentives::{ExecuteMsg as IncentivesExecuteMsg, QueryMsg as IncentivesQueryMsg}; use astroport::pair::ExecuteMsg::ProvideLiquidity; use astroport::restricted_vector::RestrictedVector; +use astroport::DecimalCheckedOps; use cosmwasm_std::{ - Addr, attr, BankMsg, Binary, coins, CosmosMsg, Decimal, Decimal256, Deps, - DepsMut, Empty, entry_point, Env, MessageInfo, Order, Response, StdError, StdResult, to_json_binary, + attr, coins, entry_point, to_json_binary, Addr, BankMsg, Binary, CosmosMsg, Decimal, + Decimal256, Deps, DepsMut, Empty, Env, MessageInfo, Order, Response, StdError, StdResult, Uint128, Uint256, WasmMsg, }; -use cw20::{BalanceResponse, Cw20ExecuteMsg, Cw20QueryMsg, MinterResponse}; use cw2::set_contract_version; +use cw20::{BalanceResponse, Cw20ExecuteMsg, Cw20QueryMsg, MinterResponse}; use astroport_periphery::lockdrop::{ LockupInfoV2 as LockdropXYKLockupInfoV2, PoolType as LockdropXYKPoolType, UserInfo as LockdropXYKUserInfo, }; use astroport_periphery::lockdrop_pcl::{ - CallbackMsg, Config, ExecuteMsg, InstantiateMsg, LockupInfo, LockUpInfoResponse, - LockUpInfoSummary, MigrateMsg, PoolInfo, PoolType, QueryMsg, State, StateResponse, UpdateConfigMsg, + CallbackMsg, Config, ExecuteMsg, InstantiateMsg, LockUpInfoResponse, LockUpInfoSummary, + LockupInfo, MigrateMsg, PoolInfo, PoolType, QueryMsg, State, StateResponse, UpdateConfigMsg, UserInfo, UserInfoResponse, UserInfoWithListResponse, }; use astroport_periphery::utils::Decimal256CheckedOps; @@ -427,7 +427,7 @@ pub fn handle_migrate_xyk_liquidity( pool_type: pool_type.into(), prev_reward_balances: prev_pending_rewards_balances, } - .to_cosmos_msg(&env)?, + .to_cosmos_msg(&env)?, ); } @@ -442,7 +442,7 @@ pub fn handle_migrate_xyk_liquidity( user_info, lockup_info, } - .to_cosmos_msg(&env)?, + .to_cosmos_msg(&env)?, ); Ok(Response::default().add_messages(cosmos_msgs)) @@ -557,7 +557,7 @@ pub fn handle_claim_rewards_and_unlock_for_lockup( pool_type, prev_reward_balances: prev_pending_rewards_balances, } - .to_cosmos_msg(&env)?, + .to_cosmos_msg(&env)?, ); } @@ -568,7 +568,7 @@ pub fn handle_claim_rewards_and_unlock_for_lockup( duration, withdraw_lp_stake, } - .to_cosmos_msg(&env)?, + .to_cosmos_msg(&env)?, ); Ok(Response::new().add_messages(cosmos_msgs)) @@ -733,7 +733,7 @@ pub fn callback_withdraw_user_rewards_for_lockup_optional_withdraw( .lp_units_locked .full_mul(balance) .checked_div(Uint256::from(pool_info.amount_in_lockups))?) - .try_into()? + .try_into()? }; let mut pending_reward_assets: Vec = vec![]; @@ -1130,7 +1130,7 @@ pub fn query_lockup_info( .lp_units_locked .full_mul(pool_astroport_lp_units) .checked_div(Uint256::from(pool_info.amount_in_lockups))?) - .try_into()? + .try_into()? }; lockup_astroport_lp_units_opt = Some(lockup_astroport_lp_units); astroport_lp_token_opt = astroport_lp_token.clone(); @@ -1221,7 +1221,7 @@ pub fn calculate_astro_incentives_for_lockup( Uint256::from(pool_incentives_share).checked_mul(lockup_weighted_balance)?, Uint256::from(total_incentives_share).checked_mul(total_weighted_amount)?, ) - .checked_mul_uint256(total_lockdrop_incentives.into())?) + .checked_mul_uint256(total_lockdrop_incentives.into())?) } }