Skip to content

Commit

Permalink
fix(pair): fixed initial provide liquidity (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
P-Yevhenii committed Aug 16, 2022
1 parent f949855 commit 75b2623
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
12 changes: 9 additions & 3 deletions contracts/pair/src/contract.rs
Expand Up @@ -10,7 +10,7 @@ use cosmwasm_std::{
use crate::response::MsgInstantiateContractResponse;
use astroport::asset::{
addr_opt_validate, addr_validate_to_lower, check_swap_parameters, format_lp_token_name, Asset,
AssetInfo, PairInfo,
AssetInfo, PairInfo, MINIMUM_LIQUIDITY_AMOUNT,
};
use astroport::decimal2decimal256;
use astroport::factory::PairType;
Expand Down Expand Up @@ -381,11 +381,17 @@ pub fn provide_liquidity(
let total_share = query_supply(&deps.querier, &config.pair_info.liquidity_token)?;
let share = if total_share.is_zero() {
// Initial share = collateral amount
Uint128::new(
let share = Uint128::new(
(U256::from(deposits[0].u128()) * U256::from(deposits[1].u128()))
.integer_sqrt()
.as_u128(),
)
);

if share.lt(&MINIMUM_LIQUIDITY_AMOUNT) {
return Err(ContractError::MinimumLiquidityAmountError {});
}

share
} else {
// Assert slippage tolerance
assert_slippage_tolerance(slippage_tolerance, &deposits, &pools)?;
Expand Down
7 changes: 7 additions & 0 deletions contracts/pair/src/error.rs
@@ -1,3 +1,4 @@
use astroport::asset::MINIMUM_LIQUIDITY_AMOUNT;
use cosmwasm_std::{OverflowError, StdError};
use thiserror::Error;

Expand Down Expand Up @@ -37,6 +38,12 @@ pub enum ContractError {

#[error("Pair is not migrated to the new admin!")]
PairIsNotMigrated {},

#[error(
"The initial LP share can not be less than {0}",
MINIMUM_LIQUIDITY_AMOUNT
)]
MinimumLiquidityAmountError {},
}

impl From<OverflowError> for ContractError {
Expand Down
26 changes: 17 additions & 9 deletions contracts/pair/tests/integration.rs
Expand Up @@ -127,15 +127,15 @@ fn test_provide_and_withdraw_liquidity() {
&[
Coin {
denom: "uusd".to_string(),
amount: Uint128::new(233u128),
amount: Uint128::new(233_000_000u128),
},
Coin {
denom: "uluna".to_string(),
amount: Uint128::new(200u128),
amount: Uint128::new(2_00_000_000u128),
},
Coin {
denom: "cny".to_string(),
amount: Uint128::from(1000u16),
amount: Uint128::from(100_000_000u128),
},
],
)
Expand Down Expand Up @@ -171,18 +171,23 @@ fn test_provide_and_withdraw_liquidity() {
&[
Coin {
denom: "uusd".to_string(),
amount: Uint128::new(100u128),
amount: Uint128::new(100_000_000u128),
},
Coin {
denom: "uluna".to_string(),
amount: Uint128::new(100u128),
amount: Uint128::new(100_000_000u128),
},
],
)
.unwrap();

// Provide liquidity
let (msg, coins) = provide_liquidity_msg(Uint128::new(100), Uint128::new(100), None, None);
let (msg, coins) = provide_liquidity_msg(
Uint128::new(100_000_000),
Uint128::new(100_000_000),
None,
None,
);
let res = router
.execute_contract(alice_address.clone(), pair_instance.clone(), &msg, &coins)
.unwrap();
Expand All @@ -194,15 +199,18 @@ fn test_provide_and_withdraw_liquidity() {
assert_eq!(res.events[1].attributes[3], attr("receiver", "alice"),);
assert_eq!(
res.events[1].attributes[4],
attr("assets", "100uusd, 100uluna")
attr("assets", "100000000uusd, 100000000uluna")
);
assert_eq!(
res.events[1].attributes[5],
attr("share", 100u128.to_string())
attr("share", 100000000u128.to_string())
);
assert_eq!(res.events[3].attributes[1], attr("action", "mint"));
assert_eq!(res.events[3].attributes[2], attr("to", "alice"));
assert_eq!(res.events[3].attributes[3], attr("amount", 100.to_string()));
assert_eq!(
res.events[3].attributes[3],
attr("amount", 100000000.to_string())
);

// Provide liquidity for receiver
let (msg, coins) = provide_liquidity_msg(
Expand Down
3 changes: 1 addition & 2 deletions contracts/pair_stable/src/utils.rs
@@ -1,11 +1,10 @@
use std::cmp::Ordering;

use cosmwasm_std::{
to_binary, wasm_execute, Addr, Api, CosmosMsg, Decimal, Decimal256, Deps, Env, QuerierWrapper,
StdResult, Storage, Uint128, Uint64,
};
use cw20::Cw20ExecuteMsg;
use itertools::Itertools;
use std::cmp::Ordering;

use astroport::asset::{Asset, AssetInfo, Decimal256Ext, DecimalAsset};
use astroport::pair::TWAP_PRECISION;
Expand Down
2 changes: 2 additions & 0 deletions packages/astroport/src/asset.rs
Expand Up @@ -18,6 +18,8 @@ use itertools::Itertools;
pub const UUSD_DENOM: &str = "uusd";
/// LUNA token denomination
pub const ULUNA_DENOM: &str = "uluna";
/// Minimum initial LP share
pub const MINIMUM_LIQUIDITY_AMOUNT: Uint128 = Uint128::new(1_000);

/// ## Description
/// This enum describes a Terra asset (native or CW20).
Expand Down

0 comments on commit 75b2623

Please sign in to comment.