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

fix: slippage calulation and added tests #82

Merged
merged 4 commits into from
Dec 19, 2024
Merged
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
8 changes: 6 additions & 2 deletions contracts/hub/vlp/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,15 @@ pub fn assert_slippage_tolerance(
pool_ratio: Decimal256,
slippage_tolerance_bps: u64,
) -> Result<bool, ContractError> {
let slippage = pool_ratio.abs_diff(ratio);
let slippage = ratio.abs_diff(pool_ratio).checked_div(pool_ratio)?;

let slippage_tolerance = Decimal256::bps(slippage_tolerance_bps);
ensure!(
slippage.le(&slippage_tolerance),
ContractError::LiquiditySlippageExceeded {}
ContractError::LiquiditySlippageExceeded {
expected: slippage,
received: slippage_tolerance,
}
);
Ok(true)
}
Expand Down
54 changes: 53 additions & 1 deletion contracts/hub/vlp/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
#[cfg(test)]
mod tests {
use crate::contract::{execute, instantiate};
use crate::query::{calculate_lp_allocation, calculate_lp_allocation_for_liquidity};

Check warning on line 5 in contracts/hub/vlp/src/tests.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `calculate_lp_allocation`
use crate::state::{State, BALANCES, CHAIN_LP_TOKENS, STATE};
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{coins, DepsMut, Response, Uint128};
use cosmwasm_std::{coins, Decimal256, DepsMut, Response, Uint128};

Check warning on line 8 in contracts/hub/vlp/src/tests.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `Decimal256`
use euclid::chain::{ChainUid, CrossChainUser};
use euclid::error::ContractError;
use euclid::fee::{DenomFees, Fee, TotalFees};
Expand Down Expand Up @@ -181,4 +182,55 @@
ContractError::new("Euclid Fee cannot exceed maximum limit")
);
}

#[test]
fn test_calculate_lp_allocation_for_liquidity() {
let token_1_liquidity = Uint128::new(980);
let token_2_liquidity = Uint128::new(1000);
let total_reserve_1 = Uint128::new(10000);
let total_reserve_2 = Uint128::new(10000);
let total_lp_tokens = Uint128::new(10000); // 1:1 ratio for lp tokens to tokens present
let slippage_tolerance_bps = Some(200); // 2% slippage tolerance

// Call the function to test
let lp_allocation = calculate_lp_allocation_for_liquidity(
token_1_liquidity,
token_2_liquidity,
total_reserve_1,
total_reserve_2,
total_lp_tokens,
slippage_tolerance_bps,
)
.unwrap();

// Assert the expected LP allocation
let expected_allocation = Uint128::new(980); // This value should be calculated based on the logic
assert_eq!(lp_allocation, expected_allocation);
}

#[test]
fn test_calculate_lp_allocation_for_liquidity_exceeded_slippage() {
let token_1_liquidity = Uint128::new(100);
let token_2_liquidity = Uint128::new(99);
let total_reserve_1 = Uint128::new(100);
let total_reserve_2 = Uint128::new(100);
let total_lp_tokens = Uint128::new(100);
let slippage_tolerance_bps = Some(0); // 0% slippage tolerance to force failure

// Call the function to test and expect an error
let err = calculate_lp_allocation_for_liquidity(
token_1_liquidity,
token_2_liquidity,
total_reserve_1,
total_reserve_2,
total_lp_tokens,
slippage_tolerance_bps,
)
.unwrap_err();

match err {
ContractError::LiquiditySlippageExceeded { .. } => (),
_ => panic!("Expected slippage exceeded error, got {:?}", err),
}
}
}
15 changes: 10 additions & 5 deletions packages/euclid/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::num::ParseIntError;

use cosmwasm_std::{
Addr, CheckedMultiplyFractionError, CheckedMultiplyRatioError, DivideByZeroError,
OverflowError, StdError, Uint128,
Addr, CheckedFromRatioError, CheckedMultiplyFractionError, CheckedMultiplyRatioError,
Decimal256, DivideByZeroError, OverflowError, StdError, Uint128,
};
use cw20_base::ContractError as Cw20ContractError;
use thiserror::Error;
Expand All @@ -23,6 +23,9 @@ pub enum ContractError {
#[error("{0}")]
CheckedMultiplyRatioError(#[from] CheckedMultiplyRatioError),

#[error("{0}")]
CheckedFromRatioError(#[from] CheckedFromRatioError),

#[error("{0}")]
DivideByZero(#[from] DivideByZeroError),

Expand Down Expand Up @@ -94,7 +97,6 @@ pub enum ContractError {

#[error("Not Implemented")]
NotImplemented {},

#[error("DenomDoesNotExist")]
DenomDoesNotExist {},

Expand Down Expand Up @@ -194,8 +196,11 @@ pub enum ContractError {
#[error("Liquity already exist in state for the sender")]
LiquidityTxAlreadyExist {},

#[error("Slippage has been exceeded when providing liquidity.")]
LiquiditySlippageExceeded {},
#[error("Slippage has been exceeded when providing liquidity. Expected: {expected}, Received: {received}")]
LiquiditySlippageExceeded {
expected: Decimal256,
received: Decimal256,
},

#[error("Pool Instantiate Failed {err}")]
PoolInstantiateFailed { err: String },
Expand Down
Loading