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

StableRatedPool drink tests #17

merged 35 commits into from
Jul 15, 2024

Conversation

JanKuczma
Copy link
Collaborator

@JanKuczma JanKuczma commented Jul 3, 2024

  • swap_exact_in tests
  • swap_received tests
  • swap_exact_out tests
  • add/remove_liqudity simulation tests
  • StablePool with external token rate tests

@JanKuczma JanKuczma requested a review from deuszx July 3, 2024 09:57
Copy link
Contributor

@deuszx deuszx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gone through the first iteration, leaving some comments.

I'm wondering – swap_exact_in and swap_received should be behaving in the exact same way, right? i.e. for the same amount_in they should be outputing the same amount_out. If that's true, it means they should share the test cases as well. It'd provide additional layer of verification.

Copy link
Contributor

@deuszx deuszx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's another invariant that should hold for all swap types and adding/removing tokens:

amount of tokens should be constant, tokens should not disappear

For example:

  1. For swaps, trader balance + pool reserves should be equivalent pre- and post- swap
  2. For add/remove liquidity, trader balance + pool reserves should be equivalent pre- and post- add/remove liquidity.

@JanKuczma JanKuczma mentioned this pull request Jul 4, 2024
300000 * ONE_LPT + 498999996725367 + 498999993395420, // -- DIFF -- 300000 * ONE_LPT + 499999996666583 + 499999993277742
"Incorrect LP token supply"
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MIssing newline at the end of the file.

);
assert_eq!(
stable_swap::fees(&mut session, stable_swap),
(25, 2000),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could extract fees and amp_coef to vars and use here and in the constructor.

Copy link
Contributor

@deuszx deuszx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you plan on merging tests_swap_exact_in and tests_swap_received into a single test file and asserting the two flows result in the same trade outputs?

BOB,
);

_ = stable_swap::add_liquidity(
Copy link
Contributor

@deuszx deuszx Jul 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In setup_stable_swap_with_tokens it's hidden that it's BOB that gets all the "moni" and it increases the allownce on stable_swap. I'd extract that b/c - it's not helping with reviewing the code.


assert_eq!(
share_price_and_total_shares(&mut session, stable_swap, None),
(last_share_price, last_total_shares + 1500 * ONE_LPT)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the last_share_price? What we're really checking is that reserves increased by 500 each and that there's 3*1500 more LP tokens. What I want to say is that we're really testing that reserves/LP tokens increase as expected. Maybe that's worth extracting to a standalone test? Maybe I'm overreacting but I like small and simple tests that test one thing only (if possible).

BOB,
);

// add more liquidity with imbalanced tokens (dave)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a separate test – for managing liquidity w/ imbalanced token amounts.

let (current_share_price, current_total_shares) =
share_price_and_total_shares(&mut session, stable_swap, None);
assert!(
current_share_price > last_share_price,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the share price increase when you add tokens in imbalanced amounts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, b/c when depositing in imbalanced amounts then fees apply, hence the value of one share in terms of tokens in the pool increases.

@JanKuczma JanKuczma marked this pull request as ready for review July 15, 2024 07:36
@JanKuczma JanKuczma requested a review from woocash2 July 15, 2024 09:42
stable_swap,
BOB,
tokens[0], // DAI
tokens[1], // USDC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be USDT, right?

const ONE_WAZERO: u128 = 10u128.pow(WAZERO_DEC as u32);
const ONE_SAZERO: u128 = 10u128.pow(SAZERO_DEC as u32);

const EXPIRE_TS: u64 = 24 * 3600 * 1000; // 24h
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What TS is supposed to mean? I see it's in millis so maybe EXPIRE_TIME_MILLIS?

vec![50000 * ONE_SAZERO, 100000 * ONE_WAZERO],
charlie(),
)
.expect("Should successfully swap");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.expect("Should successfully swap");
.expect("Should successfully add liquidity");

vec![1 * ONE_SAZERO, 1 * ONE_WAZERO],
charlie(),
)
.expect("Should successfully swap");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.expect("Should successfully swap");
.expect("Should successfully remove liquidity");

vec![100000 * ONE_WAZERO, 50000 * ONE_WAZERO, 100000 * ONE_WAZERO],
bob(),
)
.expect("Should successfully add LP");
Copy link
Contributor

@woocash2 woocash2 Jul 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.expect("Should successfully add LP");
.expect("Should successfully add liquidity");

(many occurrences)

);
assert_eq!(
psp22_utils::balance_of(&mut session, tokens[1], charlie()),
997499999501,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff?

);
assert_eq!(
stable_swap::reserves(&mut session, rated_swap),
vec![100001 * ONE_WAZERO, 99999 * ONE_WAZERO + 2500000499],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff?

vec![6, 6], // decimals
vec![100000000000, 100000000000], // initial reserves
1000, // A
600_000, // fee BPS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our fees have 1e9 precision, so BPS is not a good word here, right?

T: Add + Sub + Copy + PartialEq + PartialOrd + Eq + Ord + std::fmt::Display,
<T as Sub>::Output: PartialOrd<T>,
{
if a > b {
Copy link
Contributor

@woocash2 woocash2 Jul 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we simplify to |a - b| > delta?
Oh, I suppose not necessarily, because of the generic types

)
.expect("swap_exact_out failed");

let swap_exact_in_result = stable_swap::swap_exact_in(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very clever to use swap_exact_in for verification 🙂

Copy link
Contributor

@woocash2 woocash2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@JanKuczma JanKuczma merged commit 60f7e5f into main Jul 15, 2024
1 check passed
@JanKuczma JanKuczma deleted the drink-tests branch July 15, 2024 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants