-
Notifications
You must be signed in to change notification settings - Fork 2
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
Conversation
There was a problem hiding this 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.
There was a problem hiding this 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:
- For swaps, trader balance + pool reserves should be equivalent pre- and post- swap
- For add/remove liquidity, trader balance + pool reserves should be equivalent pre- and post- add/remove liquidity.
This reverts commit 4559499.
300000 * ONE_LPT + 498999996725367 + 498999993395420, // -- DIFF -- 300000 * ONE_LPT + 499999996666583 + 499999993277742 | ||
"Incorrect LP token supply" | ||
); | ||
} |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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.
There was a problem hiding this 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( |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Test try withdrawing too much
stable_swap, | ||
BOB, | ||
tokens[0], // DAI | ||
tokens[1], // USDC |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.expect("Should successfully swap"); | |
.expect("Should successfully add liquidity"); |
vec![1 * ONE_SAZERO, 1 * ONE_WAZERO], | ||
charlie(), | ||
) | ||
.expect("Should successfully swap"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.expect("Should successfully add LP"); | |
.expect("Should successfully add liquidity"); |
(many occurrences)
); | ||
assert_eq!( | ||
psp22_utils::balance_of(&mut session, tokens[1], charlie()), | ||
997499999501, |
There was a problem hiding this comment.
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], |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
swap_exact_in
testsswap_received
testsswap_exact_out
testsadd/remove_liqudity
simulation testsStablePool
with external token rate tests