Skip to content

Commit

Permalink
Fix bug that made s_tokenToPool to store only the last created pool
Browse files Browse the repository at this point in the history
  • Loading branch information
zarkk01 committed Mar 24, 2024
1 parent a80884a commit 054e59e
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/ThunderSwapPoolFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract ThunderSwapPoolFactory is Ownable {
mapping(address => bool) private s_supportedTokens;
mapping(address => address) private s_tokenToPool;
mapping(address => address[]) private s_tokenToPools;
mapping(address => address[]) private s_poolToTokens;

event PoolCreated(address newPool, address token1, address token2);
Expand Down Expand Up @@ -42,23 +42,19 @@ contract ThunderSwapPoolFactory is Ownable {
external
returns (ThunderSwapPool)
{
address poolFromToken1 = s_tokenToPool[_token1];
if (!s_supportedTokens[_token1]) revert TokenNotSupported(_token1);
if (!s_supportedTokens[_token2]) revert TokenNotSupported(_token2);
if (_token1 == _token2) revert PoolCannotHaveTwoTokensOfTheSameType();
if (poolFromToken1 != address(0) && poolFromToken1 == s_tokenToPool[_token2]) {
revert PoolAlreadyExists(poolFromToken1);
}


string memory poolName =
string.concat("ThunderSwap", ERC20(_token1).name(), ERC20(_token2).name());
string memory poolSymbol =
string.concat("TS", ERC20(_token1).symbol(), ERC20(_token1).symbol());
ThunderSwapPool newPool = new ThunderSwapPool(_token1, _token2, poolName, poolSymbol);

s_poolToTokens[address(newPool)] = [_token1, _token2];
s_tokenToPool[_token1] = address(newPool);
s_tokenToPool[_token2] = address(newPool);
s_tokenToPools[_token1].push(address(newPool));
s_tokenToPools[_token2].push(address(newPool));

emit PoolCreated(address(newPool), _token1, _token2);

Expand All @@ -79,8 +75,8 @@ contract ThunderSwapPoolFactory is Ownable {
* @param _token The token to find a pool for
* @return The thunder swap pool address for the given token
*/
function getPoolFromToken(address _token) external view returns (address) {
return s_tokenToPool[_token];
function getPoolsFromToken(address _token) external view returns (address[] memory) {
return s_tokenToPools[_token];
}

/**
Expand Down

0 comments on commit 054e59e

Please sign in to comment.