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

Minimum reserves check #19

Closed
wants to merge 11 commits into from
24 changes: 23 additions & 1 deletion amm/contracts/stable_pool/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ mod token_rate;
pub mod stable_pool {
use crate::token_rate::TokenRate;
use amm_helpers::{
constants::stable_pool::{MAX_AMP, MIN_AMP, RATE_PRECISION, TOKEN_TARGET_DECIMALS},
constants::stable_pool::{
MAX_AMP, MIN_AMP, MIN_RESERVE, RATE_PRECISION, TOKEN_TARGET_DECIMALS,
},
ensure,
stable_swap_math::{self as math, fees::Fees},
};
Expand Down Expand Up @@ -380,6 +382,7 @@ pub mod stable_pool {
self.pool.reserves[token_id] = self.pool.reserves[token_id]
.checked_sub(amount)
.ok_or(MathError::SubUnderflow(101))?;
self.check_min_reserve(token_id, self.pool.reserves[token_id])?;
Ok(())
}

Expand Down Expand Up @@ -556,6 +559,17 @@ pub mod stable_pool {
ensure!(amount > 0, StablePoolError::InsufficientInputAmount);
Ok(amount)
}

fn check_min_reserve(&self, token_id: usize, reserve: u128) -> Result<(), StablePoolError> {
if self.pool.precisions[token_id]
.checked_mul(reserve)
.ok_or(MathError::MulOverflow(115))?
< MIN_RESERVE
{
return Err(StablePoolError::MinReserve);
}
Ok(())
}
}

impl StablePool for StablePoolContract {
Expand All @@ -566,6 +580,14 @@ pub mod stable_pool {
amounts: Vec<u128>,
to: AccountId,
) -> Result<(u128, u128), StablePoolError> {
//check amounts if it is initial liquidity supply
if self.total_supply() == 0 {
amounts
.iter()
.enumerate()
.try_for_each(|(id, &amount)| self.check_min_reserve(id, amount))?;
deuszx marked this conversation as resolved.
Show resolved Hide resolved
}

// calc lp tokens (shares_to_mint, fee)
let (shares, fee_part) = self.get_mint_liquidity_for_amounts(amounts.clone())?;

Expand Down
4 changes: 2 additions & 2 deletions amm/drink-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[cfg(test)]
mod psp22;
#[cfg(test)]
#[allow(unused_imports)]
mod mock_sazero_rate_contract;
#[cfg(test)]
mod psp22;
#[cfg(test)]
mod stable_pool_contract;
#[cfg(test)]
mod stable_swap_tests;
Expand Down
Loading
Loading