Skip to content

Commit

Permalink
feat(PCL): enforce min LP to receive (#427)
Browse files Browse the repository at this point in the history
* feat(PCL): enforce min LP to receive

* bump pairs patch versions
  • Loading branch information
epanchee authored Aug 21, 2024
1 parent 2bb41b8 commit a687b38
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 28 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion contracts/factory/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astroport-factory"
version = "1.8.0"
version = "1.8.1"
authors = ["Astroport"]
edition = "2021"
description = "Astroport factory contract - pair contract generator and directory"
Expand Down
2 changes: 1 addition & 1 deletion contracts/pair/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astroport-pair"
version = "2.0.1"
version = "2.0.2"
authors = ["Astroport"]
edition = "2021"
description = "The Astroport constant product pool contract implementation"
Expand Down
39 changes: 32 additions & 7 deletions contracts/pair_concentrated/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::vec;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
attr, coin, ensure_eq, from_json, to_json_binary, wasm_execute, Addr, Binary, Coin, CosmosMsg,
Decimal, Decimal256, DepsMut, Empty, Env, MessageInfo, Reply, Response, StdError, StdResult,
SubMsg, SubMsgResponse, SubMsgResult, Uint128, WasmMsg,
attr, coin, ensure, ensure_eq, from_json, to_json_binary, wasm_execute, Addr, Binary, Coin,
CosmosMsg, Decimal, Decimal256, DepsMut, Empty, Env, MessageInfo, Reply, Response, StdError,
StdResult, SubMsg, SubMsgResponse, SubMsgResult, Uint128, WasmMsg,
};
use cw2::set_contract_version;
use cw2::{get_contract_version, set_contract_version};
use cw20::{Cw20ExecuteMsg, Cw20ReceiveMsg};
use cw_utils::{
one_coin, parse_reply_instantiate_data, MsgInstantiateContractResponse, PaymentError,
Expand Down Expand Up @@ -290,7 +290,7 @@ pub fn execute(
slippage_tolerance,
auto_stake,
receiver,
..
min_lp_to_receive,
} => provide_liquidity(
deps,
env,
Expand All @@ -299,6 +299,7 @@ pub fn execute(
slippage_tolerance,
auto_stake,
receiver,
min_lp_to_receive,
),
ExecuteMsg::Swap {
offer_asset,
Expand Down Expand Up @@ -419,6 +420,7 @@ fn receive_cw20(
/// If no custom receiver is specified, the pair will mint LP tokens for the function caller.
///
/// NOTE - the address that wants to provide liquidity should approve the pair contract to pull its relevant tokens.
#[allow(clippy::too_many_arguments)]
pub fn provide_liquidity(
deps: DepsMut,
env: Env,
Expand All @@ -427,6 +429,7 @@ pub fn provide_liquidity(
slippage_tolerance: Option<Decimal>,
auto_stake: Option<bool>,
receiver: Option<String>,
min_lp_to_receive: Option<Uint128>,
) -> Result<Response, ContractError> {
let mut config = CONFIG.load(deps.storage)?;

Expand Down Expand Up @@ -496,6 +499,12 @@ pub fn provide_liquidity(
)?);
}

let min_amount_lp = min_lp_to_receive.unwrap_or_default();
ensure!(
share_uint128 >= min_amount_lp,
ContractError::ProvideSlippageViolation(share_uint128, min_amount_lp,)
);

// Mint LP tokens for the sender or for the receiver (if set)
let receiver = addr_opt_validate(deps.api, &receiver)?.unwrap_or_else(|| info.sender.clone());
let auto_stake = auto_stake.unwrap_or(false);
Expand Down Expand Up @@ -892,6 +901,22 @@ fn update_config(
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(_deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, ContractError> {
unimplemented!("No safe path available for migration from cw20 to tokenfactory LP tokens")
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, ContractError> {
let contract_version = get_contract_version(deps.storage)?;

match contract_version.contract.as_ref() {
"astroport-pair-concentrated" => match contract_version.version.as_ref() {
"4.0.0" | "4.0.1" => {}
_ => return Err(ContractError::MigrationError {}),
},
_ => return Err(ContractError::MigrationError {}),
}

set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

Ok(Response::new()
.add_attribute("previous_contract_name", &contract_version.contract)
.add_attribute("previous_contract_version", &contract_version.version)
.add_attribute("new_contract_name", CONTRACT_NAME)
.add_attribute("new_contract_version", CONTRACT_VERSION))
}
5 changes: 4 additions & 1 deletion contracts/pair_concentrated/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{ConversionOverflowError, OverflowError, StdError};
use cosmwasm_std::{ConversionOverflowError, OverflowError, StdError, Uint128};
use thiserror::Error;

use cw_utils::{ParseReplyError, PaymentError};
Expand Down Expand Up @@ -66,4 +66,7 @@ pub enum ContractError {
MAX_FEE_SHARE_BPS
)]
FeeShareOutOfBounds {},

#[error("Slippage is more than expected: received {0}, expected {1} LP tokens")]
ProvideSlippageViolation(Uint128, Uint128),
}
24 changes: 24 additions & 0 deletions contracts/pair_concentrated/tests/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,30 @@ impl Helper {
.execute_contract(sender.clone(), self.pair_addr.clone(), &msg, &funds)
}

pub fn provide_liquidity_full(
&mut self,
sender: &Addr,
assets: &[Asset],
slippage_tolerance: Option<Decimal>,
auto_stake: Option<bool>,
receiver: Option<String>,
min_lp_to_receive: Option<Uint128>,
) -> AnyResult<AppResponse> {
let funds =
assets.mock_coins_sent(&mut self.app, sender, &self.pair_addr, SendType::Allowance);

let msg = ExecuteMsg::ProvideLiquidity {
assets: assets.to_vec(),
slippage_tolerance,
auto_stake,
receiver,
min_lp_to_receive,
};

self.app
.execute_contract(sender.clone(), self.pair_addr.clone(), &msg, &funds)
}

pub fn withdraw_liquidity(
&mut self,
sender: &Addr,
Expand Down
26 changes: 26 additions & 0 deletions contracts/pair_concentrated/tests/pair_concentrated_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,32 @@ fn provide_withdraw_slippage() {
helper
.provide_liquidity_with_slip_tolerance(&owner, &assets, Some(f64_to_dec(0.5)))
.unwrap();

let err = helper
.provide_liquidity_full(
&owner,
&assets,
Some(f64_to_dec(0.5)),
None,
None,
Some(10000000000u128.into()),
)
.unwrap_err();
assert_eq!(
ContractError::ProvideSlippageViolation(1000229863u128.into(), 10000000000u128.into()),
err.downcast().unwrap(),
);

helper
.provide_liquidity_full(
&owner,
&assets,
Some(f64_to_dec(0.5)),
None,
None,
Some(1000229863u128.into()),
)
.unwrap();
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion contracts/pair_stable/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astroport-pair-stable"
version = "4.0.0"
version = "4.0.1"
authors = ["Astroport"]
edition = "2021"
description = "The Astroport stableswap pair contract implementation"
Expand Down
2 changes: 1 addition & 1 deletion contracts/pair_transmuter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astroport-pair-transmuter"
version = "1.1.1"
version = "1.1.2"
authors = ["Astroport"]
edition = "2021"
description = "The Astroport constant sum pair contract implementation. Handles no fee pools with constant 1:1 ratio."
Expand Down
2 changes: 1 addition & 1 deletion contracts/pair_xyk_sale_tax/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astroport-pair-xyk-sale-tax"
version = "2.0.1"
version = "2.0.2"
authors = ["Astroport", "Sturdy"]
edition = "2021"
description = "The Astroport constant product pool contract implementation"
Expand Down
2 changes: 1 addition & 1 deletion schemas/astroport-factory/astroport-factory.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "astroport-factory",
"contract_version": "1.8.0",
"contract_version": "1.8.1",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down
2 changes: 1 addition & 1 deletion schemas/astroport-pair-stable/astroport-pair-stable.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "astroport-pair-stable",
"contract_version": "4.0.0",
"contract_version": "4.0.1",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "astroport-pair-xyk-sale-tax",
"contract_version": "2.0.1",
"contract_version": "2.0.2",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down
2 changes: 1 addition & 1 deletion schemas/astroport-pair/astroport-pair.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "astroport-pair",
"contract_version": "2.0.1",
"contract_version": "2.0.2",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down

0 comments on commit a687b38

Please sign in to comment.