-
Notifications
You must be signed in to change notification settings - Fork 37
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
Add testInitialLiquidity
to part 3 tests
#6
base: main
Are you sure you want to change the base?
Conversation
Hey @ardislu, thank you so much for the PR! We are so happy to see that people are learning from the workshops and acting upon it. This is a very good invariant to test. The truth you are assessing is that "one should not be able to provide less than the initial minimum liquidity required by the protocol". Your format of the test and the use of coverage are all great. Here are some points to consider:
Hope this helps and thanks again for the PR. |
Thank you for the feedback! On point 1, your suggestion looks like a much more elegant/efficient way to implement the // If any failure condition is met, user should not be awarded any LP tokens
else {
assert(pair.balanceOf(address(user)) == 0);
} I observed that this // success3 should be false if:
// - the minimimum liquidity was not met, OR
// - either token amount transferred is more than 2**112 - 1 (the maximum UniswapV2 reserve)
assert(amount1 > 2**112 - 1 || amount2 > 2**112 - 1 || Math.sqrt(amount1 * amount2) < 10**3); On point 2, I added assert(Math.sqrt(a).mul(Math.sqrt(b)) == Math.sqrt(a.mul(b)));
assert(Math.sqrt(a) / Math.sqrt(b) == Math.sqrt(a / b)); After running this test I can confirm that these properties do not hold when the inputs are small numbers (<100) due to integer rounding in Solidity. However, since the UniswapV2 contract always multiplies the inputs together before passing them to Still good to know about this behavior for future implementations though! Thanks again for the feedback, looking forward to the next stream. |
Hi @ardislu Thanks for this update. Here's my updated version: // amounts overflow max reserve balance
assert(amount1 > type(uint112).max ||
amount2 > type(uint112).max ||
// amounts do not pass the minimum initial liquidity check
Math.sqrt(amount1 * amount2) <= pair.MINIMUM_LIQUIDITY() ||
// amounts would mint zero liquidity
Math.min(
((balance1Before - reserve1Before) *
(pairTotalSupplyBefore)) / reserve1Before,
((balance2Before - reserve2Before) *
(pairTotalSupplyBefore)) / reserve2Before
) ==
0); |
Thank you Justin for the stream today, very insightful.
The purpose of this test is to check that this logic in the
mint
function ofUniswapV2Pair
is working as expected:The coverage report says the new
assert
s are getting hit, so I think the test is working.I'm wondering if this is a good invariant to test though? During the stream I know you set the minimum amounts for
testProvideLiquidity
to1000
. My reasoning for adding this test is that becauseMath
is a custom library, we'd want to confirm thesqrt
logic works. Or is it safe to assume this library does what it's supposed to?Appreciate any feedback and thank you all again for the great series!