Skip to content

Commit

Permalink
test: Test the ThunderSwapPoolFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
mgnfy-view committed Mar 22, 2024
1 parent bcee06b commit 92cfa7d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
43 changes: 43 additions & 0 deletions test/unit/deployPool/DeployPool.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import {PoolFactoryHelper} from "../../utils/helpers/PoolFactoryHelper.sol";
import {UniversalHelper} from "../../utils/helpers/UniversalHelper.sol";
import {Token} from "../../utils/mocks/Token.sol";

contract DeployPool is UniversalHelper, PoolFactoryHelper {
function testDeployingThunderSwapPoolRevertsIfTokenNotSupported() public {
address tokenAddress = makeAddr("unsupportedToken");

vm.expectRevert(abi.encodeWithSelector(TokenNotSupported.selector, tokenAddress));
thunderSwapPoolFactory.deployThunderSwapPool(tokenAddress, tokenAddress);
}

function testDeployingThunderSwapPoolRevertsIfBothTokensAreTheSame() public {
address tokenAddress = makeAddr("unsupportedToken");

vm.prank(deployer);
thunderSwapPoolFactory.setSupportedToken(tokenAddress);
vm.expectRevert(PoolCannotHaveTwoTokensOfTheSameType.selector);
thunderSwapPoolFactory.deployThunderSwapPool(tokenAddress, tokenAddress);
}

function testDeployingThunderSwapPoolRevertsIfPoolAlreadyExists() public distributeTokensToUsers(1e18, 2e18) addInitialLiquidity(1e18, 2e18) {
vm.expectRevert(abi.encodeWithSelector(PoolAlreadyExists.selector, address(thunderSwapPool)));
thunderSwapPoolFactory.deployThunderSwapPool(address(tokenB), address(tokenA));
}

function testDeployingThunderSwapPoolEmitsEvent() public {
Token tokenC = new Token("TokenC", "C");
Token tokenD = new Token("TokenD", "D");

vm.startPrank(deployer);
thunderSwapPoolFactory.setSupportedToken(address(tokenC));
thunderSwapPoolFactory.setSupportedToken(address(tokenD));

vm.expectEmit(false, true, true, false);
emit PoolCreated(address(0), address(tokenC), address(tokenD));
thunderSwapPoolFactory.deployThunderSwapPool(address(tokenC), address(tokenD));
vm.stopPrank();
}
}
26 changes: 26 additions & 0 deletions test/unit/miscellaneousTests/miscellaneous.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,30 @@ contract MiscellaneousTest is UniversalHelper {
thunderSwapPool.getOutputBasedOnInput(inputAmount, inputReserves, outputReserves);
assert(outputAmount > minimumOutputAmount);
}

function testIsTokenSupported() public view {
assert(thunderSwapPoolFactory.isTokenSupported(address(tokenA)));
assert(thunderSwapPoolFactory.isTokenSupported(address(tokenB)));
}

function testSetSupportedToken() public {
address supportToken = makeAddr("supportToken");

vm.prank(deployer);
thunderSwapPoolFactory.setSupportedToken(supportToken);

assert(thunderSwapPoolFactory.isTokenSupported(supportToken));
}

function testGetPoolFromToken() public view {
assertEq(thunderSwapPoolFactory.getPoolFromToken(address(tokenA)), address(thunderSwapPool));
assertEq(thunderSwapPoolFactory.getPoolFromToken(address(tokenB)), address(thunderSwapPool));
}

function testGetPoolTokensFromThunderSwapPool() public view {
address[] memory poolTokens = thunderSwapPoolFactory.getPoolTokens(address(thunderSwapPool));

assertEq(poolTokens[0], address(tokenA));
assertEq(poolTokens[1], address(tokenB));
}
}
11 changes: 11 additions & 0 deletions test/utils/helpers/PoolFactoryHelper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

contract PoolFactoryHelper {
event PoolCreated(address newPool, address token1, address token2);
event SupportedToken(address supportedToken);

error PoolAlreadyExists(address pool);
error PoolCannotHaveTwoTokensOfTheSameType();
error TokenNotSupported(address tokenAddress);
}

0 comments on commit 92cfa7d

Please sign in to comment.