Conversation
|
Good luck! |
Comment on lines
+44
to
+82
| function swap( | ||
| address pool, | ||
| address tokenIn, | ||
| address tokenOut, | ||
| address recipient, | ||
| uint priceImpactTolerance | ||
| ) external { | ||
| ILbPairV2 pair = ILbPairV2(pool); | ||
|
|
||
| // --------------- token-out balance before swap (to calculate actual amountOut after swap) | ||
| uint balanceBefore = IERC20(tokenOut).balanceOf(recipient); | ||
|
|
||
| // --------------- verify that provided tokens are correct | ||
| address tokenX = pair.getTokenX(); | ||
| require(_isValid(pair, tokenIn, tokenOut, tokenX), IncorrectTokens()); | ||
|
|
||
| // --------------- input amount should be sent on balance of the pair contract before swap call | ||
| uint amount = IERC20(tokenIn).balanceOf(address(this)); | ||
| IERC20(tokenIn).safeTransfer(pool, amount); | ||
|
|
||
| // --------------- ensure that there is enough output amount in the pair | ||
| (uint128 amountInLeft, uint128 price,) = pair.getSwapOut(uint128(amount), tokenIn == tokenX); | ||
| require(amountInLeft == 0, InsufficientOutputAmount(amountInLeft)); | ||
|
|
||
| // --------------- perform the swap | ||
| pair.swap(tokenX == tokenIn, recipient); | ||
|
|
||
| // --------------- actual amount out received by the recipient | ||
| uint balanceAfter = IERC20(tokenOut).balanceOf(recipient); | ||
| uint amountOut = balanceAfter > balanceBefore ? balanceAfter - balanceBefore : 0; | ||
|
|
||
| // --------------- verify that price impact is within the tolerance limit | ||
| uint priceImpact = (price - amountOut) * ConstantsLib.DENOMINATOR / price; | ||
| if (priceImpact >= priceImpactTolerance) { | ||
| revert(string(abi.encodePacked("!PRICE ", Strings.toString(priceImpact)))); | ||
| } | ||
|
|
||
| emit SwapInPool(pool, tokenIn, tokenOut, recipient, priceImpactTolerance, amount, amountOut); | ||
| } |
Check warning
Code scanning / Slither
Dangerous strict equalities Medium
Comment on lines
+44
to
+82
| function swap( | ||
| address pool, | ||
| address tokenIn, | ||
| address tokenOut, | ||
| address recipient, | ||
| uint priceImpactTolerance | ||
| ) external { | ||
| ILbPairV2 pair = ILbPairV2(pool); | ||
|
|
||
| // --------------- token-out balance before swap (to calculate actual amountOut after swap) | ||
| uint balanceBefore = IERC20(tokenOut).balanceOf(recipient); | ||
|
|
||
| // --------------- verify that provided tokens are correct | ||
| address tokenX = pair.getTokenX(); | ||
| require(_isValid(pair, tokenIn, tokenOut, tokenX), IncorrectTokens()); | ||
|
|
||
| // --------------- input amount should be sent on balance of the pair contract before swap call | ||
| uint amount = IERC20(tokenIn).balanceOf(address(this)); | ||
| IERC20(tokenIn).safeTransfer(pool, amount); | ||
|
|
||
| // --------------- ensure that there is enough output amount in the pair | ||
| (uint128 amountInLeft, uint128 price,) = pair.getSwapOut(uint128(amount), tokenIn == tokenX); | ||
| require(amountInLeft == 0, InsufficientOutputAmount(amountInLeft)); | ||
|
|
||
| // --------------- perform the swap | ||
| pair.swap(tokenX == tokenIn, recipient); | ||
|
|
||
| // --------------- actual amount out received by the recipient | ||
| uint balanceAfter = IERC20(tokenOut).balanceOf(recipient); | ||
| uint amountOut = balanceAfter > balanceBefore ? balanceAfter - balanceBefore : 0; | ||
|
|
||
| // --------------- verify that price impact is within the tolerance limit | ||
| uint priceImpact = (price - amountOut) * ConstantsLib.DENOMINATOR / price; | ||
| if (priceImpact >= priceImpactTolerance) { | ||
| revert(string(abi.encodePacked("!PRICE ", Strings.toString(priceImpact)))); | ||
| } | ||
|
|
||
| emit SwapInPool(pool, tokenIn, tokenOut, recipient, priceImpactTolerance, amount, amountOut); | ||
| } |
Check warning
Code scanning / Slither
Unused return Medium
Comment on lines
+44
to
+82
| function swap( | ||
| address pool, | ||
| address tokenIn, | ||
| address tokenOut, | ||
| address recipient, | ||
| uint priceImpactTolerance | ||
| ) external { | ||
| ILbPairV2 pair = ILbPairV2(pool); | ||
|
|
||
| // --------------- token-out balance before swap (to calculate actual amountOut after swap) | ||
| uint balanceBefore = IERC20(tokenOut).balanceOf(recipient); | ||
|
|
||
| // --------------- verify that provided tokens are correct | ||
| address tokenX = pair.getTokenX(); | ||
| require(_isValid(pair, tokenIn, tokenOut, tokenX), IncorrectTokens()); | ||
|
|
||
| // --------------- input amount should be sent on balance of the pair contract before swap call | ||
| uint amount = IERC20(tokenIn).balanceOf(address(this)); | ||
| IERC20(tokenIn).safeTransfer(pool, amount); | ||
|
|
||
| // --------------- ensure that there is enough output amount in the pair | ||
| (uint128 amountInLeft, uint128 price,) = pair.getSwapOut(uint128(amount), tokenIn == tokenX); | ||
| require(amountInLeft == 0, InsufficientOutputAmount(amountInLeft)); | ||
|
|
||
| // --------------- perform the swap | ||
| pair.swap(tokenX == tokenIn, recipient); | ||
|
|
||
| // --------------- actual amount out received by the recipient | ||
| uint balanceAfter = IERC20(tokenOut).balanceOf(recipient); | ||
| uint amountOut = balanceAfter > balanceBefore ? balanceAfter - balanceBefore : 0; | ||
|
|
||
| // --------------- verify that price impact is within the tolerance limit | ||
| uint priceImpact = (price - amountOut) * ConstantsLib.DENOMINATOR / price; | ||
| if (priceImpact >= priceImpactTolerance) { | ||
| revert(string(abi.encodePacked("!PRICE ", Strings.toString(priceImpact)))); | ||
| } | ||
|
|
||
| emit SwapInPool(pool, tokenIn, tokenOut, recipient, priceImpactTolerance, amount, amountOut); | ||
| } |
Check warning
Code scanning / Slither
Unused return Medium
Comment on lines
+114
to
+129
| function getPrice(address pool, address tokenIn, address tokenOut, uint amount) public view returns (uint price) { | ||
| address tokenX = ILbPairV2(pool).getTokenX(); | ||
| if (_isValid(ILbPairV2(pool), tokenIn, tokenOut, tokenX)) { | ||
| if (amount == 0) { | ||
| amount = 10 ** IERC20Metadata(tokenIn).decimals(); | ||
| } | ||
| (uint128 amountInLeft, uint128 amountOut,) = ILbPairV2(pool).getSwapOut(uint128(amount), tokenIn == tokenX); | ||
|
|
||
| // todo how to handle following case without reverting? | ||
| require(amountInLeft == 0, InsufficientOutputAmount(amountInLeft)); | ||
|
|
||
| price = amountOut; | ||
| } | ||
|
|
||
| return price; | ||
| } |
Check warning
Code scanning / Slither
Unused return Medium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.