From 93dbf689c6192239fdcea21fa81285253032f2b7 Mon Sep 17 00:00:00 2001 From: pr0n00gler Date: Mon, 1 Apr 2024 16:06:31 +0300 Subject: [PATCH] wip --- contracts/lockdrop-pcl/src/contract.rs | 59 ++++++++++++++----- contracts/lockdrop/src/contract.rs | 46 ++++++++++----- .../astroport_periphery/src/lockdrop_pcl.rs | 16 ++--- 3 files changed, 83 insertions(+), 38 deletions(-) diff --git a/contracts/lockdrop-pcl/src/contract.rs b/contracts/lockdrop-pcl/src/contract.rs index 767b9a2..43dd748 100644 --- a/contracts/lockdrop-pcl/src/contract.rs +++ b/contracts/lockdrop-pcl/src/contract.rs @@ -5,30 +5,30 @@ 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; -use crate::raw_queries::raw_incentives_deposit; +use crate::raw_queries::{raw_balance, raw_incentives_deposit}; use crate::state::{ ASSET_POOLS, CONFIG, LOCKUP_INFO, OWNERSHIP_PROPOSAL, STATE, TOTAL_USER_LOCKUP_AMOUNT, USER_INFO, @@ -154,6 +154,7 @@ pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> S duration, user_info, lockup_info, + deposit_all } => handle_migrate_xyk_liquidity( deps, env, @@ -163,6 +164,7 @@ pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> S duration, user_info, lockup_info, + deposit_all, ), } } @@ -319,6 +321,26 @@ pub fn handle_update_config( Ok(Response::new().add_attributes(attributes)) } +fn stake_messages( + config: Config, + lp_token_address: Addr, + amount: Uint128, +) -> StdResult> { + let mut cosmos_msgs = vec![]; + + cosmos_msgs.push(CosmosMsg::Wasm(WasmMsg::Execute { + contract_addr: lp_token_address.to_string(), + funds: vec![], + msg: to_json_binary(&Cw20ExecuteMsg::Send { + contract: config.incentives.to_string(), + msg: to_json_binary(&astroport::generator::Cw20HookMsg::Deposit {})?, + amount, + })?, + })); + + Ok(cosmos_msgs) +} + #[allow(clippy::too_many_arguments)] /// Creates a lockup position based on the provided parameters and XYK lockdrop contract's state. No /// validation is performed on the lockup parameters, they are expected to be valid ones because the @@ -343,6 +365,7 @@ pub fn handle_migrate_xyk_liquidity( duration: u64, user_info: LockdropXYKUserInfo, lockup_info: LockdropXYKLockupInfoV2, + deposit_all: bool, ) -> StdResult { let config = CONFIG.load(deps.storage)?; if info.sender != config.xyk_lockdrop_contract { @@ -390,7 +413,7 @@ pub fn handle_migrate_xyk_liquidity( }) .collect(), slippage_tolerance: None, - auto_stake: Some(true), + auto_stake: Some(false), receiver: None, })?, })); @@ -429,7 +452,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)?, ); } @@ -444,9 +467,15 @@ pub fn handle_migrate_xyk_liquidity( user_info, lockup_info, } - .to_cosmos_msg(&env)?, + .to_cosmos_msg(&env)?, ); + let lp_balance = raw_balance(deps.querier, &pool_info.lp_token, env.contract.address.as_bytes())?; + + if deposit_all { + cosmos_msgs.extend(stake_messages(config, pool_info.lp_token, lp_balance)?); + } + Ok(Response::default().add_messages(cosmos_msgs)) } @@ -560,7 +589,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)?, ); } @@ -572,7 +601,7 @@ pub fn handle_claim_rewards_and_unlock_for_lockup( withdraw_lp_stake, reward_tokens, } - .to_cosmos_msg(&env)?, + .to_cosmos_msg(&env)?, ); Ok(Response::new().add_messages(cosmos_msgs)) @@ -740,7 +769,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 +1159,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 +1250,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())?) } } diff --git a/contracts/lockdrop/src/contract.rs b/contracts/lockdrop/src/contract.rs index 6bd024d..bf2dd51 100644 --- a/contracts/lockdrop/src/contract.rs +++ b/contracts/lockdrop/src/contract.rs @@ -6,18 +6,18 @@ 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::generator::{ ExecuteMsg as GenExecuteMsg, PendingTokenResponse, QueryMsg as GenQueryMsg, RewardInfoResponse, }; use astroport::restricted_vector::RestrictedVector; -use astroport::DecimalCheckedOps; use cosmwasm_std::{ - attr, coins, entry_point, from_json, to_json_binary, Addr, BankMsg, Binary, Coin, CosmosMsg, - Decimal, Decimal256, Deps, DepsMut, Env, MessageInfo, Order, Response, StdError, StdResult, + Addr, attr, BankMsg, Binary, Coin, coins, CosmosMsg, Decimal, Decimal256, Deps, + DepsMut, entry_point, Env, from_json, MessageInfo, Order, Response, StdError, StdResult, to_json_binary, Uint128, Uint256, WasmMsg, }; -use cw2::set_contract_version; use cw20::{BalanceResponse, Cw20ExecuteMsg, Cw20QueryMsg, Cw20ReceiveMsg, MinterResponse}; +use cw2::set_contract_version; use astroport_periphery::lockdrop::{ CallbackMsg, Config, Cw20HookMsg, ExecuteMsg, InstantiateMsg, LockUpInfoResponse, @@ -29,7 +29,7 @@ use astroport_periphery::utils::Decimal256CheckedOps; use crate::raw_queries::{raw_balance, raw_generator_deposit}; use crate::state::{ - CompatibleLoader, ASSET_POOLS, CONFIG, LOCKUP_INFO, OWNERSHIP_PROPOSAL, PCL_LOCKDROP_CONTRACT, + ASSET_POOLS, CompatibleLoader, CONFIG, LOCKUP_INFO, OWNERSHIP_PROPOSAL, PCL_LOCKDROP_CONTRACT, STATE, TOTAL_USER_LOCKUP_AMOUNT, USER_INFO, }; @@ -569,7 +569,7 @@ pub fn handle_migrate_liquidity_to_pcl_pools( user_address: user_address.clone(), duration, } - .to_cosmos_msg(&env)?, + .to_cosmos_msg(&env)?, ); } Ok(Response::default().add_messages(cosmos_msgs)) @@ -665,7 +665,7 @@ fn callback_init_migrate_lockup_to_pcl_pools( prev_ntrn_balance: reward_token_balance, prev_proxy_reward_balances, } - .to_cosmos_msg(&env)?, + .to_cosmos_msg(&env)?, ); } @@ -675,7 +675,7 @@ fn callback_init_migrate_lockup_to_pcl_pools( user_address, duration, } - .to_cosmos_msg(&env)?, + .to_cosmos_msg(&env)?, ); Ok(Response::default().add_messages(cosmos_msgs)) @@ -1290,7 +1290,7 @@ pub fn handle_claim_rewards_and_unlock_for_lockup( prev_ntrn_balance: reward_token_balance, prev_proxy_reward_balances, } - .to_cosmos_msg(&env)?, + .to_cosmos_msg(&env)?, ); } } else if user_info.ntrn_transferred && !withdraw_lp_stake { @@ -1304,7 +1304,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)) @@ -1516,7 +1516,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()? }; // If Astro LP tokens are staked with Astro generator @@ -1731,7 +1731,7 @@ pub fn callback_transfer_all_rewards_before_migration( .lp_units_locked .full_mul(balance) .checked_div(Uint256::from(pool_info.amount_in_lockups))?) - .try_into()? + .try_into()? }; let rwi: RewardInfoResponse = deps.querier.query_wasm_smart( @@ -1833,7 +1833,7 @@ pub fn callback_transfer_all_rewards_before_migration( astroport_lp_token, astroport_lp_amount, } - .to_cosmos_msg(&env)?, + .to_cosmos_msg(&env)?, ); Ok(Response::new().add_messages(cosmos_msgs)) @@ -1941,7 +1941,7 @@ pub fn callback_withdraw_user_lockup( paired_asset_denom, paired_asset_balance, } - .to_cosmos_msg(&env)?, + .to_cosmos_msg(&env)?, ); Ok(Response::new().add_messages(cosmos_msgs)) @@ -1969,6 +1969,17 @@ pub fn callback_migrate_user_lockup_to_pcl_pair( let user_info = USER_INFO .may_load(deps.storage, &user_address)? .unwrap_or_default(); + let pool_info = ASSET_POOLS.load(deps.storage, pool_type)?; + + let deposited_amount: Uint128 = raw_generator_deposit( + deps.querier, + config + .generator + .as_ref() + .ok_or_else(|| StdError::generic_err("Should be set!"))?, + pool_info.lp_token.as_bytes(), + env.contract.address.as_bytes(), + )?; // determine withdrawn amounts let ntrn_withdrawn = deps @@ -1983,6 +1994,8 @@ pub fn callback_migrate_user_lockup_to_pcl_pair( .sub(paired_asset_balance); let pcl_lockdrop = PCL_LOCKDROP_CONTRACT.load(deps.storage)?; + + let deposit_all: bool = deposited_amount.is_zero(); Ok( Response::default().add_message(CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: pcl_lockdrop.to_string(), @@ -2002,6 +2015,7 @@ pub fn callback_migrate_user_lockup_to_pcl_pair( duration, user_info, lockup_info, + deposit_all, })?, })), ) @@ -2214,7 +2228,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(); @@ -2329,7 +2343,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())?) } } diff --git a/packages/astroport_periphery/src/lockdrop_pcl.rs b/packages/astroport_periphery/src/lockdrop_pcl.rs index 6b9fd83..0a790c5 100644 --- a/packages/astroport_periphery/src/lockdrop_pcl.rs +++ b/packages/astroport_periphery/src/lockdrop_pcl.rs @@ -1,18 +1,19 @@ -use crate::lockdrop::{ - LockupInfoV2 as LockdropXYKLockupInfoV2, PoolType as LockdropXYKPoolType, - UserInfo as LockdropXYKUserInfo, -}; use astroport::asset::{Asset, AssetInfo}; use astroport::restricted_vector::RestrictedVector; use cosmwasm_schema::{cw_serde, QueryResponses}; use cosmwasm_std::{ - to_json_binary, Addr, CosmosMsg, Decimal, Decimal256, Env, StdError, StdResult, Uint128, + Addr, CosmosMsg, Decimal, Decimal256, Env, StdError, StdResult, to_json_binary, Uint128, Uint256, WasmMsg, }; use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; +use crate::lockdrop::{ + LockupInfoV2 as LockdropXYKLockupInfoV2, PoolType as LockdropXYKPoolType, + UserInfo as LockdropXYKUserInfo, +}; + // TODO: implement display trait #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, Copy)] pub enum PoolType { @@ -168,6 +169,7 @@ pub enum ExecuteMsg { /// The lockup info from the XYK lockdrop contract. Is used to create a LockupInfoV2 entry /// on the PCL lockdrop contract's side. lockup_info: LockdropXYKLockupInfoV2, + deposit_all: bool, }, } @@ -240,13 +242,13 @@ pub enum QueryMsg { pool_type: PoolType, duration: u64, }, - #[returns(Option)] + #[returns(Option < Uint128 >)] QueryUserLockupTotalAtHeight { pool_type: PoolType, user_address: String, height: u64, }, - #[returns(Option)] + #[returns(Option < Uint128 >)] QueryLockupTotalAtHeight { pool_type: PoolType, height: u64 }, }