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

StableRatedPool drink tests #17

Merged
merged 35 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6f9e5b4
wip
JanKuczma Jul 2, 2024
9c43c4b
add ref to tests
JanKuczma Jul 2, 2024
8dc6989
add comments on differences
JanKuczma Jul 3, 2024
110128a
rename tests
JanKuczma Jul 3, 2024
4559499
mock rate contract refactor
JanKuczma Jul 3, 2024
ee0bfb2
Revert "mock rate contract refactor"
JanKuczma Jul 3, 2024
fbcba92
Merge branch 'main' into drink-tests
JanKuczma Jul 3, 2024
82a9180
fix share price calculation
JanKuczma Jul 3, 2024
b76ef43
additional swap tests
JanKuczma Jul 3, 2024
b214984
split tests
JanKuczma Jul 3, 2024
b63808a
ensure reserves updated properly
JanKuczma Jul 3, 2024
83aecb8
test rated wip
JanKuczma Jul 3, 2024
8092ec7
Merge branch 'main' into drink-tests
JanKuczma Jul 4, 2024
f59a452
fmt
JanKuczma Jul 5, 2024
07f687f
fix helper function
JanKuczma Jul 5, 2024
d327786
test rated part 1
JanKuczma Jul 5, 2024
bfe42bb
extract initial variables
JanKuczma Jul 5, 2024
1250be9
tests withdrawing all liquidity
JanKuczma Jul 5, 2024
e42549a
simplify share price helper
JanKuczma Jul 6, 2024
7041516
Merge branch 'main' into drink-tests
JanKuczma Jul 9, 2024
d6a668e
adjust fees to new precision
JanKuczma Jul 9, 2024
3aaba7c
adjust expected results in test due to change in fee precision
JanKuczma Jul 9, 2024
3531956
allow salt in test contracts deployment
JanKuczma Jul 10, 2024
4b6d765
cross tests for swap_exact_out
JanKuczma Jul 10, 2024
6b6cdfc
Merge branch 'main' into drink-tests
JanKuczma Jul 10, 2024
23a9466
wip - test lp withdraw getter
JanKuczma Jul 11, 2024
0506eee
tests expoit add-remove liqudity
JanKuczma Jul 11, 2024
2dafd4a
try exploit test
woocash2 Jul 12, 2024
62b4a24
add some comments
woocash2 Jul 12, 2024
1b82a05
Merge branch 'drink-tests' into drink-test-try-exploit
JanKuczma Jul 12, 2024
4be58d5
Merge pull request #29 from Cardinal-Cryptography/drink-test-try-exploit
JanKuczma Jul 12, 2024
a4bbcd4
Merge branch 'main' into drink-tests
JanKuczma Jul 15, 2024
28e6ac1
fix typo in comment
JanKuczma Jul 15, 2024
c4c7b00
update old comments
JanKuczma Jul 15, 2024
6b4bed8
combine swap_exact_in and swap_received tests
JanKuczma Jul 15, 2024
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
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
25 changes: 15 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,10 @@
#[allow(dead_code)]
mod tests_add_remove_lp;
mod tests_getters;
mod tests_rated;
mod tests_swap_exact_in;
mod tests_swap_exact_out;
mod tests_swap_received;

use crate::stable_pool_contract;
pub use crate::utils::*;
use primitive_types::U256;
Expand Down Expand Up @@ -35,6 +41,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 +51,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 +60,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 +78,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 +100,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 +121,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
Loading