Skip to content

Commit

Permalink
cyrin feedback (#22)
Browse files Browse the repository at this point in the history
* Cyfrin-I2: incorrect stableInfo contract for bsc testnet

* [Cyfrin-I1] - Incorrect test setup in StableSwap.t.sol

* feat: [Cyfrin-M2] proper check of amountOut in stableSwap

* [Cyfrin-M1]: add warning about v3 position manager call
  • Loading branch information
ChefMist authored Oct 8, 2024
1 parent 7fabb59 commit 1b4fe05
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
193198
193973
Original file line number Diff line number Diff line change
@@ -1 +1 @@
192654
194041
2 changes: 1 addition & 1 deletion .forge-snapshots/UniversalRouterBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24434
24566
2 changes: 1 addition & 1 deletion script/deployParameters/testnet/DeployBscTestnet.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ contract DeployBscTestnet is DeployUniversalRouter {
v2InitCodeHash: 0xd0d4c4cd0848c93cb4fd1f498d7013ee6bfb25783ea21593d5834f5d250ece66,
v3InitCodeHash: 0x6ce8eb472fa82df5469c6ab6d485f17c3ad13c8cd7af59b3d4a8026c5ce0f7e2,
stableFactory: 0xe6A00f8b819244e8Ab9Ea930e46449C2F20B6609,
stableInfo: 0xe6A00f8b819244e8Ab9Ea930e46449C2F20B6609,
stableInfo: 0x0A548d59D04096Bc01206D58C3D63c478e1e06dB,
v4Vault: 0x0a125Bb36e409957Ed951eF1FBe20e81D682EAb6,
v4ClPoolManager: 0x26Ca53c8C5CE90E22aA1FadDA68AB9a08f7BA06f,
v4BinPoolManager: 0x1DF0be383e9d17DA4448E57712849aBE5b3Fa33b,
Expand Down
1 change: 1 addition & 0 deletions src/base/Dispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ abstract contract Dispatcher is
revert NotAuthorizedForToken(tokenId);
}

/// @dev ensure there's follow-up action if v3 position's removed token are sent to router contract
(success, output) = address(V3_POSITION_MANAGER).call(inputs);
return (success, output);
} else if (command == Commands.V4_CL_POSITION_CALL) {
Expand Down
3 changes: 2 additions & 1 deletion src/modules/pancakeswap/StableSwapRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ abstract contract StableSwapRouter is RouterImmutables, Permit2Payments, Ownable
}

ERC20 tokenOut = ERC20(path[path.length - 1]);
uint256 balanceBefore = tokenOut.balanceOf(address(this));

_stableSwap(path, flag);

uint256 amountOut = tokenOut.balanceOf(address(this));
uint256 amountOut = tokenOut.balanceOf(address(this)) - balanceBefore;
if (amountOut < amountOutMinimum) revert StableTooLittleReceived();

if (recipient != address(this)) pay(address(tokenOut), recipient, amountOut);
Expand Down
33 changes: 27 additions & 6 deletions test/stableSwap/StableSwap.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,11 @@ abstract contract StableSwapTest is Test, GasSnapshot {
path[0] = token0();
path[1] = token1();
bytes[] memory inputs = new bytes[](1);
inputs[0] = abi.encode(ActionConstants.MSG_SENDER, AMOUNT, 0, path, flag(), true);
inputs[0] = abi.encode(ActionConstants.MSG_SENDER, AMOUNT, 0, path, flag(), false);

router.execute(commands, inputs);
assertGt(ERC20(token1()).balanceOf(FROM), BALANCE);
assertEq(ERC20(token0()).balanceOf(FROM), BALANCE); // no token0 taken from user, taken from router
assertGt(ERC20(token1()).balanceOf(FROM), BALANCE); // token1 received
}

function test_stableSwap_exactInput1For0FromRouter() public {
Expand All @@ -165,10 +166,28 @@ abstract contract StableSwapTest is Test, GasSnapshot {
path[0] = token1();
path[1] = token0();
bytes[] memory inputs = new bytes[](1);
inputs[0] = abi.encode(ActionConstants.MSG_SENDER, AMOUNT, 0, path, flag(), true);
inputs[0] = abi.encode(ActionConstants.MSG_SENDER, AMOUNT, 0, path, flag(), false);

router.execute(commands, inputs);
assertGt(ERC20(token0()).balanceOf(FROM), BALANCE);
assertGt(ERC20(token0()).balanceOf(FROM), BALANCE); // token0 received
assertEq(ERC20(token1()).balanceOf(FROM), BALANCE); // no token1 taken from user, taken from router
}

function test_stableSwap_exactInput0For1_StableTooLittleReceived() public {
// have some AMOUNT * 2 token1 in router, assumed from previous commands
deal(token1(), address(router), AMOUNT * 2);

bytes memory commands = abi.encodePacked(bytes1(uint8(Commands.STABLE_SWAP_EXACT_IN)));
// equivalent: abi.decode(inputs, (address, uint256, uint256, address[], uint256[], bool)
address[] memory path = new address[](2);
path[0] = token0();
path[1] = token1();
bytes[] memory inputs = new bytes[](1);
// set minOut as amount * 2 which is not achievable
inputs[0] = abi.encode(ActionConstants.MSG_SENDER, AMOUNT, AMOUNT * 2, path, flag(), true);

vm.expectRevert(StableSwapRouter.StableTooLittleReceived.selector);
router.execute(commands, inputs);
}

function test_stableSwap_exactOutput0For1() public {
Expand Down Expand Up @@ -210,9 +229,10 @@ abstract contract StableSwapTest is Test, GasSnapshot {
path[0] = token0();
path[1] = token1();
bytes[] memory inputs = new bytes[](1);
inputs[0] = abi.encode(ActionConstants.MSG_SENDER, AMOUNT, type(uint256).max, path, flag(), true);
inputs[0] = abi.encode(ActionConstants.MSG_SENDER, AMOUNT, type(uint256).max, path, flag(), false);

router.execute(commands, inputs);
assertEq(ERC20(token0()).balanceOf(FROM), BALANCE); // no token0 taken from user, taken from router
assertGe(ERC20(token1()).balanceOf(FROM), BALANCE + AMOUNT);
}

Expand All @@ -225,10 +245,11 @@ abstract contract StableSwapTest is Test, GasSnapshot {
path[0] = token1();
path[1] = token0();
bytes[] memory inputs = new bytes[](1);
inputs[0] = abi.encode(ActionConstants.MSG_SENDER, AMOUNT, type(uint256).max, path, flag(), true);
inputs[0] = abi.encode(ActionConstants.MSG_SENDER, AMOUNT, type(uint256).max, path, flag(), false);

router.execute(commands, inputs);
assertGe(ERC20(token0()).balanceOf(FROM), BALANCE + AMOUNT);
assertEq(ERC20(token1()).balanceOf(FROM), BALANCE); // no token1 taken from user, taken from router
}

function token0() internal virtual returns (address);
Expand Down

0 comments on commit 1b4fe05

Please sign in to comment.