Description:
Multi-signature wallet contract requiring multiple confirmations for transaction execution.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
{{
"language": "Solidity",
"sources": {
"src/integrations/curve/CurveStrategy.sol": {
"content": "// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.28;
import {ILiquidityGauge} from "@interfaces/curve/ILiquidityGauge.sol";
import {IMinter} from "@interfaces/curve/IMinter.sol";
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import "src/Strategy.sol";
/// @title CurveStrategy.
/// @author Stake DAO
/// @custom:github @stake-dao
/// @custom:contact contact@stakedao.org
/// @notice CurveStrategy is a specialized implementation for interacting with Curve protocol gauges.
/// It extends the base Strategy contract with Curve-specific functionality to sync and track
/// pending rewards from Curve gauges and Sidecar contracts, handle deposits and withdrawals
/// through Curve liquidity gauges, and execute transactions via the gateway pattern.
contract CurveStrategy is Strategy {
using SafeCast for uint256;
//////////////////////////////////////////////////////
// --- CONSTANTS & IMMUTABLES
//////////////////////////////////////////////////////
/// @notice The address of the Curve Minter contract
/// @dev Used to account for CRV tokens from gauge rewards
address public immutable MINTER;
/// @notice The bytes4 ID of the Curve protocol
/// @dev Used to identify the Curve protocol in the registry
bytes4 private constant CURVE_PROTOCOL_ID = bytes4(keccak256("CURVE"));
/// @notice Error thrown when the mint fails.
error MintFailed();
/// @notice Error thrown when the checkpoint fails.
error CheckpointFailed();
/// @notice Error thrown when the extra rewards claim fails.
error ClaimExtraRewards();
/// @notice Error thrown when the reward receiver is not set.
error NoRewardReceiverSet();
//////////////////////////////////////////////////////
// --- CONSTRUCTOR
//////////////////////////////////////////////////////
/// @notice Initializes the CurveStrategy contract
/// @param _registry The address of the protocol controller registry
/// @param _locker The address of the locker contract
/// @param _gateway The address of the gateway contract
constructor(address _registry, address _locker, address _gateway, address _minter)
Strategy(_registry, CURVE_PROTOCOL_ID, _locker, _gateway)
{
MINTER = _minter;
}
//////////////////////////////////////////////////////
// --- INTERNAL FUNCTIONS
//////////////////////////////////////////////////////
/// @notice Syncs and calculates pending rewards from a Curve gauge
/// @dev Retrieves allocation targets and calculates pending rewards for each target
/// @param gauge The address of the Curve gauge to sync
/// @return pendingRewards A struct containing the total and fee subject pending rewards
function _checkpointRewards(address gauge) internal override returns (PendingRewards memory pendingRewards) {
address allocator = PROTOCOL_CONTROLLER.allocator(PROTOCOL_ID);
address[] memory targets = IAllocator(allocator).getAllocationTargets(gauge);
/// @dev Checkpoint the locker
require(
_executeTransaction(gauge, abi.encodeWithSignature("user_checkpoint(address)", LOCKER)), CheckpointFailed()
);
uint256 pendingRewardsAmount;
for (uint256 i = 0; i < targets.length; i++) {
address target = targets[i];
if (target == LOCKER) {
// Calculate pending rewards for the locker by comparing total earned by gauge with already minted tokens
pendingRewardsAmount =
ILiquidityGauge(gauge).integrate_fraction(LOCKER) - IMinter(MINTER).minted(LOCKER, gauge);
pendingRewards.feeSubjectAmount += pendingRewardsAmount.toUint128();
} else {
// For sidecar contracts, use their getPendingRewards() function
pendingRewardsAmount = ISidecar(target).getPendingRewards();
}
pendingRewards.totalAmount += pendingRewardsAmount.toUint128();
}
}
/// @notice Deposits tokens into a Curve gauge
/// @dev Executes a deposit transaction through the gateway/module manager
/// @param gauge The address of the Curve gauge to deposit into
/// @param amount The amount of tokens to deposit
function _deposit(address, address gauge, uint256 amount) internal override {
bytes memory data = abi.encodeWithSignature("deposit(uint256)", amount);
require(_executeTransaction(gauge, data), DepositFailed());
}
/// @notice Withdraws tokens from a Curve gauge
/// @dev Executes a withdraw transaction through the gateway/module manager
/// @param gauge The address of the Curve gauge to withdraw from
/// @param amount The amount of tokens to withdraw
/// @param receiver The address that will receive the withdrawn tokens
function _withdraw(address asset, address gauge, uint256 amount, address receiver) internal override {
bytes memory data = abi.encodeWithSignature("withdraw(uint256)", amount);
require(_executeTransaction(gauge, data), WithdrawFailed());
// 2. Transfer the LP tokens to receiver
data = abi.encodeWithSignature("transfer(address,uint256)", receiver, amount);
require(_executeTransaction(asset, data), TransferFailed());
}
/// @notice Harvests rewards from a Curve gauge
/// @param gauge The address of the Curve gauge to harvest from
function _harvestLocker(address gauge, bytes memory extraData) internal override returns (uint256 rewardAmount) {
/// 1. Snapshot the balance before minting.
uint256 _before = IERC20(REWARD_TOKEN).balanceOf(address(LOCKER));
/// @dev Locker is deployed on mainnet.
/// @dev If the locker is the gateway, we need to mint the rewards via the gateway
/// as it means the strategy is deployed on sidechain.
if (LOCKER != GATEWAY) {
/// 2. Mint the rewards of the gauge to the locker.
IMinter(MINTER).mint_for(gauge, address(LOCKER));
} else {
/// 2. Mint the rewards of the gauge to the locker via the gateway.
bytes memory data = abi.encodeWithSignature("mint(address)", gauge);
require(_executeTransaction(MINTER, data), MintFailed());
}
/// 3. Calculate the reward amount.
rewardAmount = IERC20(REWARD_TOKEN).balanceOf(address(LOCKER)) - _before;
/// @dev If there are extra rewards, claim them.
if (extraData.length > 0) {
_claimExtraRewards(gauge);
}
}
/// @notice Claims extra rewards from a Curve gauge
/// @dev This function is called after the main rewards have been claimed
function _claimExtraRewards(address gauge) internal {
address rewardReceiver = PROTOCOL_CONTROLLER.rewardReceiver(gauge);
require(rewardReceiver != address(0), NoRewardReceiverSet());
bytes memory data = abi.encodeWithSignature("claim_rewards(address,address)", address(LOCKER), address(rewardReceiver));
require(_executeTransaction(gauge, data), ClaimExtraRewards());
}
/// @notice Claims extra rewards from a Curve gauge
function claimExtraRewards(address gauge) external {
_claimExtraRewards(gauge);
}
}"
},
"node_modules/@stake-dao/interfaces/src/interfaces/curve/ILiquidityGauge.sol": {
"content": "// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.0;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
interface IL2LiquidityGauge {
function reward_data(address arg0)
external
view
returns (address distributor, uint256 period_finish, uint256 rate, uint256 last_update, uint256 integral);
function reward_tokens(uint256 arg0) external view returns (address);
function is_killed() external view returns (bool);
function lp_token() external view returns (address);
}
interface ILiquidityGauge is IERC20 {
event ApplyOwnership(address admin);
event CommitOwnership(address admin);
event Deposit(address indexed provider, uint256 value);
event UpdateLiquidityLimit(
address user, uint256 original_balance, uint256 original_supply, uint256 working_balance, uint256 working_supply
);
event Withdraw(address indexed provider, uint256 value);
function add_reward(address _reward_token, address _distributor) external;
function approve(address _spender, uint256 _value) external returns (bool);
function claim_rewards() external;
function claim_rewards(address _addr) external;
function claim_rewards(address _addr, address _receiver) external;
function claimable_tokens(address addr) external returns (uint256);
function decreaseAllowance(address _spender, uint256 _subtracted_value) external returns (bool);
function deposit(uint256 _value) external;
function deposit(uint256 _value, address _addr) external;
function deposit(uint256 _value, address _addr, bool _claim_rewards) external;
function deposit_reward_token(address _reward_token, uint256 _amount) external;
function increaseAllowance(address _spender, uint256 _added_value) external returns (bool);
function initialize(address _lp_token) external;
function kick(address addr) external;
function set_killed(bool _is_killed) external;
function set_reward_distributor(address _reward_token, address _distributor) external;
function set_rewards_receiver(address _receiver) external;
function transfer(address _to, uint256 _value) external returns (bool);
function transferFrom(address _from, address _to, uint256 _value) external returns (bool);
function user_checkpoint(address addr) external returns (bool);
function withdraw(uint256 _value) external;
function withdraw(uint256 _value, bool _claim_rewards) external;
function allowance(address arg0, address arg1) external view returns (uint256);
function balanceOf(address arg0) external view returns (uint256);
function claimable_reward(address _user, address _reward_token) external view returns (uint256);
function claimed_reward(address _addr, address _token) external view returns (uint256);
function decimals() external view returns (uint256);
function factory() external view returns (address);
function future_epoch_time() external view returns (uint256);
function inflation_rate() external view returns (uint256);
function integrate_checkpoint() external view returns (uint256);
function integrate_checkpoint_of(address arg0) external view returns (uint256);
function integrate_fraction(address arg0) external view returns (uint256);
function integrate_inv_supply(uint256 arg0) external view returns (uint256);
function integrate_inv_supply_of(address arg0) external view returns (uint256);
function is_killed() external view returns (bool);
function lp_token() external view returns (address);
function name() external view returns (string memory);
function period() external view returns (int128);
function period_timestamp(uint256 arg0) external view returns (uint256);
function reward_count() external view returns (uint256);
function reward_data(address arg0)
external
view
returns (
address token,
address distributor,
uint256 period_finish,
uint256 rate,
uint256 last_update,
uint256 integral
);
function reward_integral_for(address arg0, address arg1) external view returns (uint256);
function reward_tokens(uint256 arg0) external view returns (address);
function rewards_receiver(address arg0) external view returns (address);
function symbol() external view returns (string memory);
function totalSupply() external view returns (uint256);
function working_balances(address arg0) external view returns (uint256);
function working_supply() external view returns (uint256);
function admin() external view returns (address);
}
"
},
"node_modules/@stake-dao/interfaces/src/interfaces/curve/IMinter.sol": {
"content": "/// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.19;
interface IMinter {
function mint_for(address gauge, address account) external;
function minted(address gauge, address account) external view returns (uint256);
}
"
},
"node_modules/@openzeppelin/contracts/utils/math/SafeCast.sol": {
"content": "// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/math/SafeCast.sol)
// This file was procedurally generated from scripts/generate/templates/SafeCast.js.
pragma solidity ^0.8.20;
/**
* @dev Wrappers over Solidity's uintXX/intXX/bool casting operators with added overflow
* checks.
*
* Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
* easily result in undesired exploitation or bugs, since developers usually
* assume that overflows raise errors. `SafeCast` restores this intuition by
* reverting the transaction when such an operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeCast {
/**
* @dev Value doesn't fit in an uint of `bits` size.
*/
error SafeCastOverflowedUintDowncast(uint8 bits, uint256 value);
/**
* @dev An int value doesn't fit in an uint of `bits` size.
*/
error SafeCastOverflowedIntToUint(int256 value);
/**
* @dev Value doesn't fit in an int of `bits` size.
*/
error SafeCastOverflowedIntDowncast(uint8 bits, int256 value);
/**
* @dev An uint value doesn't fit in an int of `bits` size.
*/
error SafeCastOverflowedUintToInt(uint256 value);
/**
* @dev Returns the downcasted uint248 from uint256, reverting on
* overflow (when the input is greater than largest uint248).
*
* Counterpart to Solidity's `uint248` operator.
*
* Requirements:
*
* - input must fit into 248 bits
*/
function toUint248(uint256 value) internal pure returns (uint248) {
if (value > type(uint248).max) {
revert SafeCastOverflowedUintDowncast(248, value);
}
return uint248(value);
}
/**
* @dev Returns the downcasted uint240 from uint256, reverting on
* overflow (when the input is greater than largest uint240).
*
* Counterpart to Solidity's `uint240` operator.
*
* Requirements:
*
* - input must fit into 240 bits
*/
function toUint240(uint256 value) internal pure returns (uint240) {
if (value > type(uint240).max) {
revert SafeCastOverflowedUintDowncast(240, value);
}
return uint240(value);
}
/**
* @dev Returns the downcasted uint232 from uint256, reverting on
* overflow (when the input is greater than largest uint232).
*
* Counterpart to Solidity's `uint232` operator.
*
* Requirements:
*
* - input must fit into 232 bits
*/
function toUint232(uint256 value) internal pure returns (uint232) {
if (value > type(uint232).max) {
revert SafeCastOverflowedUintDowncast(232, value);
}
return uint232(value);
}
/**
* @dev Returns the downcasted uint224 from uint256, reverting on
* overflow (when the input is greater than largest uint224).
*
* Counterpart to Solidity's `uint224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*/
function toUint224(uint256 value) internal pure returns (uint224) {
if (value > type(uint224).max) {
revert SafeCastOverflowedUintDowncast(224, value);
}
return uint224(value);
}
/**
* @dev Returns the downcasted uint216 from uint256, reverting on
* overflow (when the input is greater than largest uint216).
*
* Counterpart to Solidity's `uint216` operator.
*
* Requirements:
*
* - input must fit into 216 bits
*/
function toUint216(uint256 value) internal pure returns (uint216) {
if (value > type(uint216).max) {
revert SafeCastOverflowedUintDowncast(216, value);
}
return uint216(value);
}
/**
* @dev Returns the downcasted uint208 from uint256, reverting on
* overflow (when the input is greater than largest uint208).
*
* Counterpart to Solidity's `uint208` operator.
*
* Requirements:
*
* - input must fit into 208 bits
*/
function toUint208(uint256 value) internal pure returns (uint208) {
if (value > type(uint208).max) {
revert SafeCastOverflowedUintDowncast(208, value);
}
return uint208(value);
}
/**
* @dev Returns the downcasted uint200 from uint256, reverting on
* overflow (when the input is greater than largest uint200).
*
* Counterpart to Solidity's `uint200` operator.
*
* Requirements:
*
* - input must fit into 200 bits
*/
function toUint200(uint256 value) internal pure returns (uint200) {
if (value > type(uint200).max) {
revert SafeCastOverflowedUintDowncast(200, value);
}
return uint200(value);
}
/**
* @dev Returns the downcasted uint192 from uint256, reverting on
* overflow (when the input is greater than largest uint192).
*
* Counterpart to Solidity's `uint192` operator.
*
* Requirements:
*
* - input must fit into 192 bits
*/
function toUint192(uint256 value) internal pure returns (uint192) {
if (value > type(uint192).max) {
revert SafeCastOverflowedUintDowncast(192, value);
}
return uint192(value);
}
/**
* @dev Returns the downcasted uint184 from uint256, reverting on
* overflow (when the input is greater than largest uint184).
*
* Counterpart to Solidity's `uint184` operator.
*
* Requirements:
*
* - input must fit into 184 bits
*/
function toUint184(uint256 value) internal pure returns (uint184) {
if (value > type(uint184).max) {
revert SafeCastOverflowedUintDowncast(184, value);
}
return uint184(value);
}
/**
* @dev Returns the downcasted uint176 from uint256, reverting on
* overflow (when the input is greater than largest uint176).
*
* Counterpart to Solidity's `uint176` operator.
*
* Requirements:
*
* - input must fit into 176 bits
*/
function toUint176(uint256 value) internal pure returns (uint176) {
if (value > type(uint176).max) {
revert SafeCastOverflowedUintDowncast(176, value);
}
return uint176(value);
}
/**
* @dev Returns the downcasted uint168 from uint256, reverting on
* overflow (when the input is greater than largest uint168).
*
* Counterpart to Solidity's `uint168` operator.
*
* Requirements:
*
* - input must fit into 168 bits
*/
function toUint168(uint256 value) internal pure returns (uint168) {
if (value > type(uint168).max) {
revert SafeCastOverflowedUintDowncast(168, value);
}
return uint168(value);
}
/**
* @dev Returns the downcasted uint160 from uint256, reverting on
* overflow (when the input is greater than largest uint160).
*
* Counterpart to Solidity's `uint160` operator.
*
* Requirements:
*
* - input must fit into 160 bits
*/
function toUint160(uint256 value) internal pure returns (uint160) {
if (value > type(uint160).max) {
revert SafeCastOverflowedUintDowncast(160, value);
}
return uint160(value);
}
/**
* @dev Returns the downcasted uint152 from uint256, reverting on
* overflow (when the input is greater than largest uint152).
*
* Counterpart to Solidity's `uint152` operator.
*
* Requirements:
*
* - input must fit into 152 bits
*/
function toUint152(uint256 value) internal pure returns (uint152) {
if (value > type(uint152).max) {
revert SafeCastOverflowedUintDowncast(152, value);
}
return uint152(value);
}
/**
* @dev Returns the downcasted uint144 from uint256, reverting on
* overflow (when the input is greater than largest uint144).
*
* Counterpart to Solidity's `uint144` operator.
*
* Requirements:
*
* - input must fit into 144 bits
*/
function toUint144(uint256 value) internal pure returns (uint144) {
if (value > type(uint144).max) {
revert SafeCastOverflowedUintDowncast(144, value);
}
return uint144(value);
}
/**
* @dev Returns the downcasted uint136 from uint256, reverting on
* overflow (when the input is greater than largest uint136).
*
* Counterpart to Solidity's `uint136` operator.
*
* Requirements:
*
* - input must fit into 136 bits
*/
function toUint136(uint256 value) internal pure returns (uint136) {
if (value > type(uint136).max) {
revert SafeCastOverflowedUintDowncast(136, value);
}
return uint136(value);
}
/**
* @dev Returns the downcasted uint128 from uint256, reverting on
* overflow (when the input is greater than largest uint128).
*
* Counterpart to Solidity's `uint128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*/
function toUint128(uint256 value) internal pure returns (uint128) {
if (value > type(uint128).max) {
revert SafeCastOverflowedUintDowncast(128, value);
}
return uint128(value);
}
/**
* @dev Returns the downcasted uint120 from uint256, reverting on
* overflow (when the input is greater than largest uint120).
*
* Counterpart to Solidity's `uint120` operator.
*
* Requirements:
*
* - input must fit into 120 bits
*/
function toUint120(uint256 value) internal pure returns (uint120) {
if (value > type(uint120).max) {
revert SafeCastOverflowedUintDowncast(120, value);
}
return uint120(value);
}
/**
* @dev Returns the downcasted uint112 from uint256, reverting on
* overflow (when the input is greater than largest uint112).
*
* Counterpart to Solidity's `uint112` operator.
*
* Requirements:
*
* - input must fit into 112 bits
*/
function toUint112(uint256 value) internal pure returns (uint112) {
if (value > type(uint112).max) {
revert SafeCastOverflowedUintDowncast(112, value);
}
return uint112(value);
}
/**
* @dev Returns the downcasted uint104 from uint256, reverting on
* overflow (when the input is greater than largest uint104).
*
* Counterpart to Solidity's `uint104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*/
function toUint104(uint256 value) internal pure returns (uint104) {
if (value > type(uint104).max) {
revert SafeCastOverflowedUintDowncast(104, value);
}
return uint104(value);
}
/**
* @dev Returns the downcasted uint96 from uint256, reverting on
* overflow (when the input is greater than largest uint96).
*
* Counterpart to Solidity's `uint96` operator.
*
* Requirements:
*
* - input must fit into 96 bits
*/
function toUint96(uint256 value) internal pure returns (uint96) {
if (value > type(uint96).max) {
revert SafeCastOverflowedUintDowncast(96, value);
}
return uint96(value);
}
/**
* @dev Returns the downcasted uint88 from uint256, reverting on
* overflow (when the input is greater than largest uint88).
*
* Counterpart to Solidity's `uint88` operator.
*
* Requirements:
*
* - input must fit into 88 bits
*/
function toUint88(uint256 value) internal pure returns (uint88) {
if (value > type(uint88).max) {
revert SafeCastOverflowedUintDowncast(88, value);
}
return uint88(value);
}
/**
* @dev Returns the downcasted uint80 from uint256, reverting on
* overflow (when the input is greater than largest uint80).
*
* Counterpart to Solidity's `uint80` operator.
*
* Requirements:
*
* - input must fit into 80 bits
*/
function toUint80(uint256 value) internal pure returns (uint80) {
if (value > type(uint80).max) {
revert SafeCastOverflowedUintDowncast(80, value);
}
return uint80(value);
}
/**
* @dev Returns the downcasted uint72 from uint256, reverting on
* overflow (when the input is greater than largest uint72).
*
* Counterpart to Solidity's `uint72` operator.
*
* Requirements:
*
* - input must fit into 72 bits
*/
function toUint72(uint256 value) internal pure returns (uint72) {
if (value > type(uint72).max) {
revert SafeCastOverflowedUintDowncast(72, value);
}
return uint72(value);
}
/**
* @dev Returns the downcasted uint64 from uint256, reverting on
* overflow (when the input is greater than largest uint64).
*
* Counterpart to Solidity's `uint64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*/
function toUint64(uint256 value) internal pure returns (uint64) {
if (value > type(uint64).max) {
revert SafeCastOverflowedUintDowncast(64, value);
}
return uint64(value);
}
/**
* @dev Returns the downcasted uint56 from uint256, reverting on
* overflow (when the input is greater than largest uint56).
*
* Counterpart to Solidity's `uint56` operator.
*
* Requirements:
*
* - input must fit into 56 bits
*/
function toUint56(uint256 value) internal pure returns (uint56) {
if (value > type(uint56).max) {
revert SafeCastOverflowedUintDowncast(56, value);
}
return uint56(value);
}
/**
* @dev Returns the downcasted uint48 from uint256, reverting on
* overflow (when the input is greater than largest uint48).
*
* Counterpart to Solidity's `uint48` operator.
*
* Requirements:
*
* - input must fit into 48 bits
*/
function toUint48(uint256 value) internal pure returns (uint48) {
if (value > type(uint48).max) {
revert SafeCastOverflowedUintDowncast(48, value);
}
return uint48(value);
}
/**
* @dev Returns the downcasted uint40 from uint256, reverting on
* overflow (when the input is greater than largest uint40).
*
* Counterpart to Solidity's `uint40` operator.
*
* Requirements:
*
* - input must fit into 40 bits
*/
function toUint40(uint256 value) internal pure returns (uint40) {
if (value > type(uint40).max) {
revert SafeCastOverflowedUintDowncast(40, value);
}
return uint40(value);
}
/**
* @dev Returns the downcasted uint32 from uint256, reverting on
* overflow (when the input is greater than largest uint32).
*
* Counterpart to Solidity's `uint32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*/
function toUint32(uint256 value) internal pure returns (uint32) {
if (value > type(uint32).max) {
revert SafeCastOverflowedUintDowncast(32, value);
}
return uint32(value);
}
/**
* @dev Returns the downcasted uint24 from uint256, reverting on
* overflow (when the input is greater than largest uint24).
*
* Counterpart to Solidity's `uint24` operator.
*
* Requirements:
*
* - input must fit into 24 bits
*/
function toUint24(uint256 value) internal pure returns (uint24) {
if (value > type(uint24).max) {
revert SafeCastOverflowedUintDowncast(24, value);
}
return uint24(value);
}
/**
* @dev Returns the downcasted uint16 from uint256, reverting on
* overflow (when the input is greater than largest uint16).
*
* Counterpart to Solidity's `uint16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*/
function toUint16(uint256 value) internal pure returns (uint16) {
if (value > type(uint16).max) {
revert SafeCastOverflowedUintDowncast(16, value);
}
return uint16(value);
}
/**
* @dev Returns the downcasted uint8 from uint256, reverting on
* overflow (when the input is greater than largest uint8).
*
* Counterpart to Solidity's `uint8` operator.
*
* Requirements:
*
* - input must fit into 8 bits
*/
function toUint8(uint256 value) internal pure returns (uint8) {
if (value > type(uint8).max) {
revert SafeCastOverflowedUintDowncast(8, value);
}
return uint8(value);
}
/**
* @dev Converts a signed int256 into an unsigned uint256.
*
* Requirements:
*
* - input must be greater than or equal to 0.
*/
function toUint256(int256 value) internal pure returns (uint256) {
if (value < 0) {
revert SafeCastOverflowedIntToUint(value);
}
return uint256(value);
}
/**
* @dev Returns the downcasted int248 from int256, reverting on
* overflow (when the input is less than smallest int248 or
* greater than largest int248).
*
* Counterpart to Solidity's `int248` operator.
*
* Requirements:
*
* - input must fit into 248 bits
*/
function toInt248(int256 value) internal pure returns (int248 downcasted) {
downcasted = int248(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(248, value);
}
}
/**
* @dev Returns the downcasted int240 from int256, reverting on
* overflow (when the input is less than smallest int240 or
* greater than largest int240).
*
* Counterpart to Solidity's `int240` operator.
*
* Requirements:
*
* - input must fit into 240 bits
*/
function toInt240(int256 value) internal pure returns (int240 downcasted) {
downcasted = int240(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(240, value);
}
}
/**
* @dev Returns the downcasted int232 from int256, reverting on
* overflow (when the input is less than smallest int232 or
* greater than largest int232).
*
* Counterpart to Solidity's `int232` operator.
*
* Requirements:
*
* - input must fit into 232 bits
*/
function toInt232(int256 value) internal pure returns (int232 downcasted) {
downcasted = int232(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(232, value);
}
}
/**
* @dev Returns the downcasted int224 from int256, reverting on
* overflow (when the input is less than smallest int224 or
* greater than largest int224).
*
* Counterpart to Solidity's `int224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*/
function toInt224(int256 value) internal pure returns (int224 downcasted) {
downcasted = int224(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(224, value);
}
}
/**
* @dev Returns the downcasted int216 from int256, reverting on
* overflow (when the input is less than smallest int216 or
* greater than largest int216).
*
* Counterpart to Solidity's `int216` operator.
*
* Requirements:
*
* - input must fit into 216 bits
*/
function toInt216(int256 value) internal pure returns (int216 downcasted) {
downcasted = int216(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(216, value);
}
}
/**
* @dev Returns the downcasted int208 from int256, reverting on
* overflow (when the input is less than smallest int208 or
* greater than largest int208).
*
* Counterpart to Solidity's `int208` operator.
*
* Requirements:
*
* - input must fit into 208 bits
*/
function toInt208(int256 value) internal pure returns (int208 downcasted) {
downcasted = int208(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(208, value);
}
}
/**
* @dev Returns the downcasted int200 from int256, reverting on
* overflow (when the input is less than smallest int200 or
* greater than largest int200).
*
* Counterpart to Solidity's `int200` operator.
*
* Requirements:
*
* - input must fit into 200 bits
*/
function toInt200(int256 value) internal pure returns (int200 downcasted) {
downcasted = int200(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(200, value);
}
}
/**
* @dev Returns the downcasted int192 from int256, reverting on
* overflow (when the input is less than smallest int192 or
* greater than largest int192).
*
* Counterpart to Solidity's `int192` operator.
*
* Requirements:
*
* - input must fit into 192 bits
*/
function toInt192(int256 value) internal pure returns (int192 downcasted) {
downcasted = int192(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(192, value);
}
}
/**
* @dev Returns the downcasted int184 from int256, reverting on
* overflow (when the input is less than smallest int184 or
* greater than largest int184).
*
* Counterpart to Solidity's `int184` operator.
*
* Requirements:
*
* - input must fit into 184 bits
*/
function toInt184(int256 value) internal pure returns (int184 downcasted) {
downcasted = int184(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(184, value);
}
}
/**
* @dev Returns the downcasted int176 from int256, reverting on
* overflow (when the input is less than smallest int176 or
* greater than largest int176).
*
* Counterpart to Solidity's `int176` operator.
*
* Requirements:
*
* - input must fit into 176 bits
*/
function toInt176(int256 value) internal pure returns (int176 downcasted) {
downcasted = int176(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(176, value);
}
}
/**
* @dev Returns the downcasted int168 from int256, reverting on
* overflow (when the input is less than smallest int168 or
* greater than largest int168).
*
* Counterpart to Solidity's `int168` operator.
*
* Requirements:
*
* - input must fit into 168 bits
*/
function toInt168(int256 value) internal pure returns (int168 downcasted) {
downcasted = int168(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(168, value);
}
}
/**
* @dev Returns the downcasted int160 from int256, reverting on
* overflow (when the input is less than smallest int160 or
* greater than largest int160).
*
* Counterpart to Solidity's `int160` operator.
*
* Requirements:
*
* - input must fit into 160 bits
*/
function toInt160(int256 value) internal pure returns (int160 downcasted) {
downcasted = int160(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(160, value);
}
}
/**
* @dev Returns the downcasted int152 from int256, reverting on
* overflow (when the input is less than smallest int152 or
* greater than largest int152).
*
* Counterpart to Solidity's `int152` operator.
*
* Requirements:
*
* - input must fit into 152 bits
*/
function toInt152(int256 value) internal pure returns (int152 downcasted) {
downcasted = int152(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(152, value);
}
}
/**
* @dev Returns the downcasted int144 from int256, reverting on
* overflow (when the input is less than smallest int144 or
* greater than largest int144).
*
* Counterpart to Solidity's `int144` operator.
*
* Requirements:
*
* - input must fit into 144 bits
*/
function toInt144(int256 value) internal pure returns (int144 downcasted) {
downcasted = int144(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(144, value);
}
}
/**
* @dev Returns the downcasted int136 from int256, reverting on
* overflow (when the input is less than smallest int136 or
* greater than largest int136).
*
* Counterpart to Solidity's `int136` operator.
*
* Requirements:
*
* - input must fit into 136 bits
*/
function toInt136(int256 value) internal pure returns (int136 downcasted) {
downcasted = int136(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(136, value);
}
}
/**
* @dev Returns the downcasted int128 from int256, reverting on
* overflow (when the input is less than smallest int128 or
* greater than largest int128).
*
* Counterpart to Solidity's `int128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*/
function toInt128(int256 value) internal pure returns (int128 downcasted) {
downcasted = int128(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(128, value);
}
}
/**
* @dev Returns the downcasted int120 from int256, reverting on
* overflow (when the input is less than smallest int120 or
* greater than largest int120).
*
* Counterpart to Solidity's `int120` operator.
*
* Requirements:
*
* - input must fit into 120 bits
*/
function toInt120(int256 value) internal pure returns (int120 downcasted) {
downcasted = int120(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(120, value);
}
}
/**
* @dev Returns the downcasted int112 from int256, reverting on
* overflow (when the input is less than smallest int112 or
* greater than largest int112).
*
* Counterpart to Solidity's `int112` operator.
*
* Requirements:
*
* - input must fit into 112 bits
*/
function toInt112(int256 value) internal pure returns (int112 downcasted) {
downcasted = int112(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(112, value);
}
}
/**
* @dev Returns the downcasted int104 from int256, reverting on
* overflow (when the input is less than smallest int104 or
* greater than largest int104).
*
* Counterpart to Solidity's `int104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*/
function toInt104(int256 value) internal pure returns (int104 downcasted) {
downcasted = int104(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(104, value);
}
}
/**
* @dev Returns the downcasted int96 from int256, reverting on
* overflow (when the input is less than smallest int96 or
* greater than largest int96).
*
* Counterpart to Solidity's `int96` operator.
*
* Requirements:
*
* - input must fit into 96 bits
*/
function toInt96(int256 value) internal pure returns (int96 downcasted) {
downcasted = int96(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(96, value);
}
}
/**
* @dev Returns the downcasted int88 from int256, reverting on
* overflow (when the input is less than smallest int88 or
* greater than largest int88).
*
* Counterpart to Solidity's `int88` operator.
*
* Requirements:
*
* - input must fit into 88 bits
*/
function toInt88(int256 value) internal pure returns (int88 downcasted) {
downcasted = int88(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(88, value);
}
}
/**
* @dev Returns the downcasted int80 from int256, reverting on
* overflow (when the input is less than smallest int80 or
* greater than largest int80).
*
* Counterpart to Solidity's `int80` operator.
*
* Requirements:
*
* - input must fit into 80 bits
*/
function toInt80(int256 value) internal pure returns (int80 downcasted) {
downcasted = int80(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(80, value);
}
}
/**
* @dev Returns the downcasted int72 from int256, reverting on
* overflow (when the input is less than smallest int72 or
* greater than largest int72).
*
* Counterpart to Solidity's `int72` operator.
*
* Requirements:
*
* - input must fit into 72 bits
*/
function toInt72(int256 value) internal pure returns (int72 downcasted) {
downcasted = int72(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(72, value);
}
}
/**
* @dev Returns the downcasted int64 from int256, reverting on
* overflow (when the input is less than smallest int64 or
* greater than largest int64).
*
* Counterpart to Solidity's `int64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*/
function toInt64(int256 value) internal pure returns (int64 downcasted) {
downcasted = int64(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(64, value);
}
}
/**
* @dev Returns the downcasted int56 from int256, reverting on
* overflow (when the input is less than smallest int56 or
* greater than largest int56).
*
* Counterpart to Solidity's `int56` operator.
*
* Requirements:
*
* - input must fit into 56 bits
*/
function toInt56(int256 value) internal pure returns (int56 downcasted) {
downcasted = int56(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(56, value);
}
}
/**
* @dev Returns the downcasted int48 from int256, reverting on
* overflow (when the input is less than smallest int48 or
* greater than largest int48).
*
* Counterpart to Solidity's `int48` operator.
*
* Requirements:
*
* - input must fit into 48 bits
*/
function toInt48(int256 value) internal pure returns (int48 downcasted) {
downcasted = int48(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(48, value);
}
}
/**
* @dev Returns the downcasted int40 from int256, reverting on
* overflow (when the input is less than smallest int40 or
* greater than largest int40).
*
* Counterpart to Solidity's `int40` operator.
*
* Requirements:
*
* - input must fit into 40 bits
*/
function toInt40(int256 value) internal pure returns (int40 downcasted) {
downcasted = int40(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(40, value);
}
}
/**
* @dev Returns the downcasted int32 from int256, reverting on
* overflow (when the input is less than smallest int32 or
* greater than largest int32).
*
* Counterpart to Solidity's `int32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*/
function toInt32(int256 value) internal pure returns (int32 downcasted) {
downcasted = int32(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(32, value);
}
}
/**
* @dev Returns the downcasted int24 from int256, reverting on
* overflow (when the input is less than smallest int24 or
* greater than largest int24).
*
* Counterpart to Solidity's `int24` operator.
*
* Requirements:
*
* - input must fit into 24 bits
*/
function toInt24(int256 value) internal pure returns (int24 downcasted) {
downcasted = int24(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(24, value);
}
}
/**
* @dev Returns the downcasted int16 from int256, reverting on
* overflow (when the input is less than smallest int16 or
* greater than largest int16).
*
* Counterpart to Solidity's `int16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*/
function toInt16(int256 value) internal pure returns (int16 downcasted) {
downcasted = int16(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(16, value);
}
}
/**
* @dev Returns the downcasted int8 from int256, reverting on
* overflow (when the input is less than smallest int8 or
* greater than largest int8).
*
* Counterpart to Solidity's `int8` operator.
*
* Requirements:
*
* - input must fit into 8 bits
*/
function toInt8(int256 value) internal pure returns (int8 downcasted) {
downcasted = int8(value);
if (downcasted != value) {
revert SafeCastOverflowedIntDowncast(8, value);
}
}
/**
* @dev Converts an unsigned uint256 into a signed int256.
*
* Requirements:
*
* - input must be less than or equal to maxInt256.
*/
function toInt256(uint256 value) internal pure returns (int256) {
// Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
if (value > uint256(type(int256).max)) {
revert SafeCastOverflowedUintToInt(value);
}
return int256(value);
}
/**
* @dev Cast a boolean (false or true) to a uint256 (0 or 1) with no jump.
*/
function toUint(bool b) internal pure returns (uint256 u) {
assembly ("memory-safe") {
u := iszero(iszero(b))
}
}
}
"
},
"src/Strategy.sol": {
"content": "// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.28;
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import {TransientSlot} from "@openzeppelin/contracts/utils/TransientSlot.sol";
import {IERC20, IERC4626} from "@openzeppelin/contracts/interfaces/IERC4626.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {ISidecar} from "src/interfaces/ISidecar.sol";
import {ProtocolContext} from "src/ProtocolContext.sol";
import {IStrategy, IAllocator} from "src/interfaces/IStrategy.sol";
import {IBalanceProvider} from "src/interfaces/IBalanceProvider.sol";
/// @title Strategy.
/// @author Stake DAO
/// @custom:github @stake-dao
/// @custom:contact contact@stakedao.org
/// @notice Strategy is a protocol-agnostic yield strategy orchestrator that manages deposits and withdrawals
/// across multiple yield sources (locker and sidecars). It routes funds based on allocator decisions,
/// handles reward harvesting with transient storage for gas optimization, supports emergency shutdown
/// to transfer all funds back to vaults, and enables rebalancing when allocations change.
abstract contract Strategy is IStrategy, ProtocolContext {
using SafeCast for uint256;
using SafeERC20 for IERC20;
using TransientSlot for *;
/// @dev Transient storage slot for batching reward transfers during harvest
/// @dev Gas optimization: reduces multiple ERC20 transfers to single batch transfer
bytes32 internal constant FLUSH_AMOUNT_SLOT = keccak256("strategy.flushAmount");
//////////////////////////////////////////////////////
// --- ERRORS & EVENTS
//////////////////////////////////////////////////////
/// @notice Error thrown when the caller is not the vault for the gauge
error OnlyVault();
/// @notice Error thrown when the caller is not the accountant for the strategy
error OnlyAccountant();
/// @notice Error thrown when the caller is not the protocol controller
error OnlyProtocolController();
/// @notice Error thrown when the caller is not allowed to perform the action
error OnlyAllowed();
/// @notice Error thrown when trying to interact with a shutdown gauge
error GaugeShutdown();
/// @notice Error thrown when the deposit fails
error DepositFailed();
/// @notice Error thrown when the withdraw fails
error WithdrawFailed();
/// @notice Error thrown when the transfer fails
error TransferFailed();
/// @notice Error thrown when the approve fails
error ApproveFailed();
/// @notice Error thrown when rebalance is not needed
error RebalanceNotNeeded();
/// @notice Error thrown when the strategy is already shutdown
error AlreadyShutdown();
/// @notice Error thrown when the transfer to the accountant fails
error TransferToAccountantFailed();
/// @notice Error thrown when deposits are attempted while protocol is paused
error DepositsPaused();
/// @notice Event emitted when the strategy is shutdown
event Shutdown(address indexed gauge);
/// @notice Event emitted when the strategy is rebalanced
event Rebalance(address indexed gauge, address[] targets, uint256[] amounts);
//////////////////////////////////////////////////////
// --- MODIFIERS
//////////////////////////////////////////////////////
/// @notice Restricts functions to the vault associated with the gauge
modifier onlyVault(address gauge) {
require(PROTOCOL_CONTROLLER.vault(gauge) == msg.sender, OnlyVault());
_;
}
/// @notice Restricts functions to the protocol controller
modifier onlyProtocolController() {
require(msg.sender == address(PROTOCOL_CONTROLLER), OnlyProtocolController());
_;
}
/// @notice Restricts harvest flush operations to the accountant
modifier onlyAccountant() {
require(ACCOUNTANT == msg.sender, OnlyAccountant());
_;
}
//////////////////////////////////////////////////////
// --- CONSTRUCTOR
//////////////////////////////////////////////////////
/// @notice Initializes the strategy with registry, protocol ID, and locker and gateway
/// @param _registry The address of the protocol controller
/// @param _protocolId The identifier for the protocol this strategy interacts with
/// @param _locker The address of the locker contract
/// @param _gateway The address of the gateway contract
constructor(address _registry, bytes4 _protocolId, address _locker, address _gateway)
ProtocolContext(_protocolId, _registry, _locker, _gateway)
{}
//////////////////////////////////////////////////////
// --- EXTERNAL FUNCTIONS
//////////////////////////////////////////////////////
/// @notice Deposits LP tokens into gauge/sidecars according to allocator's distribution
/// @dev Called by vault after transferring LP tokens to targets
/// @param allocation Contains targets and amounts for deposit
/// @param policy Whether to harvest rewards during deposit
/// @return pendingRewards Rewards claimed if HARVEST policy
/// @custom:throws GaugeShutdown Prevents deposits to shutdown gauges
/// @custom:throws DepositsPaused Prevents deposits when protocol is paused
function deposit(IAllocator.Allocation calldata allocation, HarvestPolicy policy)
external
override
onlyVault(allocation.gauge)
returns (PendingRewards memory pendingRewards)
{
require(!PROTOCOL_CONTROLLER.isShutdown(allocation.gauge), GaugeShutdown());
require(!PROTOCOL_CONTROLLER.isPaused(PROTOCOL_ID), DepositsPaused());
// Execute deposits on each target (locker or sidecar)
for (uint256 i; i < allocation.targets.length; i++) {
if (allocation.amounts[i] > 0) {
if (allocation.targets[i] == LOCKER) {
_deposit(allocation.asset, allocation.gauge, allocation.amounts[i]);
} else {
ISidecar(allocation.targets[i]).deposit(allocation.amounts[i]);
}
}
}
pendingRewards = _harvestOrCheckpoint(allocation.gauge, policy);
}
/// @notice Withdraws LP tokens from gauge/sidecars and sends to receiver
/// @dev Skips withdrawal if gauge is shutdown (requires shutdown() instead)
/// @param allocation Contains targets and amounts for withdrawal
/// @param policy Whether to harvest rewards during withdrawal
/// @param receiver Address to receive the LP tokens
/// @return pendingRewards Rewards claimed if HARVEST policy
function withdraw(IAllocator.Allocation calldata allocation, IStrategy.HarvestPolicy policy, address receiver)
external
override
onlyVault(allocation.gauge)
returns (PendingRewards memory pendingRewards)
{
address gauge = allocation.gauge;
// For shutdown gauges, only sync rewards without withdrawing
// @dev Prevents loss of user funds by ensuring no withdrawals after shutdown
if (PROTOCOL_CONTROLLER.isShutdown(gauge)) return _harvestOrCheckpoint(gauge, policy);
// Execute withdrawals from each target
for (uint256 i; i < allocation.targets.length; i++) {
if (allocation.amounts[i] > 0) {
if (allocation.targets[i] == LOCKER) {
_withdraw(allocation.asset, gauge, allocation.amounts[i], receiver);
} else {
ISidecar(allocation.targets[i]).withdraw(allocation.amounts[i], receiver);
}
}
}
return _harvestOrCheckpoint(gauge, policy);
}
/// @notice Claims rewards from gauge and sidecars (accountant batch harvest)
/// @dev Uses transient storage to defer reward transfers for gas efficiency
/// @param gauge The gauge to harvest rewards from
/// @param extraData Protocol-specific data for claiming
/// @return pendingRewards Total rewards claimed from all sources
function harvest(address gauge, bytes memory extraData)
external
override
onlyAccountant
returns (IStrategy.PendingRewards memory pendingRewards)
{
return _harvest(gauge, extraData, true);
}
/// @notice Transfers accumulated rewards to accountant after batch harvest
/// @dev Called once after harvesting multiple gauges to save gas
function flush() public onlyAccountant {
uint256 flushAmount = _getFlushAmount();
if (flushAmount == 0) return;
_transferToAccountant(flushAmount);
_setFlushAmount(0);
}
/// @notice Emergency withdrawal of all funds back to vault
/// @dev Anyone can call if gauge is shutdown, ensuring user fund recovery
/// @param gauge The gauge to withdraw all funds from
/// @custom:throws AlreadyShutdown If already fully withdrawn
function shutdown(address gauge) public onlyProtocolController {
address vault = PROTOCOL_CONTROLLER.vault(gauge);
address asset = IERC4626(vault).asset();
address[] memory targets = _getAllocationTargets(gauge);
// Withdraw everything from locker and sidecars to vault
_withdrawFromAllTargets(asset, gauge, targets, vault);
emit Shutdown(gauge);
}
/// @notice Redistributes funds between targets when allocations change
/// @dev Withdraws all funds to strategy then re-deposits per new allocation
/// @param gauge The gauge to rebalance
/// @custom:throws RebalanceNotNeeded If only one target (nothing to rebalance)
/// @custom:throws DepositsPaused Prevents rebalancing when protocol is paused
function rebalance(address gauge) external {
require(!PROTOCOL_CONTROLLER.isShutdown(gauge), GaugeShutdown());
require(!PROTOCOL_CONTROLLER.isPaused(PROTOCOL_ID), DepositsPaused());
address allocator = PROTOCOL_CONTROLLER.allocator(PROTOCOL_ID);
IERC20 asset = IERC20(PROTOCOL_CONTROLLER.asset(gauge));
uint256 currentBalance = balanceOf(gauge);
address[] memory targets = _getAllocationTargets(gauge);
// Get new allocation from allocator
IAllocator.Allocation memory allocation =
IAllocator(allocator).getRebalancedAllocation(address(asset), gauge, currentBalance);
// Withdraw everything to this contract
_withdrawFromAllTargets(address(asset), gauge, targets, address(this));
uint256 allocationLength = allocation.targets.length;
require(allocationLength > 1, RebalanceNotNeeded());
// Re-deposit according to new allocation
for (uint256 i; i < allocationLength; i++) {
address target = allocation.targets[i];
uint256 amount = allocation.amounts[i];
asset.safeTransfer(target, amount);
if (amount > 0) {
if (target == LOCKER) {
_deposit(address(asset), gauge, amount);
} else {
ISidecar(target).deposit(amount);
}
}
}
emit Rebalance(gauge, allocation.targets, allocation.amounts);
}
/// @notice Total LP tokens managed across all targets for a gauge
/// @dev Sums balances from locker and all sidecars
/// @param gauge The gauge to check balance for
/// @return balance Combined LP token balance
function balanceOf(address gauge) public view virtual returns (uint256 balance) {
address[] memory targets = _getAllocationTargets(gauge);
uint256 length = targets.length;
for (uint256 i; i < length; i++) {
address target = targets[i];
if (target == LOCKER) {
balance += IBalanceProvider(gauge).balanceOf(target);
} else {
balance += ISidecar(target).balanceOf();
}
}
}
//////////////////////////////////////////////////////
// --- INTERNAL HELPER FUNCTIONS
//////////////////////////////////////////////////////
/// @notice Gets allocation targets for a gauge
/// @param gauge The gauge to get targets for
/// @return targets Array of target addresses
function _getAllocationTargets(address gauge) internal view returns (address[] memory targets) {
address allocator = PROTOCOL_CONTROLLER.allocator(PROTOCOL_ID);
targets = IAllocator(allocator).getAllocationTargets(gauge);
}
/// @notice Withdraws assets from all targets
/// @param asset The asset to withdraw
/// @param gauge The gauge to withdraw from
/// @param targets Array of target addresses
/// @param receiver Address to receive the withdrawn assets
function _withdrawFromAllTargets(address asset, address gauge, address[] memory targets, address receiver)
internal
{
uint256 length = targets.length;
for (uint256 i; i < length; i++) {
address target = targets[i];
uint256 balance;
if (target == LOCKER) {
balance = IBalanceProvider(gauge).balanceOf(LOCKER);
if (balance > 0) {
_withdraw(asset, gauge, balance, receiver);
}
} else {
balance = ISidecar(target).balanceOf();
if (balance > 0) {
ISidecar(target).withdraw(balance, receiver);
}
}
}
}
/// @notice Claims rewards from locker and all sidecars
/// @dev Locker rewards are fee-subject, sidecar rewards may not be
/// @param gauge The gauge to harvest from
/// @param extraData Protocol-specific harvest parameters
/// @param deferRewards If true, accumulate in transient storage for batch transfer (gas optimization)
/// @return pendingRewards Total and fee-subject reward amounts
function _harvest(address gauge, bytes memory extraData, bool deferRewards)
internal
virtual
returns (IStrategy.PendingRewards memory pendingRewards)
{
address[] memory targets = _getAllocationTargets(gauge);
uint256 pendingRewardsAmount;
uint256 length = targets.length;
for (uint256 i; i < length; i++) {
address target = targets[i];
if (target == LOCKER) {
pendingRewardsAmount = _harvestLocker(gauge, extraData);
pendingRewards.feeSubjectAmount = pendingRewardsAmount.toUint128();
if (deferRewards) {
// Batch transfers: accumulate in transient storage
uint256 currentFlushAmount = _getFlushAmount();
_setFlushAmount(currentFlushAmount + pendingRewardsAmount);
} else {
// Direct transfer for HARVEST policy
_transferToAccountant(pendingRewardsAmount);
}
} else {
pendingRewardsAmount = ISidecar(target).claim();
}
pendingRewards.totalAmount += pendingRewardsAmount.toUint128();
}
return pendingRewards;
}
/// @notice Harvests or synchronizes rewards
/// @param gauge The gauge to harvest or synchronize from
/// @param policy The harvest policy to use
/// @return pendingRewards The pending rewards after harvesting or synchronization
function _harvestOrCheckpoint(address gauge, IStrategy.HarvestPolicy policy)
internal
returns (PendingRewards memory pendingRewards)
{
pendingRewards =
policy == IStrategy.HarvestPolicy.HARVEST ? _harvest(gauge, "", false) : _checkpointRewards(gauge);
}
//////////////////////////////////////////////////////
// --- INTERNAL VIRTUAL FUNCTIONS
//////////////////////////////////////////////////////
/// @notice Gets the flush amount from transient storage
/// @return The flush amount
function _getFlushAmount() internal view virtual returns (uint256) {\
Submitted on: 2025-10-24 18:45:49
Comments
Log in to comment.
No comments yet.