// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.19;
// OpenZeppelin Contracts (last updated v4.8.0) (utils/math/Math.sol)
/**
* @dev Standard math utilities missing in the Solidity language.
*/
library Math {
enum Rounding {
Down, // Toward negative infinity
Up, // Toward infinity
Zero // Toward zero
}
/**
* @dev Returns the largest of two numbers.
*/
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a > b ? a : b;
}
/**
* @dev Returns the smallest of two numbers.
*/
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/
function average(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b) / 2 can overflow.
return (a & b) + (a ^ b) / 2;
}
/**
* @dev Returns the ceiling of the division of two numbers.
*
* This differs from standard division with `/` in that it rounds up instead
* of rounding down.
*/
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b - 1) / b can overflow on addition, so we distribute.
return a == 0 ? 0 : (a - 1) / b + 1;
}
/**
* @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
* @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
* with further edits by Uniswap Labs also under MIT license.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
// 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
// use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
// variables such that product = prod1 * 2^256 + prod0.
uint256 prod0; // Least significant 256 bits of the product
uint256 prod1; // Most significant 256 bits of the product
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division.
if (prod1 == 0) {
return prod0 / denominator;
}
// Make sure the result is less than 2^256. Also prevents denominator == 0.
require(denominator > prod1, "Math: mulDiv overflow");
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [prod1 prod0].
uint256 remainder;
assembly {
// Compute remainder using mulmod.
remainder := mulmod(x, y, denominator)
// Subtract 256 bit number from 512 bit number.
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
// Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
// See https://cs.stackexchange.com/q/138556/92363.
// Does not overflow because the denominator cannot be zero at this stage in the function.
uint256 twos = denominator & (~denominator + 1);
assembly {
// Divide denominator by twos.
denominator := div(denominator, twos)
// Divide [prod1 prod0] by twos.
prod0 := div(prod0, twos)
// Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
twos := add(div(sub(0, twos), twos), 1)
}
// Shift in bits from prod1 into prod0.
prod0 |= prod1 * twos;
// Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
// that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
// four bits. That is, denominator * inv = 1 mod 2^4.
uint256 inverse = (3 * denominator) ^ 2;
// Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
// in modular arithmetic, doubling the correct bits in each step.
inverse *= 2 - denominator * inverse; // inverse mod 2^8
inverse *= 2 - denominator * inverse; // inverse mod 2^16
inverse *= 2 - denominator * inverse; // inverse mod 2^32
inverse *= 2 - denominator * inverse; // inverse mod 2^64
inverse *= 2 - denominator * inverse; // inverse mod 2^128
inverse *= 2 - denominator * inverse; // inverse mod 2^256
// Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
// This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
// less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
// is no longer required.
result = prod0 * inverse;
return result;
}
}
/**
* @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
uint256 result = mulDiv(x, y, denominator);
if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
result += 1;
}
return result;
}
/**
* @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
*
* Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
*/
function sqrt(uint256 a) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
// For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
//
// We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
// `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
//
// This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
// → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
// → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
//
// Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
uint256 result = 1 << (log2(a) >> 1);
// At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
// since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
// every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
// into the expected uint128 result.
unchecked {
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
return min(result, a / result);
}
}
/**
* @notice Calculates sqrt(a), following the selected rounding direction.
*/
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
}
}
/**
* @dev Return the log in base 2, rounded down, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 128;
}
if (value >> 64 > 0) {
value >>= 64;
result += 64;
}
if (value >> 32 > 0) {
value >>= 32;
result += 32;
}
if (value >> 16 > 0) {
value >>= 16;
result += 16;
}
if (value >> 8 > 0) {
value >>= 8;
result += 8;
}
if (value >> 4 > 0) {
value >>= 4;
result += 4;
}
if (value >> 2 > 0) {
value >>= 2;
result += 2;
}
if (value >> 1 > 0) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 2, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log2(value);
return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 10, rounded down, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >= 10 ** 64) {
value /= 10 ** 64;
result += 64;
}
if (value >= 10 ** 32) {
value /= 10 ** 32;
result += 32;
}
if (value >= 10 ** 16) {
value /= 10 ** 16;
result += 16;
}
if (value >= 10 ** 8) {
value /= 10 ** 8;
result += 8;
}
if (value >= 10 ** 4) {
value /= 10 ** 4;
result += 4;
}
if (value >= 10 ** 2) {
value /= 10 ** 2;
result += 2;
}
if (value >= 10 ** 1) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 10, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log10(value);
return result + (rounding == Rounding.Up && 10 ** result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 256, rounded down, of a positive value.
* Returns 0 if given 0.
*
* Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
*/
function log256(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 16;
}
if (value >> 64 > 0) {
value >>= 64;
result += 8;
}
if (value >> 32 > 0) {
value >>= 32;
result += 4;
}
if (value >> 16 > 0) {
value >>= 16;
result += 2;
}
if (value >> 8 > 0) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 256, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log256(value);
return result + (rounding == Rounding.Up && 1 << (result << 3) < value ? 1 : 0);
}
}
}
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;
}
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);
}
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 amount) external returns (bool);
}
interface IWETH is IERC20 {
function deposit() external payable;
function withdraw(uint256) external;
}
interface IRouter {
struct Route {
address from;
address to;
bool stable;
address factory;
}
error ETHTransferFailed();
error Expired();
error InsufficientAmount();
error InsufficientAmountA();
error InsufficientAmountB();
error InsufficientAmountADesired();
error InsufficientAmountBDesired();
error InsufficientAmountAOptimal();
error InsufficientLiquidity();
error InsufficientOutputAmount();
error InvalidAmountInForETHDeposit();
error InvalidTokenInForETHDeposit();
error InvalidPath();
error InvalidRouteA();
error InvalidRouteB();
error OnlyWETH();
error PoolDoesNotExist();
error PoolFactoryDoesNotExist();
error SameAddresses();
error ZeroAddress();
/// @notice Address of FactoryRegistry.sol
function factoryRegistry() external view returns (address);
/// @notice Address of Protocol PoolFactory.sol
function defaultFactory() external view returns (address);
/// @notice Address of Voter.sol
function voter() external view returns (address);
/// @notice Interface of WETH contract used for WETH => ETH wrapping/unwrapping
function weth() external view returns (IWETH);
/// @dev Represents Ether. Used by zapper to determine whether to return assets as ETH/WETH.
function ETHER() external view returns (address);
/// @dev Struct containing information necessary to zap in and out of pools
/// @param tokenA .
/// @param tokenB .
/// @param stable Stable or volatile pool
/// @param factory factory of pool
/// @param amountOutMinA Minimum amount expected from swap leg of zap via routesA
/// @param amountOutMinB Minimum amount expected from swap leg of zap via routesB
/// @param amountAMin Minimum amount of tokenA expected from liquidity leg of zap
/// @param amountBMin Minimum amount of tokenB expected from liquidity leg of zap
struct Zap {
address tokenA;
address tokenB;
bool stable;
address factory;
uint256 amountOutMinA;
uint256 amountOutMinB;
uint256 amountAMin;
uint256 amountBMin;
}
/// @notice Sort two tokens by which address value is less than the other
/// @param tokenA Address of token to sort
/// @param tokenB Address of token to sort
/// @return token0 Lower address value between tokenA and tokenB
/// @return token1 Higher address value between tokenA and tokenB
function sortTokens(address tokenA, address tokenB) external pure returns (address token0, address token1);
/// @notice Calculate the address of a pool by its' factory.
/// Used by all Router functions containing a `Route[]` or `_factory` argument.
/// Reverts if _factory is not approved by the FactoryRegistry
/// @dev Returns a randomly generated address for a nonexistent pool
/// @param tokenA Address of token to query
/// @param tokenB Address of token to query
/// @param stable True if pool is stable, false if volatile
/// @param _factory Address of factory which created the pool
function poolFor(
address tokenA,
address tokenB,
bool stable,
address _factory
) external view returns (address pool);
/// @notice Fetch and sort the reserves for a pool
/// @param tokenA .
/// @param tokenB .
/// @param stable True if pool is stable, false if volatile
/// @param _factory Address of PoolFactory for tokenA and tokenB
/// @return reserveA Amount of reserves of the sorted token A
/// @return reserveB Amount of reserves of the sorted token B
function getReserves(
address tokenA,
address tokenB,
bool stable,
address _factory
) external view returns (uint256 reserveA, uint256 reserveB);
/// @notice Perform chained getAmountOut calculations on any number of pools
function getAmountsOut(uint256 amountIn, Route[] memory routes) external view returns (uint256[] memory amounts);
// **** ADD LIQUIDITY ****
/// @notice Quote the amount deposited into a Pool
/// @param tokenA .
/// @param tokenB .
/// @param stable True if pool is stable, false if volatile
/// @param _factory Address of PoolFactory for tokenA and tokenB
/// @param amountADesired Amount of tokenA desired to deposit
/// @param amountBDesired Amount of tokenB desired to deposit
/// @return amountA Amount of tokenA to actually deposit
/// @return amountB Amount of tokenB to actually deposit
/// @return liquidity Amount of liquidity token returned from deposit
function quoteAddLiquidity(
address tokenA,
address tokenB,
bool stable,
address _factory,
uint256 amountADesired,
uint256 amountBDesired
) external view returns (uint256 amountA, uint256 amountB, uint256 liquidity);
/// @notice Quote the amount of liquidity removed from a Pool
/// @param tokenA .
/// @param tokenB .
/// @param stable True if pool is stable, false if volatile
/// @param _factory Address of PoolFactory for tokenA and tokenB
/// @param liquidity Amount of liquidity to remove
/// @return amountA Amount of tokenA received
/// @return amountB Amount of tokenB received
function quoteRemoveLiquidity(
address tokenA,
address tokenB,
bool stable,
address _factory,
uint256 liquidity
) external view returns (uint256 amountA, uint256 amountB);
/// @notice Add liquidity of two tokens to a Pool
/// @param tokenA .
/// @param tokenB .
/// @param stable True if pool is stable, false if volatile
/// @param amountADesired Amount of tokenA desired to deposit
/// @param amountBDesired Amount of tokenB desired to deposit
/// @param amountAMin Minimum amount of tokenA to deposit
/// @param amountBMin Minimum amount of tokenB to deposit
/// @param to Recipient of liquidity token
/// @param deadline Deadline to receive liquidity
/// @return amountA Amount of tokenA to actually deposit
/// @return amountB Amount of tokenB to actually deposit
/// @return liquidity Amount of liquidity token returned from deposit
function addLiquidity(
address tokenA,
address tokenB,
bool stable,
uint256 amountADesired,
uint256 amountBDesired,
uint256 amountAMin,
uint256 amountBMin,
address to,
uint256 deadline
) external returns (uint256 amountA, uint256 amountB, uint256 liquidity);
/// @notice Add liquidity of a token and WETH (transferred as ETH) to a Pool
/// @param token .
/// @param stable True if pool is stable, false if volatile
/// @param amountTokenDesired Amount of token desired to deposit
/// @param amountTokenMin Minimum amount of token to deposit
/// @param amountETHMin Minimum amount of ETH to deposit
/// @param to Recipient of liquidity token
/// @param deadline Deadline to add liquidity
/// @return amountToken Amount of token to actually deposit
/// @return amountETH Amount of tokenETH to actually deposit
/// @return liquidity Amount of liquidity token returned from deposit
function addLiquidityETH(
address token,
bool stable,
uint256 amountTokenDesired,
uint256 amountTokenMin,
uint256 amountETHMin,
address to,
uint256 deadline
) external payable returns (uint256 amountToken, uint256 amountETH, uint256 liquidity);
// **** REMOVE LIQUIDITY ****
/// @notice Remove liquidity of two tokens from a Pool
/// @param tokenA .
/// @param tokenB .
/// @param stable True if pool is stable, false if volatile
/// @param liquidity Amount of liquidity to remove
/// @param amountAMin Minimum amount of tokenA to receive
/// @param amountBMin Minimum amount of tokenB to receive
/// @param to Recipient of tokens received
/// @param deadline Deadline to remove liquidity
/// @return amountA Amount of tokenA received
/// @return amountB Amount of tokenB received
function removeLiquidity(
address tokenA,
address tokenB,
bool stable,
uint256 liquidity,
uint256 amountAMin,
uint256 amountBMin,
address to,
uint256 deadline
) external returns (uint256 amountA, uint256 amountB);
/// @notice Remove liquidity of a token and WETH (returned as ETH) from a Pool
/// @param token .
/// @param stable True if pool is stable, false if volatile
/// @param liquidity Amount of liquidity to remove
/// @param amountTokenMin Minimum amount of token to receive
/// @param amountETHMin Minimum amount of ETH to receive
/// @param to Recipient of liquidity token
/// @param deadline Deadline to receive liquidity
/// @return amountToken Amount of token received
/// @return amountETH Amount of ETH received
function removeLiquidityETH(
address token,
bool stable,
uint256 liquidity,
uint256 amountTokenMin,
uint256 amountETHMin,
address to,
uint256 deadline
) external returns (uint256 amountToken, uint256 amountETH);
/// @notice Remove liquidity of a fee-on-transfer token and WETH (returned as ETH) from a Pool
/// @param token .
/// @param stable True if pool is stable, false if volatile
/// @param liquidity Amount of liquidity to remove
/// @param amountTokenMin Minimum amount of token to receive
/// @param amountETHMin Minimum amount of ETH to receive
/// @param to Recipient of liquidity token
/// @param deadline Deadline to receive liquidity
/// @return amountETH Amount of ETH received
function removeLiquidityETHSupportingFeeOnTransferTokens(
address token,
bool stable,
uint256 liquidity,
uint256 amountTokenMin,
uint256 amountETHMin,
address to,
uint256 deadline
) external returns (uint256 amountETH);
// **** SWAP ****
/// @notice Swap one token for another
/// @param amountIn Amount of token in
/// @param amountOutMin Minimum amount of desired token received
/// @param routes Array of trade routes used in the swap
/// @param to Recipient of the tokens received
/// @param deadline Deadline to receive tokens
/// @return amounts Array of amounts returned per route
function swapExactTokensForTokens(
uint256 amountIn,
uint256 amountOutMin,
Route[] calldata routes,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
/// @notice Swap ETH for a token
/// @param amountOutMin Minimum amount of desired token received
/// @param routes Array of trade routes used in the swap
/// @param to Recipient of the tokens received
/// @param deadline Deadline to receive tokens
/// @return amounts Array of amounts returned per route
function swapExactETHForTokens(
uint256 amountOutMin,
Route[] calldata routes,
address to,
uint256 deadline
) external payable returns (uint256[] memory amounts);
/// @notice Swap a token for WETH (returned as ETH)
/// @param amountIn Amount of token in
/// @param amountOutMin Minimum amount of desired ETH
/// @param routes Array of trade routes used in the swap
/// @param to Recipient of the tokens received
/// @param deadline Deadline to receive tokens
/// @return amounts Array of amounts returned per route
function swapExactTokensForETH(
uint256 amountIn,
uint256 amountOutMin,
Route[] calldata routes,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
/// @notice Swap one token for another without slippage protection
/// @return amounts Array of amounts to swap per route
/// @param routes Array of trade routes used in the swap
/// @param to Recipient of the tokens received
/// @param deadline Deadline to receive tokens
function UNSAFE_swapExactTokensForTokens(
uint256[] memory amounts,
Route[] calldata routes,
address to,
uint256 deadline
) external returns (uint256[] memory);
// **** SWAP (supporting fee-on-transfer tokens) ****
/// @notice Swap one token for another supporting fee-on-transfer tokens
/// @param amountIn Amount of token in
/// @param amountOutMin Minimum amount of desired token received
/// @param routes Array of trade routes used in the swap
/// @param to Recipient of the tokens received
/// @param deadline Deadline to receive tokens
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint256 amountIn,
uint256 amountOutMin,
Route[] calldata routes,
address to,
uint256 deadline
) external;
/// @notice Swap ETH for a token supporting fee-on-transfer tokens
/// @param amountOutMin Minimum amount of desired token received
/// @param routes Array of trade routes used in the swap
/// @param to Recipient of the tokens received
/// @param deadline Deadline to receive tokens
function swapExactETHForTokensSupportingFeeOnTransferTokens(
uint256 amountOutMin,
Route[] calldata routes,
address to,
uint256 deadline
) external payable;
/// @notice Swap a token for WETH (returned as ETH) supporting fee-on-transfer tokens
/// @param amountIn Amount of token in
/// @param amountOutMin Minimum amount of desired ETH
/// @param routes Array of trade routes used in the swap
/// @param to Recipient of the tokens received
/// @param deadline Deadline to receive tokens
function swapExactTokensForETHSupportingFeeOnTransferTokens(
uint256 amountIn,
uint256 amountOutMin,
Route[] calldata routes,
address to,
uint256 deadline
) external;
/// @notice Zap a token A into a pool (B, C). (A can be equal to B or C).
/// Supports standard ERC20 tokens only (i.e. not fee-on-transfer tokens etc).
/// Slippage is required for the initial swap.
/// Additional slippage may be required when adding liquidity as the
/// price of the token may have changed.
/// @param tokenIn Token you are zapping in from (i.e. input token).
/// @param amountInA Amount of input token you wish to send down routesA
/// @param amountInB Amount of input token you wish to send down routesB
/// @param zapInPool Contains zap struct information. See Zap struct.
/// @param routesA Route used to convert input token to tokenA
/// @param routesB Route used to convert input token to tokenB
/// @param to Address you wish to mint liquidity to.
/// @param stake Auto-stake liquidity in corresponding gauge.
/// @return liquidity Amount of LP tokens created from zapping in.
function zapIn(
address tokenIn,
uint256 amountInA,
uint256 amountInB,
Zap calldata zapInPool,
Route[] calldata routesA,
Route[] calldata routesB,
address to,
bool stake
) external payable returns (uint256 liquidity);
/// @notice Zap out a pool (B, C) into A.
/// Supports standard ERC20 tokens only (i.e. not fee-on-transfer tokens etc).
/// Slippage is required for the removal of liquidity.
/// Additional slippage may be required on the swap as the
/// price of the token may have changed.
/// @param tokenOut Token you are zapping out to (i.e. output token).
/// @param liquidity Amount of liquidity you wish to remove.
/// @param zapOutPool Contains zap struct information. See Zap struct.
/// @param routesA Route used to convert tokenA into output token.
/// @param routesB Route used to convert tokenB into output token.
function zapOut(
address tokenOut,
uint256 liquidity,
Zap calldata zapOutPool,
Route[] calldata routesA,
Route[] calldata routesB
) external;
/// @notice Used to generate params required for zapping in.
/// Zap in => remove liquidity then swap.
/// Apply slippage to expected swap values to account for changes in reserves in between.
/// @dev Output token refers to the token you want to zap in from.
/// @param tokenA .
/// @param tokenB .
/// @param stable .
/// @param _factory .
/// @param amountInA Amount of input token you wish to send down routesA
/// @param amountInB Amount of input token you wish to send down routesB
/// @param routesA Route used to convert input token to tokenA
/// @param routesB Route used to convert input token to tokenB
/// @return amountOutMinA Minimum output expected from swapping input token to tokenA.
/// @return amountOutMinB Minimum output expected from swapping input token to tokenB.
/// @return amountAMin Minimum amount of tokenA expected from depositing liquidity.
/// @return amountBMin Minimum amount of tokenB expected from depositing liquidity.
function generateZapInParams(
address tokenA,
address tokenB,
bool stable,
address _factory,
uint256 amountInA,
uint256 amountInB,
Route[] calldata routesA,
Route[] calldata routesB
) external view returns (uint256 amountOutMinA, uint256 amountOutMinB, uint256 amountAMin, uint256 amountBMin);
/// @notice Used to generate params required for zapping out.
/// Zap out => swap then add liquidity.
/// Apply slippage to expected liquidity values to account for changes in reserves in between.
/// @dev Output token refers to the token you want to zap out of.
/// @param tokenA .
/// @param tokenB .
/// @param stable .
/// @param _factory .
/// @param liquidity Amount of liquidity being zapped out of into a given output token.
/// @param routesA Route used to convert tokenA into output token.
/// @param routesB Route used to convert tokenB into output token.
/// @return amountOutMinA Minimum output expected from swapping tokenA into output token.
/// @return amountOutMinB Minimum output expected from swapping tokenB into output token.
/// @return amountAMin Minimum amount of tokenA expected from withdrawing liquidity.
/// @return amountBMin Minimum amount of tokenB expected from withdrawing liquidity.
function generateZapOutParams(
address tokenA,
address tokenB,
bool stable,
address _factory,
uint256 liquidity,
Route[] calldata routesA,
Route[] calldata routesB
) external view returns (uint256 amountOutMinA, uint256 amountOutMinB, uint256 amountAMin, uint256 amountBMin);
/// @notice Used by zapper to determine appropriate ratio of A to B to deposit liquidity. Assumes stable pool.
/// @dev Returns stable liquidity ratio of B to (A + B).
/// E.g. if ratio is 0.4, it means there is more of A than there is of B.
/// Therefore you should deposit more of token A than B.
/// @param tokenA tokenA of stable pool you are zapping into.
/// @param tokenB tokenB of stable pool you are zapping into.
/// @param factory Factory that created stable pool.
/// @return ratio Ratio of token0 to token1 required to deposit into zap.
function quoteStableLiquidityRatio(
address tokenA,
address tokenB,
address factory
) external view returns (uint256 ratio);
}
interface IVoter {
error AlreadyVotedOrDeposited();
error DistributeWindow();
error FactoryPathNotApproved();
error GaugeAlreadyKilled();
error GaugeAlreadyRevived();
error GaugeExists();
error GaugeDoesNotExist(address _pool);
error GaugeNotAlive(address _gauge);
error InactiveManagedNFT();
error MaximumVotingNumberTooLow();
error NonZeroVotes();
error NotAPool();
error NotApprovedOrOwner();
error NotGovernor();
error NotEmergencyCouncil();
error NotMinter();
error NotWhitelistedNFT();
error NotWhitelistedToken();
error SameValue();
error SpecialVotingWindow();
error TooManyPools();
error UnequalLengths();
error ZeroBalance();
error ZeroAddress();
event GaugeCreated(
address indexed poolFactory,
address indexed votingRewardsFactory,
address indexed gaugeFactory,
address pool,
address bribeVotingReward,
address feeVotingReward,
address gauge,
address creator
);
event GaugeKilled(address indexed gauge);
event GaugeRevived(address indexed gauge);
event Voted(
address indexed voter,
address indexed pool,
uint256 indexed tokenId,
uint256 weight,
uint256 totalWeight,
uint256 timestamp
);
event Abstained(
address indexed voter,
address indexed pool,
uint256 indexed tokenId,
uint256 weight,
uint256 totalWeight,
uint256 timestamp
);
event NotifyReward(address indexed sender, address indexed reward, uint256 amount);
event DistributeReward(address indexed sender, address indexed gauge, uint256 amount);
event WhitelistToken(address indexed whitelister, address indexed token, bool indexed _bool);
event WhitelistNFT(address indexed whitelister, uint256 indexed tokenId, bool indexed _bool);
/// @notice Store trusted forwarder address to pass into factories
function forwarder() external view returns (address);
/// @notice The ve token that governs these contracts
function ve() external view returns (address);
/// @notice Factory registry for valid pool / gauge / rewards factories
function factoryRegistry() external view returns (address);
/// @notice Address of Minter.sol
function minter() external view returns (address);
/// @notice Standard OZ IGovernor using ve for vote weights.
function governor() external view returns (address);
/// @notice Custom Epoch Governor using ve for vote weights.
function epochGovernor() external view returns (address);
/// @notice credibly neutral party similar to Curve's Emergency DAO
function emergencyCouncil() external view returns (address);
/// @dev Total Voting Weights
function totalWeight() external view returns (uint256);
/// @dev Most number of pools one voter can vote for at once
function maxVotingNum() external view returns (uint256);
// mappings
/// @dev Pool => Gauge
function gauges(address pool) external view returns (address);
/// @dev Gauge => Pool
function poolForGauge(address gauge) external view returns (address);
/// @dev Gauge => Fees Voting Reward
function gaugeToFees(address gauge) external view returns (address);
/// @dev Gauge => Bribes Voting Reward
function gaugeToBribe(address gauge) external view returns (address);
/// @dev Pool => Weights
function weights(address pool) external view returns (uint256);
/// @dev NFT => Pool => Votes
function votes(uint256 tokenId, address pool) external view returns (uint256);
/// @dev NFT => Total voting weight of NFT
function usedWeights(uint256 tokenId) external view returns (uint256);
/// @dev Nft => Timestamp of last vote (ensures single vote per epoch)
function lastVoted(uint256 tokenId) external view returns (uint256);
/// @dev Address => Gauge
function isGauge(address) external view returns (bool);
/// @dev Token => Whitelisted status
function isWhitelistedToken(address token) external view returns (bool);
/// @dev TokenId => Whitelisted status
function isWhitelistedNFT(uint256 tokenId) external view returns (bool);
/// @dev Gauge => Liveness status
function isAlive(address gauge) external view returns (bool);
/// @dev Gauge => Amount claimable
function claimable(address gauge) external view returns (uint256);
/// @notice Number of pools with a Gauge
function length() external view returns (uint256);
/// @notice Called by Minter to distribute weekly emissions rewards for disbursement amongst gauges.
/// @dev Assumes totalWeight != 0 (Will never be zero as long as users are voting).
/// Throws if not called by minter.
/// @param _amount Amount of rewards to distribute.
function notifyRewardAmount(uint256 _amount) external;
/// @dev Utility to distribute to gauges of pools in range _start to _finish.
/// @param _start Starting index of gauges to distribute to.
/// @param _finish Ending index of gauges to distribute to.
function distribute(uint256 _start, uint256 _finish) external;
/// @dev Utility to distribute to gauges of pools in array.
/// @param _gauges Array of gauges to distribute to.
function distribute(address[] memory _gauges) external;
/// @notice Called by users to update voting balances in voting rewards contracts.
/// @param _tokenId Id of veNFT whose balance you wish to update.
function poke(uint256 _tokenId) external;
/// @notice Called by users to vote for pools. Votes distributed proportionally based on weights.
/// Can only vote or deposit into a managed NFT once per epoch.
/// Can only vote for gauges that have not been killed.
/// @dev Weights are distributed proportional to the sum of the weights in the array.
/// Throws if length of _poolVote and _weights do not match.
/// @param _tokenId Id of veNFT you are voting with.
/// @param _poolVote Array of pools you are voting for.
/// @param _weights Weights of pools.
function vote(uint256 _tokenId, address[] calldata _poolVote, uint256[] calldata _weights) external;
/// @notice Called by users to reset voting state. Required if you wish to make changes to
/// veNFT state (e.g. merge, split, deposit into managed etc).
/// Cannot reset in the same epoch that you voted in.
/// Can vote or deposit into a managed NFT again after reset.
/// @param _tokenId Id of veNFT you are reseting.
function reset(uint256 _tokenId) external;
/// @notice Called by users to deposit into a managed NFT.
/// Can only vote or deposit into a managed NFT once per epoch.
/// Note that NFTs deposited into a managed NFT will be re-locked
/// to the maximum lock time on withdrawal.
/// @dev Throws if not approved or owner.
/// Throws if managed NFT is inactive.
/// Throws if depositing within privileged window (one hour prior to epoch flip).
function depositManaged(uint256 _tokenId, uint256 _mTokenId) external;
/// @notice Called by users to withdraw from a managed NFT.
/// Cannot do it in the same epoch that you deposited into a managed NFT.
/// Can vote or deposit into a managed NFT again after withdrawing.
/// Note that the NFT withdrawn is re-locked to the maximum lock time.
function withdrawManaged(uint256 _tokenId) external;
/// @notice Claim emissions from gauges.
/// @param _gauges Array of gauges to collect emissions from.
function claimRewards(address[] memory _gauges) external;
/// @notice Claim bribes for a given NFT.
/// @dev Utility to help batch bribe claims.
/// @param _bribes Array of BribeVotingReward contracts to collect from.
/// @param _tokens Array of tokens that are used as bribes.
/// @param _tokenId Id of veNFT that you wish to claim bribes for.
function claimBribes(address[] memory _bribes, address[][] memory _tokens, uint256 _tokenId) external;
/// @notice Claim fees for a given NFT.
/// @dev Utility to help batch fee claims.
/// @param _fees Array of FeesVotingReward contracts to collect from.
/// @param _tokens Array of tokens that are used as fees.
/// @param _tokenId Id of veNFT that you wish to claim fees for.
function claimFees(address[] memory _fees, address[][] memory _tokens, uint256 _tokenId) external;
/// @notice Set new governor.
/// @dev Throws if not called by governor.
/// @param _governor .
function setGovernor(address _governor) external;
/// @notice Set new epoch based governor.
/// @dev Throws if not called by governor.
/// @param _epochGovernor .
function setEpochGovernor(address _epochGovernor) external;
/// @notice Set new emergency council.
/// @dev Throws if not called by emergency council.
/// @param _emergencyCouncil .
function setEmergencyCouncil(address _emergencyCouncil) external;
/// @notice Set maximum number of gauges that can be voted for.
/// @dev Throws if not called by governor.
/// Throws if _maxVotingNum is too low.
/// Throws if the values are the same.
/// @param _maxVotingNum .
function setMaxVotingNum(uint256 _maxVotingNum) external;
/// @notice Whitelist (or unwhitelist) token for use in bribes.
/// @dev Throws if not called by governor.
/// @param _token .
/// @param _bool .
function whitelistToken(address _token, bool _bool) external;
/// @notice Whitelist (or unwhitelist) token id for voting in last hour prior to epoch flip.
/// @dev Throws if not called by governor.
/// Throws if already whitelisted.
/// @param _tokenId .
/// @param _bool .
function whitelistNFT(uint256 _tokenId, bool _bool) external;
/// @notice Create a new gauge (unpermissioned).
/// @dev Governor can create a new gauge for a pool with any address.
/// @param _poolFactory .
/// @param _pool .
function createGauge(address _poolFactory, address _pool) external returns (address);
/// @notice Kills a gauge. The gauge will not receive any new emissions and cannot be deposited into.
/// Can still withdraw from gauge.
/// @dev Throws if not called by emergency council.
/// Throws if gauge already killed.
/// @param _gauge .
function killGauge(address _gauge) external;
/// @notice Revives a killed gauge. Gauge will can receive emissions and deposits again.
/// @dev Throws if not called by emergency council.
/// Throws if gauge is not killed.
/// @param _gauge .
function reviveGauge(address _gauge) external;
/// @dev Update claims to emissions for an array of gauges.
/// @param _gauges Array of gauges to update emissions for.
function updateFor(address[] memory _gauges) external;
/// @dev Update claims to emissions for gauges based on their pool id as stored in Voter.
/// @param _start Starting index of pools.
/// @param _end Ending index of pools.
function updateFor(uint256 _start, uint256 _end) external;
/// @dev Update claims to emissions for single gauge
/// @param _gauge .
function updateFor(address _gauge) external;
}
interface IGauge {
error NotAlive();
error NotAuthorized();
error NotVoter();
error NotTeam();
error RewardRateTooHigh();
error ZeroAmount();
error ZeroRewardRate();
event Deposit(address indexed from, address indexed to, uint256 amount);
event Withdraw(address indexed from, uint256 amount);
event NotifyReward(address indexed from, uint256 amount);
event ClaimFees(address indexed from, uint256 claimed0, uint256 claimed1);
event ClaimRewards(address indexed from, uint256 amount);
/// @notice Address of the pool LP token which is deposited (staked) for rewards
function stakingToken() external view returns (address);
/// @notice Address of the token (AERO) rewarded to stakers
function rewardToken() external view returns (address);
/// @notice Address of the FeesVotingReward contract linked to the gauge
function feesVotingReward() external view returns (address);
/// @notice Address of Protocol Voter
function voter() external view returns (address);
/// @notice Address of Protocol Voting Escrow
function ve() external view returns (address);
/// @notice Returns if gauge is linked to a legitimate Protocol pool
function isPool() external view returns (bool);
/// @notice Timestamp end of current rewards period
function periodFinish() external view returns (uint256);
/// @notice Current reward rate of rewardToken to distribute per second
function rewardRate() external view returns (uint256);
/// @notice Most recent timestamp contract has updated state
function lastUpdateTime() external view returns (uint256);
/// @notice Most recent stored value of rewardPerToken
function rewardPerTokenStored() external view returns (uint256);
/// @notice Amount of stakingToken deposited for rewards
function totalSupply() external view returns (uint256);
/// @notice Get the amount of stakingToken deposited by an account
function balanceOf(address) external view returns (uint256);
/// @notice Cached rewardPerTokenStored for an account based on their most recent action
function userRewardPerTokenPaid(address) external view returns (uint256);
/// @notice Cached amount of rewardToken earned for an account
function rewards(address) external view returns (uint256);
/// @notice View to see the rewardRate given the timestamp of the start of the epoch
function rewardRateByEpoch(uint256) external view returns (uint256);
/// @notice Cached amount of fees generated from the Pool linked to the Gauge of token0
function fees0() external view returns (uint256);
/// @notice Cached amount of fees generated from the Pool linked to the Gauge of token1
function fees1() external view returns (uint256);
/// @notice Get the current reward rate per unit of stakingToken deposited
function rewardPerToken() external view returns (uint256 _rewardPerToken);
/// @notice Returns the last time the reward was modified or periodFinish if the reward has ended
function lastTimeRewardApplicable() external view returns (uint256 _time);
/// @notice Returns accrued balance to date from last claim / first deposit.
function earned(address _account) external view returns (uint256 _earned);
/// @notice Total amount of rewardToken to distribute for the current rewards period
function left() external view returns (uint256 _left);
/// @notice Retrieve rewards for an address.
/// @dev Throws if not called by same address or voter.
/// @param _account .
function getReward(address _account) external;
/// @notice Deposit LP tokens into gauge for msg.sender
/// @param _amount .
function deposit(uint256 _amount) external;
/// @notice Deposit LP tokens into gauge for any user
/// @param _amount .
/// @param _recipient Recipient to give balance to
function deposit(uint256 _amount, address _recipient) external;
/// @notice Withdraw LP tokens for user
/// @param _amount .
function withdraw(uint256 _amount) external;
/// @dev Notifies gauge of gauge rewards. Assumes gauge reward tokens is 18 decimals.
/// If not 18 decimals, rewardRate may have rounding issues.
function notifyRewardAmount(uint256 amount) external;
/// @dev Notifies gauge of gauge rewards without distributing its fees.
/// Assumes gauge reward tokens is 18 decimals.
/// If not 18 decimals, rewardRate may have rounding issues.
function notifyRewardWithoutClaim(uint256 amount) external;
}
interface IFactoryRegistry {
error FallbackFactory();
error InvalidFactoriesToPoolFactory();
error PathAlreadyApproved();
error PathNotApproved();
error SameAddress();
error ZeroAddress();
event Approve(address indexed poolFactory, address indexed votingRewardsFactory, address indexed gaugeFactory);
event Unapprove(address indexed poolFactory, address indexed votingRewardsFactory, address indexed gaugeFactory);
event SetManagedRewardsFactory(address indexed _newRewardsFactory);
/// @notice Approve a set of factories used in the Protocol.
/// Router.sol is able to swap any poolFactories currently approved.
/// Cannot approve address(0) factories.
/// Cannot aprove path that is already approved.
/// Each poolFactory has one unique set and maintains state. In the case a poolFactory is unapproved
/// and then re-approved, the same set of factories must be used. In other words, you cannot overwrite
/// the factories tied to a poolFactory address.
/// VotingRewardsFactories and GaugeFactories may use the same address across multiple poolFactories.
/// @dev Callable by onlyOwner
/// @param poolFactory .
/// @param votingRewardsFactory .
/// @param gaugeFactory .
function approve(address poolFactory, address votingRewardsFactory, address gaugeFactory) external;
/// @notice Unapprove a set of factories used in the Protocol.
/// While a poolFactory is unapproved, Router.sol cannot swap with pools made from the corresponding factory
/// Can only unapprove an approved path.
/// Cannot unapprove the fallback path (core v2 factories).
/// @dev Callable by onlyOwner
/// @param poolFactory .
function unapprove(address poolFactory) external;
/// @notice Factory to create free and locked rewards for a managed veNFT
function managedRewardsFactory() external view returns (address);
/// @notice Set the rewards factory address
/// @dev Callable by onlyOwner
/// @param _newManagedRewardsFactory address of new managedRewardsFactory
function setManagedRewardsFactory(address _newManagedRewardsFactory) external;
/// @notice Get the factories correlated to a poolFactory.
/// Once set, this can never be modified.
/// Returns the correlated factories even after an approved poolFactory is unapproved.
function factoriesToPoolFactory(
address poolFactory
) external view returns (address votingRewardsFactory, address gaugeFactory);
/// @notice Get all PoolFactories approved by the registry
/// @dev The same PoolFactory address cannot be used twice
/// @return Array of PoolFactory addresses
function poolFactories() external view returns (address[] memory);
/// @notice Check if a PoolFactory is approved within the factory registry. Router uses this method to
/// ensure a pool swapped from is approved.
/// @param poolFactory .
/// @return True if PoolFactory is approved, else false
function isPoolFactoryApproved(address poolFactory) external view returns (bool);
/// @notice Get the length of the poolFactories array
function poolFactoriesLength() external view returns (uint256);
}
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)
/**
* @dev Interface for the optional metadata functions from the ERC20 standard.
*
* _Available since v4.1._
*/
interface IERC20Metadata is IERC20 {
/**
* @dev Returns the name of the token.
*/
function name() external view returns (string memory);
/**
* @dev Returns the symbol of the token.
*/
function symbol() external view returns (string memory);
/**
* @dev Returns the decimals places of the token.
*/
function decimals() external view returns (uint8);
}
// OpenZeppelin Contracts (last updated v4.8.0) (token/ERC20/utils/SafeERC20.sol)
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Permit.sol)
/**
* @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
* https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
*
* Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
* presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
* need to send a transaction, and thus is not required to hold Ether at all.
*/
interface IERC20Permit {
/**
* @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
* given ``owner``'s signed approval.
*
* IMPORTANT: The same issues {IERC20-approve} has related to transaction
* ordering also apply here.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `deadline` must be a timestamp in the future.
* - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
* over the EIP712-formatted function arguments.
* - the signature must use ``owner``'s current nonce (see {nonces}).
*
* For more information on the signature format, see the
* https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
* section].
*/
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
/**
* @dev Returns the current nonce for `owner`. This value must be
* included whenever a signature is generated for {permit}.
*
* Every successful call to {permit} increases ``owner``'s nonce by one. This
* prevents a signature from being used multiple times.
*/
function nonces(address owner) external view returns (uint256);
/**
* @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
*/
// solhint-disable-next-line func-name-mixedcase
function DOMAIN_SEPARATOR() external view returns (bytes32);
}
// OpenZeppelin Contracts (last updated v4.8.0) (utils/Address.sol)
/**
* @dev Collection of functions related to the address type
*/
library Address {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
*
* Furthermore, `isContract` will also return true if the target contract within
* the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
* which only has an effect at the end of a transaction.
* ====
*
* [IMPORTANT]
* ====
* You shouldn't rely on `isContract` to protect against flash loan attacks!
*
* Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
* like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
* constructor.
* ====
*/
function isContract(address account) internal view returns (bool) {
// This method relies on extcodesize/address.code.length, which returns 0
// for contracts in construction, since the code is only stored at the end
// of the constructor execution.
return account.code.length > 0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(
address target,
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
* the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
*
* _Available since v4.8._
*/
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata,
string memory errorMessage
) internal view returns (bytes memory) {
if (success) {
if (returndata.length == 0) {
// only check isContract if the call was successful and the return data is empty
// otherwise we already know that it was a contract
require(isContract(target), "Address: call to non-contract");
}
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
/**
* @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
* revert reason or using the provided one.
*
* _Available since v4.3._
*/
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function _revert(bytes memory returndata, string memory errorMessage) private pure {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
/// @solidity memory-safe-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20 {
using Address for address;
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
/**
* @dev Deprecated. This function has issues similar to the ones found in
* {IERC20-approve}, and its usage is discouraged.
*
* Whenever possible, use {safeIncreaseAllowance} and
* {safeDecreaseAllowance} instead.
*/
function safeApprove(IERC20 token, address spender, uint256 value) internal {
// safeApprove should only be called when setting an initial allowance,
// or when resetting it to zero. To increase and decrease it, use
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
require(
(value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender) + value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
uint256 newAllowance = oldAllowance - value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
}
function safePermit(
IERC20Permit token,
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
uint256 nonceBefore = token.nonces(owner);
token.permit(owner, spender, value, deadline, v, r, s);
uint256 nonceAfter = token.nonces(owner);
require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/
function _callOptionalReturn(IERC20 token, bytes memory data) private {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that
// the target address contains contract code and also asserts for success in the low-level call.
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
// Return data is optional
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
// OpenZeppelin Contracts (last updated v4.7.0) (metatx/ERC2771Context.sol)
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
/**
* @dev Context variant with ERC2771 support.
*/
abstract contract ERC2771Context is Context {
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
address private immutable _trustedForwarder;
/// @custom:oz-upgrades-unsafe-allow constructor
constructor(address trustedForwarder) {
_trustedForwarder = trustedForwarder;
}
function isTrustedForwarder(address forwarder) public view virtual returns (bool) {
return forwarder == _trustedForwarder;
}
function _msgSender() internal view virtual override returns (address sender) {
if (isTrustedForwarder(msg.sender)) {
// The assembly code is more direct than the Solidity version using `abi.decode`.
/// @solidity memory-safe-assembly
assembly {
sender := shr(96, calldataload(sub(calldatasize(), 20)))
}
} else {
return super._msgSender();
}
}
function _msgData() internal view virtual override returns (bytes calldata) {
if (isTrustedForwarder(msg.sender)) {
return msg.data[:msg.data.length - 20];
} else {
return super._msgData();
}
}
}
// 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));
}
}
/// @title Protocol Router
/// @author velodrome.finance, @pegahcarter
/// @notice Router allows routes through any pools created by any factory adhering to univ2 interface.
contract Router is IRouter, ERC2771Context {
using SafeERC20 for IERC20;
/// @inheritdoc IRouter
address public immutable factoryRegistry;
/// @inheritdoc IRouter
address public immutable defaultFactory;
/// @inheritdoc IRouter
address public immutable voter;
/// @inheritdoc IRouter
IWETH public immutable weth;
uint256 internal constant MINIMUM_LIQUIDITY = 10 ** 3;
/// @inheritdoc IRouter
address public constant ETHER = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
modifier ensure(uint256 deadline) {
_ensureDeadline(deadline);
_;
}
function _ensureDeadline(uint256 deadline) internal view {
if (deadline < block.timestamp) revert Expired();
}
constructor(
address _forwarder,
address _factoryRegistry,
address _factory,
address _voter,
address _weth
) ERC2771Context(_forwarder) {
factoryRegistry = _factoryRegistry;
defaultFactory = _factory;
voter = _voter;
weth = IWETH(_weth);
}
receive() external payable {
if (msg.sender != address(weth)) revert OnlyWETH();
}
/// @inheritdoc IRouter
function sortTokens(address tokenA, address tokenB) public pure returns (address token0, address token1) {
if (tokenA == tokenB) revert SameAddresses();
(token0, token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
if (token0 == address(0)) revert ZeroAddress();
}
/// @inheritdoc IRouter
function poolFor(address tokenA, address tokenB, bool stable, address _factory) public view returns (address pool) {
address _defaultFactory = defaultFactory;
address factory = _factory == address(0) ? _defaultFactory : _factory;
if (!IFactoryRegistry(factoryRegistry).isPoolFactoryApproved(factory)) revert PoolFactoryDoesNotExist();
(address token0, address token1) = sortTokens(tokenA, tokenB);
bytes32 salt = keccak256(abi.encodePacked(token0, token1, stable));
pool = Clones.predictDeterministicAddress(IPoolFactory(factory).implementation(), salt, factory);
}
/// @dev given some amount of an asset and pool reserves, returns an equivalent amount of the other asset
/// @dev this only accounts for volatile pools and may return insufficient liquidity for stable pools
function quoteLiquidity(
uint256 amountA,
uint256 reserveA,
uint256 reserveB
) internal pure returns (uint256 amountB) {
if (amountA == 0) revert InsufficientAmount();
if (reserveA == 0 || reserveB == 0) revert InsufficientLiquidity();
amountB = (amountA * reserveB) / reserveA;
}
/// @inheritdoc IRouter
function getReserves(
address tokenA,
address tokenB,
bool stable,
address _factory
) public view returns (uint256 reserveA, uint256 reserveB) {
(address token0, ) = sortTokens(tokenA, tokenB);
(uint256 reserve0, uint256 reserve1, ) = IPool(poolFor(tokenA, tokenB, stable, _factory)).getReserves();
(reserveA, reserveB) = tokenA == token0 ? (reserve0, reserve1) : (reserve1, reserve0);
}
/// @inheritdoc IRouter
function getAmountsOut(uint256 amountIn, Route[] memory routes) public view returns (uint256[] memory amounts) {
if (routes.length < 1) revert InvalidPath();
amounts = new uint256[](routes.length + 1);
amounts[0] = amountIn;
uint256 _length = routes.length;
for (uint256 i = 0; i < _length; i++) {
address factory = routes[i].factory == address(0) ? defaultFactory : routes[i].factory; // default to v2
address pool = poolFor(routes[i].from, routes[i].to, routes[i].stable, factory);
if (IPoolFactory(factory).isPool(pool)) {
amounts[i + 1] = IPool(pool).getAmountOut(amounts[i], routes[i].from);
}
}
}
/// @inheritdoc IRouter
function quoteAddLiquidity(
address tokenA,
address tokenB,
bool stable,
address _factory,
uint256 amountADesired,
uint256 amountBDesired
) public view returns (uint256 amountA, uint256 amountB, uint256 liquidity) {
address _pool = IPoolFactory(_factory).getPool(tokenA, tokenB, stable);
(uint256 reserveA, uint256 reserveB) = (0, 0);
uint256 _totalSupply = 0;
if (_pool != address(0)) {
_totalSupply = IERC20(_pool).totalSupply();
(reserveA, reserveB) = getReserves(tokenA, tokenB, stable, _factory);
}
if (reserveA == 0 && reserveB == 0) {
(amountA, amountB) = (amountADesired, amountBDesired);
liquidity = Math.sqrt(amountA * amountB) - MINIMUM_LIQUIDITY;
} else {
uint256 amountBOptimal = quoteLiquidity(amountADesired, reserveA, reserveB);
if (amountBOptimal <= amountBDesired) {
(amountA, amountB) = (amountADesired, amountBOptimal);
liquidity = Math.min((amountA * _totalSupply) / reserveA, (amountB * _totalSupply) / reserveB);
} else {
uint256 amountAOptimal = quoteLiquidity(amountBDesired, reserveB, reserveA);
(amountA, amountB) = (amountAOptimal, amountBDesired);
liquidity = Math.min((amountA * _totalSupply) / reserveA, (amountB * _totalSupply) / reserveB);
}
}
}
/// @inheritdoc IRouter
function quoteRemoveLiquidity(
address tokenA,
address tokenB,
bool stable,
address _factory,
uint256 liquidity
) public view returns (uint256 amountA, uint256 amountB) {
address _pool = IPoolFactory(_factory).getPool(tokenA, tokenB, stable);
if (_pool == address(0)) {
return (0, 0);
}
(uint256 reserveA, uint256 reserveB) = getReserves(tokenA, tokenB, stable, _factory);
uint256 _totalSupply = IERC20(_pool).totalSupply();
amountA = (liquidity * reserveA) / _totalSupply; // using balances ensures pro-rata distribution
amountB = (liquidity * reserveB) / _totalSupply; // using balances ensures pro-rata distribution
}
function _addLiquidity(
address tokenA,
address tokenB,
bool stable,
uint256 amountADesired,
uint256 amountBDesired,
uint256 amountAMin,
uint256 amountBMin
) internal returns (uint256 amountA, uint256 amountB) {
if (amountADesired < amountAMin) revert InsufficientAmountADesired();
if (amountBDesired < amountBMin) revert InsufficientAmountBDesired();
// create the pool if it doesn't exist yet
address _pool = IPoolFactory(defaultFactory).getPool(tokenA, tokenB, stable);
if (_pool == address(0)) {
_pool = IPoolFactory(defaultFactory).createPool(tokenA, tokenB, stable);
}
(uint256 reserveA, uint256 reserveB) = getReserves(tokenA, tokenB, stable, defaultFactory);
if (reserveA == 0 && reserveB == 0) {
(amountA, amountB) = (amountADesired, amountBDesired);
} else {
uint256 amountBOptimal = quoteLiquidity(amountADesired, reserveA, reserveB);
if (amountBOptimal <= amountBDesired) {
if (amountBOptimal < amountBMin) revert InsufficientAmountB();
(amountA, amountB) = (amountADesired, amountBOptimal);
} else {
uint256 amountAOptimal = quoteLiquidity(amountBDesired, reserveB, reserveA);
assert(amountAOptimal <= amountADesired);
if (amountAOptimal < amountAMin) revert InsufficientAmountA();
(amountA, amountB) = (amountAOptimal, amountBDesired);
}
}
}
/// @inheritdoc IRouter
function addLiquidity(
address tokenA,
address tokenB,
bool stable,
uint256 amountADesired,
uint256 amountBDesired,
uint256 amountAMin,
uint256 amountBMin,
address to,
uint256 deadline
) public ensure(deadline) returns (uint256 amountA, uint256 amountB, uint256 liquidity) {
(amountA, amountB) = _addLiquidity(
tokenA,
tokenB,
stable,
amountADesired,
amountBDesired,
amountAMin,
amountBMin
);
address pool = poolFor(tokenA, tokenB, stable, defaultFactory);
_safeTransferFrom(tokenA, _msgSender(), pool, amountA);
_safeTransferFrom(tokenB, _msgSender(), pool, amountB);
liquidity = IPool(pool).mint(to);
}
/// @inheritdoc IRouter
function addLiquidityETH(
address token,
bool stable,
uint256 amountTokenDesired,
uint256 amountTokenMin,
uint256 amountETHMin,
address to,
uint256 deadline
) external payable ensure(deadline) returns (uint256 amountToken, uint256 amountETH, uint256 liquidity) {
(amountToken, amountETH) = _addLiquidity(
token,
address(weth),
stable,
amountTokenDesired,
msg.value,
amountTokenMin,
amountETHMin
);
address pool = poolFor(token, address(weth), stable, defaultFactory);
_safeTransferFrom(token, _msgSender(), pool, amountToken);
weth.deposit{value: amountETH}();
assert(weth.transfer(pool, amountETH));
liquidity = IPool(pool).mint(to);
// refund dust eth, if any
if (msg.value > amountETH) _safeTransferETH(_msgSender(), msg.value - amountETH);
}
// **** REMOVE LIQUIDITY ****
/// @inheritdoc IRouter
function removeLiquidity(
address tokenA,
address tokenB,
bool stable,
uint256 liquidity,
uint256 amountAMin,
uint256 amountBMin,
address to,
uint256 deadline
) public ensure(deadline) returns (uint256 amountA, uint256 amountB) {
address pool = poolFor(tokenA, tokenB, stable, defaultFactory);
IERC20(pool).safeTransferFrom(_msgSender(), pool, liquidity);
(uint256 amount0, uint256 amount1) = IPool(pool).burn(to);
(address token0, ) = sortTokens(tokenA, tokenB);
(amountA, amountB) = tokenA == token0 ? (amount0, amount1) : (amount1, amount0);
if (amountA < amountAMin) revert InsufficientAmountA();
if (amountB < amountBMin) revert InsufficientAmountB();
}
/// @inheritdoc IRouter
function removeLiquidityETH(
address token,
bool stable,
uint256 liquidity,
uint256 amountTokenMin,
uint256 amountETHMin,
address to,
uint256 deadline
) public ensure(deadline) returns (uint256 amountToken, uint256 amountETH) {
(amountToken, amountETH) = removeLiquidity(
token,
address(weth),
stable,
liquidity,
amountTokenMin,
amountETHMin,
address(this),
deadline
);
_safeTransfer(token, to, amountToken);
weth.withdraw(amountETH);
_safeTransferETH(to, amountETH);
}
// **** REMOVE LIQUIDITY (supporting fee-on-transfer tokens) ****
function removeLiquidityETHSupportingFeeOnTransferTokens(
address token,
bool stable,
uint256 liquidity,
uint256 amountTokenMin,
uint256 amountETHMin,
address to,
uint256 deadline
) public ensure(deadline) returns (uint256 amountETH) {
(, amountETH) = removeLiquidity(
token,
address(weth),
stable,
liquidity,
amountTokenMin,
amountETHMin,
address(this),
deadline
);
_safeTransfer(token, to, IERC20(token).balanceOf(address(this)));
weth.withdraw(amountETH);
_safeTransferETH(to, amountETH);
}
// **** SWAP ****
/// @dev requires the initial amount to have already been sent to the first pool
function _swap(uint256[] memory amounts, Route[] memory routes, address _to) internal virtual {
uint256 _length = routes.length;
for (uint256 i = 0; i < _length; i++) {
(address token0, ) = sortTokens(routes[i].from, routes[i].to);
uint256 amountOut = amounts[i + 1];
(uint256 amount0Out, uint256 amount1Out) = routes[i].from == token0
? (uint256(0), amountOut)
: (amountOut, uint256(0));
address to = i < routes.length - 1
? poolFor(routes[i + 1].from, routes[i + 1].to, routes[i + 1].stable, routes[i + 1].factory)
: _to;
IPool(poolFor(routes[i].from, routes[i].to, routes[i].stable, routes[i].factory)).swap(
amount0Out,
amount1Out,
to,
new bytes(0)
);
}
}
function swapExactTokensForTokens(
uint256 amountIn,
uint256 amountOutMin,
Route[] calldata routes,
address to,
uint256 deadline
) external ensure(deadline) returns (uint256[] memory amounts) {
amounts = getAmountsOut(amountIn, routes);
if (amounts[amounts.length - 1] < amountOutMin) revert InsufficientOutputAmount();
_safeTransferFrom(
routes[0].from,
_msgSender(),
poolFor(routes[0].from, routes[0].to, routes[0].stable, routes[0].factory),
amounts[0]
);
_swap(amounts, routes, to);
}
function swapExactETHForTokens(
uint256 amountOutMin,
Route[] calldata routes,
address to,
uint256 deadline
) external payable ensure(deadline) returns (uint256[] memory amounts) {
if (routes[0].from != address(weth)) revert InvalidPath();
amounts = getAmountsOut(msg.value, routes);
if (amounts[amounts.length - 1] < amountOutMin) revert InsufficientOutputAmount();
weth.deposit{value: amounts[0]}();
assert(weth.transfer(poolFor(routes[0].from, routes[0].to, routes[0].stable, routes[0].factory), amounts[0]));
_swap(amounts, routes, to);
}
function swapExactTokensForETH(
uint256 amountIn,
uint256 amountOutMin,
Route[] calldata routes,
address to,
uint256 deadline
) external ensure(deadline) returns (uint256[] memory amounts) {
if (routes[routes.length - 1].to != address(weth)) revert InvalidPath();
amounts = getAmountsOut(amountIn, routes);
if (amounts[amounts.length - 1] < amountOutMin) revert InsufficientOutputAmount();
_safeTransferFrom(
routes[0].from,
_msgSender(),
poolFor(routes[0].from, routes[0].to, routes[0].stable, routes[0].factory),
amounts[0]
);
_swap(amounts, routes, address(this));
weth.withdraw(amounts[amounts.length - 1]);
_safeTransferETH(to, amounts[amounts.length - 1]);
}
function UNSAFE_swapExactTokensForTokens(
uint256[] memory amounts,
Route[] calldata routes,
address to,
uint256 deadline
) external ensure(deadline) returns (uint256[] memory) {
_safeTransferFrom(
routes[0].from,
_msgSender(),
poolFor(routes[0].from, routes[0].to, routes[0].stable, routes[0].factory),
amounts[0]
);
_swap(amounts, routes, to);
return amounts;
}
// **** SWAP (supporting fee-on-transfer tokens) ****
/// @dev requires the initial amount to have already been sent to the first pool
function _swapSupportingFeeOnTransferTokens(Route[] memory routes, address _to) internal virtual {
uint256 _length = routes.length;
for (uint256 i; i < _length; i++) {
(address token0, ) = sortTokens(routes[i].from, routes[i].to);
address pool = poolFor(routes[i].from, routes[i].to, routes[i].stable, routes[i].factory);
uint256 amountInput;
uint256 amountOutput;
{
// stack too deep
(uint256 reserveA, ) = getReserves(routes[i].from, routes[i].to, routes[i].stable, routes[i].factory); // getReserves sorts it for us i.e. reserveA is always for from
amountInput = IERC20(routes[i].from).balanceOf(pool) - reserveA;
}
amountOutput = IPool(pool).getAmountOut(amountInput, routes[i].from);
(uint256 amount0Out, uint256 amount1Out) = routes[i].from == token0
? (uint256(0), amountOutput)
: (amountOutput, uint256(0));
address to = i < routes.length - 1
? poolFor(routes[i + 1].from, routes[i + 1].to, routes[i + 1].stable, routes[i + 1].factory)
: _to;
IPool(pool).swap(amount0Out, amount1Out, to, new bytes(0));
}
}
/// @inheritdoc IRouter
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint256 amountIn,
uint256 amountOutMin,
Route[] calldata routes,
address to,
uint256 deadline
) external ensure(deadline) {
_safeTransferFrom(
routes[0].from,
_msgSender(),
poolFor(routes[0].from, routes[0].to, routes[0].stable, routes[0].factory),
amountIn
);
uint256 _length = routes.length - 1;
uint256 balanceBefore = IERC20(routes[_length].to).balanceOf(to);
_swapSupportingFeeOnTransferTokens(routes, to);
if (IERC20(routes[_length].to).balanceOf(to) - balanceBefore < amountOutMin) revert InsufficientOutputAmount();
}
/// @inheritdoc IRouter
function swapExactETHForTokensSupportingFeeOnTransferTokens(
uint256 amountOutMin,
Route[] calldata routes,
address to,
uint256 deadline
) external payable ensure(deadline) {
if (routes[0].from != address(weth)) revert InvalidPath();
uint256 amountIn = msg.value;
weth.deposit{value: amountIn}();
assert(weth.transfer(poolFor(routes[0].from, routes[0].to, routes[0].stable, routes[0].factory), amountIn));
uint256 _length = routes.length - 1;
uint256 balanceBefore = IERC20(routes[_length].to).balanceOf(to);
_swapSupportingFeeOnTransferTokens(routes, to);
if (IERC20(routes[_length].to).balanceOf(to) - balanceBefore < amountOutMin) revert InsufficientOutputAmount();
}
/// @inheritdoc IRouter
function swapExactTokensForETHSupportingFeeOnTransferTokens(
uint256 amountIn,
uint256 amountOutMin,
Route[] calldata routes,
address to,
uint256 deadline
) external ensure(deadline) {
if (routes[routes.length - 1].to != address(weth)) revert InvalidPath();
_safeTransferFrom(
routes[0].from,
_msgSender(),
poolFor(routes[0].from, routes[0].to, routes[0].stable, routes[0].factory),
amountIn
);
_swapSupportingFeeOnTransferTokens(routes, address(this));
uint256 amountOut = weth.balanceOf(address(this));
if (amountOut < amountOutMin) revert InsufficientOutputAmount();
weth.withdraw(amountOut);
_safeTransferETH(to, amountOut);
}
/// @inheritdoc IRouter
function zapIn(
address tokenIn,
uint256 amountInA,
uint256 amountInB,
Zap calldata zapInPool,
Route[] calldata routesA,
Route[] calldata routesB,
address to,
bool stake
) external payable returns (uint256 liquidity) {
uint256 amountIn = amountInA + amountInB;
address _tokenIn = tokenIn;
uint256 value = msg.value;
if (tokenIn == ETHER) {
if (amountIn != value) revert InvalidAmountInForETHDeposit();
_tokenIn = address(weth);
weth.deposit{value: value}();
} else {
if (value != 0) revert InvalidTokenInForETHDeposit();
_safeTransferFrom(_tokenIn, _msgSender(), address(this), amountIn);
}
_zapSwap(_tokenIn, amountInA, amountInB, zapInPool, routesA, routesB);
_zapInLiquidity(zapInPool);
address pool = poolFor(zapInPool.tokenA, zapInPool.tokenB, zapInPool.stable, zapInPool.factory);
if (stake) {
liquidity = IPool(pool).mint(address(this));
address gauge = IVoter(voter).gauges(pool);
IERC20(pool).safeApprove(address(gauge), liquidity);
IGauge(gauge).deposit(liquidity, to);
IERC20(pool).safeApprove(address(gauge), 0);
} else {
liquidity = IPool(pool).mint(to);
}
_returnAssets(tokenIn);
_returnAssets(zapInPool.tokenA);
_returnAssets(zapInPool.tokenB);
}
/// @dev Handles swap leg of zap in (i.e. convert tokenIn into tokenA and tokenB).
function _zapSwap(
address tokenIn,
uint256 amountInA,
uint256 amountInB,
Zap calldata zapInPool,
Route[] calldata routesA,
Route[] calldata routesB
) internal {
address tokenA = zapInPool.tokenA;
address tokenB = zapInPool.tokenB;
bool stable = zapInPool.stable;
address factory = zapInPool.factory;
address pool = poolFor(tokenA, tokenB, stable, factory);
{
(uint256 reserve0, uint256 reserve1, ) = IPool(pool).getReserves();
if (reserve0 <= MINIMUM_LIQUIDITY || reserve1 <= MINIMUM_LIQUIDITY) revert PoolDoesNotExist();
}
if (tokenIn != tokenA) {
if (routesA[routesA.length - 1].to != tokenA) revert InvalidRouteA();
_internalSwap(tokenIn, amountInA, zapInPool.amountOutMinA, routesA);
}
if (tokenIn != tokenB) {
if (routesB[routesB.length - 1].to != tokenB) revert InvalidRouteB();
_internalSwap(tokenIn, amountInB, zapInPool.amountOutMinB, routesB);
}
}
/// @dev Handles liquidity adding component of zap in.
function _zapInLiquidity(Zap calldata zapInPool) internal {
address tokenA = zapInPool.tokenA;
address tokenB = zapInPool.tokenB;
bool stable = zapInPool.stable;
address factory = zapInPool.factory;
address pool = poolFor(tokenA, tokenB, stable, factory);
(uint256 amountA, uint256 amountB) = _quoteZapLiquidity(
tokenA,
tokenB,
stable,
factory,
IERC20(tokenA).balanceOf(address(this)),
IERC20(tokenB).balanceOf(address(this)),
zapInPool.amountAMin,
zapInPool.amountBMin
);
_safeTransfer(tokenA, pool, amountA);
_safeTransfer(tokenB, pool, amountB);
}
/// @dev Similar to _addLiquidity. Assumes a pool exists, and accepts a factory argument.
function _quoteZapLiquidity(
address tokenA,
address tokenB,
bool stable,
address _factory,
uint256 amountADesired,
uint256 amountBDesired,
uint256 amountAMin,
uint256 amountBMin
) internal view returns (uint256 amountA, uint256 amountB) {
if (amountADesired < amountAMin) revert InsufficientAmountADesired();
if (amountBDesired < amountBMin) revert InsufficientAmountBDesired();
(uint256 reserveA, uint256 reserveB) = getReserves(tokenA, tokenB, stable, _factory);
if (reserveA == 0 && reserveB == 0) {
(amountA, amountB) = (amountADesired, amountBDesired);
} else {
uint256 amountBOptimal = quoteLiquidity(amountADesired, reserveA, reserveB);
if (amountBOptimal <= amountBDesired) {
if (amountBOptimal < amountBMin) revert InsufficientAmountB();
(amountA, amountB) = (amountADesired, amountBOptimal);
} else {
uint256 amountAOptimal = quoteLiquidity(amountBDesired, reserveB, reserveA);
assert(amountAOptimal <= amountADesired);
if (amountAOptimal < amountAMin) revert InsufficientAmountA();
(amountA, amountB) = (amountAOptimal, amountBDesired);
}
}
}
/// @dev Handles swaps internally for zaps.
function _internalSwap(address tokenIn, uint256 amountIn, uint256 amountOutMin, Route[] memory routes) internal {
uint256[] memory amounts = getAmountsOut(amountIn, routes);
if (amounts[amounts.length - 1] < amountOutMin) revert InsufficientOutputAmount();
address pool = poolFor(routes[0].from, routes[0].to, routes[0].stable, routes[0].factory);
_safeTransfer(tokenIn, pool, amountIn);
_swap(amounts, routes, address(this));
}
/// @inheritdoc IRouter
function zapOut(
address tokenOut,
uint256 liquidity,
Zap calldata zapOutPool,
Route[] calldata routesA,
Route[] calldata routesB
) external {
address tokenA = zapOutPool.tokenA;
address tokenB = zapOutPool.tokenB;
address _tokenOut = (tokenOut == ETHER) ? address(weth) : tokenOut;
_zapOutLiquidity(liquidity, zapOutPool);
uint256 balance;
if (tokenA != _tokenOut) {
balance = IERC20(tokenA).balanceOf(address(this));
if (routesA[routesA.length - 1].to != _tokenOut) revert InvalidRouteA();
_internalSwap(tokenA, balance, zapOutPool.amountOutMinA, routesA);
}
if (tokenB != _tokenOut) {
balance = IERC20(tokenB).balanceOf(address(this));
if (routesB[routesB.length - 1].to != _tokenOut) revert InvalidRouteB();
_internalSwap(tokenB, balance, zapOutPool.amountOutMinB, routesB);
}
_returnAssets(tokenOut);
}
/// @dev Handles liquidity removing component of zap out.
function _zapOutLiquidity(uint256 liquidity, Zap calldata zapOutPool) internal {
address tokenA = zapOutPool.tokenA;
address tokenB = zapOutPool.tokenB;
address pool = poolFor(tokenA, tokenB, zapOutPool.stable, zapOutPool.factory);
IERC20(pool).safeTransferFrom(msg.sender, pool, liquidity);
(address token0, ) = sortTokens(tokenA, tokenB);
(uint256 amount0, uint256 amount1) = IPool(pool).burn(address(this));
(uint256 amountA, uint256 amountB) = tokenA == token0 ? (amount0, amount1) : (amount1, amount0);
if (amountA < zapOutPool.amountAMin) revert InsufficientAmountA();
if (amountB < zapOutPool.amountBMin) revert InsufficientAmountB();
}
/// @inheritdoc IRouter
function generateZapInParams(
address tokenA,
address tokenB,
bool stable,
address _factory,
uint256 amountInA,
uint256 amountInB,
Route[] calldata routesA,
Route[] calldata routesB
) external view returns (uint256 amountOutMinA, uint256 amountOutMinB, uint256 amountAMin, uint256 amountBMin) {
amountOutMinA = amountInA;
amountOutMinB = amountInB;
uint256[] memory amounts;
if (routesA.length > 0) {
amounts = getAmountsOut(amountInA, routesA);
amountOutMinA = amounts[amounts.length - 1];
}
if (routesB.length > 0) {
amounts = getAmountsOut(amountInB, routesB);
amountOutMinB = amounts[amounts.length - 1];
}
(amountAMin, amountBMin, ) = quoteAddLiquidity(tokenA, tokenB, stable, _factory, amountOutMinA, amountOutMinB);
}
/// @inheritdoc IRouter
function generateZapOutParams(
address tokenA,
address tokenB,
bool stable,
address _factory,
uint256 liquidity,
Route[] calldata routesA,
Route[] calldata routesB
) external view returns (uint256 amountOutMinA, uint256 amountOutMinB, uint256 amountAMin, uint256 amountBMin) {
(amountAMin, amountBMin) = quoteRemoveLiquidity(tokenA, tokenB, stable, _factory, liquidity);
amountOutMinA = amountAMin;
amountOutMinB = amountBMin;
uint256[] memory amounts;
if (routesA.length > 0) {
amounts = getAmountsOut(amountAMin, routesA);
amountOutMinA = amounts[amounts.length - 1];
}
if (routesB.length > 0) {
amounts = getAmountsOut(amountBMin, routesB);
amountOutMinB = amounts[amounts.length - 1];
}
}
/// @dev Return residual assets from zapping.
/// @param token token to return, put `ETHER` if you want Ether back.
function _returnAssets(address token) internal {
address sender = _msgSender();
uint256 balance;
if (token == ETHER) {
balance = IERC20(weth).balanceOf(address(this));
if (balance > 0) {
IWETH(weth).withdraw(balance);
_safeTransferETH(sender, balance);
}
} else {
balance = IERC20(token).balanceOf(address(this));
if (balance > 0) {
IERC20(token).safeTransfer(sender, balance);
}
}
}
/// @inheritdoc IRouter
function quoteStableLiquidityRatio(
address tokenA,
address tokenB,
address _factory
) external view returns (uint256 ratio) {
IPool pool = IPool(poolFor(tokenA, tokenB, true, _factory));
uint256 decimalsA = 10 ** IERC20Metadata(tokenA).decimals();
uint256 decimalsB = 10 ** IERC20Metadata(tokenB).decimals();
uint256 investment = decimalsA;
uint256 out = pool.getAmountOut(investment, tokenA);
(uint256 amountA, uint256 amountB, ) = quoteAddLiquidity(tokenA, tokenB, true, _factory, investment, out);
amountA = (amountA * 1e18) / decimalsA;
amountB = (amountB * 1e18) / decimalsB;
out = (out * 1e18) / decimalsB;
investment = (investment * 1e18) / decimalsA;
ratio = (((out * 1e18) / investment) * amountA) / amountB;
return (investment * 1e18) / (ratio + 1e18);
}
function _safeTransferETH(address to, uint256 value) internal {
(bool success, ) = to.call{value: value}(new bytes(0));
if (!success) revert ETHTransferFailed();
}
function _safeTransfer(address token, address to, uint256 value) internal {
require(token.code.length > 0);
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(IERC20.transfer.selector, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))));
}
function _safeTransferFrom(address token, address from, address to, uint256 value) internal {
require(token.code.length > 0);
(bool success, bytes memory data) = token.call(
abi.encodeWithSelector(IERC20.transferFrom.selector, from, to, value)
);
require(success && (data.length == 0 || abi.decode(data, (bool))));
}
}
{
"compilationTarget": {
"Router.sol": "Router"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_forwarder","type":"address"},{"internalType":"address","name":"_factoryRegistry","type":"address"},{"internalType":"address","name":"_factory","type":"address"},{"internalType":"address","name":"_voter","type":"address"},{"internalType":"address","name":"_weth","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"ETHTransferFailed","type":"error"},{"inputs":[],"name":"Expired","type":"error"},{"inputs":[],"name":"InsufficientAmount","type":"error"},{"inputs":[],"name":"InsufficientAmountA","type":"error"},{"inputs":[],"name":"InsufficientAmountADesired","type":"error"},{"inputs":[],"name":"InsufficientAmountAOptimal","type":"error"},{"inputs":[],"name":"InsufficientAmountB","type":"error"},{"inputs":[],"name":"InsufficientAmountBDesired","type":"error"},{"inputs":[],"name":"InsufficientLiquidity","type":"error"},{"inputs":[],"name":"InsufficientOutputAmount","type":"error"},{"inputs":[],"name":"InvalidAmountInForETHDeposit","type":"error"},{"inputs":[],"name":"InvalidPath","type":"error"},{"inputs":[],"name":"InvalidRouteA","type":"error"},{"inputs":[],"name":"InvalidRouteB","type":"error"},{"inputs":[],"name":"InvalidTokenInForETHDeposit","type":"error"},{"inputs":[],"name":"OnlyWETH","type":"error"},{"inputs":[],"name":"PoolDoesNotExist","type":"error"},{"inputs":[],"name":"PoolFactoryDoesNotExist","type":"error"},{"inputs":[],"name":"SameAddresses","type":"error"},{"inputs":[],"name":"ZeroAddress","type":"error"},{"inputs":[],"name":"ETHER","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"}],"internalType":"struct IRouter.Route[]","name":"routes","type":"tuple[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"UNSAFE_swapExactTokensForTokens","outputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"uint256","name":"amountADesired","type":"uint256"},{"internalType":"uint256","name":"amountBDesired","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"addLiquidity","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"},{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"uint256","name":"amountTokenDesired","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"addLiquidityETH","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"},{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"defaultFactory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"factoryRegistry","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"_factory","type":"address"},{"internalType":"uint256","name":"amountInA","type":"uint256"},{"internalType":"uint256","name":"amountInB","type":"uint256"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"}],"internalType":"struct IRouter.Route[]","name":"routesA","type":"tuple[]"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"}],"internalType":"struct IRouter.Route[]","name":"routesB","type":"tuple[]"}],"name":"generateZapInParams","outputs":[{"internalType":"uint256","name":"amountOutMinA","type":"uint256"},{"internalType":"uint256","name":"amountOutMinB","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"_factory","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"}],"internalType":"struct IRouter.Route[]","name":"routesA","type":"tuple[]"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"}],"internalType":"struct IRouter.Route[]","name":"routesB","type":"tuple[]"}],"name":"generateZapOutParams","outputs":[{"internalType":"uint256","name":"amountOutMinA","type":"uint256"},{"internalType":"uint256","name":"amountOutMinB","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"}],"internalType":"struct IRouter.Route[]","name":"routes","type":"tuple[]"}],"name":"getAmountsOut","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"_factory","type":"address"}],"name":"getReserves","outputs":[{"internalType":"uint256","name":"reserveA","type":"uint256"},{"internalType":"uint256","name":"reserveB","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"forwarder","type":"address"}],"name":"isTrustedForwarder","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"_factory","type":"address"}],"name":"poolFor","outputs":[{"internalType":"address","name":"pool","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"_factory","type":"address"},{"internalType":"uint256","name":"amountADesired","type":"uint256"},{"internalType":"uint256","name":"amountBDesired","type":"uint256"}],"name":"quoteAddLiquidity","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"},{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"_factory","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"}],"name":"quoteRemoveLiquidity","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"address","name":"_factory","type":"address"}],"name":"quoteStableLiquidityRatio","outputs":[{"internalType":"uint256","name":"ratio","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidity","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidityETH","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidityETHSupportingFeeOnTransferTokens","outputs":[{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"}],"name":"sortTokens","outputs":[{"internalType":"address","name":"token0","type":"address"},{"internalType":"address","name":"token1","type":"address"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"}],"internalType":"struct IRouter.Route[]","name":"routes","type":"tuple[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactETHForTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"}],"internalType":"struct IRouter.Route[]","name":"routes","type":"tuple[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactETHForTokensSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"}],"internalType":"struct IRouter.Route[]","name":"routes","type":"tuple[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForETH","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"}],"internalType":"struct IRouter.Route[]","name":"routes","type":"tuple[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForETHSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"}],"internalType":"struct IRouter.Route[]","name":"routes","type":"tuple[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"}],"internalType":"struct IRouter.Route[]","name":"routes","type":"tuple[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForTokensSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"voter","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"weth","outputs":[{"internalType":"contract IWETH","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"uint256","name":"amountInA","type":"uint256"},{"internalType":"uint256","name":"amountInB","type":"uint256"},{"components":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"},{"internalType":"uint256","name":"amountOutMinA","type":"uint256"},{"internalType":"uint256","name":"amountOutMinB","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"}],"internalType":"struct IRouter.Zap","name":"zapInPool","type":"tuple"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"}],"internalType":"struct IRouter.Route[]","name":"routesA","type":"tuple[]"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"}],"internalType":"struct IRouter.Route[]","name":"routesB","type":"tuple[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stake","type":"bool"}],"name":"zapIn","outputs":[{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenOut","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"components":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"},{"internalType":"uint256","name":"amountOutMinA","type":"uint256"},{"internalType":"uint256","name":"amountOutMinB","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"}],"internalType":"struct IRouter.Zap","name":"zapOutPool","type":"tuple"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"}],"internalType":"struct IRouter.Route[]","name":"routesA","type":"tuple[]"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"},{"internalType":"address","name":"factory","type":"address"}],"internalType":"struct IRouter.Route[]","name":"routesB","type":"tuple[]"}],"name":"zapOut","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]