From 43a027a0ecff219164db38ada94afa8ca03cc51d Mon Sep 17 00:00:00 2001 From: Sahil-Gujrati Date: Fri, 15 Mar 2024 11:02:36 +0530 Subject: [PATCH] test: Forgot to stage the changes before committing --- test/unit/liquidityTests/addLiquidity.t.sol | 125 ++++++++++++++++++++ test/utils/mocks/Token.sol | 13 ++ 2 files changed, 138 insertions(+) create mode 100644 test/unit/liquidityTests/addLiquidity.t.sol create mode 100644 test/utils/mocks/Token.sol diff --git a/test/unit/liquidityTests/addLiquidity.t.sol b/test/unit/liquidityTests/addLiquidity.t.sol new file mode 100644 index 0000000..f805477 --- /dev/null +++ b/test/unit/liquidityTests/addLiquidity.t.sol @@ -0,0 +1,125 @@ +// SPDX-License-Identifier: MIT +pragma solidity 0.8.20; + +import { Test } from "forge-std/Test.sol"; +import { Init } from "../../utils/helpers/Init.sol"; + +contract TestAddingLiquidity is Test, Init { + function testAddInitialLiquidity() public distributeTokensToUsers(1e18, 2e18) addInitialLiquidity(1e18, 2e18) { + uint256 poolToken1Amount = 1e18; + uint256 poolToken2Amount = 2e18; + + assertEq(thunderSwapPool.getTotalLiquidityProviderTokenSupply(), poolToken1Amount); + assertEq(thunderSwapPool.getLiquidityProviderToken().balanceOf(deployer), poolToken1Amount); + assertEq(tokenA.balanceOf(address(thunderSwapPool)), poolToken1Amount); + assertEq(tokenB.balanceOf(address(thunderSwapPool)), poolToken2Amount); + } + + function testaddingLiquidityFailsIfInputTokenAmountIsZero() public { + vm.startPrank(user1); + vm.expectRevert(InputValueZeroNotAllowed.selector); + thunderSwapPool.addLiquidity(0, 0, 0, 0, uint256(block.timestamp) - 1); + vm.stopPrank(); + } + + function testAddingLiquidityFailsIfDeadlineHasPassed() public distributeTokensToUsers(1e18, 2e18) { + uint256 poolToken1Amount = 1e18; + uint256 poolToken2Amount = 1e18; + + vm.startPrank(user1); + tokenA.approve(address(thunderSwapPool), poolToken1Amount); + tokenB.approve(address(thunderSwapPool), poolToken2Amount); + vm.warp(1 days); + uint256 deadline = uint256(block.timestamp) - 10; + vm.expectRevert(abi.encodeWithSelector(DeadlinePassed.selector, deadline)); + thunderSwapPool.addLiquidity(poolToken1Amount, poolToken2Amount, poolToken2Amount, 0, deadline); + vm.stopPrank(); + } + + function testAddingLiquidityFailsIfAmountTooLow() public distributeTokensToUsers(1e18, 2e18) { + uint256 poolToken1Amount = 1e8; + uint256 poolToken2Amount = 2e8; + uint256 minimumLiquidtyToSupply = thunderSwapPool.getMinimumPoolToken1ToSupply(); + + vm.startPrank(user1); + tokenA.approve(address(thunderSwapPool), poolToken1Amount); + tokenB.approve(address(thunderSwapPool), poolToken2Amount); + vm.expectRevert( + abi.encodeWithSelector(LiquidityToAddTooLow.selector, poolToken1Amount, minimumLiquidtyToSupply) + ); + thunderSwapPool.addLiquidity(poolToken1Amount, poolToken2Amount, poolToken2Amount, 0, uint256(block.timestamp)); + vm.stopPrank(); + } + + function testAddingLiquidityRevertsIfPoolToken2ToDepositGreaterThanMaximumPoolToken2ToDeposit() + public + distributeTokensToUsers(1e18, 2e18) + addInitialLiquidity(1e18, 2e18) + { + uint256 poolToken1Amount = 1e18; + uint256 poolToken2Amount = 1e18; + uint256 poolToken1Reserves = tokenA.balanceOf(address(thunderSwapPool)); + uint256 poolToken2Reserves = tokenB.balanceOf(address(thunderSwapPool)); + uint256 expectedPoolTokensToDeposit = + thunderSwapPool.getOutputAmountBasedOnInput(poolToken1Amount, poolToken1Reserves, poolToken2Reserves); + uint256 maximumPoolToken2AmountToDeposit = 15e17; + + vm.startPrank(user1); + tokenA.approve(address(thunderSwapPool), poolToken1Amount); + tokenB.approve(address(thunderSwapPool), poolToken2Amount); + vm.expectRevert( + abi.encodeWithSelector( + PoolTokensToDepositGreaterThanMaximumPoolTokensToDeposit.selector, + thunderSwapPool.getPoolToken2(), + expectedPoolTokensToDeposit, + maximumPoolToken2AmountToDeposit + ) + ); + thunderSwapPool.addLiquidity( + poolToken1Amount, poolToken2Amount, maximumPoolToken2AmountToDeposit, 0, uint256(block.timestamp) + ); + vm.stopPrank(); + } + + function testAddingLiquidtyRevertsIfMinimumLiquidityProviderTokensToMintTooLow() + public + distributeTokensToUsers(1e18, 2e18) + addInitialLiquidity(1e18, 2e18) + { + uint256 poolToken1Amount = 1e18; + uint256 poolToken2Amount = 2e18; + uint256 minimumLiquidityProviderTokensToMint = 1e19; + + vm.startPrank(user1); + tokenA.approve(address(thunderSwapPool), poolToken1Amount); + tokenB.approve(address(thunderSwapPool), poolToken2Amount); + vm.expectRevert( + abi.encodeWithSelector( + LiquidityProviderTokensToMintTooLow.selector, poolToken1Amount, minimumLiquidityProviderTokensToMint + ) + ); + thunderSwapPool.addLiquidity( + poolToken1Amount, + poolToken2Amount, + poolToken2Amount, + minimumLiquidityProviderTokensToMint, + uint256(block.timestamp) + ); + vm.stopPrank(); + } + + function testAddingLiquidityEmitsEvent() public distributeTokensToUsers(1e18, 2e18) { + uint256 poolToken1Amount = 1e18; + uint256 poolToken2Amount = 2e18; + + vm.startPrank(deployer); + tokenA.approve(address(thunderSwapPool), poolToken1Amount); + tokenB.approve(address(thunderSwapPool), poolToken2Amount); + vm.expectEmit(true, true, true, false); + emit LiquidityAdded(deployer, poolToken1Amount, poolToken2Amount); + thunderSwapPool.addLiquidity( + poolToken1Amount, poolToken2Amount, poolToken2Amount, poolToken1Amount, uint256(block.timestamp) + ); + vm.stopPrank(); + } +} diff --git a/test/utils/mocks/Token.sol b/test/utils/mocks/Token.sol new file mode 100644 index 0000000..c2c0c1d --- /dev/null +++ b/test/utils/mocks/Token.sol @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: MIT +pragma solidity 0.8.20; + +import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; +import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol"; + +contract Token is ERC20, Ownable { + constructor(string memory _name, string memory _symbol) ERC20(_name, _symbol) Ownable(msg.sender) { } + + function mint(address _account, uint256 _value) external onlyOwner { + _mint(_account, _value); + } +}