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/farms/PendleV2FarmV3.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {FixedPointMathLib} from "@solmate/src/utils/FixedPointMathLib.sol";
import {IOracle} from "@interfaces/IOracle.sol";
import {ISYToken} from "@interfaces/pendle/ISYToken.sol";
import {CoreRoles} from "@libraries/CoreRoles.sol";
import {Accounting} from "@finance/Accounting.sol";
import {IPendleMarket} from "@interfaces/pendle/IPendleMarket.sol";
import {IPendleOracle} from "@interfaces/pendle/IPendleOracle.sol";
import {MultiAssetFarm} from "@integrations/MultiAssetFarm.sol";
import {CoWSwapFarmBase} from "@integrations/farms/CoWSwapFarmBase.sol";
import {IMaturityFarm, IFarm} from "@interfaces/IMaturityFarm.sol";
/// @title Pendle V2 Farm (V3)
/// @notice This contract is used to deploy assets to Pendle v2.
/// The V3 inherits from MultiAssetFarm and CoWSwapFarmBase. Investment into PTs is done in 2
/// steps: first, assetTokens have to be swapped to underlyingTokens using CoWSwap, then the
/// underlyingTokens can be swapped to PTs using Pendle's AMM. Divestment works the same way,
/// in the opposite direction.
/// Because the farm is a MultiAssetFarm, it is possible to move the underlyingTokens directly
/// between this farm and other farms, such that the swap fees to convert back to assetTokens is
/// not paid by the protocol every time there is a maturity event.
/// @dev Example deployment: PT-sUSDe-29MAY2025 market, USDC assetToken, sUSDe underlying token.
/// @dev It is V3 because yield token is considered same as underlying token
contract PendleV2FarmV3 is CoWSwapFarmBase, IMaturityFarm {
using SafeERC20 for IERC20;
using FixedPointMathLib for uint256;
error PTAlreadyMatured(uint256 maturity);
error PTNotMatured(uint256 maturity);
error SwapFailed(bytes reason);
event PTBought(
uint256 indexed timestamp,
uint256 timeToMaturity,
uint256 yieldTokenIn,
uint256 ptReceived,
uint256 assetsSpent,
uint256 assetsReceived,
uint256 assetsAtMaturity
);
event PTSold(
uint256 indexed timestamp,
uint256 ptTokensIn,
uint256 yieldTokensReceived,
uint256 assetsSpent,
uint256 assetsReceived
);
/// @notice Maturity of the Pendle market.
uint256 public immutable maturity;
/// @notice Reference to the Pendle market.
address public immutable pendleMarket;
/// @notice Reference to the Pendle oracle (for PT <-> underlying exchange rates).
address public immutable pendleOracle;
uint32 private constant _PENDLE_ORACLE_TWAP_DURATION = 1800;
/// @notice Reference to the Pendle market's underlying token (the reference
/// token PTs appreciate against).
address public immutable underlyingToken;
/// @notice Reference to the Pendle market's yield token (the token into which
/// PTs convert at maturity)
address public immutable yieldToken;
/// @notice Reference to the Principal Token of the Pendle market.
address public immutable ptToken;
/// @notice Reference to the SY token of the Pendle market
address public immutable syToken;
/// @notice address of the Pendle router used for swaps
address public pendleRouter;
/// @notice Number of yieldTokens wrapped as PTs
uint256 public totalWrappedYieldTokens;
/// @notice Number of PTs received from wrapping yieldTokens
uint256 public totalReceivedPTs;
/// @notice Total yield already interpolated
/// @dev this should be updated everytime we deposit and wrap assets
uint256 public alreadyInterpolatedYield;
/// @notice Timestamp of the last wrapping
uint256 public lastWrappedTimestamp;
/// @notice Discounting of assets at maturity for the value of PTs
/// This is in place to account for potential swap losses at maturity, and has the effect
/// of reducing the yield distributed while PTs are held, and causing a potential small
/// yield spike when unwrapping PTs at maturity
uint256 public maturityPTDiscount;
constructor(
address _core,
address _assetToken,
address _pendleMarket,
address _pendleOracle,
address _accounting,
address _pendleRouter,
address _settlementContract,
address _vaultRelayer
) CoWSwapFarmBase(_settlementContract, _vaultRelayer) MultiAssetFarm(_core, _assetToken, _accounting) {
pendleMarket = _pendleMarket;
pendleOracle = _pendleOracle;
pendleRouter = _pendleRouter;
// read contracts and keep some immutable variables to save gas
(syToken, ptToken,) = IPendleMarket(_pendleMarket).readTokens();
(, underlyingToken,) = ISYToken(syToken).assetInfo();
yieldToken = underlyingToken;
maturity = IPendleMarket(_pendleMarket).expiry();
// set default slippage tolerance to 0.3%
maxSlippage = 0.997e18;
// set default maturity discounting to 0.2%
maturityPTDiscount = 0.998e18;
// ensure pendle oracle is initialized for this market
// https://docs.pendle.finance/Developers/Oracles/HowToIntegratePtAndLpOracle
// this call will revert if the oracle is not initialized or if the cardinality
// of the oracle has to be increased (if so, any eoa can do it on the Pendle contract
// directly prior to deploying this farm).
IPendleOracle(pendleOracle).getPtToAssetRate(_pendleMarket, _PENDLE_ORACLE_TWAP_DURATION);
}
function setPendleRouter(address _pendleRouter) external onlyCoreRole(CoreRoles.PROTOCOL_PARAMETERS) {
pendleRouter = _pendleRouter;
}
/// @dev Be careful when setting this value, as calling it on a farm with invested PTs is going to cause a jump
/// in the reported assets() value.
function setMaturityPTDiscount(uint256 _maturityPTDiscount) external onlyCoreRole(CoreRoles.PROTOCOL_PARAMETERS) {
maturityPTDiscount = _maturityPTDiscount;
}
function assetTokens() public view override returns (address[] memory) {
address[] memory tokens = new address[](2);
tokens[0] = assetToken;
tokens[1] = yieldToken;
return tokens;
}
function isAssetSupported(address _asset) public view override returns (bool) {
return _asset == assetToken || _asset == yieldToken;
}
/// @notice Returns the total assets in the farm
/// before maturity, the assets are the sum of assets in the farm + assets wrapped + the interpolated yield
/// after maturity, the assets are the sum of the assets() + the value of the PTs based on oracle prices
/// @dev Note that the assets() function includes the current balance of assetTokens,
/// this is because deposit()s and withdraw()als in this farm are handled asynchronously,
/// as they have to go through swaps which calldata has to be generated offchain.
/// This farm therefore holds its reserve in 3 tokens, assetToken, yieldTokens, and ptTokens.
/// This farm's assets() reported does not take into account the slippage we might incur from
/// converting assetTokens to yieldTokens and yieldTokens to ptTokens.
function assets() public view override(MultiAssetFarm, IFarm) returns (uint256) {
uint256 supportedAssetBalance = MultiAssetFarm.assets();
if (block.timestamp < maturity) {
// before maturity, interpolate yield
return supportedAssetBalance + yieldTokensToAssets(totalWrappedYieldTokens) + interpolatingYield();
}
// after maturity, return the total USDC held in the farm +
// the PTs value if any are still held
uint256 balanceOfPTs = IERC20(ptToken).balanceOf(address(this));
uint256 ptAssetsValue = 0;
if (balanceOfPTs > 0) {
// estimate the value of the PTs at maturity,
// accounting for possible max slippage
ptAssetsValue = ptToAssets(balanceOfPTs).mulWadDown(maturityPTDiscount);
}
return supportedAssetBalance + ptAssetsValue;
}
/// @notice swap a token in [assetToken, yieldToken] to a token out [assetToken, yieldToken]
function signSwapOrder(address _tokenIn, address _tokenOut, uint256 _amountIn, uint256 _minAmountOut)
external
whenNotPaused
onlyCoreRole(CoreRoles.FARM_SWAP_CALLER)
returns (bytes memory)
{
require(_tokenIn == assetToken || _tokenIn == yieldToken, InvalidToken(_tokenIn));
require(_tokenOut == assetToken || _tokenOut == yieldToken, InvalidToken(_tokenOut));
require(_tokenIn != _tokenOut, InvalidToken(_tokenOut));
return _checkSwapApproveAndSignOrder(_tokenIn, _tokenOut, _amountIn, _minAmountOut, maxSlippage);
}
/// @notice Wraps yieldTokens to PTs.
/// @dev The transaction may be submitted privately to avoid sandwiching, and the function
/// can be called multiple times with partial amounts to help reduce slippage.
/// @dev The caller is trusted to not be sandwiching the swap to steal yield.
function wrapYieldTokenToPt(uint256 _yieldTokenIn, bytes memory _calldata)
external
whenNotPaused
onlyCoreRole(CoreRoles.FARM_SWAP_CALLER)
{
require(block.timestamp < maturity, PTAlreadyMatured(maturity));
// update the already interpolated yield on each wrap
alreadyInterpolatedYield = interpolatingYield();
uint256 ptBalanceBefore = IERC20(ptToken).balanceOf(address(this));
// do swap
IERC20(yieldToken).forceApprove(pendleRouter, _yieldTokenIn);
(bool success, bytes memory reason) = pendleRouter.call(_calldata);
require(success, SwapFailed(reason));
// check slippage
uint256 ptBalanceAfter = IERC20(ptToken).balanceOf(address(this));
uint256 ptReceived = ptBalanceAfter - ptBalanceBefore;
uint256 minOut = _yieldTokenIn.mulWadDown(maxSlippage);
uint256 actualOut = ptToYieldToken(ptReceived);
require(actualOut >= minOut, SlippageTooHigh(minOut, actualOut));
// update wrapped assets
// @dev we are not doing totalWrappedYieldTokens += actualOut because we do not want to
// report losses from buying PTs, as the PTs will earn yield towards maturity that should
// make up for it.
totalWrappedYieldTokens += _yieldTokenIn;
totalReceivedPTs += ptReceived;
lastWrappedTimestamp = block.timestamp;
// emit event
emit PTBought(
block.timestamp,
maturity - block.timestamp,
_yieldTokenIn,
ptReceived,
yieldTokensToAssets(_yieldTokenIn),
ptToAssets(ptReceived),
ptReceived.mulWadDown(assetToPtUnderlyingRate())
);
}
/// @notice Unwraps PTs to yieldTokens.
/// @dev The transaction may be submitted privately to avoid sandwiching, and the function
/// can be called multiple times with partial amounts to help reduce slippage.
function unwrapPtToYieldToken(uint256 _ptTokensIn, bytes memory _calldata)
external
whenNotPaused
onlyCoreRole(CoreRoles.FARM_SWAP_CALLER)
{
// MANUAL_REBALANCER role can bypass the maturity check and manually
// exit positions before maturity.
if (!core().hasRole(CoreRoles.MANUAL_REBALANCER, msg.sender)) {
require(block.timestamp >= maturity, PTNotMatured(maturity));
} else if (block.timestamp < maturity) {
// early exit case: an address with MANUAL_REBALANCER can swap all the PTs
// to yieldTokens before maturity, should it be needed.
// a step jump will occur in reported assets(), because the contract conservatively
// discounts for potential slippage during interpolation, but actual unwrap often
// recovers more value.
require(_ptTokensIn == totalReceivedPTs, SwapFailed("Must unwrap all"));
totalWrappedYieldTokens = 0;
totalReceivedPTs = 0;
lastWrappedTimestamp = 0;
alreadyInterpolatedYield = 0;
}
uint256 yieldTokensBefore = IERC20(yieldToken).balanceOf(address(this));
// do swap
IERC20(ptToken).forceApprove(pendleRouter, _ptTokensIn);
(bool success, bytes memory reason) = pendleRouter.call(_calldata);
require(success, SwapFailed(reason));
// check slippage
uint256 yieldTokensAfter = IERC20(yieldToken).balanceOf(address(this));
uint256 yieldTokensReceived = yieldTokensAfter - yieldTokensBefore;
uint256 minOut = ptToYieldToken(_ptTokensIn).mulWadDown(maxSlippage);
require(yieldTokensReceived >= minOut, SlippageTooHigh(minOut, yieldTokensReceived));
// emit event
emit PTSold(
block.timestamp,
_ptTokensIn,
yieldTokensReceived,
_ptTokensIn.mulWadDown(assetToPtUnderlyingRate()),
yieldTokensToAssets(yieldTokensReceived)
);
}
function ptToYieldToken(uint256 _ptAmount) public view returns (uint256) {
if (_ptAmount == 0) return 0;
uint256 yieldTokenPrice = Accounting(accounting).price(yieldToken);
uint256 underlyingPrice = Accounting(accounting).price(underlyingToken);
return ptToUnderlying(_ptAmount).mulDivDown(underlyingPrice, yieldTokenPrice);
}
function yieldTokensToAssets(uint256 _yieldTokensAmount) public view returns (uint256) {
uint256 assetPrice = Accounting(accounting).price(assetToken);
uint256 yieldTokenPrice = Accounting(accounting).price(yieldToken);
return _yieldTokensAmount.mulDivDown(yieldTokenPrice, assetPrice);
}
/// @dev e.g. for ptToken = PT-USDe-29MAY2025 and assetToken = USDC,
/// this oracle returns the exchange rate of USDe (the underlying token) to USDC.
/// Since USDe has 18 decimals and USDC has 6, and the exchange rate is ~1:1,
/// the oracle should return a value ~= 1e6 because the USDC oracle returns 1e30
/// and the USDe oracle returns 1e18.
function assetToPtUnderlyingRate() public view returns (uint256) {
uint256 assetPrice = Accounting(accounting).price(assetToken);
uint256 underlyingPrice = Accounting(accounting).price(underlyingToken);
return underlyingPrice.divWadDown(assetPrice);
}
/// @notice Converts a number of underlyingTokens to assetTokens based on oracle rates.
function underlyingToAssets(uint256 _underlyingAmount) public view returns (uint256) {
if (_underlyingAmount == 0) return 0;
return _underlyingAmount.mulWadDown(assetToPtUnderlyingRate());
}
/// @notice Converts a number of PTs to assetTokens based on oracle rates.
function ptToAssets(uint256 _ptAmount) public view returns (uint256) {
if (_ptAmount == 0) return 0;
return ptToUnderlying(_ptAmount).mulWadDown(assetToPtUnderlyingRate());
}
/// @notice Converts a number of PTs to underlyingTokens based on oracle rates.
function ptToUnderlying(uint256 _ptAmount) public view returns (uint256) {
if (_ptAmount == 0) return 0;
// read oracles
uint256 ptToUnderlyingRate =
IPendleOracle(pendleOracle).getPtToAssetRate(pendleMarket, _PENDLE_ORACLE_TWAP_DURATION);
// convert
return _ptAmount.mulWadDown(ptToUnderlyingRate);
}
/// @notice Computes the yield to interpolate from the last deposit to maturity.
/// @dev this function is and should only be called before maturity
function interpolatingYield() public view returns (uint256) {
// if no wrapping has been made yet, no yield to interpolate
if (lastWrappedTimestamp == 0) return 0;
uint256 balanceOfPTs = IERC20(ptToken).balanceOf(address(this));
// if not PTs held, no need to interpolate
if (balanceOfPTs == 0) return 0;
// we want to interpolate the yield from the current time to maturity
// to do that, we first need to compute how much USDC we should be able to get once maturity is reached
// at maturity, 1 PT is worth 1 underlying PT asset (e.g. USDE)
// so we can compute the amount of assets (eg USDC) we should get at maturity by using the assetToPtUnderlyingRate
// in this example, assetToPtUnderlyingRate gives the price of USDE in USDC. probably close to 1:1
uint256 maturityAssetAmount = balanceOfPTs.mulWadDown(assetToPtUnderlyingRate());
// account for slippage, because unwrapping PTs => assets will cause some slippage using pendle's AMM
maturityAssetAmount = maturityAssetAmount.mulWadDown(maturityPTDiscount);
// compute the yield to interpolate, which is the target amount (maturityAssetAmount) minus the amount of assets
// wrapped minus the already interpolated yield (can be != 0 if we made multiple wraps)
uint256 totalWrappedAssets = yieldTokensToAssets(totalWrappedYieldTokens);
int256 totalYieldRemainingToInterpolate =
int256(maturityAssetAmount) - int256(totalWrappedAssets) - int256(alreadyInterpolatedYield);
// in case the rate moved against us, we return the already interpolated yield
if (totalYieldRemainingToInterpolate < 0) {
return alreadyInterpolatedYield;
}
// cannot underflow because lastWrappedTimestamp cannot be after maturity as we cannot wrap after maturity
// and lastWrappedTimestamp is always > 0 otherwise the first line of this function would have returned 0
uint256 yieldPerSecond =
(uint256(totalYieldRemainingToInterpolate) * FixedPointMathLib.WAD) / (maturity - lastWrappedTimestamp);
uint256 secondsSinceLastWrap = block.timestamp - lastWrappedTimestamp;
uint256 interpolatedYield = yieldPerSecond * secondsSinceLastWrap;
return alreadyInterpolatedYield + interpolatedYield / FixedPointMathLib.WAD;
}
}
"
},
"lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol": {
"content": "// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC-20 standard as defined in the ERC.
*/
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);
}
"
},
"lib/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol": {
"content": "// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.2.0) (token/ERC20/utils/SafeERC20.sol)
pragma solidity ^0.8.20;
import {IERC20} from "../IERC20.sol";
import {IERC1363} from "../../../interfaces/IERC1363.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC-20 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 {
/**
* @dev An operation with an ERC-20 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 Variant of {safeTransfer} that returns a bool instead of reverting if the operation is not successful.
*/
function trySafeTransfer(IERC20 token, address to, uint256 value) internal returns (bool) {
return _callOptionalReturnBool(token, abi.encodeCall(token.transfer, (to, value)));
}
/**
* @dev Variant of {safeTransferFrom} that returns a bool instead of reverting if the operation is not successful.
*/
function trySafeTransferFrom(IERC20 token, address from, address to, uint256 value) internal returns (bool) {
return _callOptionalReturnBool(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.
*
* IMPORTANT: If the token implements ERC-7674 (ERC-20 with temporary allowance), and if the "client"
* smart contract uses ERC-7674 to set temporary allowances, then the "client" smart contract should avoid using
* this function. Performing a {safeIncreaseAllowance} or {safeDecreaseAllowance} operation on a token contract
* that has a non-zero temporary allowance (for that particular owner-spender) will result in unexpected behavior.
*/
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.
*
* IMPORTANT: If the token implements ERC-7674 (ERC-20 with temporary allowance), and if the "client"
* smart contract uses ERC-7674 to set temporary allowances, then the "client" smart contract should avoid using
* this function. Performing a {safeIncreaseAllowance} or {safeDecreaseAllowance} operation on a token contract
* that has a non-zero temporary allowance (for that particular owner-spender) will result in unexpected behavior.
*/
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.
*
* NOTE: If the token implements ERC-7674, this function will not modify any temporary allowance. This function
* only sets the "standard" allowance. Any temporary allowance will remain active, in addition to the value being
* set here.
*/
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 Performs an {ERC1363} transferAndCall, with a fallback to the simple {ERC20} transfer if the target has no
* code. This can be used to implement an {ERC721}-like safe transfer that rely on {ERC1363} checks when
* targeting contracts.
*
* Reverts if the returned value is other than `true`.
*/
function transferAndCallRelaxed(IERC1363 token, address to, uint256 value, bytes memory data) internal {
if (to.code.length == 0) {
safeTransfer(token, to, value);
} else if (!token.transferAndCall(to, value, data)) {
revert SafeERC20FailedOperation(address(token));
}
}
/**
* @dev Performs an {ERC1363} transferFromAndCall, with a fallback to the simple {ERC20} transferFrom if the target
* has no code. This can be used to implement an {ERC721}-like safe transfer that rely on {ERC1363} checks when
* targeting contracts.
*
* Reverts if the returned value is other than `true`.
*/
function transferFromAndCallRelaxed(
IERC1363 token,
address from,
address to,
uint256 value,
bytes memory data
) internal {
if (to.code.length == 0) {
safeTransferFrom(token, from, to, value);
} else if (!token.transferFromAndCall(from, to, value, data)) {
revert SafeERC20FailedOperation(address(token));
}
}
/**
* @dev Performs an {ERC1363} approveAndCall, with a fallback to the simple {ERC20} approve if the target has no
* code. This can be used to implement an {ERC721}-like safe transfer that rely on {ERC1363} checks when
* targeting contracts.
*
* NOTE: When the recipient address (`to`) has no code (i.e. is an EOA), this function behaves as {forceApprove}.
* Opposedly, when the recipient address (`to`) has code, this function only attempts to call {ERC1363-approveAndCall}
* once without retrying, and relies on the returned value to be true.
*
* Reverts if the returned value is other than `true`.
*/
function approveAndCallRelaxed(IERC1363 token, address to, uint256 value, bytes memory data) internal {
if (to.code.length == 0) {
forceApprove(token, to, value);
} else if (!token.approveAndCall(to, value, data)) {
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 {_callOptionalReturnBool} that reverts if call fails to meet the requirements.
*/
function _callOptionalReturn(IERC20 token, bytes memory data) private {
uint256 returnSize;
uint256 returnValue;
assembly ("memory-safe") {
let success := call(gas(), token, 0, add(data, 0x20), mload(data), 0, 0x20)
// bubble errors
if iszero(success) {
let ptr := mload(0x40)
returndatacopy(ptr, 0, returndatasize())
revert(ptr, returndatasize())
}
returnSize := returndatasize()
returnValue := mload(0)
}
if (returnSize == 0 ? address(token).code.length == 0 : returnValue != 1) {
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 silently catches all reverts and returns a bool instead.
*/
function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
bool success;
uint256 returnSize;
uint256 returnValue;
assembly ("memory-safe") {
success := call(gas(), token, 0, add(data, 0x20), mload(data), 0, 0x20)
returnSize := returndatasize()
returnValue := mload(0)
}
return success && (returnSize == 0 ? address(token).code.length > 0 : returnValue == 1);
}
}
"
},
"lib/solmate/src/utils/FixedPointMathLib.sol": {
"content": "// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;
/// @notice Arithmetic library with operations for fixed-point numbers.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/FixedPointMathLib.sol)
/// @author Inspired by USM (https://github.com/usmfum/USM/blob/master/contracts/WadMath.sol)
library FixedPointMathLib {
/*//////////////////////////////////////////////////////////////
SIMPLIFIED FIXED POINT OPERATIONS
//////////////////////////////////////////////////////////////*/
uint256 internal constant MAX_UINT256 = 2**256 - 1;
uint256 internal constant WAD = 1e18; // The scalar of ETH and most ERC20s.
function mulWadDown(uint256 x, uint256 y) internal pure returns (uint256) {
return mulDivDown(x, y, WAD); // Equivalent to (x * y) / WAD rounded down.
}
function mulWadUp(uint256 x, uint256 y) internal pure returns (uint256) {
return mulDivUp(x, y, WAD); // Equivalent to (x * y) / WAD rounded up.
}
function divWadDown(uint256 x, uint256 y) internal pure returns (uint256) {
return mulDivDown(x, WAD, y); // Equivalent to (x * WAD) / y rounded down.
}
function divWadUp(uint256 x, uint256 y) internal pure returns (uint256) {
return mulDivUp(x, WAD, y); // Equivalent to (x * WAD) / y rounded up.
}
/*//////////////////////////////////////////////////////////////
LOW LEVEL FIXED POINT OPERATIONS
//////////////////////////////////////////////////////////////*/
function mulDivDown(
uint256 x,
uint256 y,
uint256 denominator
) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Equivalent to require(denominator != 0 && (y == 0 || x <= type(uint256).max / y))
if iszero(mul(denominator, iszero(mul(y, gt(x, div(MAX_UINT256, y)))))) {
revert(0, 0)
}
// Divide x * y by the denominator.
z := div(mul(x, y), denominator)
}
}
function mulDivUp(
uint256 x,
uint256 y,
uint256 denominator
) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Equivalent to require(denominator != 0 && (y == 0 || x <= type(uint256).max / y))
if iszero(mul(denominator, iszero(mul(y, gt(x, div(MAX_UINT256, y)))))) {
revert(0, 0)
}
// If x * y modulo the denominator is strictly greater than 0,
// 1 is added to round up the division of x * y by the denominator.
z := add(gt(mod(mul(x, y), denominator), 0), div(mul(x, y), denominator))
}
}
function rpow(
uint256 x,
uint256 n,
uint256 scalar
) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
switch x
case 0 {
switch n
case 0 {
// 0 ** 0 = 1
z := scalar
}
default {
// 0 ** n = 0
z := 0
}
}
default {
switch mod(n, 2)
case 0 {
// If n is even, store scalar in z for now.
z := scalar
}
default {
// If n is odd, store x in z for now.
z := x
}
// Shifting right by 1 is like dividing by 2.
let half := shr(1, scalar)
for {
// Shift n right by 1 before looping to halve it.
n := shr(1, n)
} n {
// Shift n right by 1 each iteration to halve it.
n := shr(1, n)
} {
// Revert immediately if x ** 2 would overflow.
// Equivalent to iszero(eq(div(xx, x), x)) here.
if shr(128, x) {
revert(0, 0)
}
// Store x squared.
let xx := mul(x, x)
// Round to the nearest number.
let xxRound := add(xx, half)
// Revert if xx + half overflowed.
if lt(xxRound, xx) {
revert(0, 0)
}
// Set x to scaled xxRound.
x := div(xxRound, scalar)
// If n is even:
if mod(n, 2) {
// Compute z * x.
let zx := mul(z, x)
// If z * x overflowed:
if iszero(eq(div(zx, x), z)) {
// Revert if x is non-zero.
if iszero(iszero(x)) {
revert(0, 0)
}
}
// Round to the nearest number.
let zxRound := add(zx, half)
// Revert if zx + half overflowed.
if lt(zxRound, zx) {
revert(0, 0)
}
// Return properly scaled zxRound.
z := div(zxRound, scalar)
}
}
}
}
}
/*//////////////////////////////////////////////////////////////
GENERAL NUMBER UTILITIES
//////////////////////////////////////////////////////////////*/
function sqrt(uint256 x) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
let y := x // We start y at x, which will help us make our initial estimate.
z := 181 // The "correct" value is 1, but this saves a multiplication later.
// This segment is to get a reasonable initial estimate for the Babylonian method. With a bad
// start, the correct # of bits increases ~linearly each iteration instead of ~quadratically.
// We check y >= 2^(k + 8) but shift right by k bits
// each branch to ensure that if x >= 256, then y >= 256.
if iszero(lt(y, 0x10000000000000000000000000000000000)) {
y := shr(128, y)
z := shl(64, z)
}
if iszero(lt(y, 0x1000000000000000000)) {
y := shr(64, y)
z := shl(32, z)
}
if iszero(lt(y, 0x10000000000)) {
y := shr(32, y)
z := shl(16, z)
}
if iszero(lt(y, 0x1000000)) {
y := shr(16, y)
z := shl(8, z)
}
// Goal was to get z*z*y within a small factor of x. More iterations could
// get y in a tighter range. Currently, we will have y in [256, 256*2^16).
// We ensured y >= 256 so that the relative difference between y and y+1 is small.
// That's not possible if x < 256 but we can just verify those cases exhaustively.
// Now, z*z*y <= x < z*z*(y+1), and y <= 2^(16+8), and either y >= 256, or x < 256.
// Correctness can be checked exhaustively for x < 256, so we assume y >= 256.
// Then z*sqrt(y) is within sqrt(257)/sqrt(256) of sqrt(x), or about 20bps.
// For s in the range [1/256, 256], the estimate f(s) = (181/1024) * (s+1) is in the range
// (1/2.84 * sqrt(s), 2.84 * sqrt(s)), with largest error when s = 1 and when s = 256 or 1/256.
// Since y is in [256, 256*2^16), let a = y/65536, so that a is in [1/256, 256). Then we can estimate
// sqrt(y) using sqrt(65536) * 181/1024 * (a + 1) = 181/4 * (y + 65536)/65536 = 181 * (y + 65536)/2^18.
// There is no overflow risk here since y < 2^136 after the first branch above.
z := shr(18, mul(z, add(y, 65536))) // A mul() is saved from starting z at 181.
// Given the worst case multiplicative error of 2.84 above, 7 iterations should be enough.
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
// If x+1 is a perfect square, the Babylonian method cycles between
// floor(sqrt(x)) and ceil(sqrt(x)). This statement ensures we return floor.
// See: https://en.wikipedia.org/wiki/Integer_square_root#Using_only_integer_division
// Since the ceil is rare, we save gas on the assignment and repeat division in the rare case.
// If you don't care whether the floor or ceil square root is returned, you can remove this statement.
z := sub(z, lt(div(x, z), z))
}
}
function unsafeMod(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Mod x by y. Note this will return
// 0 instead of reverting if y is zero.
z := mod(x, y)
}
}
function unsafeDiv(uint256 x, uint256 y) internal pure returns (uint256 r) {
/// @solidity memory-safe-assembly
assembly {
// Divide x by y. Note this will return
// 0 instead of reverting if y is zero.
r := div(x, y)
}
}
function unsafeDivUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Add 1 to x * y if x % y > 0. Note this will
// return 0 instead of reverting if y is zero.
z := add(gt(mod(x, y), 0), div(x, y))
}
}
}
"
},
"src/interfaces/IOracle.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
interface IOracle {
/// @notice price of a token expressed in a reference token.
/// @dev be mindful of the decimals here, because if quote token
/// doesn't have 18 decimals, value is used to scale the decimals.
/// For example, for USDC quote (6 decimals) expressed in
/// DAI reference (18 decimals), value should be around ~1e30,
/// so that price is:
/// 1e6 * 1e30 / WAD (1e18)
/// ~= WAD (1e18)
/// ~= 1:1
function price() external view returns (uint256);
}
"
},
"src/interfaces/pendle/ISYToken.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
interface ISYToken {
function getAbsoluteSupplyCap() external view returns (uint256);
function getAbsoluteTotalSupply() external view returns (uint256);
function assetInfo() external view returns (uint8 assetType, address assetAddress, uint8 assetDecimals);
function yieldToken() external view returns (address);
}
"
},
"src/libraries/CoreRoles.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
/// @notice Holds a complete list of all roles which can be held by contracts inside the InfiniFi protocol.
library CoreRoles {
/// ----------- Core roles for access control --------------
/// @notice the all-powerful role. Controls all other roles and protocol functionality.
bytes32 internal constant GOVERNOR = keccak256("GOVERNOR");
/// @notice Can pause contracts in an emergency.
bytes32 internal constant PAUSE = keccak256("PAUSE");
/// @notice Can unpause contracts after an emergency.
bytes32 internal constant UNPAUSE = keccak256("UNPAUSE");
/// @notice can tweak protocol parameters
bytes32 internal constant PROTOCOL_PARAMETERS = keccak256("PROTOCOL_PARAMETERS");
/// @notice can manage minor roles
bytes32 internal constant MINOR_ROLES_MANAGER = keccak256("MINOR_ROLES_MANAGER");
/// ----------- User Flow Management -----------------------
/// @notice Granted to the user entry point of the system
bytes32 internal constant ENTRY_POINT = keccak256("ENTRY_POINT");
/// ----------- Token Management ---------------------------
/// @notice can mint DebtToken arbitrarily
bytes32 internal constant RECEIPT_TOKEN_MINTER = keccak256("RECEIPT_TOKEN_MINTER");
/// @notice can burn DebtToken tokens
bytes32 internal constant RECEIPT_TOKEN_BURNER = keccak256("RECEIPT_TOKEN_BURNER");
/// @notice can mint arbitrarily & burn held LockedPositionToken
bytes32 internal constant LOCKED_TOKEN_MANAGER = keccak256("LOCKED_TOKEN_MANAGER");
/// @notice can prevent transfers of LockedPositionToken
bytes32 internal constant TRANSFER_RESTRICTOR = keccak256("TRANSFER_RESTRICTOR");
/// ----------- Funds Management & Accounting --------------
/// @notice contract that can allocate funds between farms
bytes32 internal constant FARM_MANAGER = keccak256("FARM_MANAGER");
/// @notice addresses who can use the manual rebalancer
bytes32 internal constant MANUAL_REBALANCER = keccak256("MANUAL_REBALANCER");
/// @notice addresses who can use the periodic rebalancer
bytes32 internal constant PERIODIC_REBALANCER = keccak256("PERIODIC_REBALANCER");
/// @notice addresses who can move funds from farms to a safe address
bytes32 internal constant EMERGENCY_WITHDRAWAL = keccak256("EMERGENCY_WITHDRAWAL");
/// @notice addresses who can trigger swaps in Farms
bytes32 internal constant FARM_SWAP_CALLER = keccak256("FARM_SWAP_CALLER");
/// @notice can set oracles references within the system
bytes32 internal constant ORACLE_MANAGER = keccak256("ORACLE_MANAGER");
/// @notice trusted to report profit and losses in the system.
/// This role can be used to slash depositors in case of losses, and
/// can also deposit profits for distribution to end users.
bytes32 internal constant FINANCE_MANAGER = keccak256("FINANCE_MANAGER");
/// ----------- Timelock management ------------------------
/// The hashes are the same as OpenZeppelins's roles in TimelockController
/// @notice can propose new actions in timelocks
bytes32 internal constant PROPOSER_ROLE = keccak256("PROPOSER_ROLE");
/// @notice can execute actions in timelocks after their delay
bytes32 internal constant EXECUTOR_ROLE = keccak256("EXECUTOR_ROLE");
/// @notice can cancel actions in timelocks
bytes32 internal constant CANCELLER_ROLE = keccak256("CANCELLER_ROLE");
}
"
},
"src/finance/Accounting.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
import {FixedPointMathLib} from "@solmate/src/utils/FixedPointMathLib.sol";
import {IFarm} from "@interfaces/IFarm.sol";
import {IOracle} from "@interfaces/IOracle.sol";
import {CoreRoles} from "@libraries/CoreRoles.sol";
import {FarmRegistry} from "@integrations/FarmRegistry.sol";
import {CoreControlled} from "@core/CoreControlled.sol";
import {FixedPriceOracle} from "@finance/oracles/FixedPriceOracle.sol";
/// @notice InfiniFi Accounting contract
contract Accounting is CoreControlled {
using FixedPointMathLib for uint256;
event PriceSet(uint256 indexed timestamp, address indexed asset, uint256 price);
event OracleSet(uint256 indexed timestamp, address indexed asset, address oracle);
/// @notice reference to the farm registry
address public immutable farmRegistry;
constructor(address _core, address _farmRegistry) CoreControlled(_core) {
farmRegistry = _farmRegistry;
}
/// @notice mapping from asset to oracle
mapping(address => address) public oracle;
/// @notice returns the price of an asset
function price(address _asset) external view returns (uint256) {
return IOracle(oracle[_asset]).price();
}
/// @notice set the oracle for an asset
function setOracle(address _asset, address _oracle) external onlyCoreRole(CoreRoles.ORACLE_MANAGER) {
oracle[_asset] = _oracle;
emit OracleSet(block.timestamp, _asset, _oracle);
}
/// -------------------------------------------------------------------------------------------
/// Reference token getters (e.g. USD for iUSD, ETH for iETH, ...)
/// @dev note that the "USD" token does not exist, it is just an abstract unit of account
/// used in the protocol to represent stablecoins pegged to USD, that allows to uniformly
/// account for a diverse reserve composed of USDC, DAI, FRAX, etc.
/// -------------------------------------------------------------------------------------------
/// @notice returns the sum of the value of all assets held on protocol contracts listed in the farm registry.
function totalAssetsValue() external view returns (uint256 _totalValue) {
address[] memory assets = FarmRegistry(farmRegistry).getEnabledAssets();
for (uint256 i = 0; i < assets.length; i++) {
uint256 assetPrice = IOracle(oracle[assets[i]]).price();
uint256 _assets = _calculateTotalAssets(FarmRegistry(farmRegistry).getAssetFarms(assets[i]));
_totalValue += _assets.mulWadDown(assetPrice);
}
}
/// @notice returns the sum of the value of all liquid assets held on protocol contracts listed in the farm registry.
/// @dev see totalAssetsValue()
function totalAssetsValueOf(uint256 _type) external view returns (uint256 _totalValue) {
address[] memory assets = FarmRegistry(farmRegistry).getEnabledAssets();
for (uint256 i = 0; i < assets.length; i++) {
uint256 assetPrice = IOracle(oracle[assets[i]]).price();
address[] memory assetFarms = FarmRegistry(farmRegistry).getAssetTypeFarms(assets[i], uint256(_type));
uint256 _assets = _calculateTotalAssets(assetFarms);
_totalValue += _assets.mulWadDown(assetPrice);
}
}
/// -------------------------------------------------------------------------------------------
/// Specific asset getters (e.g. USDC, DAI, ...)
/// -------------------------------------------------------------------------------------------
/// @notice returns the sum of the balance of all farms of a given asset.
function totalAssets(address _asset) external view returns (uint256) {
return _calculateTotalAssets(FarmRegistry(farmRegistry).getAssetFarms(_asset));
}
function totalAssetsOf(address _asset, uint256 _type) external view returns (uint256) {
return _calculateTotalAssets(FarmRegistry(farmRegistry).getAssetTypeFarms(_asset, uint256(_type)));
}
/// -------------------------------------------------------------------------------------------
/// Internal helpers
/// -------------------------------------------------------------------------------------------
function _calculateTotalAssets(address[] memory _farms) internal view returns (uint256 _totalAssets) {
uint256 length = _farms.length;
for (uint256 index = 0; index < length; index++) {
_totalAssets += IFarm(_farms[index]).assets();
}
}
}
"
},
"src/interfaces/pendle/IPendleMarket.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
interface IPendleMarket {
function readTokens() external view returns (address sy, address pt, address yt);
function expiry() external view returns (uint256 timestamp);
}
"
},
"src/interfaces/pendle/IPendleOracle.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
interface IPendleOracle {
/// @notice Get the PT to SY rate
/// @param market The address of the Pendle market
/// @param twapDuration The duration of the TWAP
/// @return The PT to SY rate with 18 decimals of precision
function getPtToSyRate(address market, uint32 twapDuration) external view returns (uint256);
/// @notice Get the PT to asset rate
/// @param market The address of the Pendle market
/// @param twapDuration The duration of the TWAP
/// @return The PT to asset rate with 18 decimals of precision
function getPtToAssetRate(address market, uint32 twapDuration) external view returns (uint256);
}
"
},
"src/integrations/MultiAssetFarm.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {FixedPointMathLib} from "@solmate/src/utils/FixedPointMathLib.sol";
import {CoreRoles} from "@libraries/CoreRoles.sol";
import {Accounting} from "@finance/Accounting.sol";
import {Farm, IFarm} from "@integrations/Farm.sol";
/// @notice InfiniFi Farm that can hold multiple asset tokens.
abstract contract MultiAssetFarm is Farm {
using SafeERC20 for IERC20;
using FixedPointMathLib for uint256;
/// @notice reference to the accounting contract
address public immutable accounting;
error InvalidAsset(address asset);
error InvalidFarm(address farm);
constructor(address _core, address _assetToken, address _accounting) Farm(_core, _assetToken) {
accounting = _accounting;
}
/// @notice the asset tokens that the farm can hold.
/// @dev MUST include the assetToken of the farm.
/// @dev MUST only include tokens that can be freely airdropped to the farm
/// while being accounted properly in the assets() function.
function assetTokens() public view virtual returns (address[] memory);
/// @notice return true if the farm can hold the given asset token.
function isAssetSupported(address _asset) public view virtual returns (bool);
/// @dev note that there may be conversion fees between supported assets and the assetToken.
/// This is not reflected in the amount returned by assets().
function assets() public view virtual override returns (uint256) {
uint256 assetTokenBalance = IERC20(assetToken).balanceOf(address(this));
uint256 assetTokenPrice = Accounting(accounting).price(assetToken);
address[] memory supportedAssets = assetTokens();
for (uint256 i = 0; i < supportedAssets.length; i++) {
if (supportedAssets[i] == assetToken) continue;
uint256 balance = IERC20(supportedAssets[i]).balanceOf(address(this));
uint256 price = Accounting(accounting).price(supportedAssets[i]);
assetTokenBalance += balance.mulDivDown(price, assetTokenPrice);
}
return assetTokenBalance;
}
/// @notice Current liquidity of the farm is the held reference assetToken.
function liquidity() public view override returns (uint256) {
return IERC20(assetToken).balanceOf(address(this));
}
/// @dev Deposit does nothing, assetTokens are just held on this farm.
/// @dev There should be other functions to do conversions between the assetTokens or deploying
/// the funds to a productive yield source.
function _deposit(uint256) internal view virtual override {}
function deposit() external virtual override onlyCoreRole(CoreRoles.FARM_MANAGER) whenNotPaused {
uint256 currentAssets = assets();
if (currentAssets > cap) {
revert CapExceeded(currentAssets, cap);
}
_deposit(0);
/// @dev note that in airdrops we do not know the amount of assets before the deposit,
/// therefore we emit an event that contains twice the assets after the deposit.
emit AssetsUpdated(block.timestamp, currentAssets, currentAssets);
}
/// @dev Withdrawal can only handle the reference assetToken (i.e. the liquidity()).
/// @dev There should be other functions to do conversions between the assetTokens or pulling
/// the funds out of a productive yield source.
function _withdraw(uint256 _amount, address _to) internal virtual override {
IERC20(assetToken).safeTransfer(_to, _amount);
}
/// @notice withdraw the reference assetToken.
function withdraw(uint256 amount, address to)
external
virtual
override
onlyCoreRole(CoreRoles.FARM_MANAGER)
whenNotPaused
{
uint256 assetsBefore = assets();
_withdraw(amount, to);
emit AssetsUpdated(block.timestamp, assetsBefore, assetsBefore - amount);
}
/// @notice function used to withdraw any supported assetTokens.
function withdrawSecondaryAsset(address _asset, uint256 _amount, address _to)
external
onlyCoreRole(CoreRoles.FARM_MANAGER)
whenNotPaused
{
require(isAssetSupported(_asset) && _asset != assetToken, InvalidAsset(_asset));
uint256 assetsBefore = assets();
IERC20(_asset).safeTransfer(_to, _amount);
uint256 assetsAfter = assets();
emit AssetsUpdated(block.timestamp, assetsBefore, assetsAfter);
}
}
"
},
"src/integrations/farms/CoWSwapFarmBase.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {GPv2Settlement} from "@cowprotocol/contracts/GPv2Settlement.sol";
import {FixedPointMathLib} from "@solmate/src/utils/FixedPointMathLib.sol";
import {GPv2Order, IERC20 as ICoWERC20} from "@cowprotocol/contracts/libraries/GPv2Order.sol";
import {Farm} from "@integrations/Farm.sol";
import {Accounting} from "@finance/Accounting.sol";
import {MultiAssetFarm} from "@integrations/MultiAssetFarm.sol";
/// @title CoWSwap Farm Base
abstract contract CoWSwapFarmBase is MultiAssetFarm {
using SafeERC20 for IERC20;
using FixedPointMathLib for uint256;
event OrderSigned(
uint256 indexed timestamp, bytes orderUid, GPv2Order.Data order, uint32 validTo, uint256 buyAmount
);
error SwapCooldown();
error InvalidToken(address token);
error InvalidAmountIn(uint256 amountIn);
error InvalidAmountOut(uint256 minOut, uint256 provided);
/// @notice timestamp of last order
uint256 public lastOrderSignTimestamp = 1;
/// @notice cooldown period between order signings
uint256 public constant _SIGN_COOLDOWN = 20 minutes;
/// @notice address of the GPv2Settlement contract
address public immutable settlementContract;
/// @notice address of the GPv2VaultRelayer contract
address public immutable vaultRelayer;
constructor(address _settlementContract, address _vaultRelayer) {
settlementContract = _settlementContract;
vaultRelayer = _vaultRelayer;
}
/// @notice Converts a number of tokens to another token based on oracle rates.
function convert(address _tokenIn, address _tokenOut, uint256 _amountIn) public view returns (uint256) {
uint256 tokenInPrice = Accounting(accounting).price(_tokenIn);
uint256 tokenOutPrice = Accounting(accounting).price(_tokenOut);
return _amountIn.mulDivDown(tokenInPrice, tokenOutPrice);
}
function _checkSwapApproveAndSignOrder(
address _tokenIn,
address _tokenOut,
uint256 _amountIn,
uint256 _minAmountOut,
uint256 _maxSlippage
) internal returns (bytes memory) {
require(_amountIn > 0 && _amountIn <= IERC20(_tokenIn).balanceOf(address(this)), InvalidAmountIn(_amountIn));
require(isAssetSupported(_tokenOut), InvalidToken(_tokenOut));
require(block.timestamp > lastOrderSignTimestamp + _SIGN_COOLDOWN, SwapCooldown());
lastOrderSignTimestamp = block.timestamp;
uint256 minOutSlippage = convert(_tokenIn, _tokenOut, _amountIn).mulWadDown(_maxSlippage);
require(_minAmountOut >= minOutSlippage, InvalidAmountOut(minOutSlippage, _minAmountOut));
IERC20(_tokenIn).forceApprove(vaultRelayer, _amountIn);
return _signOrder(_order(_tokenIn, _tokenOut, _amountIn, _minAmountOut));
}
function _order(address _tokenIn, address _tokenOut, uint256 _amountIn, uint256 _minAmountOut)
internal
view
returns (GPv2Order.Data memory)
{
return GPv2Order.Data({
sellToken: ICoWERC20(_tokenIn),
buyToken: ICoWERC20(_tokenOut),
receiver: address(this),
sellAmount: _amountIn,
buyAmount: _minAmountOut,
validTo: uint32(block.timestamp + _SIGN_COOLDOWN),
// keccak256 {"appCode":"infiniFi","version":"1.0.0","metadata":{}}
appData: 0x3cac71ef99d0dfbf5b937334b5b7ab672b679ba2bbd4d6fe8e0c54a2dab31109,
feeAmount: 0,
kind: GPv2Order.KIND_SELL,
partiallyFillable: false,
sellTokenBalance: GPv2Order.BALANCE_ERC20,
buyTokenBalance: GPv2Order.BALANCE_ERC20
});
}
function _signOrder(GPv2Order.Data memory order) internal returns (bytes memory) {
GPv2Settlement settlement = GPv2Settlement(payable(settlementContract));
bytes32 orderDigest = GPv2Order.hash(order, settlement.domainSeparator());
bytes memory orderUid = new bytes(GPv2Order.UID_LENGTH);
GPv2Order.packOrderUidParams(orderUid, orderDigest, address(this), order.validTo);
settlement.setPreSignature(orderUid, true);
emit OrderSigned(block.timestamp, orderUid, order, order.validTo, order.buyAmount);
return orderUid;
}
}
"
},
"src/interfaces/IMaturityFarm.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
import {IFarm} from "@interfaces/IFarm.sol";
/// @notice Interface for an InfiniFi Farm contract that has a maturity date
/// @dev These farms represent illiquid farm/asset class
interface IMaturityFarm is IFarm {
/// @notice timestamp at which more funds can be made available for withdrawal
function maturity() external view returns (uint256);
}
"
},
"lib/openzeppelin-contracts/contracts/interfaces/IERC1363.sol": {
"content": "// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (interfaces/IERC1363.sol)
pragma solidity ^0.8.20;
import {IERC20} from "./IERC20.sol";
import {IERC165} from "./IERC165.sol";
/**
* @title IERC1363
* @dev Interface of the ERC-1363 standard as defined in the https://eips.ethereum.org/EIPS/eip-1363[ERC-1363].
*
* Defines an extension interface for ERC-20 tokens that supports executing code on a recipient contract
* after `transfer` or `transferFrom`, or code on a spender contract after `approve`, in a single transaction.
*/
interface IERC1363 is IERC20, IERC165 {
/*
* Note: the ERC-165 identifier for this interface is 0xb0202a11.
* 0xb0202a11 ===
* bytes4(keccak256('transferAndCall(address,uint256)')) ^
* bytes4(keccak256('transferAndCall(address,uint256,bytes)')) ^
* bytes4(keccak256('transferFromAndCall(address,address,uint256)')) ^
* bytes4(keccak256('transferFromAndCall(address,address,uint256,bytes)')) ^
* bytes4(keccak256('approveAndCall(address,uint256)')) ^
* bytes4(keccak256('approveAndCall(address,uint256,bytes)'))
*/
/**
* @dev Moves a `value` amount of tokens from the caller's account to `to`
* and then calls {IERC1363Receiver-onTransferReceived} on `to`.
* @param to The address which you want to transfer to.
* @param value The amount of tokens to be transferred.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function transferAndCall(address to, uint256 value) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from the caller's account to `to`
* and then calls {IERC1363Receiver-onTransferReceived} on `to`.
* @param to The address which you want to transfer to.
* @param value The amount of tokens to be transferred.
* @param data Additional data with no specified format, sent in call to `to`.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function transferAndCall(address to, uint256 value, bytes calldata data) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the allowance mechanism
* and then calls {IERC1363Receiver-onTransferReceived} on `to`.
* @param from The address which you want to send
Submitted on: 2025-09-19 17:14:44
Comments
Log in to comment.
No comments yet.