Contract Name:
PoolFactory
Contract Source Code:
File 1 of 1 : PoolFactory
// SPDX-License-Identifier: GPL-3.0-or-later
// Hydrometer combines powerful liquidity incentives, low slippage, and a vote-locked governance model using $HYDRO and $veHYDRO tokens, ensuring an innovative and decentralized experience for all users.
//https://x.com/Hydrometer_Fi
pragma solidity 0.8.19;
interface IPoolFactory {
event SetFeeManager(address feeManager);
event SetPauser(address pauser);
event SetPauseState(bool state);
event SetVoter(address voter);
event PoolCreated(address indexed token0, address indexed token1, bool indexed stable, address pool, uint256);
event SetCustomFee(address indexed pool, uint256 fee);
error FeeInvalid();
error FeeTooHigh();
error InvalidPool();
error NotFeeManager();
error NotPauser();
error NotVoter();
error PoolAlreadyExists();
error SameAddress();
error ZeroFee();
error ZeroAddress();
/// @notice returns the number of pools created from this factory
function allPoolsLength() external view returns (uint256);
/// @notice Is a valid pool created by this factory.
/// @param .
function isPool(address pool) external view returns (bool);
/// @notice Return address of pool created by this factory
/// @param tokenA .
/// @param tokenB .
/// @param stable True if stable, false if volatile
function getPool(address tokenA, address tokenB, bool stable) external view returns (address);
/// @notice Support for v3-style pools which wraps around getPool(tokenA,tokenB,stable)
/// @dev fee is converted to stable boolean.
/// @param tokenA .
/// @param tokenB .
/// @param fee 1 if stable, 0 if volatile, else returns address(0)
function getPool(address tokenA, address tokenB, uint24 fee) external view returns (address);
/// @dev Only called once to set to Voter.sol - Voter does not have a function
/// to call this contract method, so once set it's immutable.
/// This also follows convention of setVoterAndDistributor() in VotingEscrow.sol
/// @param _voter .
function setVoter(address _voter) external;
function setPauser(address _pauser) external;
function setPauseState(bool _state) external;
function setFeeManager(address _feeManager) external;
/// @notice Set default fee for stable and volatile pools.
/// @dev Throws if higher than maximum fee.
/// Throws if fee is zero.
/// @param _stable Stable or volatile pool.
/// @param _fee .
function setFee(bool _stable, uint256 _fee) external;
/// @notice Set overriding fee for a pool from the default
/// @dev A custom fee of zero means the default fee will be used.
function setCustomFee(address _pool, uint256 _fee) external;
/// @notice Returns fee for a pool, as custom fees are possible.
function getFee(address _pool, bool _stable) external view returns (uint256);
/// @notice Create a pool given two tokens and if they're stable/volatile
/// @dev token order does not matter
/// @param tokenA .
/// @param tokenB .
/// @param stable .
function createPool(address tokenA, address tokenB, bool stable) external returns (address pool);
/// @notice Support for v3-style pools which wraps around createPool(tokena,tokenB,stable)
/// @dev fee is converted to stable boolean
/// @dev token order does not matter
/// @param tokenA .
/// @param tokenB .
/// @param fee 1 if stable, 0 if volatile, else revert
function createPool(address tokenA, address tokenB, uint24 fee) external returns (address pool);
function isPaused() external view returns (bool);
function voter() external view returns (address);
function implementation() external view returns (address);
}
interface IPool {
error DepositsNotEqual();
error BelowMinimumK();
error FactoryAlreadySet();
error InsufficientLiquidity();
error InsufficientLiquidityMinted();
error InsufficientLiquidityBurned();
error InsufficientOutputAmount();
error InsufficientInputAmount();
error IsPaused();
error InvalidTo();
error K();
error NotEmergencyCouncil();
event Fees(address indexed sender, uint256 amount0, uint256 amount1);
event Mint(address indexed sender, uint256 amount0, uint256 amount1);
event Burn(address indexed sender, address indexed to, uint256 amount0, uint256 amount1);
event Swap(
address indexed sender,
address indexed to,
uint256 amount0In,
uint256 amount1In,
uint256 amount0Out,
uint256 amount1Out
);
event Sync(uint256 reserve0, uint256 reserve1);
event Claim(address indexed sender, address indexed recipient, uint256 amount0, uint256 amount1);
// Struct to capture time period obervations every 30 minutes, used for local oracles
struct Observation {
uint256 timestamp;
uint256 reserve0Cumulative;
uint256 reserve1Cumulative;
}
/// @notice Returns the decimal (dec), reserves (r), stable (st), and tokens (t) of token0 and token1
function metadata()
external
view
returns (uint256 dec0, uint256 dec1, uint256 r0, uint256 r1, bool st, address t0, address t1);
/// @notice Claim accumulated but unclaimed fees (claimable0 and claimable1)
function claimFees() external returns (uint256, uint256);
/// @notice Returns [token0, token1]
function tokens() external view returns (address, address);
/// @notice Address of token in the pool with the lower address value
function token0() external view returns (address);
/// @notice Address of token in the poool with the higher address value
function token1() external view returns (address);
/// @notice Address of linked PoolFees.sol
function poolFees() external view returns (address);
/// @notice Address of PoolFactory that created this contract
function factory() external view returns (address);
/// @notice Capture oracle reading every 30 minutes (1800 seconds)
function periodSize() external view returns (uint256);
/// @notice Amount of token0 in pool
function reserve0() external view returns (uint256);
/// @notice Amount of token1 in pool
function reserve1() external view returns (uint256);
/// @notice Timestamp of last update to pool
function blockTimestampLast() external view returns (uint256);
/// @notice Cumulative of reserve0 factoring in time elapsed
function reserve0CumulativeLast() external view returns (uint256);
/// @notice Cumulative of reserve1 factoring in time elapsed
function reserve1CumulativeLast() external view returns (uint256);
/// @notice Accumulated fees of token0 (global)
function index0() external view returns (uint256);
/// @notice Accumulated fees of token1 (global)
function index1() external view returns (uint256);
/// @notice Get an LP's relative index0 to index0
function supplyIndex0(address) external view returns (uint256);
/// @notice Get an LP's relative index1 to index1
function supplyIndex1(address) external view returns (uint256);
/// @notice Amount of unclaimed, but claimable tokens from fees of token0 for an LP
function claimable0(address) external view returns (uint256);
/// @notice Amount of unclaimed, but claimable tokens from fees of token1 for an LP
function claimable1(address) external view returns (uint256);
/// @notice Returns the value of K in the Pool, based on its reserves.
function getK() external returns (uint256);
/// @notice Set pool name
/// Only callable by Voter.emergencyCouncil()
/// @param __name String of new name
function setName(string calldata __name) external;
/// @notice Set pool symbol
/// Only callable by Voter.emergencyCouncil()
/// @param __symbol String of new symbol
function setSymbol(string calldata __symbol) external;
/// @notice Get the number of observations recorded
function observationLength() external view returns (uint256);
/// @notice Get the value of the most recent observation
function lastObservation() external view returns (Observation memory);
/// @notice True if pool is stable, false if volatile
function stable() external view returns (bool);
/// @notice Produces the cumulative price using counterfactuals to save gas and avoid a call to sync.
function currentCumulativePrices()
external
view
returns (uint256 reserve0Cumulative, uint256 reserve1Cumulative, uint256 blockTimestamp);
/// @notice Provides twap price with user configured granularity, up to the full window size
/// @param tokenIn .
/// @param amountIn .
/// @param granularity .
/// @return amountOut .
function quote(address tokenIn, uint256 amountIn, uint256 granularity) external view returns (uint256 amountOut);
/// @notice Returns a memory set of TWAP prices
/// Same as calling sample(tokenIn, amountIn, points, 1)
/// @param tokenIn .
/// @param amountIn .
/// @param points Number of points to return
/// @return Array of TWAP prices
function prices(address tokenIn, uint256 amountIn, uint256 points) external view returns (uint256[] memory);
/// @notice Same as prices with with an additional window argument.
/// Window = 2 means 2 * 30min (or 1 hr) between observations
/// @param tokenIn .
/// @param amountIn .
/// @param points .
/// @param window .
/// @return Array of TWAP prices
function sample(
address tokenIn,
uint256 amountIn,
uint256 points,
uint256 window
) external view returns (uint256[] memory);
/// @notice This low-level function should be called from a contract which performs important safety checks
/// @param amount0Out Amount of token0 to send to `to`
/// @param amount1Out Amount of token1 to send to `to`
/// @param to Address to recieve the swapped output
/// @param data Additional calldata for flashloans
function swap(uint256 amount0Out, uint256 amount1Out, address to, bytes calldata data) external;
/// @notice This low-level function should be called from a contract which performs important safety checks
/// standard uniswap v2 implementation
/// @param to Address to receive token0 and token1 from burning the pool token
/// @return amount0 Amount of token0 returned
/// @return amount1 Amount of token1 returned
function burn(address to) external returns (uint256 amount0, uint256 amount1);
/// @notice This low-level function should be called by addLiquidity functions in Router.sol, which performs important safety checks
/// standard uniswap v2 implementation
/// @param to Address to receive the minted LP token
/// @return liquidity Amount of LP token minted
function mint(address to) external returns (uint256 liquidity);
/// @notice Update reserves and, on the first call per block, price accumulators
/// @return _reserve0 .
/// @return _reserve1 .
/// @return _blockTimestampLast .
function getReserves() external view returns (uint256 _reserve0, uint256 _reserve1, uint256 _blockTimestampLast);
/// @notice Get the amount of tokenOut given the amount of tokenIn
/// @param amountIn Amount of token in
/// @param tokenIn Address of token
/// @return Amount out
function getAmountOut(uint256 amountIn, address tokenIn) external view returns (uint256);
/// @notice Force balances to match reserves
/// @param to Address to receive any skimmed rewards
function skim(address to) external;
/// @notice Force reserves to match balances
function sync() external;
/// @notice Called on pool creation by PoolFactory
/// @param _token0 Address of token0
/// @param _token1 Address of token1
/// @param _stable True if stable, false if volatile
function initialize(address _token0, address _token1, bool _stable) external;
}
// OpenZeppelin Contracts (last updated v4.8.0) (proxy/Clones.sol)
/**
* @dev https://eips.ethereum.org/EIPS/eip-1167[EIP 1167] is a standard for
* deploying minimal proxy contracts, also known as "clones".
*
* > To simply and cheaply clone contract functionality in an immutable way, this standard specifies
* > a minimal bytecode implementation that delegates all calls to a known, fixed address.
*
* The library includes functions to deploy a proxy using either `create` (traditional deployment) or `create2`
* (salted deterministic deployment). It also includes functions to predict the addresses of clones deployed using the
* deterministic method.
*
* _Available since v3.4._
*/
library Clones {
/**
* @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
*
* This function uses the create opcode, which should never revert.
*/
function clone(address implementation) internal returns (address instance) {
/// @solidity memory-safe-assembly
assembly {
// Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
// of the `implementation` address with the bytecode before the address.
mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
// Packs the remaining 17 bytes of `implementation` with the bytecode after the address.
mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
instance := create(0, 0x09, 0x37)
}
require(instance != address(0), "ERC1167: create failed");
}
/**
* @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
*
* This function uses the create2 opcode and a `salt` to deterministically deploy
* the clone. Using the same `implementation` and `salt` multiple time will revert, since
* the clones cannot be deployed twice at the same address.
*/
function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) {
/// @solidity memory-safe-assembly
assembly {
// Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
// of the `implementation` address with the bytecode before the address.
mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
// Packs the remaining 17 bytes of `implementation` with the bytecode after the address.
mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
instance := create2(0, 0x09, 0x37, salt)
}
require(instance != address(0), "ERC1167: create2 failed");
}
/**
* @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
*/
function predictDeterministicAddress(
address implementation,
bytes32 salt,
address deployer
) internal pure returns (address predicted) {
/// @solidity memory-safe-assembly
assembly {
let ptr := mload(0x40)
mstore(add(ptr, 0x38), deployer)
mstore(add(ptr, 0x24), 0x5af43d82803e903d91602b57fd5bf3ff)
mstore(add(ptr, 0x14), implementation)
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73)
mstore(add(ptr, 0x58), salt)
mstore(add(ptr, 0x78), keccak256(add(ptr, 0x0c), 0x37))
predicted := keccak256(add(ptr, 0x43), 0x55)
}
}
/**
* @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
*/
function predictDeterministicAddress(
address implementation,
bytes32 salt
) internal view returns (address predicted) {
return predictDeterministicAddress(implementation, salt, address(this));
}
}
contract PoolFactory is IPoolFactory {
address public immutable implementation;
bool public isPaused;
address public pauser;
uint256 public stableFee;
uint256 public volatileFee;
uint256 public constant MAX_FEE = 300; // 3%
// Override to indicate there is custom 0% fee - as a 0 value in the customFee mapping indicates
// that no custom fee rate has been set
uint256 public constant ZERO_FEE_INDICATOR = 420;
address public feeManager;
/// @dev used to change the name/symbol of the pool by calling emergencyCouncil
address public voter;
mapping(address => mapping(address => mapping(bool => address))) private _getPool;
address[] public allPools;
mapping(address => bool) private _isPool; // simplified check if its a pool, given that `stable` flag might not be available in peripherals
mapping(address => uint256) public customFee; // override for custom fees
address internal _temp0;
address internal _temp1;
bool internal _temp;
constructor(address _implementation) {
implementation = _implementation;
voter = msg.sender;
pauser = msg.sender;
feeManager = msg.sender;
isPaused = false;
stableFee = 5; // 0.05%
volatileFee = 30; // 0.3%
}
/// @inheritdoc IPoolFactory
function allPoolsLength() external view returns (uint256) {
return allPools.length;
}
/// @inheritdoc IPoolFactory
function getPool(address tokenA, address tokenB, uint24 fee) external view returns (address) {
return fee > 1 ? address(0) : fee == 1 ? _getPool[tokenA][tokenB][true] : _getPool[tokenA][tokenB][false];
}
/// @inheritdoc IPoolFactory
function getPool(address tokenA, address tokenB, bool stable) external view returns (address) {
return _getPool[tokenA][tokenB][stable];
}
/// @inheritdoc IPoolFactory
function isPool(address pool) external view returns (bool) {
return _isPool[pool];
}
/// @inheritdoc IPoolFactory
function setVoter(address _voter) external {
if (msg.sender != voter) revert NotVoter();
voter = _voter;
emit SetVoter(_voter);
}
function setPauser(address _pauser) external {
if (msg.sender != pauser) revert NotPauser();
if (_pauser == address(0)) revert ZeroAddress();
pauser = _pauser;
emit SetPauser(_pauser);
}
function setPauseState(bool _state) external {
if (msg.sender != pauser) revert NotPauser();
isPaused = _state;
emit SetPauseState(_state);
}
function setFeeManager(address _feeManager) external {
if (msg.sender != feeManager) revert NotFeeManager();
if (_feeManager == address(0)) revert ZeroAddress();
feeManager = _feeManager;
emit SetFeeManager(_feeManager);
}
/// @inheritdoc IPoolFactory
function setFee(bool _stable, uint256 _fee) external {
if (msg.sender != feeManager) revert NotFeeManager();
if (_fee > MAX_FEE) revert FeeTooHigh();
if (_fee == 0) revert ZeroFee();
if (_stable) {
stableFee = _fee;
} else {
volatileFee = _fee;
}
}
/// @inheritdoc IPoolFactory
function setCustomFee(address pool, uint256 fee) external {
if (msg.sender != feeManager) revert NotFeeManager();
if (fee > MAX_FEE && fee != ZERO_FEE_INDICATOR) revert FeeTooHigh();
if (!_isPool[pool]) revert InvalidPool();
customFee[pool] = fee;
emit SetCustomFee(pool, fee);
}
/// @inheritdoc IPoolFactory
function getFee(address pool, bool _stable) public view returns (uint256) {
uint256 fee = customFee[pool];
return fee == ZERO_FEE_INDICATOR ? 0 : fee != 0 ? fee : _stable ? stableFee : volatileFee;
}
/// @inheritdoc IPoolFactory
function createPool(address tokenA, address tokenB, uint24 fee) external returns (address pool) {
if (fee > 1) revert FeeInvalid();
bool stable = fee == 1;
return createPool(tokenA, tokenB, stable);
}
/// @inheritdoc IPoolFactory
function createPool(address tokenA, address tokenB, bool stable) public returns (address pool) {
if (tokenA == tokenB) revert SameAddress();
(address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
if (token0 == address(0)) revert ZeroAddress();
if (_getPool[token0][token1][stable] != address(0)) revert PoolAlreadyExists();
bytes32 salt = keccak256(abi.encodePacked(token0, token1, stable)); // salt includes stable as well, 3 parameters
pool = Clones.cloneDeterministic(implementation, salt);
IPool(pool).initialize(token0, token1, stable);
_getPool[token0][token1][stable] = pool;
_getPool[token1][token0][stable] = pool; // populate mapping in the reverse direction
allPools.push(pool);
_isPool[pool] = true;
emit PoolCreated(token0, token1, stable, pool, allPools.length);
}
}