Skip to content

Commit

Permalink
feat: make it possible to lock the LP when providing liquidity into a…
Browse files Browse the repository at this point in the history
… pool
  • Loading branch information
kerber0x committed Apr 25, 2024
1 parent f14fb74 commit 880815a
Show file tree
Hide file tree
Showing 15 changed files with 641 additions and 55 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ incentive-factory = { path = "./contracts/liquidity_hub/pool-network/incentive_f
terraswap-token = { path = "./contracts/liquidity_hub/pool-network/terraswap_token" }
terraswap-pair = { path = "./contracts/liquidity_hub/pool-network/terraswap_pair" }
epoch-manager = { path = "./contracts/liquidity_hub/epoch-manager" }
incentive-manager = { path = "./contracts/liquidity_hub/incentive-manager" }

[workspace.metadata.dylint]
libraries = [{ git = "https://github.com/0xFable/cw-lint" }]
Expand Down
2 changes: 1 addition & 1 deletion contracts/liquidity_hub/incentive-manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn instantiate(

let config = Config {
epoch_manager_addr: deps.api.addr_validate(&msg.epoch_manager_addr)?,
whale_lair_addr: deps.api.addr_validate(&msg.whale_lair_addr)?,
whale_lair_addr: deps.api.addr_validate(&msg.bonding_manager_addr)?,
create_incentive_fee: msg.create_incentive_fee,
max_concurrent_incentives: msg.max_concurrent_incentives,
max_incentive_epoch_buffer: msg.max_incentive_epoch_buffer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl TestingSuite {
let msg = InstantiateMsg {
owner: self.creator().to_string(),
epoch_manager_addr,
whale_lair_addr,
whale_lair_addr: bonding_manager_addr,
create_incentive_fee,
max_concurrent_incentives,
max_incentive_epoch_buffer,
Expand Down Expand Up @@ -257,7 +257,7 @@ impl TestingSuite {
let msg = InstantiateMsg {
owner: self.creator().to_string(),
epoch_manager_addr,
whale_lair_addr,
whale_lair_addr: bonding_manager_addr,
create_incentive_fee,
max_concurrent_incentives,
max_incentive_epoch_buffer,
Expand Down
2 changes: 2 additions & 0 deletions contracts/liquidity_hub/pool-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ cw-multi-test.workspace = true
anyhow.workspace = true
test-case.workspace = true
whale-lair.workspace = true
incentive-manager.workspace = true
epoch-manager.workspace = true
white-whale-testing.workspace = true
7 changes: 6 additions & 1 deletion contracts/liquidity_hub/pool-manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ pub fn instantiate(
) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
let config: Config = Config {
whale_lair_addr: deps.api.addr_validate(&msg.fee_collector_addr)?,
bonding_manager_addr: deps.api.addr_validate(&msg.bonding_manager_addr)?,
incentive_manager_addr: deps.api.addr_validate(&msg.incentive_manager_addr)?,
// We must set a creation fee on instantiation to prevent spamming of pools
pool_creation_fee: msg.pool_creation_fee,
feature_toggle: FeatureToggle {
Expand Down Expand Up @@ -70,13 +71,17 @@ pub fn execute(
slippage_tolerance,
receiver,
pair_identifier,
unlocking_duration,
lock_position_identifier,
} => liquidity::commands::provide_liquidity(
deps,
env,
info,
slippage_tolerance,
receiver,
pair_identifier,
unlocking_duration,
lock_position_identifier,
),
ExecuteMsg::Swap {
offer_asset,
Expand Down
50 changes: 40 additions & 10 deletions contracts/liquidity_hub/pool-manager/src/liquidity/commands.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use cosmwasm_std::{BankMsg, Coin, CosmosMsg, DepsMut, Env, MessageInfo, Response};
use cosmwasm_std::{
coins, wasm_execute, BankMsg, Coin, CosmosMsg, DepsMut, Env, MessageInfo, Response,
};
use white_whale_std::pool_network::asset::PairType;

use crate::{
Expand All @@ -20,16 +22,16 @@ pub const MAX_ASSETS_PER_POOL: usize = 4;

// todo allow providing liquidity with a single asset

//todo allow passing an optional locking period for the LP once the liquidity is provided, so tokens
// are locked in the incentive manager

#[allow(clippy::too_many_arguments)]
pub fn provide_liquidity(
deps: DepsMut,
env: Env,
info: MessageInfo,
slippage_tolerance: Option<Decimal>,
receiver: Option<String>,
pair_identifier: String,
unlocking_duration: Option<u64>,
lock_position_identifier: Option<String>,
) -> Result<Response, ContractError> {
let config = MANAGER_CONFIG.load(deps.storage)?;
// check if the deposit feature is enabled
Expand Down Expand Up @@ -145,12 +147,40 @@ pub fn provide_liquidity(
// mint LP token to sender
let receiver = receiver.unwrap_or_else(|| info.sender.to_string());

messages.push(white_whale_std::lp_common::mint_lp_token_msg(
liquidity_token,
&info.sender,
&env.contract.address,
share,
)?);
// if the unlocking duration is set, lock the LP tokens in the incentive manager
if let Some(unlocking_duration) = unlocking_duration {
// mint the lp tokens to the contract
messages.push(white_whale_std::lp_common::mint_lp_token_msg(
liquidity_token.clone(),
&env.contract.address,
&env.contract.address,
share,
)?);

// lock the lp tokens in the incentive manager on behalf of the receiver
messages.push(
wasm_execute(
config.incentive_manager_addr,
&white_whale_std::incentive_manager::ExecuteMsg::ManagePosition {
action: white_whale_std::incentive_manager::PositionAction::Fill {
identifier: lock_position_identifier,
unlocking_duration,
receiver: Some(receiver.clone()),
},
},
coins(share.u128(), liquidity_token),
)?
.into(),
);
} else {
// if not, just mint the LP tokens to the receiver
messages.push(white_whale_std::lp_common::mint_lp_token_msg(
liquidity_token,
&info.sender,
&env.contract.address,
share,
)?);
}

pair.assets = pool_assets.clone();
PAIRS.save(deps.storage, &pair_identifier, &pair)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn create_pair(

// send pair creation fee to whale lair i.e the new fee_collector
messages.push(fill_rewards_msg(
config.whale_lair_addr.into_string(),
config.bonding_manager_addr.into_string(),
creation_fee,
)?);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn update_config(
MANAGER_CONFIG.update(deps.storage, |mut config| {
if let Some(whale_lair_addr) = whale_lair_addr {
let whale_lair_addr = deps.api.addr_validate(&whale_lair_addr)?;
config.whale_lair_addr = whale_lair_addr;
config.bonding_manager_addr = whale_lair_addr;
}

if let Some(pool_creation_fee) = pool_creation_fee {
Expand Down
4 changes: 2 additions & 2 deletions contracts/liquidity_hub/pool-manager/src/router/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn execute_swap_operations(
}
if !swap_result.protocol_fee_asset.amount.is_zero() {
fee_messages.push(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: config.whale_lair_addr.to_string(),
contract_addr: config.bonding_manager_addr.to_string(),
msg: to_json_binary(&whale_lair::ExecuteMsg::FillRewards {
assets: vec![swap_result.protocol_fee_asset.clone()],
})?,
Expand All @@ -127,7 +127,7 @@ pub fn execute_swap_operations(
// todo remove, the swap_fee_asset stays in the pool
if !swap_result.swap_fee_asset.amount.is_zero() {
fee_messages.push(CosmosMsg::Bank(BankMsg::Send {
to_address: config.whale_lair_addr.to_string(),
to_address: config.bonding_manager_addr.to_string(),
amount: vec![swap_result.swap_fee_asset],
}));
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/liquidity_hub/pool-manager/src/swap/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn swap(
}
if !swap_result.protocol_fee_asset.amount.is_zero() {
messages.push(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: config.whale_lair_addr.to_string(),
contract_addr: config.bonding_manager_addr.to_string(),
msg: to_json_binary(&whale_lair::ExecuteMsg::FillRewards {
assets: vec![swap_result.protocol_fee_asset.clone()],
})?,
Expand All @@ -75,7 +75,7 @@ pub fn swap(
// todo remove, this stays within the pool
if !swap_result.swap_fee_asset.amount.is_zero() {
messages.push(CosmosMsg::Bank(BankMsg::Send {
to_address: config.whale_lair_addr.to_string(),
to_address: config.bonding_manager_addr.to_string(),
amount: vec![swap_result.swap_fee_asset.clone()],
}));
}
Expand Down
Loading

0 comments on commit 880815a

Please sign in to comment.