Verified contractExactMIT
StrategyPassiveManagerAero
Address0xebc7f47cfe545c80fcb62cb67e726a40519a09b8
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.24
- Same code
Only this contract
Details
- Compiler
- solc 0.8.24+commit.e11b9ed9
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- cancun
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,455,347
- Creation transaction
- 0x601389992f…b4036c2d71
33 files
- contracts/strategies/aero/StrategyPassiveManagerAero.solmain39.03 KB
- contracts/interfaces/aero/IAeroGauge.sol1.07 KB
- contracts/interfaces/aero/IAeroPool.sol2.06 KB
- contracts/interfaces/aero/IAeroVoter.sol758 B
- contracts/interfaces/beefy/IBeefySwapper.sol866 B
- contracts/interfaces/beefy/IBeefyVaultConcLiq.sol765 B
- contracts/interfaces/beefy/IFeeConfig.sol574 B
- contracts/interfaces/beefy/IRewardPool.sol384 B
- contracts/interfaces/beefy/IStrategyConcLiq.sol867 B
- contracts/interfaces/beefy/IStrategyFactory.sol1.44 KB
- contracts/interfaces/beefy/IStrategyVelodrome.sol402 B
- contracts/interfaces/velodrome/INftPositionManager.sol7.37 KB
- contracts/strategies/StratFeeManagerInitializable.sol6.75 KB
- contracts/strategies/harvest-manager/public/StratHarvestManager.sol1.42 KB
- contracts/utils/BytesLib.sol19.47 KB
- contracts/utils/FullMath.sol5.57 KB
- contracts/utils/LiquidityAmounts.sol5.85 KB
- contracts/utils/Path.sol2.71 KB
- contracts/utils/TickMath.sol4.29 KB
- contracts/utils/TickUtils.sol3.68 KB
- dependencies/@openzeppelin-contracts-5.0.2/token/ERC20/IERC20.sol2.78 KB
- dependencies/@openzeppelin-contracts-5.0.2/token/ERC20/extensions/IERC20Metadata.sol683 B
- dependencies/@openzeppelin-contracts-5.0.2/token/ERC20/extensions/IERC20Permit.sol3.8 KB
- dependencies/@openzeppelin-contracts-5.0.2/token/ERC20/utils/SafeERC20.sol5.77 KB
- dependencies/@openzeppelin-contracts-5.0.2/token/ERC721/IERC721.sol4.93 KB
- dependencies/@openzeppelin-contracts-5.0.2/utils/Address.sol6.26 KB
- dependencies/@openzeppelin-contracts-5.0.2/utils/introspection/IERC165.sol869 B
- dependencies/@openzeppelin-contracts-5.0.2/utils/math/SignedMath.sol1.26 KB
- dependencies/@openzeppelin-contracts-upgradeable-4.9.6/access/OwnableUpgradeable.sol3.17 KB
- dependencies/@openzeppelin-contracts-upgradeable-4.9.6/proxy/utils/Initializable.sol6.64 KB
- dependencies/@openzeppelin-contracts-upgradeable-4.9.6/security/PausableUpgradeable.sol3.11 KB
- dependencies/@openzeppelin-contracts-upgradeable-4.9.6/utils/AddressUpgradeable.sol9.38 KB
- dependencies/@openzeppelin-contracts-upgradeable-4.9.6/utils/ContextUpgradeable.sol1.48 KB
contracts/strategies/aero/StrategyPassiveManagerAero.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
import {IERC20Metadata} from "@openzeppelin-5/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import {IERC721} from "@openzeppelin-5/contracts/token/ERC721/IERC721.sol";
import {SafeERC20} from "@openzeppelin-5/contracts/token/ERC20/utils/SafeERC20.sol";
import {SignedMath} from "@openzeppelin-5/contracts/utils/math/SignedMath.sol";
import {StratHarvestManager} from "harvest-manager/StratHarvestManager.sol";
import {IFeeConfig} from "../../interfaces/beefy/IFeeConfig.sol";
import {IAeroPool} from "../../interfaces/aero/IAeroPool.sol";
import {LiquidityAmounts} from "../../utils/LiquidityAmounts.sol";
import {TickMath} from "../../utils/TickMath.sol";
import {TickUtils, FullMath} from "../../utils/TickUtils.sol";
import {IBeefyVaultConcLiq} from "../../interfaces/beefy/IBeefyVaultConcLiq.sol";
import {IStrategyConcLiq} from "../../interfaces/beefy/IStrategyConcLiq.sol";
import {IStrategyVelodrome} from "../../interfaces/beefy/IStrategyVelodrome.sol";
import {IStrategyFactory} from "../../interfaces/beefy/IStrategyFactory.sol";
import {INftPositionManager} from "../../interfaces/velodrome/INftPositionManager.sol";
import {IAeroGauge, IAeroGaugeFactory} from "../../interfaces/aero/IAeroGauge.sol";
import {
IAeroLeafVoter,
IAeroLeafMessageOrchestrator,
IAeroRootLocalAdapter,
IAeroRootMessageOrchestrator,
IAeroRootVoter
} from "../../interfaces/aero/IAeroVoter.sol";
import {IRewardPool} from "../../interfaces/beefy/IRewardPool.sol";
import {IBeefySwapper} from "../../interfaces/beefy/IBeefySwapper.sol";
/// @title Beefy Passive Position Manager. Version: Aero
/// @author weso and frondoto, Beefy
/// @notice This is a contract for managing a passive concentrated liquidity position on Aero, with optional gauge staking.
contract StrategyPassiveManagerAero is StratHarvestManager, IStrategyConcLiq, IStrategyVelodrome {
using SafeERC20 for IERC20Metadata;
using TickMath for int24;
/// @notice The precision for pricing.
uint256 private constant PRECISION = 1e36;
uint256 private constant SQRT_PRECISION = 1e18;
/// @notice The max and min ticks univ3 allows.
int56 private constant MIN_TICK = -887272;
int56 private constant MAX_TICK = 887272;
/// @notice The address of the Aero pool.
address public pool;
/// @notice The address of the NFT position manager.
address public nftManager;
/// @notice The address of the gauge.
address public gauge;
/// @notice The address of the first token in the liquidity pool.
address public lpToken0;
/// @notice The address of the second token in the liquidity pool.
address public lpToken1;
/// @notice The address of the rewardPool.
address public rewardPool;
/// @notice Collected trading fees reserved until harvest.
uint256 public fees0;
uint256 public fees1;
/// @notice The configured emissions token and its unharvested balance.
address public rewardToken;
uint256 public rewardBalance;
/// @notice The struct to store our tick positioning.
struct Position {
uint256 nftId;
int24 tickLower;
int24 tickUpper;
}
/// @notice The main position of the strategy.
/// @dev this will always be a 50/50 position that will be equal to position width * tickSpacing on each side.
Position public positionMain;
/// @notice The alternative position of the strategy.
/// @dev this will always be a single sided (limit order) position that will start closest to current tick and continue to width * tickSpacing.
/// This will always be in the token that has the most value after we fill our main position.
Position public positionAlt;
/// @notice The width of the position, thats a multiplier for tick spacing to find our range.
int24 public positionWidth;
/// @notice the max tick deviations we will allow for deposits/setTick.
int56 public maxTickDeviation;
/// @notice The twap interval seconds we use for the twap check.
uint32 public twapInterval;
/// @notice Initializes the ticks on first deposit.
bool private initTicks;
/// @notice The timestamp of the last deposit
uint256 private lastDeposit;
/// @notice The block number of the most recent gauge deposit.
uint256 public lastGaugeDepositBlock;
// Errors
error NotAuthorized();
error NotPool();
error InvalidEntry();
error NotVault();
error InvalidInput();
error InvalidOutput();
error NotCalm();
error TooMuchSlippage();
error InvalidTicks();
error ActionLocked(uint256 unlockBlock);
error DepositTooRecent();
// Events
event TVL(uint256 bal0, uint256 bal1);
event Harvest(uint256 fee0, uint256 fee1);
event ClaimedRewards(uint256 fees);
event HarvestRewards(uint256 fees);
event SetPositionWidth(int24 oldWidth, int24 width);
event SetDeviation(int56 maxTickDeviation);
event SetTwapInterval(uint32 oldInterval, uint32 interval);
event SetGauge(address gauge);
event SetReward(address reward);
event SetRewardPool(address rewardPool);
event ChargedFees(uint256 callFeeAmount, uint256 beefyFeeAmount, uint256 strategistFeeAmount);
event ClaimedFees(uint256 feeMain0, uint256 feeMain1, uint256 feeAlt0, uint256 feeAlt1);
/// @notice Modifier to only allow deposit/setTick actions when current price is within a certain deviation of twap.
modifier onlyCalmPeriods() {
_onlyCalmPeriods();
_;
}
/// @notice function to only allow deposit/setTick actions when current price is within a certain deviation of twap.
function _onlyCalmPeriods() private view {
if (!isCalm()) revert NotCalm();
}
modifier onlyRebalancers() {
if (!IStrategyFactory(factory).rebalancers(msg.sender)) revert NotAuthorized();
_;
}
/// @notice function to only allow deposit/harvest actions when current price is within a certain deviation of twap.
function isCalm() public view returns (bool) {
int24 tick = currentTick();
int56 twapTick = twap();
int56 shortTwapTick = shortTwap();
int56 minCalmTick = int56(SignedMath.max(twapTick - maxTickDeviation, MIN_TICK));
int56 maxCalmTick = int56(SignedMath.min(twapTick + maxTickDeviation, MAX_TICK));
// Calculate if greater than deviation % from twap and revert if it is.
if (minCalmTick > tick || maxCalmTick < tick) return false;
else if (minCalmTick > shortTwapTick || maxCalmTick < shortTwapTick) return false;
else return true;
}
/// @notice First block in which gauge actions avoid the early-claim penalty; zero when unstaked.
function actionableBlock() public view returns (uint256) {
if (gauge == address(0) || lastGaugeDepositBlock == 0) return 0;
return lastGaugeDepositBlock + minStakeBlocks();
}
/// @notice Whether the gauge penalty window has elapsed.
function actionable() external view returns (bool) {
return _blockNumber() >= actionableBlock();
}
/// @dev override to match gauge lock method
function _blockNumber() internal view virtual returns (uint256) {
return block.number;
}
/// @notice Minimum stake duration in blocks, read from the gauge's current effective configuration.
function minStakeBlocks() public view returns (uint256) {
if (gauge == address(0)) return 0;
IAeroGaugeFactory.PenaltyConfig memory config = _gaugeFactory().effectivePenaltyConfig(gauge);
return config.penaltyRate == 0 ? 0 : config.minStakeBlocks;
}
function _gaugeFactory() private view returns (IAeroGaugeFactory) {
return IAeroGaugeFactory(IAeroGauge(gauge).gaugeFactory());
}
function _enforceActionDelay() private view {
uint256 unlockBlock = actionableBlock();
if (unlockBlock != 0 && _blockNumber() < unlockBlock) revert ActionLocked(unlockBlock);
}
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
/**
* @notice Initializes the strategy and the inherited strat fee manager.
* @dev Make sure cardinality is set appropriately for the twap.
* @param _pool The underlying Aero pool.
* @param _nftManager The NFT position manager.
* @param _positionWidth The multiplier for tick spacing to find our range.
* @param _commonAddresses The common addresses needed for the strat fee manager.
*/
function initialize(
address _pool,
address _nftManager,
address _rewardPool,
int24 _positionWidth,
CommonAddresses calldata _commonAddresses
) external initializer {
__StratFeeManager_init(_commonAddresses);
pool = _pool;
nftManager = _nftManager;
rewardPool = _rewardPool;
lpToken0 = IAeroPool(_pool).token0();
lpToken1 = IAeroPool(_pool).token1();
// Our width multiplier. The tick distance of each side will be width * tickSpacing.
if (_positionWidth <= 1) revert InvalidInput();
positionWidth = _positionWidth;
// Set the twap interval to 120 seconds.
twapInterval = 120;
_giveAllowances();
}
/// @notice Only allows the vault to call a function.
function _onlyVault() private view {
if (msg.sender != vault) revert NotVault();
}
/// @notice Called during deposit and withdraw to remove liquidity and harvest fees for accounting purposes.
function beforeAction() external {
_onlyVault();
_enforceActionDelay();
_removeLiquidity(false);
}
/// @notice Called during deposit to add all liquidity back to their positions.
function deposit() external onlyCalmPeriods {
_onlyVault();
if (!initTicks) {
_setTicks();
initTicks = true;
} else {
int24 tick = currentTick();
if (tick < positionMain.tickLower || tick >= positionMain.tickUpper) revert NotCalm();
}
// Add all liquidity
_addLiquidity();
(uint256 bal0, uint256 bal1) = balances();
lastDeposit = block.timestamp;
// TVL Balances after deposit
emit TVL(bal0, bal1);
}
/**
* @notice Withdraws the specified amount of tokens from the strategy as calculated by the vault.
* @param _amount0 The amount of token0 to withdraw.
* @param _amount1 The amount of token1 to withdraw.
*/
function withdraw(uint256 _amount0, uint256 _amount1) external {
_onlyVault();
if (block.timestamp == lastDeposit) revert DepositTooRecent();
// Liquidity has already been removed in beforeAction() so this is just a simple withdraw.
if (_amount0 > 0) IERC20Metadata(lpToken0).safeTransfer(vault, _amount0);
if (_amount1 > 0) IERC20Metadata(lpToken1).safeTransfer(vault, _amount1);
// After we take what is needed we add it all back to our positions.
if (!_isPaused()) _addLiquidity();
(uint256 bal0, uint256 bal1) = balances();
// TVL Balances after withdraw
emit TVL(bal0, bal1);
}
/// @notice Adds liquidity to the main and alternative positions called on deposit, harvest and withdraw.
function _addLiquidity() private onlyCalmPeriods {
_whenStrategyNotPaused();
(uint256 bal0, uint256 bal1) = balancesOfThis();
int24 mainLower = positionMain.tickLower;
int24 mainUpper = positionMain.tickUpper;
int24 altLower = positionAlt.tickLower;
int24 altUpper = positionAlt.tickUpper;
uint160 sqrtprice = sqrtPrice();
uint128 liquidity;
uint256 amount0;
uint256 amount1;
// Then we fetch how much liquidity we get for adding at the main position ticks with our token balances.
{
uint160 mainLowerSqrt = TickMath.getSqrtRatioAtTick(mainLower);
uint160 mainUpperSqrt = TickMath.getSqrtRatioAtTick(mainUpper);
liquidity = LiquidityAmounts.getLiquidityForAmounts(sqrtprice, mainLowerSqrt, mainUpperSqrt, bal0, bal1);
(amount0, amount1) =
LiquidityAmounts.getAmountsForLiquidity(sqrtprice, mainLowerSqrt, mainUpperSqrt, liquidity);
}
// Mint or add liquidity to the position.
if (liquidity > 0 && !(amount0 == 0 || amount1 == 0)) {
_mintPosition(mainLower, mainUpper, amount0, amount1, true);
(bal0, bal1) = balancesOfThis();
} else {
_onlyCalmPeriods();
}
// Fetch how much liquidity we get for adding at the alternative position ticks with our token balances.
{
uint160 altLowerSqrt = TickMath.getSqrtRatioAtTick(altLower);
uint160 altUpperSqrt = TickMath.getSqrtRatioAtTick(altUpper);
liquidity = LiquidityAmounts.getLiquidityForAmounts(sqrtprice, altLowerSqrt, altUpperSqrt, bal0, bal1);
(amount0, amount1) =
LiquidityAmounts.getAmountsForLiquidity(sqrtprice, altLowerSqrt, altUpperSqrt, liquidity);
liquidity = LiquidityAmounts.getLiquidityForAmounts(sqrtprice, altLowerSqrt, altUpperSqrt, amount0, amount1);
}
// Mint or add liquidity to the position.
if (liquidity > 0 && (amount0 > 0 || amount1 > 0)) {
_mintPosition(altLower, altUpper, amount0, amount1, false);
}
if (gauge != address(0)) {
if (positionMain.nftId != 0) IAeroGauge(gauge).deposit(positionMain.nftId);
if (positionAlt.nftId != 0) IAeroGauge(gauge).deposit(positionAlt.nftId);
if (positionMain.nftId != 0 || positionAlt.nftId != 0) lastGaugeDepositBlock = _blockNumber();
}
}
/// @notice Mints a new position for the main or alternative position.
function _mintPosition(int24 _tickLower, int24 _tickUpper, uint256 _amount0, uint256 _amount1, bool _mainPosition)
private
{
INftPositionManager.MintParams memory mintParams = INftPositionManager.MintParams({
token0: lpToken0,
token1: lpToken1,
tickSpacing: _tickDistance(),
tickLower: _tickLower,
tickUpper: _tickUpper,
amount0Desired: _amount0,
amount1Desired: _amount1,
amount0Min: 0,
amount1Min: 0,
recipient: address(this),
deadline: block.timestamp,
sqrtPriceX96: 0
});
(uint256 nftId,,,) = INftPositionManager(nftManager).mint(mintParams);
if (_mainPosition) positionMain.nftId = nftId;
else positionAlt.nftId = nftId;
if (gauge != address(0)) IERC721(nftManager).approve(gauge, nftId);
}
/// @notice Removes liquidity from the main and alternative positions, called on deposit, withdraw and harvest.
/// @param _emergency Unstake via the gauge's emergencyWithdraw when its factory has it enabled. That path skips
/// the emissions claim, forfeiting emissions accrued since the last action; deferred emissions stay claimable.
function _removeLiquidity(bool _emergency) private {
if (gauge != address(0) && lastGaugeDepositBlock != 0) {
uint256 beforeBalance = _balanceOfReward();
function(uint256) external unstake = _emergency && _gaugeFactory().emergencyWithdrawEnabled()
? IAeroGauge(gauge).emergencyWithdraw
: IAeroGauge(gauge).withdraw;
if (positionMain.nftId != 0) unstake(positionMain.nftId);
if (positionAlt.nftId != 0) unstake(positionAlt.nftId);
_bookRewards(beforeBalance);
lastGaugeDepositBlock = 0;
}
_removePosition(positionMain.nftId);
_removePosition(positionAlt.nftId);
positionMain.nftId = 0;
positionAlt.nftId = 0;
}
/// @dev Collect trading fees before decreasing liquidity so principal is never booked as earnings.
function _removePosition(uint256 _nftId) private {
if (_nftId == 0) return;
_collectFees(_nftId);
(,,,,,,, uint128 liquidity,,,,) = INftPositionManager(nftManager).positions(_nftId);
if (liquidity > 0) {
INftPositionManager(nftManager)
.decreaseLiquidity(
INftPositionManager.DecreaseLiquidityParams({
tokenId: _nftId, liquidity: liquidity, amount0Min: 0, amount1Min: 0, deadline: block.timestamp
})
);
_collect(_nftId);
}
INftPositionManager(nftManager).burn(_nftId);
}
/// @notice Function called to rebalance the position
function moveTicks() external onlyCalmPeriods onlyRebalancers {
_enforceActionDelay();
_removeLiquidity(false);
_setTicks();
_addLiquidity();
(uint256 bal0, uint256 bal1) = balances();
emit TVL(bal0, bal1);
}
/// @notice Compound trading fees and distribute net emissions through the reward pool.
function _harvest(address _callFeeRecipient) internal override onlyCalmPeriods {
_whenStrategyNotPaused();
_enforceActionDelay();
_claimEarnings();
bool compound = fees0 > 0 || fees1 > 0;
if (compound) _removeLiquidity(false);
uint256 nativeRewards = _swapRewards();
(uint256 fee0, uint256 fee1) = _chargeFees(_callFeeRecipient, fees0, fees1, nativeRewards);
fees0 = 0;
fees1 = 0;
if (compound) _addLiquidity();
(uint256 currentLock0, uint256 currentLock1) = lockedProfit();
totalLocked0 = fee0 + currentLock0;
totalLocked1 = fee1 + currentLock1;
lastHarvest = block.timestamp;
if (gauge == address(0) || fee0 > 0 || fee1 > 0) {
emit Harvest(fee0, fee1);
}
}
/// @notice Collect trading fees or claim emissions without changing the gauge deposit block.
function claimEarnings()
external
returns (uint256 fee0, uint256 fee1, uint256 feeAlt0, uint256 feeAlt1, uint256 rewards)
{
_enforceActionDelay();
return _claimEarnings();
}
function _claimEarnings()
private
returns (uint256 fee0, uint256 fee1, uint256 feeAlt0, uint256 feeAlt1, uint256 rewards)
{
if (gauge != address(0)) {
if (lastGaugeDepositBlock != 0 || IAeroGauge(gauge).deferredEmissions(address(this)) > 0) {
rewards = _claimGaugeRewards();
}
} else {
(fee0, fee1) = _collectFees(positionMain.nftId);
(feeAlt0, feeAlt1) = _collectFees(positionAlt.nftId);
}
}
function _collectFees(uint256 _nftId) private returns (uint256 fee0, uint256 fee1) {
if (_nftId == 0) return (0, 0);
(fee0, fee1) = _collect(_nftId);
fees0 += fee0;
fees1 += fee1;
if (gauge != address(0) && fee0 == 0 && fee1 == 0) return (0, 0);
if (_nftId == positionMain.nftId) {
emit ClaimedFees(fee0, fee1, 0, 0);
} else {
emit ClaimedFees(0, 0, fee0, fee1);
}
}
function _collect(uint256 _nftId) private returns (uint256 amount0, uint256 amount1) {
(amount0, amount1) = INftPositionManager(nftManager)
.collect(
INftPositionManager.CollectParams({
tokenId: _nftId,
recipient: address(this),
amount0Max: type(uint128).max,
amount1Max: type(uint128).max
})
);
}
function output() external view returns (address) {
return rewardToken;
}
function _balanceOfReward() private view returns (uint256) {
return rewardToken == address(0) ? 0 : IERC20Metadata(rewardToken).balanceOf(address(this));
}
function _claimGaugeRewards() private returns (uint256 claimed) {
uint256 mainId = positionMain.nftId;
uint256 altId = positionAlt.nftId;
uint256[] memory tokenIds = new uint256[]((mainId != 0 ? 1 : 0) + (altId != 0 ? 1 : 0));
uint256 index;
if (mainId != 0) tokenIds[index++] = mainId;
if (altId != 0) tokenIds[index] = altId;
uint256 beforeBalance = _balanceOfReward();
IAeroGauge(gauge).claimEmissions(address(this), address(this), tokenIds);
return _bookRewards(beforeBalance);
}
/// @dev Reserve emissions received since `_beforeBalance` for the next harvest.
function _bookRewards(uint256 _beforeBalance) private returns (uint256 claimed) {
claimed = _balanceOfReward() - _beforeBalance;
rewardBalance += claimed;
emit ClaimedRewards(claimed);
}
/// @dev Keep net rewards separate from CLM principal, including when the reward is one of the pool tokens.
function _swapRewards() private returns (uint256 nativeEarned) {
uint256 amount = rewardBalance;
if (amount == 0) return 0;
rewardBalance = 0;
IFeeConfig.FeeCategory memory fee = getFees();
uint256 remaining;
(remaining, nativeEarned) = _takeFee(rewardToken, amount, fee.total);
if (remaining > 0) {
IRewardPool(rewardPool).notifyRewardAmount(rewardToken, remaining, 1 days);
emit HarvestRewards(remaining);
}
}
function _chargeFees(address _callFeeRecipient, uint256 _amount0, uint256 _amount1, uint256 _nativeRewards)
private
returns (uint256 _amountLeft0, uint256 _amountLeft1)
{
IFeeConfig.FeeCategory memory fee = getFees();
uint256 native0;
uint256 native1;
(_amountLeft0, native0) = _takeFee(lpToken0, _amount0, fee.total);
(_amountLeft1, native1) = _takeFee(lpToken1, _amount1, fee.total);
uint256 nativeEarned = _nativeRewards + native0 + native1;
uint256 callFeeAmount = nativeEarned * fee.call / DIVISOR;
uint256 strategistFeeAmount = nativeEarned * fee.strategist / DIVISOR;
uint256 beefyFeeAmount = nativeEarned - callFeeAmount - strategistFeeAmount;
if (callFeeAmount > 0) IERC20Metadata(native).safeTransfer(_callFeeRecipient, callFeeAmount);
if (strategistFeeAmount > 0) IERC20Metadata(native).safeTransfer(strategist, strategistFeeAmount);
if (beefyFeeAmount > 0) IERC20Metadata(native).safeTransfer(beefyFeeRecipient(), beefyFeeAmount);
emit ChargedFees(callFeeAmount, beefyFeeAmount, strategistFeeAmount);
}
function _takeFee(address _token, uint256 _amount, uint256 _fee)
private
returns (uint256 remaining, uint256 nativeEarned)
{
uint256 feeAmount = _amount * _fee / DIVISOR;
remaining = _amount - feeAmount;
if (feeAmount > 0) {
nativeEarned = _token == native ? feeAmount : IBeefySwapper(unirouter).swap(_token, native, feeAmount);
}
}
/**
* @notice Returns total token balances in the strategy.
* @return token0Bal The amount of token0 in the strategy.
* @return token1Bal The amount of token1 in the strategy.
*/
function balances() public view returns (uint256 token0Bal, uint256 token1Bal) {
(uint256 thisBal0, uint256 thisBal1) = balancesOfThis();
(uint256 poolBal0, uint256 poolBal1,,,,) = balancesOfPool();
(uint256 locked0, uint256 locked1) = lockedProfit();
return (thisBal0 + poolBal0 - locked0, thisBal1 + poolBal1 - locked1);
}
/**
* @notice Returns total tokens sitting in the strategy.
* @return token0Bal The amount of token0 in the strategy.
* @return token1Bal The amount of token1 in the strategy.
*/
function balancesOfThis() public view returns (uint256 token0Bal, uint256 token1Bal) {
token0Bal = IERC20Metadata(lpToken0).balanceOf(address(this)) - fees0;
token1Bal = IERC20Metadata(lpToken1).balanceOf(address(this)) - fees1;
if (rewardToken == lpToken0) token0Bal -= rewardBalance;
if (rewardToken == lpToken1) token1Bal -= rewardBalance;
}
/**
* @notice Returns total tokens in pool positions (is a calculation which means it could be a little off by a few wei).
* @return token0Bal The amount of token0 in the pool.
* @return token1Bal The amount of token1 in the pool.
* @return mainAmount0 The amount of token0 in the main position.
* @return mainAmount1 The amount of token1 in the main position.
* @return altAmount0 The amount of token0 in the alt position.
* @return altAmount1 The amount of token1 in the alt position.
*/
function balancesOfPool()
public
view
returns (
uint256 token0Bal,
uint256 token1Bal,
uint256 mainAmount0,
uint256 mainAmount1,
uint256 altAmount0,
uint256 altAmount1
)
{
uint160 sqrtPriceX96 = sqrtPrice();
uint128 liquidity;
uint128 altLiquidity;
uint256 owed0;
uint256 owed1;
uint256 altOwed0;
uint256 altOwed1;
if (positionMain.nftId != 0) (liquidity, owed0, owed1) = _positions(positionMain.nftId);
if (positionAlt.nftId != 0) (altLiquidity, altOwed0, altOwed1) = _positions(positionAlt.nftId);
(mainAmount0, mainAmount1) = LiquidityAmounts.getAmountsForLiquidity(
sqrtPriceX96,
TickMath.getSqrtRatioAtTick(positionMain.tickLower),
TickMath.getSqrtRatioAtTick(positionMain.tickUpper),
liquidity
);
(altAmount0, altAmount1) = LiquidityAmounts.getAmountsForLiquidity(
sqrtPriceX96,
TickMath.getSqrtRatioAtTick(positionAlt.tickLower),
TickMath.getSqrtRatioAtTick(positionAlt.tickUpper),
altLiquidity
);
mainAmount0 += owed0;
mainAmount1 += owed1;
altAmount0 += altOwed0;
altAmount1 += altOwed1;
token0Bal = mainAmount0 + altAmount0;
token1Bal = mainAmount1 + altAmount1;
}
/// @notice Returns the position of the nft.
function _positions(uint256 _nftId) private view returns (uint128 liquidity, uint256 owed0, uint256 owed1) {
(,,,,,,, liquidity,,, owed0, owed1) = INftPositionManager(nftManager).positions(_nftId);
}
function lockedProfit() public view override returns (uint256 locked0, uint256 locked1) {
(uint256 balThis0, uint256 balThis1) = balancesOfThis();
(uint256 balPool0, uint256 balPool1,,,,) = balancesOfPool();
uint256 totalBal0 = balThis0 + balPool0;
uint256 totalBal1 = balThis1 + balPool1;
uint256 elapsed = block.timestamp - lastHarvest;
uint256 remaining = elapsed < DURATION ? DURATION - elapsed : 0;
// Make sure we don't lock more than we have.
if (totalBal0 > totalLocked0) locked0 = totalLocked0 * remaining / DURATION;
else locked0 = totalBal0 * remaining / DURATION;
if (totalBal1 > totalLocked1) locked1 = totalLocked1 * remaining / DURATION;
else locked1 = totalBal1 * remaining / DURATION;
}
/**
* @notice Returns the range of the pool, will always be the main position.
* @return lowerPrice The lower price of the position.
* @return upperPrice The upper price of the position.
*/
function range() external view returns (uint256 lowerPrice, uint256 upperPrice) {
// the main position is always covering the alt range
lowerPrice = _fetchPrice(positionMain.tickLower);
upperPrice = _fetchPrice(positionMain.tickUpper);
}
/// @notice Fetch price for range
function _fetchPrice(int24 _tick) private pure returns (uint256) {
return FullMath.mulDiv(uint256(TickMath.getSqrtRatioAtTick(_tick)), SQRT_PRECISION, (2 ** 96)) ** 2;
}
/**
* @notice The current tick of the pool.
* @return tick The current tick of the pool.
*/
function currentTick() public view returns (int24 tick) {
(, tick,,,,) = IAeroPool(pool).slot0();
}
/**
* @notice The current price of the pool.
* @return _price The current price of the pool.
*/
function price() public view returns (uint256 _price) {
uint160 sqrtPriceX96 = sqrtPrice();
_price = FullMath.mulDiv(uint256(sqrtPriceX96), SQRT_PRECISION, (2 ** 96)) ** 2;
}
/**
* @notice The sqrt price of the pool.
* @return sqrtPriceX96 The sqrt price of the pool.
*/
function sqrtPrice() public view returns (uint160 sqrtPriceX96) {
(sqrtPriceX96,,,,,) = IAeroPool(pool).slot0();
}
/**
* @notice The swap fee variable is the fee charged for swaps in the underlying pool in 18 decimals
* @return fee The swap fee of the underlying pool
*/
function swapFee() external view override returns (uint256 fee) {
fee = uint256(IAeroPool(pool).fee()) * SQRT_PRECISION / 1e6;
}
/**
* @notice The tick distance of the pool.
* @return int24 The tick distance/spacing of the pool.
*/
function _tickDistance() private view returns (int24) {
return IAeroPool(pool).tickSpacing();
}
/// @notice Sets the tick positions for the main and alternative positions.
function _setTicks() private onlyCalmPeriods {
int24 tick = currentTick();
int24 distance = _tickDistance();
int24 width = positionWidth * distance;
_setMainTick(tick, distance, width);
_setAltTick(tick, distance, width);
lastPositionAdjustment = block.timestamp;
}
/// @notice Sets the main tick position.
function _setMainTick(int24 tick, int24 distance, int24 width) private {
(positionMain.tickLower, positionMain.tickUpper) = TickUtils.baseTicks(tick, width, distance);
}
/// @notice Sets the alternative tick position.
function _setAltTick(int24 tick, int24 distance, int24 width) private {
(uint256 bal0, uint256 bal1) = balancesOfThis();
// We calculate how much token0 we have in the price of token1.
uint256 amount0;
if (bal0 > 0) {
amount0 = bal0 * price() / PRECISION;
}
// We set the alternative position based on the token that has the most value available.
if (amount0 < bal1) {
(positionAlt.tickLower,) = TickUtils.baseTicks(tick, width, distance);
(positionAlt.tickUpper,) = TickUtils.baseTicks(tick, distance, distance);
} else {
(, positionAlt.tickLower) = TickUtils.baseTicks(tick, distance, distance);
(, positionAlt.tickUpper) = TickUtils.baseTicks(tick, width, distance);
}
if (positionMain.tickLower == positionAlt.tickLower && positionMain.tickUpper == positionAlt.tickUpper) {
revert InvalidTicks();
}
}
/**
* @notice Sets the deviation from the twap we will allow on adding liquidity.
* @param _maxDeviation The max deviation from twap we will allow.
*/
function setDeviation(int56 _maxDeviation) external onlyOwner {
emit SetDeviation(_maxDeviation);
// Require the deviation to be less than 40 times the tick spacing.
if (_maxDeviation < 0 || _maxDeviation >= _tickDistance() * 40) revert InvalidInput();
maxTickDeviation = _maxDeviation;
}
/**
* @notice The twap of the last minute from the pool.
* @return twapTick The twap of the last minute from the pool.
*/
function twap() public view returns (int56 twapTick) {
return _twap(twapInterval);
}
/**
* @notice The twap of the last 3 seconds from the pool.
* @return twapTick The twap of the last 3 seconds from the pool.
*/
function shortTwap() public view returns (int56 twapTick) {
return _twap(3);
}
function _twap(uint32 _interval) private view returns (int56 twapTick) {
uint32[] memory secondsAgo = new uint32[](2);
secondsAgo[0] = _interval;
(int56[] memory tickCuml,) = IAeroPool(pool).observe(secondsAgo);
twapTick = (tickCuml[1] - tickCuml[0]) / int32(_interval);
}
/**
* @notice Sets the twap interval for the strategy.
* @param _interval The new twap interval.
*/
function setTwapInterval(uint32 _interval) external onlyOwner {
emit SetTwapInterval(twapInterval, _interval);
// Require the interval to be greater than 60 seconds.
if (_interval < 60) revert InvalidInput();
twapInterval = _interval;
}
/**
* @notice Sets our position width and readjusts our positions.
* @param _width The new width multiplier of the position.
*/
function setPositionWidth(int24 _width) external onlyOwner {
if (_width <= 1) revert InvalidInput();
_enforceActionDelay();
emit SetPositionWidth(positionWidth, _width);
_removeLiquidity(false);
positionWidth = _width;
_setTicks();
_addLiquidity();
}
/// @notice Stake into the pool's gauge and configure its emissions token without changing the CLM's ticks.
/// @dev The emissions token is derived only here. If it ever changes, harvest first so no rewardBalance remains.
function enableGauge() external onlyOwner {
if (gauge != address(0)) revert InvalidInput();
address _gauge = IAeroPool(pool).gauge();
if (_gauge == address(0)) revert InvalidInput();
_validateGauge(_gauge);
address _reward = _gaugeReward(_gauge);
if (_reward == address(0)) revert InvalidInput();
if (_reward != rewardToken) {
if (rewardBalance != 0) revert InvalidInput();
_removeAllowances();
rewardToken = _reward;
if (!_isPaused()) _giveAllowances();
emit SetReward(_reward);
}
_setGauge(_gauge);
}
/// @notice Unstake from the gauge without changing the CLM's ticks, keeping the emissions token for harvests.
/// @dev The pool's gauge never changes, so re-enabling later also recovers any deferred emissions.
function disableGauge() external onlyManager {
if (gauge == address(0)) revert InvalidInput();
_enforceActionDelay();
_setGauge(address(0));
}
function _setGauge(address _gauge) private {
_removeLiquidity(false);
gauge = _gauge;
if (initTicks && !_isPaused()) _addLiquidity();
emit SetGauge(_gauge);
}
function _validateGauge(address _gauge) private view {
if (IAeroGauge(_gauge).pool() != pool || IAeroGauge(_gauge).nft() != nftManager) revert InvalidInput();
}
/// @dev Leaf chains pay emissions in the voter's receipt token; the root redeems it for the root voter's token.
function _gaugeReward(address _gauge) private view returns (address) {
IAeroLeafVoter voter = IAeroLeafVoter(IAeroGauge(_gauge).voter());
IAeroLeafMessageOrchestrator orchestrator = IAeroLeafMessageOrchestrator(voter.ORCHESTRATOR());
if (orchestrator.ROOT_CHAIN_ID() != block.chainid) return voter.RECEIPT_TOKEN();
address rootOrchestrator = IAeroRootLocalAdapter(orchestrator.adapter()).ROOT_MESSAGE_ORCHESTRATOR();
return IAeroRootVoter(IAeroRootMessageOrchestrator(rootOrchestrator).VOTER()).TOKEN();
}
/**
* @notice Sets the reward pool address.
* @param _rewardPool The new reward pool address.
*/
function setRewardPool(address _rewardPool) external onlyOwner {
_removeAllowances();
rewardPool = _rewardPool;
if (!_isPaused()) _giveAllowances();
emit SetRewardPool(_rewardPool);
}
/**
* @notice set the unirouter address
* @param _unirouter The new unirouter address
*/
function setUnirouter(address _unirouter) external override onlyOwner {
_removeAllowances();
unirouter = _unirouter;
if (!_isPaused()) _giveAllowances();
emit SetUnirouter(_unirouter);
}
/// @notice Retire the strategy and return all the dust to the fee recipient.
function retireVault() external onlyOwner {
if (IBeefyVaultConcLiq(vault).totalSupply() != 10 ** 3) revert NotAuthorized();
panic(0, 0);
address feeRecipient = beefyFeeRecipient();
IERC20Metadata(lpToken0).safeTransfer(feeRecipient, IERC20Metadata(lpToken0).balanceOf(address(this)));
IERC20Metadata(lpToken1).safeTransfer(feeRecipient, IERC20Metadata(lpToken1).balanceOf(address(this)));
uint256 balance = _balanceOfReward();
if (balance > 0) IERC20Metadata(rewardToken).safeTransfer(feeRecipient, balance);
_transferOwnership(address(0));
}
/**
* @notice Remove Liquidity and Allowances, then pause deposits.
* @param _minAmount0 The minimum amount of token0 in the strategy after panic.
* @param _minAmount1 The minimum amount of token1 in the strategy after panic.
*/
function panic(uint256 _minAmount0, uint256 _minAmount1) public onlyManager {
// Gauge withdrawal claims or defers emissions itself; do not let a failed explicit claim block exit.
// If the gauge factory enables emergency withdraw, unstake without touching emissions at all.
_removeLiquidity(true);
_removeAllowances();
if (!paused()) _pause();
(uint256 bal0, uint256 bal1) = balances();
if (bal0 < _minAmount0 || bal1 < _minAmount1) revert TooMuchSlippage();
}
/// @notice Unpause deposits, give allowances and add liquidity.
function unpause() external onlyManager {
if (owner() == address(0)) revert NotAuthorized();
_giveAllowances();
_unpause();
initTicks = true;
_setTicks();
_addLiquidity();
}
/// @notice gives swap permisions for the tokens to the unirouter.
function _giveAllowances() private {
_setAllowances(type(uint256).max);
}
/// @notice removes swap permisions for the tokens from the unirouter.
function _removeAllowances() private {
_setAllowances(0);
}
function _setAllowances(uint256 _amount) private {
IERC20Metadata(lpToken0).forceApprove(nftManager, _amount);
IERC20Metadata(lpToken1).forceApprove(nftManager, _amount);
IERC20Metadata(lpToken0).forceApprove(unirouter, _amount);
IERC20Metadata(lpToken1).forceApprove(unirouter, _amount);
if (rewardToken != address(0)) {
IERC20Metadata(rewardToken).forceApprove(unirouter, _amount);
IERC20Metadata(rewardToken).forceApprove(rewardPool, _amount);
}
}
function onERC721Received(address, address, uint256, bytes calldata) external view returns (bytes4) {
if (msg.sender != nftManager) revert NotAuthorized();
return this.onERC721Received.selector;
}
receive() external payable {
if (msg.sender != nftManager) revert NotAuthorized();
}
}
contracts/interfaces/aero/IAeroGauge.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
/// @notice Aero v3 gauge calls used by the passive manager.
interface IAeroGauge {
function pool() external view returns (address);
function nft() external view returns (address);
function gaugeFactory() external view returns (address);
function voter() external view returns (address);
function deposit(uint256 tokenId) external;
function withdraw(uint256 tokenId) external;
function emergencyWithdraw(uint256 tokenId) external;
function claimEmissions(address account, address recipient) external;
function claimEmissions(address account, address recipient, uint256[] calldata tokenIds) external;
function deferredEmissions(address account) external view returns (uint256);
}
interface IAeroGaugeFactory {
struct PenaltyConfig {
uint256 minStakeBlocks;
uint256 penaltyRate;
}
function effectivePenaltyConfig(address gauge) external view returns (PenaltyConfig memory);
function emergencyWithdrawEnabled() external view returns (bool);
}
contracts/interfaces/aero/IAeroPool.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
/// @notice Aero CL pool calls used by the passive manager.
interface IAeroPool {
/// @notice The first of the two tokens of the pool, sorted by address
/// @return The token contract address
function token0() external view returns (address);
/// @notice The second of the two tokens of the pool, sorted by address
/// @return The token contract address
function token1() external view returns (address);
/// @notice The gauge registered for the pool, or zero if none
/// @return The gauge contract address
function gauge() external view returns (address);
/// @notice The pool tick spacing
/// @return The tick spacing
function tickSpacing() external view returns (int24);
/// @notice The pool's swap fee in pips, i.e. 1e-6
/// @return The swap fee
function fee() external view returns (uint24);
/// @notice The 0th storage slot in the pool stores many values, and is exposed as a single method to save gas
/// when accessed externally.
function slot0()
external
view
returns (
uint160 sqrtPriceX96,
int24 tick,
uint16 observationIndex,
uint16 observationCardinality,
uint16 observationCardinalityNext,
bool unlocked
);
/// @notice Returns the cumulative tick and liquidity as of each timestamp `secondsAgo` from the current block timestamp
/// @param secondsAgos From how long ago each cumulative tick and liquidity value should be returned
/// @return tickCumulatives Cumulative tick values as of each `secondsAgos` from the current block timestamp
/// @return secondsPerLiquidityCumulativeX128s Cumulative seconds per liquidity-in-range value as of each `secondsAgos`
/// from the current block timestamp
function observe(uint32[] calldata secondsAgos)
external
view
returns (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s);
}
contracts/interfaces/aero/IAeroVoter.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
/// @notice Aero voter calls used by the passive manager to derive a gauge's emissions token.
interface IAeroLeafVoter {
function ORCHESTRATOR() external view returns (address);
function RECEIPT_TOKEN() external view returns (address);
}
interface IAeroLeafMessageOrchestrator {
function ROOT_CHAIN_ID() external view returns (uint256);
function adapter() external view returns (address);
}
interface IAeroRootLocalAdapter {
function ROOT_MESSAGE_ORCHESTRATOR() external view returns (address);
}
interface IAeroRootMessageOrchestrator {
function VOTER() external view returns (address);
}
interface IAeroRootVoter {
function TOKEN() external view returns (address);
}
contracts/interfaces/beefy/IBeefySwapper.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IBeefySwapper {
function swap(address fromToken, address toToken, uint256 amountIn) external returns (uint256 amountOut);
function swap(address fromToken, address toToken, uint256 amountIn, uint256 minAmountOut)
external
returns (uint256 amountOut);
function getAmountOut(address _fromToken, address _toToken, uint256 _amountIn)
external
view
returns (uint256 amountOut);
struct SwapInfo {
address router;
bytes data;
uint256 amountIndex;
uint256 minIndex;
int8 minAmountSign;
}
function setSwapInfo(address _fromToken, address _toToken, SwapInfo calldata _swapInfo) external;
function oracle() external view returns (address);
function owner() external view returns (address);
}
contracts/interfaces/beefy/IBeefyVaultConcLiq.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
interface IBeefyVaultConcLiq {
function previewDeposit(uint256 _amount0, uint256 _amount1)
external
view
returns (uint256 shares, uint256 amount0, uint256 amount1, uint256 fee0, uint256 fee1);
function previewWithdraw(uint256 shares) external view returns (uint256 amount0, uint256 amount1);
function strategy() external view returns (address);
function totalSupply() external view returns (uint256);
function wants() external view returns (address, address);
function balances() external view returns (uint256, uint256);
function deposit(uint256 amount0, uint256 amount1, uint256 minShares) external;
function isCalm() external view returns (bool);
}
contracts/interfaces/beefy/IFeeConfig.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IFeeConfig {
struct FeeCategory {
uint256 total;
uint256 beefy;
uint256 call;
uint256 strategist;
string label;
bool active;
}
struct AllFees {
FeeCategory performance;
uint256 deposit;
uint256 withdraw;
}
function getFees(address strategy) external view returns (FeeCategory memory);
function stratFeeId(address strategy) external view returns (uint256);
function setStratFeeId(uint256 feeId) external;
}
contracts/interfaces/beefy/IRewardPool.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
interface IRewardPool {
function stake(uint256 amount) external;
function withdraw(uint256 amount) external;
function getReward() external;
function notifyRewardAmount(address token, uint256 reward, uint256 duration) external;
function earned(address user, address token) external view returns (uint256);
}
contracts/interfaces/beefy/IStrategyConcLiq.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
interface IStrategyConcLiq {
function balances() external view returns (uint256, uint256);
function beforeAction() external;
function deposit() external;
function withdraw(uint256 _amount0, uint256 _amount1) external;
function pool() external view returns (address);
function lpToken0() external view returns (address);
function lpToken1() external view returns (address);
function isCalm() external view returns (bool);
function swapFee() external view returns (uint256);
function setDeviation(int56 _maxDeviation) external;
/// @notice The current price of the pool in token1, encoded with `36 + lpToken1.decimals - lpToken0.decimals`.
/// @return _price The current price of the pool in token1.
function price() external view returns (uint256 _price);
}
contracts/interfaces/beefy/IStrategyFactory.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IStrategyFactory {
function owner() external view returns (address);
function instances(string calldata name) external view returns (address);
function rebalancers(address) external view returns (bool);
function strategyTypes(uint256 index) external view returns (string memory);
function native() external view returns (address);
function keeper() external view returns (address);
function beefyFeeRecipient() external view returns (address);
function beefyFeeConfig() external view returns (address);
function globalPause() external view returns (bool);
function createStrategy(string calldata name) external returns (address);
function upgradeTo(string calldata name, address newImplementation) external;
function addStrategy(string calldata name, address implementation) external;
function pauseAllStrats() external;
function unpauseAllStrats() external;
function addRebalancer(address rebalancer) external;
function removeRebalancer(address rebalancer) external;
function setBeefyFeeRecipient(address newRecipient) external;
function setBeefyFeeConfig(address newConfig) external;
function setKeeper(address newKeeper) external;
function getImplementation(string calldata name) external view returns (address);
function getStrategyTypes() external view returns (string[] memory);
}
contracts/interfaces/beefy/IStrategyVelodrome.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
/// @notice Interface for the Uniswap V3 strategy contract.
interface IStrategyVelodrome {
/// @notice The sqrt price of the pool.
function sqrtPrice() external view returns (uint160 sqrtPriceX96);
/// @notice The range covered by the strategy.
function range() external view returns (uint256 lowerPrice, uint256 upperPrice);
}
contracts/interfaces/velodrome/INftPositionManager.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
/// @title Non-fungible token for positions
/// @notice Wraps CL positions in a non-fungible token interface which allows for them to be transferred
/// and authorized.
interface INftPositionManager {
/// @notice Returns the position information associated with a given token ID.
/// @dev Throws if the token ID is not valid.
/// @param tokenId The ID of the token that represents the position
/// @return nonce The nonce for permits
/// @return operator The address that is approved for spending
/// @return token0 The address of the token0 for a specific pool
/// @return token1 The address of the token1 for a specific pool
/// @return tickSpacing The tick spacing associated with the pool
/// @return tickLower The lower end of the tick range for the position
/// @return tickUpper The higher end of the tick range for the position
/// @return liquidity The liquidity of the position
/// @return feeGrowthInside0LastX128 The fee growth of token0 as of the last action on the individual position
/// @return feeGrowthInside1LastX128 The fee growth of token1 as of the last action on the individual position
/// @return tokensOwed0 The uncollected amount of token0 owed to the position as of the last computation
/// @return tokensOwed1 The uncollected amount of token1 owed to the position as of the last computation
function positions(uint256 tokenId)
external
view
returns (
uint96 nonce,
address operator,
address token0,
address token1,
int24 tickSpacing,
int24 tickLower,
int24 tickUpper,
uint128 liquidity,
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128,
uint128 tokensOwed0,
uint128 tokensOwed1
);
/// @notice Returns the address of the Token Descriptor, that handles generating token URIs for Positions
function tokenDescriptor() external view returns (address);
/// @notice Returns the address of the Owner, that is allowed to set a new TokenDescriptor
function owner() external view returns (address);
struct MintParams {
address token0;
address token1;
int24 tickSpacing;
int24 tickLower;
int24 tickUpper;
uint256 amount0Desired;
uint256 amount1Desired;
uint256 amount0Min;
uint256 amount1Min;
address recipient;
uint256 deadline;
uint160 sqrtPriceX96;
}
/// @notice Creates a new position wrapped in a NFT
/// @dev Call this when the pool does exist and is initialized. Note that if the pool is created but not initialized
/// a method does not exist, i.e. the pool is assumed to be initialized.
/// @param params The params necessary to mint a position, encoded as `MintParams` in calldata
/// @return tokenId The ID of the token that represents the minted position
/// @return liquidity The amount of liquidity for this position
/// @return amount0 The amount of token0
/// @return amount1 The amount of token1
function mint(MintParams calldata params)
external
payable
returns (uint256 tokenId, uint128 liquidity, uint256 amount0, uint256 amount1);
struct IncreaseLiquidityParams {
uint256 tokenId;
uint256 amount0Desired;
uint256 amount1Desired;
uint256 amount0Min;
uint256 amount1Min;
uint256 deadline;
}
/// @notice Increases the amount of liquidity in a position, with tokens paid by the `msg.sender`
/// @param params tokenId The ID of the token for which liquidity is being increased,
/// amount0Desired The desired amount of token0 to be spent,
/// amount1Desired The desired amount of token1 to be spent,
/// amount0Min The minimum amount of token0 to spend, which serves as a slippage check,
/// amount1Min The minimum amount of token1 to spend, which serves as a slippage check,
/// deadline The time by which the transaction must be included to effect the change
/// @return liquidity The new liquidity amount as a result of the increase
/// @return amount0 The amount of token0 to acheive resulting liquidity
/// @return amount1 The amount of token1 to acheive resulting liquidity
function increaseLiquidity(IncreaseLiquidityParams calldata params)
external
payable
returns (uint128 liquidity, uint256 amount0, uint256 amount1);
struct DecreaseLiquidityParams {
uint256 tokenId;
uint128 liquidity;
uint256 amount0Min;
uint256 amount1Min;
uint256 deadline;
}
/// @notice Decreases the amount of liquidity in a position and accounts it to the position
/// @param params tokenId The ID of the token for which liquidity is being decreased,
/// amount The amount by which liquidity will be decreased,
/// amount0Min The minimum amount of token0 that should be accounted for the burned liquidity,
/// amount1Min The minimum amount of token1 that should be accounted for the burned liquidity,
/// deadline The time by which the transaction must be included to effect the change
/// @return amount0 The amount of token0 accounted to the position's tokens owed
/// @return amount1 The amount of token1 accounted to the position's tokens owed
/// @dev The use of this function can cause a loss to users of the NonfungiblePositionManager
/// @dev for tokens that have very high decimals.
/// @dev The amount of tokens necessary for the loss is: 3.4028237e+38.
/// @dev This is equivalent to 1e20 value with 18 decimals.
function decreaseLiquidity(DecreaseLiquidityParams calldata params)
external
payable
returns (uint256 amount0, uint256 amount1);
struct CollectParams {
uint256 tokenId;
address recipient;
uint128 amount0Max;
uint128 amount1Max;
}
/// @notice Collects up to a maximum amount of fees owed to a specific position to the recipient
/// @notice Used to update staked positions before deposit and withdraw
/// @param params tokenId The ID of the NFT for which tokens are being collected,
/// recipient The account that should receive the tokens,
/// amount0Max The maximum amount of token0 to collect,
/// amount1Max The maximum amount of token1 to collect
/// @return amount0 The amount of fees collected in token0
/// @return amount1 The amount of fees collected in token1
function collect(CollectParams calldata params) external payable returns (uint256 amount0, uint256 amount1);
/// @notice Burns a token ID, which deletes it from the NFT contract. The token must have 0 liquidity and all tokens
/// must be collected first.
/// @param tokenId The ID of the token that is being burned
function burn(uint256 tokenId) external payable;
/// @notice Sets a new Token Descriptor
/// @param _tokenDescriptor Address of the new Token Descriptor to be chosen
function setTokenDescriptor(address _tokenDescriptor) external;
/// @notice Sets a new Owner address
/// @param _owner Address of the new Owner to be chosen
function setOwner(address _owner) external;
}
contracts/strategies/StratFeeManagerInitializable.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {OwnableUpgradeable} from "@openzeppelin-4/contracts-upgradeable/access/OwnableUpgradeable.sol";
import {PausableUpgradeable} from "@openzeppelin-4/contracts-upgradeable/security/PausableUpgradeable.sol";
import {IFeeConfig} from "../interfaces/beefy/IFeeConfig.sol";
import {IStrategyFactory} from "../interfaces/beefy/IStrategyFactory.sol";
contract StratFeeManagerInitializable is OwnableUpgradeable, PausableUpgradeable {
struct CommonAddresses {
address vault;
address unirouter;
address strategist;
address factory;
}
/// @notice The native address of the chain
address public native;
/// @notice The address of the vault
address public vault;
/// @notice The address of the unirouter
address public unirouter;
/// @notice The address of the strategist
address public strategist;
/// @notice The address of the strategy factory
IStrategyFactory public factory;
/// @notice The total amount of token0 locked in the vault
uint256 public totalLocked0;
/// @notice The total amount of token1 locked in the vault
uint256 public totalLocked1;
/// @notice The last time the strat harvested
uint256 public lastHarvest;
/// @notice The last time we adjusted the position
uint256 public lastPositionAdjustment;
/// @notice The duration of the locked rewards
uint256 constant DURATION = 1 hours;
/// @notice The divisor used to calculate the fee
uint256 constant DIVISOR = 1 ether;
// Events
event SetStratFeeId(uint256 feeId);
event SetUnirouter(address unirouter);
event SetStrategist(address strategist);
// Errors
error NotManager();
error NotStrategist();
error OverLimit();
error StrategyPaused();
/**
* @notice Initialize the Strategy Fee Manager inherited contract with the common addresses
* @param _commonAddresses The common addresses of the vault, unirouter, keeper, strategist, beefyFeeRecipient and beefyFeeConfig
*/
// forge-lint: disable-next-line(mixed-case-function)
function __StratFeeManager_init(CommonAddresses calldata _commonAddresses) internal onlyInitializing {
__Ownable_init();
__Pausable_init();
vault = _commonAddresses.vault;
unirouter = _commonAddresses.unirouter;
strategist = _commonAddresses.strategist;
factory = IStrategyFactory(_commonAddresses.factory);
native = factory.native();
}
/**
* @notice function that throws if the strategy is paused
*/
function _whenStrategyNotPaused() internal view {
if (paused() || factory.globalPause()) revert StrategyPaused();
}
/**
* @notice function that returns true if the strategy is paused
*/
function _isPaused() internal view returns (bool) {
return paused() || factory.globalPause();
}
/**
* @notice Modifier that throws if called by any account other than the manager or the owner
*/
modifier onlyManager() {
if (msg.sender != owner() && msg.sender != keeper()) revert NotManager();
_;
}
/// @notice The address of the keeper, set on the factory.
function keeper() public view returns (address) {
return factory.keeper();
}
/// @notice The address of the beefy fee recipient, set on the factory.
function beefyFeeRecipient() public view returns (address) {
return factory.beefyFeeRecipient();
}
/// @notice The address of the beefy fee config, set on the factory.
function beefyFeeConfig() public view returns (IFeeConfig) {
return IFeeConfig(factory.beefyFeeConfig());
}
/**
* @notice get the fees breakdown from the fee config for this contract
* @return IFeeConfig.FeeCategory The fees breakdown
*/
function getFees() internal view returns (IFeeConfig.FeeCategory memory) {
return beefyFeeConfig().getFees(address(this));
}
/**
* @notice get all the fees from the fee config for this contract
* @return IFeeConfig.AllFees The fees
*/
function getAllFees() external view returns (IFeeConfig.AllFees memory) {
return IFeeConfig.AllFees(getFees(), depositFee(), withdrawFee());
}
/**
* @notice get the strat fee id from the fee config
* @return uint256 The strat fee id
*/
function getStratFeeId() external view returns (uint256) {
return beefyFeeConfig().stratFeeId(address(this));
}
/**
* @notice set the strat fee id in the fee config
* @param _feeId The new strat fee id
*/
function setStratFeeId(uint256 _feeId) external onlyManager {
beefyFeeConfig().setStratFeeId(_feeId);
emit SetStratFeeId(_feeId);
}
/**
* @notice set the unirouter address
* @param _unirouter The new unirouter address
*/
function setUnirouter(address _unirouter) external virtual onlyOwner {
unirouter = _unirouter;
emit SetUnirouter(_unirouter);
}
/**
* @notice set the strategist address
* @param _strategist The new strategist address
*/
function setStrategist(address _strategist) external {
if (msg.sender != strategist) revert NotStrategist();
strategist = _strategist;
emit SetStrategist(_strategist);
}
/**
* @notice The deposit fee variable will alwasy be 0. This is used by the UI.
* @return uint256 The deposit fee
*/
function depositFee() public view virtual returns (uint256) {
return 0;
}
/**
* @notice The withdraw fee variable will alwasy be 0. This is used by the UI.
* @return uint256 The withdraw fee
*/
function withdrawFee() public view virtual returns (uint256) {
return 0;
}
/**
* @notice The locked profit is the amount of token0 and token1 that is locked in the vault, this can be overriden by the strategy contract.
* @return locked0 The amount of token0 locked
* @return locked1 The amount of token1 locked
*/
function lockedProfit() public view virtual returns (uint256 locked0, uint256 locked1) {
uint256 elapsed = block.timestamp - lastHarvest;
uint256 remaining = elapsed < DURATION ? DURATION - elapsed : 0;
return (totalLocked0 * remaining / DURATION, totalLocked1 * remaining / DURATION);
}
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[49] private __gap;
}
contracts/strategies/harvest-manager/public/StratHarvestManager.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
import {StratFeeManagerInitializable} from "../../StratFeeManagerInitializable.sol";
/// @title Public StratHarvestManager
/// @author Chimp, Beefy
/// @notice This is the base contract for harvest manager strategies, harvests are open to public and there is no harvester management.
/// @dev For more control over harvesters, see StratHarvestManagerConfigurable.
/// Do not add any state variables to this contract (as inheriters are upgradeable)
abstract contract StratHarvestManager is StratFeeManagerInitializable {
/// @notice Harvest call to claim rewards from gauge then charge fees for Beefy and notify rewards.
/// @param _callFeeRecipient The address to send the call fee to.
function harvest(address _callFeeRecipient) external {
_harvest(_callFeeRecipient);
}
/// @notice Harvest call to claim rewards from gauge then charge fees for Beefy and notify rewards.
/// @dev Call fee goes to the tx.origin.
function harvest() external {
_harvest(tx.origin);
}
/// @notice Internal function to claim rewards from gauge then charge fees for Beefy and notify rewards
/// @param _callFeeRecipient The address to send the call fee to.
/// @dev When overriding this function do not forget to add onlyCalmPeriods modifier to it.
function _harvest(address _callFeeRecipient) internal virtual;
}
contracts/utils/BytesLib.sol
// SPDX-License-Identifier: Unlicense
/*
* @title Solidity Bytes Arrays Utils
* @author Gonçalo Sá <[email protected]>
*
* @dev Bytes tightly packed arrays utility library for ethereum contracts written in Solidity.
* The library lets you concatenate, slice and type cast bytes arrays both in memory and storage.
*/
pragma solidity >=0.8.0 <0.9.0;
library BytesLib {
function concat(bytes memory _preBytes, bytes memory _postBytes) internal pure returns (bytes memory) {
bytes memory tempBytes;
assembly {
// Get a location of some free memory and store it in tempBytes as
// Solidity does for memory variables.
tempBytes := mload(0x40)
// Store the length of the first bytes array at the beginning of
// the memory for tempBytes.
let length := mload(_preBytes)
mstore(tempBytes, length)
// Maintain a memory counter for the current write location in the
// temp bytes array by adding the 32 bytes for the array length to
// the starting location.
let mc := add(tempBytes, 0x20)
// Stop copying when the memory counter reaches the length of the
// first bytes array.
let end := add(mc, length)
for {
// Initialize a copy counter to the start of the _preBytes data,
// 32 bytes into its memory.
let cc := add(_preBytes, 0x20)
} lt(mc, end) {
// Increase both counters by 32 bytes each iteration.
mc := add(mc, 0x20)
cc := add(cc, 0x20)
} {
// Write the _preBytes data into the tempBytes memory 32 bytes
// at a time.
mstore(mc, mload(cc))
}
// Add the length of _postBytes to the current length of tempBytes
// and store it as the new length in the first 32 bytes of the
// tempBytes memory.
length := mload(_postBytes)
mstore(tempBytes, add(length, mload(tempBytes)))
// Move the memory counter back from a multiple of 0x20 to the
// actual end of the _preBytes data.
mc := end
// Stop copying when the memory counter reaches the new combined
// length of the arrays.
end := add(mc, length)
for {
let cc := add(_postBytes, 0x20)
} lt(mc, end) {
mc := add(mc, 0x20)
cc := add(cc, 0x20)
} {
mstore(mc, mload(cc))
}
// Update the free-memory pointer by padding our last write location
// to 32 bytes: add 31 bytes to the end of tempBytes to move to the
// next 32 byte block, then round down to the nearest multiple of
// 32. If the sum of the length of the two arrays is zero then add
// one before rounding down to leave a blank 32 bytes (the length block with 0).
mstore(
0x40,
and(
add(add(end, iszero(add(length, mload(_preBytes)))), 31),
not(31) // Round down to the nearest 32 bytes.
)
)
}
return tempBytes;
}
function concatStorage(bytes storage _preBytes, bytes memory _postBytes) internal {
assembly {
// Read the first 32 bytes of _preBytes storage, which is the length
// of the array. (We don't need to use the offset into the slot
// because arrays use the entire slot.)
let fslot := sload(_preBytes.slot)
// Arrays of 31 bytes or less have an even value in their slot,
// while longer arrays have an odd value. The actual length is
// the slot divided by two for odd values, and the lowest order
// byte divided by two for even values.
// If the slot is even, bitwise and the slot with 255 and divide by
// two to get the length. If the slot is odd, bitwise and the slot
// with -1 and divide by two.
let slength := div(and(fslot, sub(mul(0x100, iszero(and(fslot, 1))), 1)), 2)
let mlength := mload(_postBytes)
let newlength := add(slength, mlength)
// slength can contain both the length and contents of the array
// if length < 32 bytes so let's prepare for that
// v. http://solidity.readthedocs.io/en/latest/miscellaneous.html#layout-of-state-variables-in-storage
switch add(lt(slength, 32), lt(newlength, 32))
case 2 {
// Since the new array still fits in the slot, we just need to
// update the contents of the slot.
// uint256(bytes_storage) = uint256(bytes_storage) + uint256(bytes_memory) + new_length
sstore(
_preBytes.slot,
// all the modifications to the slot are inside this
// next block
add(
// we can just add to the slot contents because the
// bytes we want to change are the LSBs
fslot,
add(
mul(
div(
// load the bytes from memory
mload(add(_postBytes, 0x20)),
// zero all bytes to the right
exp(0x100, sub(32, mlength))
),
// and now shift left the number of bytes to
// leave space for the length in the slot
exp(0x100, sub(32, newlength))
),
// increase length by the double of the memory
// bytes length
mul(mlength, 2)
)
)
)
}
case 1 {
// The stored value fits in the slot, but the combined value
// will exceed it.
// get the keccak hash to get the contents of the array
mstore(0x0, _preBytes.slot)
let sc := add(keccak256(0x0, 0x20), div(slength, 32))
// save new length
sstore(_preBytes.slot, add(mul(newlength, 2), 1))
// The contents of the _postBytes array start 32 bytes into
// the structure. Our first read should obtain the `submod`
// bytes that can fit into the unused space in the last word
// of the stored array. To get this, we read 32 bytes starting
// from `submod`, so the data we read overlaps with the array
// contents by `submod` bytes. Masking the lowest-order
// `submod` bytes allows us to add that value directly to the
// stored value.
let submod := sub(32, slength)
let mc := add(_postBytes, submod)
let end := add(_postBytes, mlength)
let mask := sub(exp(0x100, submod), 1)
sstore(
sc,
add(
and(fslot, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00),
and(mload(mc), mask)
)
)
for {
mc := add(mc, 0x20)
sc := add(sc, 1)
} lt(mc, end) {
sc := add(sc, 1)
mc := add(mc, 0x20)
} {
sstore(sc, mload(mc))
}
mask := exp(0x100, sub(mc, end))
sstore(sc, mul(div(mload(mc), mask), mask))
}
default {
// get the keccak hash to get the contents of the array
mstore(0x0, _preBytes.slot)
// Start copying to the last used word of the stored array.
let sc := add(keccak256(0x0, 0x20), div(slength, 32))
// save new length
sstore(_preBytes.slot, add(mul(newlength, 2), 1))
// Copy over the first `submod` bytes of the new data as in
// case 1 above.
let slengthmod := mod(slength, 32)
let mlengthmod := mod(mlength, 32)
let submod := sub(32, slengthmod)
let mc := add(_postBytes, submod)
let end := add(_postBytes, mlength)
let mask := sub(exp(0x100, submod), 1)
sstore(sc, add(sload(sc), and(mload(mc), mask)))
for {
sc := add(sc, 1)
mc := add(mc, 0x20)
} lt(mc, end) {
sc := add(sc, 1)
mc := add(mc, 0x20)
} {
sstore(sc, mload(mc))
}
mask := exp(0x100, sub(mc, end))
sstore(sc, mul(div(mload(mc), mask), mask))
}
}
}
function slice(bytes memory _bytes, uint256 _start, uint256 _length) internal pure returns (bytes memory) {
require(_length + 31 >= _length, "slice_overflow");
require(_bytes.length >= _start + _length, "slice_outOfBounds");
bytes memory tempBytes;
assembly {
switch iszero(_length)
case 0 {
// Get a location of some free memory and store it in tempBytes as
// Solidity does for memory variables.
tempBytes := mload(0x40)
// The first word of the slice result is potentially a partial
// word read from the original array. To read it, we calculate
// the length of that partial word and start copying that many
// bytes into the array. The first word we copy will start with
// data we don't care about, but the last `lengthmod` bytes will
// land at the beginning of the contents of the new array. When
// we're done copying, we overwrite the full first word with
// the actual length of the slice.
let lengthmod := and(_length, 31)
// The multiplication in the next line is necessary
// because when slicing multiples of 32 bytes (lengthmod == 0)
// the following copy loop was copying the origin's length
// and then ending prematurely not copying everything it should.
let mc := add(add(tempBytes, lengthmod), mul(0x20, iszero(lengthmod)))
let end := add(mc, _length)
for {
// The multiplication in the next line has the same exact purpose
// as the one above.
let cc := add(add(add(_bytes, lengthmod), mul(0x20, iszero(lengthmod))), _start)
} lt(mc, end) {
mc := add(mc, 0x20)
cc := add(cc, 0x20)
} {
mstore(mc, mload(cc))
}
mstore(tempBytes, _length)
//update free-memory pointer
//allocating the array padded to 32 bytes like the compiler does now
mstore(0x40, and(add(mc, 31), not(31)))
}
//if we want a zero-length slice let's just return a zero-length array
default {
tempBytes := mload(0x40)
//zero out the 32 bytes slice we are about to return
//we need to do it because Solidity does not garbage collect
mstore(tempBytes, 0)
mstore(0x40, add(tempBytes, 0x20))
}
}
return tempBytes;
}
function toAddress(bytes memory _bytes, uint256 _start) internal pure returns (address) {
require(_bytes.length >= _start + 20, "toAddress_outOfBounds");
address tempAddress;
assembly {
tempAddress := div(mload(add(add(_bytes, 0x20), _start)), 0x1000000000000000000000000)
}
return tempAddress;
}
function toUint8(bytes memory _bytes, uint256 _start) internal pure returns (uint8) {
require(_bytes.length >= _start + 1, "toUint8_outOfBounds");
uint8 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x1), _start))
}
return tempUint;
}
function toUint16(bytes memory _bytes, uint256 _start) internal pure returns (uint16) {
require(_bytes.length >= _start + 2, "toUint16_outOfBounds");
uint16 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x2), _start))
}
return tempUint;
}
function toUint24(bytes memory _bytes, uint256 _start) internal pure returns (uint24) {
require(_start + 3 >= _start, "toUint24_overflow");
require(_bytes.length >= _start + 3, "toUint24_outOfBounds");
uint24 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x3), _start))
}
return tempUint;
}
function toUint32(bytes memory _bytes, uint256 _start) internal pure returns (uint32) {
require(_bytes.length >= _start + 4, "toUint32_outOfBounds");
uint32 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x4), _start))
}
return tempUint;
}
function toUint64(bytes memory _bytes, uint256 _start) internal pure returns (uint64) {
require(_bytes.length >= _start + 8, "toUint64_outOfBounds");
uint64 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x8), _start))
}
return tempUint;
}
function toUint96(bytes memory _bytes, uint256 _start) internal pure returns (uint96) {
require(_bytes.length >= _start + 12, "toUint96_outOfBounds");
uint96 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0xc), _start))
}
return tempUint;
}
function toUint128(bytes memory _bytes, uint256 _start) internal pure returns (uint128) {
require(_bytes.length >= _start + 16, "toUint128_outOfBounds");
uint128 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x10), _start))
}
return tempUint;
}
function toUint256(bytes memory _bytes, uint256 _start) internal pure returns (uint256) {
require(_bytes.length >= _start + 32, "toUint256_outOfBounds");
uint256 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x20), _start))
}
return tempUint;
}
function toBytes32(bytes memory _bytes, uint256 _start) internal pure returns (bytes32) {
require(_bytes.length >= _start + 32, "toBytes32_outOfBounds");
bytes32 tempBytes32;
assembly {
tempBytes32 := mload(add(add(_bytes, 0x20), _start))
}
return tempBytes32;
}
function equal(bytes memory _preBytes, bytes memory _postBytes) internal pure returns (bool) {
bool success = true;
assembly {
let length := mload(_preBytes)
// if lengths don't match the arrays are not equal
switch eq(length, mload(_postBytes))
case 1 {
// cb is a circuit breaker in the for loop since there's
// no said feature for inline assembly loops
// cb = 1 - don't breaker
// cb = 0 - break
let cb := 1
let mc := add(_preBytes, 0x20)
let end := add(mc, length)
for {
let cc := add(_postBytes, 0x20)
// the next line is the loop condition:
// while(uint256(mc < end) + cb == 2)
} eq(add(lt(mc, end), cb), 2) {
mc := add(mc, 0x20)
cc := add(cc, 0x20)
} {
// if any of these checks fails then arrays are not equal
if iszero(eq(mload(mc), mload(cc))) {
// unsuccess:
success := 0
cb := 0
}
}
}
default {
// unsuccess:
success := 0
}
}
return success;
}
function equalStorage(bytes storage _preBytes, bytes memory _postBytes) internal view returns (bool) {
bool success = true;
assembly {
// we know _preBytes_offset is 0
let fslot := sload(_preBytes.slot)
// Decode the length of the stored array like in concatStorage().
let slength := div(and(fslot, sub(mul(0x100, iszero(and(fslot, 1))), 1)), 2)
let mlength := mload(_postBytes)
// if lengths don't match the arrays are not equal
switch eq(slength, mlength)
case 1 {
// slength can contain both the length and contents of the array
// if length < 32 bytes so let's prepare for that
// v. http://solidity.readthedocs.io/en/latest/miscellaneous.html#layout-of-state-variables-in-storage
if iszero(iszero(slength)) {
switch lt(slength, 32)
case 1 {
// blank the last byte which is the length
fslot := mul(div(fslot, 0x100), 0x100)
if iszero(eq(fslot, mload(add(_postBytes, 0x20)))) {
// unsuccess:
success := 0
}
}
default {
// cb is a circuit breaker in the for loop since there's
// no said feature for inline assembly loops
// cb = 1 - don't breaker
// cb = 0 - break
let cb := 1
// get the keccak hash to get the contents of the array
mstore(0x0, _preBytes.slot)
let sc := keccak256(0x0, 0x20)
let mc := add(_postBytes, 0x20)
let end := add(mc, mlength)
// the next line is the loop condition:
// while(uint256(mc < end) + cb == 2)
for {} eq(add(lt(mc, end), cb), 2) {
sc := add(sc, 1)
mc := add(mc, 0x20)
} {
if iszero(eq(sload(sc), mload(mc))) {
// unsuccess:
success := 0
cb := 0
}
}
}
}
}
default {
// unsuccess:
success := 0
}
}
return success;
}
}
contracts/utils/FullMath.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/// @title Contains 512-bit math functions
/// @notice Facilitates multiplication and division that can have overflow of an intermediate value without any loss of precision
/// @dev Handles "phantom overflow" i.e., allows multiplication and division where an intermediate value overflows 256 bits
library FullMath {
/// @notice Calculates floor(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
/// @param a The multiplicand
/// @param b The multiplier
/// @param denominator The divisor
/// @return result The 256-bit result
/// @dev Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv
function mulDiv(uint256 a, uint256 b, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
// 512-bit multiply [prod1 prod0] = a * b
// Compute the product mod 2**256 and mod 2**256 - 1
// then 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(a, b, not(0))
prod0 := mul(a, b)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division
if (prod1 == 0) {
require(denominator > 0);
assembly {
result := div(prod0, denominator)
}
return result;
}
// Make sure the result is less than 2**256.
// Also prevents denominator == 0
require(denominator > prod1);
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [prod1 prod0]
// Compute remainder using mulmod
uint256 remainder;
assembly {
remainder := mulmod(a, b, denominator)
}
// Subtract 256 bit number from 512 bit number
assembly {
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
// Factor powers of two out of denominator
// Compute largest power of two divisor of denominator.
// Always >= 1.
// EDIT for 0.8 compatibility:
// see: https://ethereum.stackexchange.com/questions/96642/unary-operator-cannot-be-applied-to-type-uint256
uint256 twos = denominator & (~denominator + 1);
// Divide denominator by power of two
assembly {
denominator := div(denominator, twos)
}
// Divide [prod1 prod0] by the factors of two
assembly {
prod0 := div(prod0, twos)
}
// Shift in bits from prod1 into prod0. For this we need
// to flip `twos` such that it is 2**256 / twos.
// If twos is zero, then it becomes one
assembly {
twos := add(div(sub(0, twos), twos), 1)
}
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
// correct for four bits. That is, denominator * inv = 1 mod 2**4
uint256 inv = (3 * denominator) ^ 2;
// Now use 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.
inv *= 2 - denominator * inv; // inverse mod 2**8
inv *= 2 - denominator * inv; // inverse mod 2**16
inv *= 2 - denominator * inv; // inverse mod 2**32
inv *= 2 - denominator * inv; // inverse mod 2**64
inv *= 2 - denominator * inv; // inverse mod 2**128
inv *= 2 - denominator * inv; // 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 precoditions 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 * inv;
return result;
}
}
/// @notice Calculates ceil(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
/// @param a The multiplicand
/// @param b The multiplier
/// @param denominator The divisor
/// @return result The 256-bit result
function mulDivRoundingUp(uint256 a, uint256 b, uint256 denominator) internal pure returns (uint256 result) {
result = mulDiv(a, b, denominator);
if (mulmod(a, b, denominator) > 0) {
require(result < type(uint256).max);
result++;
}
}
}
contracts/utils/LiquidityAmounts.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./FullMath.sol";
/// @title FixedPoint96
/// @notice A library for handling binary fixed point numbers, see https://en.wikipedia.org/wiki/Q_(number_format)
/// @dev Used in SqrtPriceMath.sol
library FixedPoint96 {
uint8 internal constant RESOLUTION = 96;
uint256 internal constant Q96 = 0x1000000000000000000000000;
}
/// @title Liquidity amount functions
/// @notice Provides functions for computing liquidity amounts from token amounts and prices
library LiquidityAmounts {
function toUint128(uint256 x) private pure returns (uint128 y) {
require((y = uint128(x)) == x);
}
/// @notice Computes the amount of liquidity received for a given amount of token0 and price range
/// @dev Calculates amount0 * (sqrt(upper) * sqrt(lower)) / (sqrt(upper) - sqrt(lower)).
/// @param sqrtRatioAX96 A sqrt price
/// @param sqrtRatioBX96 Another sqrt price
/// @param amount0 The amount0 being sent in
/// @return liquidity The amount of returned liquidity
function getLiquidityForAmount0(uint160 sqrtRatioAX96, uint160 sqrtRatioBX96, uint256 amount0)
internal
pure
returns (uint128 liquidity)
{
if (sqrtRatioAX96 > sqrtRatioBX96) {
(sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
}
uint256 intermediate = FullMath.mulDiv(sqrtRatioAX96, sqrtRatioBX96, FixedPoint96.Q96);
return toUint128(FullMath.mulDiv(amount0, intermediate, sqrtRatioBX96 - sqrtRatioAX96));
}
/// @notice Computes the amount of liquidity received for a given amount of token1 and price range
/// @dev Calculates amount1 / (sqrt(upper) - sqrt(lower)).
/// @param sqrtRatioAX96 A sqrt price
/// @param sqrtRatioBX96 Another sqrt price
/// @param amount1 The amount1 being sent in
/// @return liquidity The amount of returned liquidity
function getLiquidityForAmount1(uint160 sqrtRatioAX96, uint160 sqrtRatioBX96, uint256 amount1)
internal
pure
returns (uint128 liquidity)
{
if (sqrtRatioAX96 > sqrtRatioBX96) {
(sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
}
return toUint128(FullMath.mulDiv(amount1, FixedPoint96.Q96, sqrtRatioBX96 - sqrtRatioAX96));
}
/// @notice Computes the maximum amount of liquidity received for a given amount of token0, token1, the current
/// pool prices and the prices at the tick boundaries
function getLiquidityForAmounts(
uint160 sqrtRatioX96,
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint256 amount0,
uint256 amount1
) internal pure returns (uint128 liquidity) {
if (sqrtRatioAX96 > sqrtRatioBX96) {
(sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
}
if (sqrtRatioX96 <= sqrtRatioAX96) {
liquidity = getLiquidityForAmount0(sqrtRatioAX96, sqrtRatioBX96, amount0);
} else if (sqrtRatioX96 < sqrtRatioBX96) {
uint128 liquidity0 = getLiquidityForAmount0(sqrtRatioX96, sqrtRatioBX96, amount0);
uint128 liquidity1 = getLiquidityForAmount1(sqrtRatioAX96, sqrtRatioX96, amount1);
liquidity = liquidity0 < liquidity1 ? liquidity0 : liquidity1;
} else {
liquidity = getLiquidityForAmount1(sqrtRatioAX96, sqrtRatioBX96, amount1);
}
}
/// @notice Computes the amount of token0 for a given amount of liquidity and a price range
/// @param sqrtRatioAX96 A sqrt price
/// @param sqrtRatioBX96 Another sqrt price
/// @param liquidity The liquidity being valued
/// @return amount0 The amount0
function getAmount0ForLiquidity(uint160 sqrtRatioAX96, uint160 sqrtRatioBX96, uint128 liquidity)
internal
pure
returns (uint256 amount0)
{
if (sqrtRatioAX96 > sqrtRatioBX96) {
(sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
}
return FullMath.mulDiv(
uint256(liquidity) << FixedPoint96.RESOLUTION, sqrtRatioBX96 - sqrtRatioAX96, sqrtRatioBX96
) / sqrtRatioAX96;
}
/// @notice Computes the amount of token1 for a given amount of liquidity and a price range
/// @param sqrtRatioAX96 A sqrt price
/// @param sqrtRatioBX96 Another sqrt price
/// @param liquidity The liquidity being valued
/// @return amount1 The amount1
function getAmount1ForLiquidity(uint160 sqrtRatioAX96, uint160 sqrtRatioBX96, uint128 liquidity)
internal
pure
returns (uint256 amount1)
{
if (sqrtRatioAX96 > sqrtRatioBX96) {
(sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
}
return FullMath.mulDiv(liquidity, sqrtRatioBX96 - sqrtRatioAX96, FixedPoint96.Q96);
}
/// @notice Computes the token0 and token1 value for a given amount of liquidity, the current
/// pool prices and the prices at the tick boundaries
function getAmountsForLiquidity(
uint160 sqrtRatioX96,
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint128 liquidity
) internal pure returns (uint256 amount0, uint256 amount1) {
if (sqrtRatioAX96 > sqrtRatioBX96) {
(sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
}
if (sqrtRatioX96 <= sqrtRatioAX96) {
amount0 = getAmount0ForLiquidity(sqrtRatioAX96, sqrtRatioBX96, liquidity);
} else if (sqrtRatioX96 < sqrtRatioBX96) {
amount0 = getAmount0ForLiquidity(sqrtRatioX96, sqrtRatioBX96, liquidity);
amount1 = getAmount1ForLiquidity(sqrtRatioAX96, sqrtRatioX96, liquidity);
} else {
amount1 = getAmount1ForLiquidity(sqrtRatioAX96, sqrtRatioBX96, liquidity);
}
}
}
contracts/utils/Path.sol
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;
import "./BytesLib.sol";
/// @title Functions for manipulating path data for multihop swaps
library Path {
using BytesLib for bytes;
/// @dev The length of the bytes encoded address
uint256 private constant ADDR_SIZE = 20;
/// @dev The length of the bytes encoded fee
uint256 private constant FEE_SIZE = 3;
/// @dev The offset of a single token address and pool fee
uint256 private constant NEXT_OFFSET = ADDR_SIZE + FEE_SIZE;
/// @dev The offset of an encoded pool key
uint256 private constant POP_OFFSET = NEXT_OFFSET + ADDR_SIZE;
/// @dev The minimum length of an encoding that contains 2 or more pools
uint256 private constant MULTIPLE_POOLS_MIN_LENGTH = POP_OFFSET + NEXT_OFFSET;
/// @notice Returns true iff the path contains two or more pools
/// @param path The encoded swap path
/// @return True if path contains two or more pools, otherwise false
function hasMultiplePools(bytes memory path) internal pure returns (bool) {
return path.length >= MULTIPLE_POOLS_MIN_LENGTH;
}
/// @notice Returns the number of pools in the path
/// @param path The encoded swap path
/// @return The number of pools in the path
function numPools(bytes memory path) internal pure returns (uint256) {
// Ignore the first token address. From then on every fee and token offset indicates a pool.
return ((path.length - ADDR_SIZE) / NEXT_OFFSET);
}
/// @notice Decodes the first pool in path
/// @param path The bytes encoded swap path
/// @return tokenA The first token of the given pool
/// @return tokenB The second token of the given pool
/// @return fee The fee level of the pool
function decodeFirstPool(bytes memory path) internal pure returns (address tokenA, address tokenB, uint24 fee) {
tokenA = path.toAddress(0);
fee = path.toUint24(ADDR_SIZE);
tokenB = path.toAddress(NEXT_OFFSET);
}
/// @notice Gets the segment corresponding to the first pool in the path
/// @param path The bytes encoded swap path
/// @return The segment containing all data necessary to target the first pool in the path
function getFirstPool(bytes memory path) internal pure returns (bytes memory) {
return path.slice(0, POP_OFFSET);
}
/// @notice Skips a token + fee element from the buffer and returns the remainder
/// @param path The swap path
/// @return The remaining token + fee elements in the path
function skipToken(bytes memory path) internal pure returns (bytes memory) {
return path.slice(NEXT_OFFSET, path.length - NEXT_OFFSET);
}
}
contracts/utils/TickMath.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
/// @title Math library for computing sqrt prices from ticks and vice versa
/// @notice Computes sqrt price for ticks of size 1.0001, i.e. sqrt(1.0001^tick) as fixed point Q64.96 numbers. Supports
/// prices between 2**-128 and 2**128
library TickMath {
/// @dev The minimum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**-128
int24 internal constant MIN_TICK = -887272;
/// @dev The maximum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**128
int24 internal constant MAX_TICK = -MIN_TICK;
/// @dev The minimum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MIN_TICK)
uint160 internal constant MIN_SQRT_RATIO = 4295128739;
/// @dev The maximum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MAX_TICK)
uint160 internal constant MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342;
/// @notice Calculates sqrt(1.0001^tick) * 2^96
/// @dev Throws if |tick| > max tick
/// @param tick The input tick for the above formula
/// @return sqrtP A Fixed point Q64.96 number representing the sqrt of the ratio of the two assets (token1/token0)
/// at the given tick
function getSqrtRatioAtTick(int24 tick) internal pure returns (uint160 sqrtP) {
unchecked {
uint256 absTick = uint256(tick < 0 ? -int256(tick) : int256(tick));
require(absTick <= uint256(int256(MAX_TICK)), "T");
// do bitwise comparison, if i-th bit is turned on,
// multiply ratio by hardcoded values of sqrt(1.0001^-(2^i)) * 2^128
// where 0 <= i <= 19
uint256 ratio =
(absTick & 0x1 != 0) ? 0xfffcb933bd6fad37aa2d162d1a594001 : 0x100000000000000000000000000000000;
if (absTick & 0x2 != 0) ratio = (ratio * 0xfff97272373d413259a46990580e213a) >> 128;
if (absTick & 0x4 != 0) ratio = (ratio * 0xfff2e50f5f656932ef12357cf3c7fdcc) >> 128;
if (absTick & 0x8 != 0) ratio = (ratio * 0xffe5caca7e10e4e61c3624eaa0941cd0) >> 128;
if (absTick & 0x10 != 0) ratio = (ratio * 0xffcb9843d60f6159c9db58835c926644) >> 128;
if (absTick & 0x20 != 0) ratio = (ratio * 0xff973b41fa98c081472e6896dfb254c0) >> 128;
if (absTick & 0x40 != 0) ratio = (ratio * 0xff2ea16466c96a3843ec78b326b52861) >> 128;
if (absTick & 0x80 != 0) ratio = (ratio * 0xfe5dee046a99a2a811c461f1969c3053) >> 128;
if (absTick & 0x100 != 0) ratio = (ratio * 0xfcbe86c7900a88aedcffc83b479aa3a4) >> 128;
if (absTick & 0x200 != 0) ratio = (ratio * 0xf987a7253ac413176f2b074cf7815e54) >> 128;
if (absTick & 0x400 != 0) ratio = (ratio * 0xf3392b0822b70005940c7a398e4b70f3) >> 128;
if (absTick & 0x800 != 0) ratio = (ratio * 0xe7159475a2c29b7443b29c7fa6e889d9) >> 128;
if (absTick & 0x1000 != 0) ratio = (ratio * 0xd097f3bdfd2022b8845ad8f792aa5825) >> 128;
if (absTick & 0x2000 != 0) ratio = (ratio * 0xa9f746462d870fdf8a65dc1f90e061e5) >> 128;
if (absTick & 0x4000 != 0) ratio = (ratio * 0x70d869a156d2a1b890bb3df62baf32f7) >> 128;
if (absTick & 0x8000 != 0) ratio = (ratio * 0x31be135f97d08fd981231505542fcfa6) >> 128;
if (absTick & 0x10000 != 0) ratio = (ratio * 0x9aa508b5b7a84e1c677de54f3e99bc9) >> 128;
if (absTick & 0x20000 != 0) ratio = (ratio * 0x5d6af8dedb81196699c329225ee604) >> 128;
if (absTick & 0x40000 != 0) ratio = (ratio * 0x2216e584f5fa1ea926041bedfe98) >> 128;
if (absTick & 0x80000 != 0) ratio = (ratio * 0x48a170391f7dc42444e8fa2) >> 128;
// take reciprocal for positive tick values
if (tick > 0) ratio = type(uint256).max / ratio;
// this divides by 1<<32 rounding up to go from a Q128.128 to a Q128.96.
// we then downcast because we know the result always fits within 160 bits due to our tick input constraint
// we round up in the division so getTickAtSqrtRatio of the output price is always consistent
sqrtP = uint160((ratio >> 32) + (ratio % (1 << 32) == 0 ? 0 : 1));
}
}
}
contracts/utils/TickUtils.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./Path.sol";
import "./TickMath.sol";
import "./LiquidityAmounts.sol";
library TickUtils {
using Path for bytes;
using TickMath for int24;
function floor(int24 tick, int24 tickSpacing) internal pure returns (int24) {
int24 compressed = tick / tickSpacing;
if (tick < 0 && tick % tickSpacing != 0) compressed--;
return compressed * tickSpacing;
}
/// @dev Calc base ticks depending on base threshold and tickspacing
function baseTicks(int24 currentTick, int24 baseThreshold, int24 tickSpacing)
internal
pure
returns (int24 tickLower, int24 tickUpper)
{
int24 tickFloor = floor(currentTick, tickSpacing);
tickLower = tickFloor - baseThreshold;
tickUpper = tickFloor + baseThreshold;
}
function quoteAddLiquidity(int24 _currentTick, int24 _lowerTick, int24 _upperTick, uint256 _amt0, uint256 _amt1)
internal
pure
returns (uint256 _actualAmount0, uint256 _actualAmount1, uint256 _liquidity)
{
// Grab the amount of liquidity for our token balances
_liquidity = LiquidityAmounts.getLiquidityForAmounts(
_currentTick.getSqrtRatioAtTick(),
_lowerTick.getSqrtRatioAtTick(),
_upperTick.getSqrtRatioAtTick(),
_amt0,
_amt1
);
(_actualAmount0, _actualAmount1) = LiquidityAmounts.getAmountsForLiquidity(
_currentTick.getSqrtRatioAtTick(),
_lowerTick.getSqrtRatioAtTick(),
_upperTick.getSqrtRatioAtTick(),
uint128(_liquidity)
);
}
function getQuoteAtTick(int24 tick, uint128 baseAmount, address baseToken, address quoteToken)
internal
pure
returns (uint256 quoteAmount)
{
uint160 sqrtRatioX96 = TickMath.getSqrtRatioAtTick(tick);
// Calculate quoteAmount with better precision if it doesn't overflow when multiplied by itself
if (sqrtRatioX96 <= type(uint128).max) {
uint256 ratioX192 = uint256(sqrtRatioX96) * sqrtRatioX96;
quoteAmount = baseToken < quoteToken
? FullMath.mulDiv(ratioX192, baseAmount, 1 << 192)
: FullMath.mulDiv(1 << 192, baseAmount, ratioX192);
} else {
uint256 ratioX128 = FullMath.mulDiv(sqrtRatioX96, sqrtRatioX96, 1 << 64);
quoteAmount = baseToken < quoteToken
? FullMath.mulDiv(ratioX128, baseAmount, 1 << 128)
: FullMath.mulDiv(1 << 128, baseAmount, ratioX128);
}
}
// Convert encoded path to token route
function pathToRoute(bytes memory _path) internal pure returns (address[] memory, uint24[] memory) {
uint256 numPools = _path.numPools();
address[] memory route = new address[](numPools + 1);
uint24[] memory fees = new uint24[](numPools);
for (uint256 i; i < numPools; i++) {
(address tokenA, address tokenB, uint24 fee) = _path.decodeFirstPool();
route[i] = tokenA;
route[i + 1] = tokenB;
fees[i] = fee;
_path = _path.skipToken();
}
return (route, fees);
}
// Convert token route to encoded path
// uint24 type for fees so path is packed tightly
function routeToPath(address[] memory _route, uint24[] memory _fee) internal pure returns (bytes memory path) {
path = abi.encodePacked(_route[0]);
uint256 feeLength = _fee.length;
for (uint256 i = 0; i < feeLength; i++) {
path = abi.encodePacked(path, _fee[i], _route[i + 1]);
}
}
}
dependencies/@openzeppelin-contracts-5.0.2/token/ERC20/IERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.20;
/**
* @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 value of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the value of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves a `value` amount of 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 value) 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 a `value` amount of tokens 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 value) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the
* allowance mechanism. `value` 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 value) external returns (bool);
}
dependencies/@openzeppelin-contracts-5.0.2/token/ERC20/extensions/IERC20Metadata.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/extensions/IERC20Metadata.sol)
pragma solidity ^0.8.20;
import {IERC20} from "../IERC20.sol";
/**
* @dev Interface for the optional metadata functions from the ERC20 standard.
*/
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);
}
dependencies/@openzeppelin-contracts-5.0.2/token/ERC20/extensions/IERC20Permit.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/extensions/IERC20Permit.sol)
pragma solidity ^0.8.20;
/**
* @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.
*
* ==== Security Considerations
*
* There are two important considerations concerning the use of `permit`. The first is that a valid permit signature
* expresses an allowance, and it should not be assumed to convey additional meaning. In particular, it should not be
* considered as an intention to spend the allowance in any specific way. The second is that because permits have
* built-in replay protection and can be submitted by anyone, they can be frontrun. A protocol that uses permits should
* take this into consideration and allow a `permit` call to fail. Combining these two aspects, a pattern that may be
* generally recommended is:
*
* ```solidity
* function doThingWithPermit(..., uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public {
* try token.permit(msg.sender, address(this), value, deadline, v, r, s) {} catch {}
* doThing(..., value);
* }
*
* function doThing(..., uint256 value) public {
* token.safeTransferFrom(msg.sender, address(this), value);
* ...
* }
* ```
*
* Observe that: 1) `msg.sender` is used as the owner, leaving no ambiguity as to the signer intent, and 2) the use of
* `try/catch` allows the permit to fail and makes the code tolerant to frontrunning. (See also
* {SafeERC20-safeTransferFrom}).
*
* Additionally, note that smart contract wallets (such as Argent or Safe) are not able to produce permit signatures, so
* contracts should have entry points that don't rely on permit.
*/
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].
*
* CAUTION: See Security Considerations above.
*/
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);
}
dependencies/@openzeppelin-contracts-5.0.2/token/ERC20/utils/SafeERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/utils/SafeERC20.sol)
pragma solidity ^0.8.20;
import {IERC20} from "../IERC20.sol";
import {IERC20Permit} from "../extensions/IERC20Permit.sol";
import {Address} from "../../../utils/Address.sol";
/**
* @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;
/**
* @dev An operation with an ERC20 token failed.
*/
error SafeERC20FailedOperation(address token);
/**
* @dev Indicates a failed `decreaseAllowance` request.
*/
error SafeERC20FailedDecreaseAllowance(address spender, uint256 currentAllowance, uint256 requestedDecrease);
/**
* @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeCall(token.transfer, (to, value)));
}
/**
* @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the
* calling contract. If `token` returns no value, non-reverting calls are assumed to be successful.
*/
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeCall(token.transferFrom, (from, to, value)));
}
/**
* @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 oldAllowance = token.allowance(address(this), spender);
forceApprove(token, spender, oldAllowance + value);
}
/**
* @dev Decrease the calling contract's allowance toward `spender` by `requestedDecrease`. If `token` returns no
* value, non-reverting calls are assumed to be successful.
*/
function safeDecreaseAllowance(IERC20 token, address spender, uint256 requestedDecrease) internal {
unchecked {
uint256 currentAllowance = token.allowance(address(this), spender);
if (currentAllowance < requestedDecrease) {
revert SafeERC20FailedDecreaseAllowance(spender, currentAllowance, requestedDecrease);
}
forceApprove(token, spender, currentAllowance - requestedDecrease);
}
}
/**
* @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful. Meant to be used with tokens that require the approval
* to be set to zero before setting it to a non-zero value, such as USDT.
*/
function forceApprove(IERC20 token, address spender, uint256 value) internal {
bytes memory approvalCall = abi.encodeCall(token.approve, (spender, value));
if (!_callOptionalReturnBool(token, approvalCall)) {
_callOptionalReturn(token, abi.encodeCall(token.approve, (spender, 0)));
_callOptionalReturn(token, approvalCall);
}
}
/**
* @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);
if (returndata.length != 0 && !abi.decode(returndata, (bool))) {
revert SafeERC20FailedOperation(address(token));
}
}
/**
* @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).
*
* This is a variant of {_callOptionalReturn} that silents catches all reverts and returns a bool instead.
*/
function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
// 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 cannot use {Address-functionCall} here since this should return false
// and not revert is the subcall reverts.
(bool success, bytes memory returndata) = address(token).call(data);
return success && (returndata.length == 0 || abi.decode(returndata, (bool))) && address(token).code.length > 0;
}
}
dependencies/@openzeppelin-contracts-5.0.2/token/ERC721/IERC721.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC721/IERC721.sol)
pragma solidity ^0.8.20;
import {IERC165} from "../../utils/introspection/IERC165.sol";
/**
* @dev Required interface of an ERC721 compliant contract.
*/
interface IERC721 is IERC165 {
/**
* @dev Emitted when `tokenId` token is transferred from `from` to `to`.
*/
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
*/
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
*/
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
/**
* @dev Returns the number of tokens in ``owner``'s account.
*/
function balanceOf(address owner) external view returns (uint256 balance);
/**
* @dev Returns the owner of the `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function ownerOf(uint256 tokenId) external view returns (address owner);
/**
* @dev Safely transfers `tokenId` token from `from` to `to`.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon
* a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
/**
* @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
* are aware of the ERC721 protocol to prevent tokens from being forever locked.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must have been allowed to move this token by either {approve} or
* {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon
* a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(address from, address to, uint256 tokenId) external;
/**
* @dev Transfers `tokenId` token from `from` to `to`.
*
* WARNING: Note that the caller is responsible to confirm that the recipient is capable of receiving ERC721
* or else they may be permanently lost. Usage of {safeTransferFrom} prevents loss, though the caller must
* understand this adds an external call which potentially creates a reentrancy vulnerability.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 tokenId) external;
/**
* @dev Gives permission to `to` to transfer `tokenId` token to another account.
* The approval is cleared when the token is transferred.
*
* Only a single account can be approved at a time, so approving the zero address clears previous approvals.
*
* Requirements:
*
* - The caller must own the token or be an approved operator.
* - `tokenId` must exist.
*
* Emits an {Approval} event.
*/
function approve(address to, uint256 tokenId) external;
/**
* @dev Approve or remove `operator` as an operator for the caller.
* Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
*
* Requirements:
*
* - The `operator` cannot be the address zero.
*
* Emits an {ApprovalForAll} event.
*/
function setApprovalForAll(address operator, bool approved) external;
/**
* @dev Returns the account approved for `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function getApproved(uint256 tokenId) external view returns (address operator);
/**
* @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
*
* See {setApprovalForAll}
*/
function isApprovedForAll(address owner, address operator) external view returns (bool);
}
dependencies/@openzeppelin-contracts-5.0.2/utils/Address.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/Address.sol)
pragma solidity ^0.8.20;
/**
* @dev Collection of functions related to the address type
*/
library Address {
/**
* @dev The ETH balance of the account is not enough to perform the operation.
*/
error AddressInsufficientBalance(address account);
/**
* @dev There's no code at `target` (it is not a contract).
*/
error AddressEmptyCode(address target);
/**
* @dev A call to an address target failed. The target may have reverted.
*/
error FailedInnerCall();
/**
* @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.8.20/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
if (address(this).balance < amount) {
revert AddressInsufficientBalance(address(this));
}
(bool success, ) = recipient.call{value: amount}("");
if (!success) {
revert FailedInnerCall();
}
}
/**
* @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 or custom error, it is bubbled
* up by this function (like regular Solidity function calls). However, if
* the call reverted with no returned reason, this function reverts with a
* {FailedInnerCall} error.
*
* 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.
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0);
}
/**
* @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`.
*/
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
if (address(this).balance < value) {
revert AddressInsufficientBalance(address(this));
}
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, success, returndata);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, success, returndata);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*/
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata);
}
/**
* @dev Tool to verify that a low level call to smart-contract was successful, and reverts if the target
* was not a contract or bubbling up the revert reason (falling back to {FailedInnerCall}) in case of an
* unsuccessful call.
*/
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata
) internal view returns (bytes memory) {
if (!success) {
_revert(returndata);
} else {
// only check if target is a contract if the call was successful and the return data is empty
// otherwise we already know that it was a contract
if (returndata.length == 0 && target.code.length == 0) {
revert AddressEmptyCode(target);
}
return returndata;
}
}
/**
* @dev Tool to verify that a low level call was successful, and reverts if it wasn't, either by bubbling the
* revert reason or with a default {FailedInnerCall} error.
*/
function verifyCallResult(bool success, bytes memory returndata) internal pure returns (bytes memory) {
if (!success) {
_revert(returndata);
} else {
return returndata;
}
}
/**
* @dev Reverts with returndata if present. Otherwise reverts with {FailedInnerCall}.
*/
function _revert(bytes memory returndata) 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 FailedInnerCall();
}
}
}
dependencies/@openzeppelin-contracts-5.0.2/utils/introspection/IERC165.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/introspection/IERC165.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[EIP].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
dependencies/@openzeppelin-contracts-5.0.2/utils/math/SignedMath.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/math/SignedMath.sol)
pragma solidity ^0.8.20;
/**
* @dev Standard signed math utilities missing in the Solidity language.
*/
library SignedMath {
/**
* @dev Returns the largest of two signed numbers.
*/
function max(int256 a, int256 b) internal pure returns (int256) {
return a > b ? a : b;
}
/**
* @dev Returns the smallest of two signed numbers.
*/
function min(int256 a, int256 b) internal pure returns (int256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two signed numbers without overflow.
* The result is rounded towards zero.
*/
function average(int256 a, int256 b) internal pure returns (int256) {
// Formula from the book "Hacker's Delight"
int256 x = (a & b) + ((a ^ b) >> 1);
return x + (int256(uint256(x) >> 255) & (a ^ b));
}
/**
* @dev Returns the absolute unsigned value of a signed value.
*/
function abs(int256 n) internal pure returns (uint256) {
unchecked {
// must be unchecked in order to support `n = type(int256).min`
return uint256(n >= 0 ? n : -n);
}
}
}
dependencies/@openzeppelin-contracts-upgradeable-4.9.6/access/OwnableUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (access/Ownable.sol)
pragma solidity ^0.8.0;
import "../utils/ContextUpgradeable.sol";
import {Initializable} from "../proxy/utils/Initializable.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract OwnableUpgradeable is Initializable, ContextUpgradeable {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
function __Ownable_init() internal onlyInitializing {
__Ownable_init_unchained();
}
function __Ownable_init_unchained() internal onlyInitializing {
_transferOwnership(_msgSender());
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[49] private __gap;
}
dependencies/@openzeppelin-contracts-upgradeable-4.9.6/proxy/utils/Initializable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (proxy/utils/Initializable.sol)
pragma solidity ^0.8.2;
import "../../utils/AddressUpgradeable.sol";
/**
* @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
* behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
* external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
* function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
*
* The initialization functions use a version number. Once a version number is used, it is consumed and cannot be
* reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in
* case an upgrade adds a module that needs to be initialized.
*
* For example:
*
* [.hljs-theme-light.nopadding]
* ```solidity
* contract MyToken is ERC20Upgradeable {
* function initialize() initializer public {
* __ERC20_init("MyToken", "MTK");
* }
* }
*
* contract MyTokenV2 is MyToken, ERC20PermitUpgradeable {
* function initializeV2() reinitializer(2) public {
* __ERC20Permit_init("MyToken");
* }
* }
* ```
*
* TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
* possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
*
* CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
* that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
*
* [CAUTION]
* ====
* Avoid leaving a contract uninitialized.
*
* An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
* contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke
* the {_disableInitializers} function in the constructor to automatically lock it when it is deployed:
*
* [.hljs-theme-light.nopadding]
* ```
* /// @custom:oz-upgrades-unsafe-allow constructor
* constructor() {
* _disableInitializers();
* }
* ```
* ====
*/
abstract contract Initializable {
/**
* @dev Indicates that the contract has been initialized.
* @custom:oz-retyped-from bool
*/
uint8 private _initialized;
/**
* @dev Indicates that the contract is in the process of being initialized.
*/
bool private _initializing;
/**
* @dev Triggered when the contract has been initialized or reinitialized.
*/
event Initialized(uint8 version);
/**
* @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope,
* `onlyInitializing` functions can be used to initialize parent contracts.
*
* Similar to `reinitializer(1)`, except that functions marked with `initializer` can be nested in the context of a
* constructor.
*
* Emits an {Initialized} event.
*/
modifier initializer() {
bool isTopLevelCall = !_initializing;
require(
(isTopLevelCall && _initialized < 1) || (!AddressUpgradeable.isContract(address(this)) && _initialized == 1),
"Initializable: contract is already initialized"
);
_initialized = 1;
if (isTopLevelCall) {
_initializing = true;
}
_;
if (isTopLevelCall) {
_initializing = false;
emit Initialized(1);
}
}
/**
* @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the
* contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be
* used to initialize parent contracts.
*
* A reinitializer may be used after the original initialization step. This is essential to configure modules that
* are added through upgrades and that require initialization.
*
* When `version` is 1, this modifier is similar to `initializer`, except that functions marked with `reinitializer`
* cannot be nested. If one is invoked in the context of another, execution will revert.
*
* Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in
* a contract, executing them in the right order is up to the developer or operator.
*
* WARNING: setting the version to 255 will prevent any future reinitialization.
*
* Emits an {Initialized} event.
*/
modifier reinitializer(uint8 version) {
require(!_initializing && _initialized < version, "Initializable: contract is already initialized");
_initialized = version;
_initializing = true;
_;
_initializing = false;
emit Initialized(version);
}
/**
* @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
* {initializer} and {reinitializer} modifiers, directly or indirectly.
*/
modifier onlyInitializing() {
require(_initializing, "Initializable: contract is not initializing");
_;
}
/**
* @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call.
* Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized
* to any version. It is recommended to use this to lock implementation contracts that are designed to be called
* through proxies.
*
* Emits an {Initialized} event the first time it is successfully executed.
*/
function _disableInitializers() internal virtual {
require(!_initializing, "Initializable: contract is initializing");
if (_initialized != type(uint8).max) {
_initialized = type(uint8).max;
emit Initialized(type(uint8).max);
}
}
/**
* @dev Returns the highest version that has been initialized. See {reinitializer}.
*/
function _getInitializedVersion() internal view returns (uint8) {
return _initialized;
}
/**
* @dev Returns `true` if the contract is currently initializing. See {onlyInitializing}.
*/
function _isInitializing() internal view returns (bool) {
return _initializing;
}
}
dependencies/@openzeppelin-contracts-upgradeable-4.9.6/security/PausableUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (security/Pausable.sol)
pragma solidity ^0.8.0;
import "../utils/ContextUpgradeable.sol";
import {Initializable} from "../proxy/utils/Initializable.sol";
/**
* @dev Contract module which allows children to implement an emergency stop
* mechanism that can be triggered by an authorized account.
*
* This module is used through inheritance. It will make available the
* modifiers `whenNotPaused` and `whenPaused`, which can be applied to
* the functions of your contract. Note that they will not be pausable by
* simply including this module, only once the modifiers are put in place.
*/
abstract contract PausableUpgradeable is Initializable, ContextUpgradeable {
/**
* @dev Emitted when the pause is triggered by `account`.
*/
event Paused(address account);
/**
* @dev Emitted when the pause is lifted by `account`.
*/
event Unpaused(address account);
bool private _paused;
/**
* @dev Initializes the contract in unpaused state.
*/
function __Pausable_init() internal onlyInitializing {
__Pausable_init_unchained();
}
function __Pausable_init_unchained() internal onlyInitializing {
_paused = false;
}
/**
* @dev Modifier to make a function callable only when the contract is not paused.
*
* Requirements:
*
* - The contract must not be paused.
*/
modifier whenNotPaused() {
_requireNotPaused();
_;
}
/**
* @dev Modifier to make a function callable only when the contract is paused.
*
* Requirements:
*
* - The contract must be paused.
*/
modifier whenPaused() {
_requirePaused();
_;
}
/**
* @dev Returns true if the contract is paused, and false otherwise.
*/
function paused() public view virtual returns (bool) {
return _paused;
}
/**
* @dev Throws if the contract is paused.
*/
function _requireNotPaused() internal view virtual {
require(!paused(), "Pausable: paused");
}
/**
* @dev Throws if the contract is not paused.
*/
function _requirePaused() internal view virtual {
require(paused(), "Pausable: not paused");
}
/**
* @dev Triggers stopped state.
*
* Requirements:
*
* - The contract must not be paused.
*/
function _pause() internal virtual whenNotPaused {
_paused = true;
emit Paused(_msgSender());
}
/**
* @dev Returns to normal state.
*
* Requirements:
*
* - The contract must be paused.
*/
function _unpause() internal virtual whenPaused {
_paused = false;
emit Unpaused(_msgSender());
}
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[49] private __gap;
}
dependencies/@openzeppelin-contracts-upgradeable-4.9.6/utils/AddressUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/Address.sol)
pragma solidity ^0.8.1;
/**
* @dev Collection of functions related to the address type
*/
library AddressUpgradeable {
/**
* @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.8.0/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);
}
}
}
dependencies/@openzeppelin-contracts-upgradeable-4.9.6/utils/ContextUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.4) (utils/Context.sol)
pragma solidity ^0.8.0;
import {Initializable} from "../proxy/utils/Initializable.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 ContextUpgradeable is Initializable {
function __Context_init() internal onlyInitializing {
}
function __Context_init_unchained() internal onlyInitializing {
}
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[50] private __gap;
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"@openzeppelin-5/contracts/=dependencies/@openzeppelin-contracts-5.0.2/",
"@openzeppelin-4/contracts-upgradeable/=dependencies/@openzeppelin-contracts-upgradeable-4.9.6/",
"forge-std/=dependencies/forge-std-1.15.0/src/",
"harvest-manager/=contracts/strategies/harvest-manager/public/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.