Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stake after full migration #88

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 44 additions & 15 deletions contracts/lockdrop-pcl/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -163,6 +164,7 @@ pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> S
duration,
user_info,
lockup_info,
deposit_all,
),
}
}
Expand Down Expand Up @@ -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<Vec<CosmosMsg>> {
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
Expand All @@ -343,6 +365,7 @@ pub fn handle_migrate_xyk_liquidity(
duration: u64,
user_info: LockdropXYKUserInfo,
lockup_info: LockdropXYKLockupInfoV2,
deposit_all: bool,
) -> StdResult<Response> {
let config = CONFIG.load(deps.storage)?;
if info.sender != config.xyk_lockdrop_contract {
Expand Down Expand Up @@ -390,7 +413,7 @@ pub fn handle_migrate_xyk_liquidity(
})
.collect(),
slippage_tolerance: None,
auto_stake: Some(true),
auto_stake: Some(false),
receiver: None,
})?,
}));
Expand Down Expand Up @@ -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)?,
);
}

Expand All @@ -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))
}

Expand Down Expand Up @@ -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)?,
);
}

Expand All @@ -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))
Expand Down Expand Up @@ -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<Asset> = vec![];
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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())?)
}
}

Expand Down
46 changes: 30 additions & 16 deletions contracts/lockdrop/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
};

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)?,
);
}

Expand All @@ -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))
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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(),
Expand All @@ -2002,6 +2015,7 @@ pub fn callback_migrate_user_lockup_to_pcl_pair(
duration,
user_info,
lockup_info,
deposit_all,
})?,
})),
)
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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())?)
}
}

Expand Down
16 changes: 9 additions & 7 deletions packages/astroport_periphery/src/lockdrop_pcl.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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,
},
}

Expand Down Expand Up @@ -240,13 +242,13 @@ pub enum QueryMsg {
pool_type: PoolType,
duration: u64,
},
#[returns(Option<Uint128>)]
#[returns(Option < Uint128 >)]
QueryUserLockupTotalAtHeight {
pool_type: PoolType,
user_address: String,
height: u64,
},
#[returns(Option<Uint128>)]
#[returns(Option < Uint128 >)]
QueryLockupTotalAtHeight { pool_type: PoolType, height: u64 },
}

Expand Down
Loading