Skip to content

Commit

Permalink
StableRatedPool drink tests (#17)
Browse files Browse the repository at this point in the history
* wip

* add ref to tests

* add comments on differences

* rename tests

* mock rate contract refactor

* Revert "mock rate contract refactor"

This reverts commit 4559499.

* fix share price calculation

* additional swap tests

* split tests

* ensure reserves updated properly

* test rated wip

* fmt

* fix helper function

* test rated part 1

* extract initial variables

* tests withdrawing all liquidity

* simplify share price helper

* adjust fees to new precision

* adjust expected results in test due to change in fee precision

* allow salt in test contracts deployment

* cross tests for swap_exact_out

* wip - test lp withdraw getter

* tests expoit add-remove liqudity

* try exploit test

* add some comments

* fix typo in comment

* update old comments

* combine swap_exact_in and swap_received tests

---------

Co-authored-by: woocash2 <[email protected]>
  • Loading branch information
JanKuczma and woocash2 authored Jul 15, 2024
1 parent 5123a07 commit 60f7e5f
Show file tree
Hide file tree
Showing 8 changed files with 2,679 additions and 12 deletions.
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
24 changes: 14 additions & 10 deletions amm/drink-tests/src/stable_swap_tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#[allow(dead_code)]
mod tests_add_remove_lp;
mod tests_getters;
mod tests_rated;
mod tests_swap_exact_in_received;
mod tests_swap_exact_out;

use crate::stable_pool_contract;
pub use crate::utils::*;
use primitive_types::U256;
Expand Down Expand Up @@ -35,6 +40,7 @@ pub fn setup_stable_swap_with_tokens(
trade_fee: u32,
protocol_trade_fee: u32,
caller: AccountId32,
salt: Vec<u8>,
) -> (AccountId, Vec<AccountId>) {
let _ = session.set_actor(caller);

Expand All @@ -44,6 +50,7 @@ pub fn setup_stable_swap_with_tokens(

upload_all(session);

let salty_str = String::from_utf8(salt.clone()).unwrap_or("Test token".to_string());
// instantiate tokens
let tokens: Vec<AccountId> = token_decimals
.iter()
Expand All @@ -52,7 +59,7 @@ pub fn setup_stable_swap_with_tokens(
.map(|(id, (&decimals, &supply))| {
psp22_utils::setup_with_amounts(
session,
format!("Test Token {id}").to_string(),
format!("{salty_str} {id}").to_string(),
decimals,
supply,
BOB,
Expand All @@ -70,7 +77,8 @@ pub fn setup_stable_swap_with_tokens(
trade_fee,
protocol_trade_fee,
Some(fee_receiver()),
);
)
.with_salt(salt);

let stable_swap: stable_pool_contract::Instance = session
.instantiate(instance)
Expand All @@ -91,15 +99,11 @@ pub fn setup_stable_swap_with_tokens(
pub fn share_price_and_total_shares(
session: &mut Session<MinimalRuntime>,
stable_swap: AccountId,
token_rates: Option<Vec<u128>>,
) -> (u128, u128) {
let total_shares = psp22_utils::total_supply(session, stable_swap);
let reserves = stable_swap::reserves(session, stable_swap);
let token_rates: Vec<u128> = if let Some(rates) = token_rates {
rates
} else {
reserves.iter().map(|_| RATE_PRECISION).collect()
};
let token_rates = stable_swap::token_rates(session, stable_swap);

let sum_token = stable_swap::tokens(session, stable_swap)
.iter()
.zip(reserves.iter())
Expand All @@ -116,7 +120,7 @@ pub fn share_price_and_total_shares(
.checked_mul(100000000.into())
.unwrap()
.checked_div(total_shares.into())
.unwrap_or(100000000.into())
.unwrap_or(0.into()) // return 0 if total shares 0
.as_u128(),
total_shares,
)
Expand Down
Loading

0 comments on commit 60f7e5f

Please sign in to comment.