-
Notifications
You must be signed in to change notification settings - Fork 14
436 amm adapter for lfj #437
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
base: main
Are you sure you want to change the base?
Conversation
|
Good luck! |
| 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
| 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
| 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
| 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
No description provided.