Contract Name:
LiquidationHelper
Contract Source Code:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
import {Address} from "openzeppelin5/utils/Address.sol";
import {IERC20} from "openzeppelin5/token/ERC20/IERC20.sol";
import {IERC3156FlashBorrower} from "../../interfaces/IERC3156FlashBorrower.sol";
import {IPartialLiquidation} from "../../interfaces/IPartialLiquidation.sol";
import {ILiquidationHelper} from "../../interfaces/ILiquidationHelper.sol";
import {ISilo} from "../../interfaces/ISilo.sol";
import {ISiloConfig} from "../../interfaces/ISiloConfig.sol";
import {IWrappedNativeToken} from "../../interfaces/IWrappedNativeToken.sol";
import {DexSwap} from "./DexSwap.sol";
/// @notice LiquidationHelper IS NOT PART OF THE PROTOCOL.
contract LiquidationHelper is ILiquidationHelper, IERC3156FlashBorrower, DexSwap {
using Address for address payable;
// solhint-disable-next-line var-name-mixedcase
bytes32 internal constant _FLASHLOAN_CALLBACK = keccak256("ERC3156FlashBorrower.onFlashLoan");
/// @dev token receiver will get all rewards from liquidation, does not matter who will execute tx
address payable public immutable TOKENS_RECEIVER;
/// @dev address of wrapped native blockchain token eg. WETH on Ethereum
address public immutable NATIVE_TOKEN;
uint256 private transient _withdrawCollateral;
uint256 private transient _repayDebtAssets;
error NoDebtToCover();
error STokenNotSupported();
/// @param _nativeToken address of wrapped native blockchain token eg. WETH on Ethereum
/// @param _exchangeProxy exchange address, where to send swap data on liquidation
/// @param _tokensReceiver all leftover tokens (debt and collateral) will be send to this address after liquidation
constructor (
address _nativeToken,
address _exchangeProxy,
address payable _tokensReceiver
) DexSwap(_exchangeProxy) {
NATIVE_TOKEN = _nativeToken;
EXCHANGE_PROXY = _exchangeProxy;
TOKENS_RECEIVER = _tokensReceiver;
}
receive() external payable {}
/// @inheritdoc ILiquidationHelper
/// @dev entry point for liquidation
/// @notice for now we does not support liquidation with sTokens.
/// On not profitable liquidation we will revert, because we will not be able to repay flashloan with fee
/// (collateral will not be enough to cover loan + fee)
function executeLiquidation(
ISilo _flashLoanFrom,
address _debtAsset,
uint256 _maxDebtToCover,
LiquidationData calldata _liquidation,
DexSwapInput[] calldata _swapsInputs0x
) external virtual returns (uint256 withdrawCollateral, uint256 repayDebtAssets) {
require(_maxDebtToCover != 0, NoDebtToCover());
_flashLoanFrom.flashLoan(this, _debtAsset, _maxDebtToCover, abi.encode(_liquidation, _swapsInputs0x));
IERC20(_debtAsset).approve(address(_flashLoanFrom), 0);
withdrawCollateral = _withdrawCollateral;
repayDebtAssets = _repayDebtAssets;
}
/// @inheritdoc IERC3156FlashBorrower
function onFlashLoan(
address /* _initiator */,
address _debtAsset,
uint256 _maxDebtToCover,
uint256 _fee,
bytes calldata _data
)
external
virtual
returns (bytes32)
{
(
LiquidationData memory _liquidation,
DexSwapInput[] memory _swapInputs
) = abi.decode(_data, (LiquidationData, DexSwapInput[]));
IERC20(_debtAsset).approve(address(_liquidation.hook), _maxDebtToCover);
(
_withdrawCollateral, _repayDebtAssets
) = _liquidation.hook.liquidationCall({
_collateralAsset: _liquidation.collateralAsset,
_debtAsset: _debtAsset,
_user: _liquidation.user,
_maxDebtToCover: _maxDebtToCover,
_receiveSToken: false
});
IERC20(_debtAsset).approve(address(_liquidation.hook), 0);
uint256 flashLoanWithFee = _maxDebtToCover + _fee;
if (_liquidation.collateralAsset == _debtAsset) {
uint256 balance = IERC20(_liquidation.collateralAsset).balanceOf(address(this));
// bad debt is not supported, we will get underflow on bad debt
_transferToReceiver(_liquidation.collateralAsset, balance - flashLoanWithFee);
} else {
// swap all collateral for debt
// most likely there will be dust left in collateral tokens, this dust will be "recovered"
// once we will liquidate "oposite" position
_executeSwap(_swapInputs);
uint256 debtBalance = IERC20(_debtAsset).balanceOf(address(this));
if (flashLoanWithFee < debtBalance) {
unchecked {
// safe because of `if (flashLoanWithFee < debtBalance)`
_transferToReceiver(_debtAsset, debtBalance - flashLoanWithFee);
}
}
}
IERC20(_debtAsset).approve(msg.sender, flashLoanWithFee);
return _FLASHLOAN_CALLBACK;
}
function _executeSwap(DexSwapInput[] memory _swapInputs) internal virtual {
for (uint256 i; i < _swapInputs.length; i++) {
fillQuote(_swapInputs[i].sellToken, _swapInputs[i].allowanceTarget, _swapInputs[i].swapCallData);
}
}
function _transferToReceiver(address _asset, uint256 _amount) internal virtual {
if (_amount == 0) return;
if (_asset == NATIVE_TOKEN) {
_transferNative(_amount);
} else {
IERC20(_asset).transfer(TOKENS_RECEIVER, _amount);
}
}
/// @notice We assume that quoteToken is wrapped native token
function _transferNative(uint256 _amount) internal virtual {
IWrappedNativeToken(address(NATIVE_TOKEN)).withdraw(_amount);
TOKENS_RECEIVER.sendValue(_amount);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/Address.sol)
pragma solidity ^0.8.20;
import {Errors} from "./Errors.sol";
/**
* @dev Collection of functions related to the address type
*/
library Address {
/**
* @dev There's no code at `target` (it is not a contract).
*/
error AddressEmptyCode(address target);
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.8.20/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
if (address(this).balance < amount) {
revert Errors.InsufficientBalance(address(this).balance, amount);
}
(bool success, ) = recipient.call{value: amount}("");
if (!success) {
revert Errors.FailedCall();
}
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason or custom error, it is bubbled
* up by this function (like regular Solidity function calls). However, if
* the call reverted with no returned reason, this function reverts with a
* {Errors.FailedCall} error.
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*/
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
if (address(this).balance < value) {
revert Errors.InsufficientBalance(address(this).balance, value);
}
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, success, returndata);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, success, returndata);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*/
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata);
}
/**
* @dev Tool to verify that a low level call to smart-contract was successful, and reverts if the target
* was not a contract or bubbling up the revert reason (falling back to {Errors.FailedCall}) in case
* of an unsuccessful call.
*/
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata
) internal view returns (bytes memory) {
if (!success) {
_revert(returndata);
} else {
// only check if target is a contract if the call was successful and the return data is empty
// otherwise we already know that it was a contract
if (returndata.length == 0 && target.code.length == 0) {
revert AddressEmptyCode(target);
}
return returndata;
}
}
/**
* @dev Tool to verify that a low level call was successful, and reverts if it wasn't, either by bubbling the
* revert reason or with a default {Errors.FailedCall} error.
*/
function verifyCallResult(bool success, bytes memory returndata) internal pure returns (bytes memory) {
if (!success) {
_revert(returndata);
} else {
return returndata;
}
}
/**
* @dev Reverts with returndata if present. Otherwise reverts with {Errors.FailedCall}.
*/
function _revert(bytes memory returndata) private pure {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
/// @solidity memory-safe-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert Errors.FailedCall();
}
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.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);
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0;
interface IERC3156FlashBorrower {
/// @notice During the execution of the flashloan, Silo methods are not taking into consideration the fact,
/// that some (or all) tokens were transferred as flashloan, therefore some methods can return invalid state
/// eg. maxWithdraw can return amount that are not available to withdraw during flashlon.
/// @dev Receive a flash loan.
/// @param _initiator The initiator of the loan.
/// @param _token The loan currency.
/// @param _amount The amount of tokens lent.
/// @param _fee The additional amount of tokens to repay.
/// @param _data Arbitrary data structure, intended to contain user-defined parameters.
/// @return The keccak256 hash of "ERC3156FlashBorrower.onFlashLoan"
function onFlashLoan(address _initiator, address _token, uint256 _amount, uint256 _fee, bytes calldata _data)
external
returns (bytes32);
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0;
interface IPartialLiquidation {
struct HookSetup {
/// @param this is the same as in siloConfig
address hookReceiver;
/// @param hooks bitmap
uint24 hooksBefore;
/// @param hooks bitmap
uint24 hooksAfter;
}
/// @dev Emitted when a borrower is liquidated.
/// @param liquidator The address of the liquidator
/// @param silo The address of the silo on which position was liquidated
/// @param repayDebtAssets Repay amount
/// @param withdrawCollateral Total (collateral + protected) withdraw amount, in case `receiveSToken` is TRUE
/// then this is estimated withdraw, and representation of this amount in sToken was transferred
/// @param receiveSToken True if the liquidators wants to receive the collateral sTokens, `false` if he wants
/// to receive the underlying collateral asset directly
event LiquidationCall(
address indexed liquidator,
address indexed silo,
uint256 repayDebtAssets,
uint256 withdrawCollateral,
bool receiveSToken
);
/// @dev Revert if provided silo configuration during initialization is empty
error EmptySiloConfig();
/// @dev Revert if the hook receiver is already configured/initialized
error AlreadyConfigured();
error UnexpectedCollateralToken();
error UnexpectedDebtToken();
error NoDebtToCover();
error FullLiquidationRequired();
error UserIsSolvent();
error UnknownRatio();
error NoRepayAssets();
/// @notice Function to liquidate insolvent position
/// - The caller (liquidator) covers `debtToCover` amount of debt of the user getting liquidated, and receives
/// an equivalent amount in `collateralAsset` plus a liquidation fee to cover market risk
/// @dev this method reverts when:
/// - `_maxDebtToCover` is zero
/// - `_collateralAsset` is not `_user` collateral token (note, that user can have both tokens in Silo, but only one
/// is for backing debt
/// - `_debtAsset` is not a token that `_user` borrow
/// - `_user` is solvent and there is no debt to cover
/// - `_maxDebtToCover` is set to cover only part of the debt but full liquidation is required
/// - when not enough liquidity to transfer from `_user` collateral to liquidator
/// (use `_receiveSToken == true` in that case)
/// @param _collateralAsset The address of the underlying asset used as collateral, to receive as result
/// @param _debtAsset The address of the underlying borrowed asset to be repaid with the liquidation
/// @param _user The address of the borrower getting liquidated
/// @param _maxDebtToCover The maximum debt amount of borrowed `asset` the liquidator wants to cover,
/// in case this amount is too big, it will be reduced to maximum allowed liquidation amount
/// @param _receiveSToken True if the liquidators wants to receive the collateral sTokens, `false` if he wants
/// to receive the underlying collateral asset directly
/// @return withdrawCollateral collateral that was send to `msg.sender`, in case of `_receiveSToken` is TRUE,
/// `withdrawCollateral` will be estimated, on redeem one can expect this value to be rounded down
/// @return repayDebtAssets actual debt value that was repaid by `msg.sender`
function liquidationCall(
address _collateralAsset,
address _debtAsset,
address _user,
uint256 _maxDebtToCover,
bool _receiveSToken
)
external
returns (uint256 withdrawCollateral, uint256 repayDebtAssets);
/// @dev debt is keep growing over time, so when dApp use this view to calculate max, tx should never revert
/// because actual max can be only higher
/// @return collateralToLiquidate underestimated amount of collateral liquidator will get
/// @return debtToRepay debt amount needed to be repay to get `collateralToLiquidate`
/// @return sTokenRequired TRUE, when liquidation with underlying asset is not possible because of not enough
/// liquidity
function maxLiquidation(address _borrower)
external
view
returns (uint256 collateralToLiquidate, uint256 debtToRepay, bool sTokenRequired);
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0;
import {ISilo} from "./ISilo.sol";
import {IPartialLiquidation} from "./IPartialLiquidation.sol";
/// @notice LiquidationHelper IS NOT PART OF THE PROTOCOL. SILO CREATED THIS TOOL, MOSTLY AS AN EXAMPLE.
interface ILiquidationHelper {
/// @param sellToken The `sellTokenAddress` field from the API response.
/// @param buyToken The `buyTokenAddress` field from the API response.
/// @param allowanceTarget The `allowanceTarget` field from the API response.
/// @param swapCallData The `data` field from the API response.
struct DexSwapInput {
address sellToken;
address allowanceTarget;
bytes swapCallData;
}
/// @param hook partial liquidation hook address assigned to silo
/// you can get hook address by calling: silo.config().getConfig(_silo).hookReceiver
/// @param collateralAsset address of underlying collateral token of `user` position
/// @param user silo borrower address
struct LiquidationData {
IPartialLiquidation hook;
address collateralAsset;
address user;
}
/// @param _flashLoanFrom silo from where we can flashloan `_maxDebtToCover` amount of `_debtAsset` to repay debt
/// @param _debtAsset address of debt token (underlying token)
/// @param _maxDebtToCover maximum amount we want to repay,
/// you can use `IPartialLiquidation.maxLiquidation()` to get maximum possible value
/// @param _liquidation see desc for `LiquidationData` struct
/// @param _dexSwapInput swap bytes that allows to swap all collateral assets to debt asset,
/// this is optional and required only for two assets position
function executeLiquidation(
ISilo _flashLoanFrom,
address _debtAsset,
uint256 _maxDebtToCover,
LiquidationData calldata _liquidation,
DexSwapInput[] calldata _dexSwapInput
) external returns (uint256 withdrawCollateral, uint256 repayDebtAssets);
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0;
import {IERC4626, IERC20, IERC20Metadata} from "openzeppelin5/interfaces/IERC4626.sol";
import {IERC3156FlashLender} from "./IERC3156FlashLender.sol";
import {ISiloConfig} from "./ISiloConfig.sol";
import {ISiloFactory} from "./ISiloFactory.sol";
import {IHookReceiver} from "./IHookReceiver.sol";
// solhint-disable ordering
interface ISilo is IERC20, IERC4626, IERC3156FlashLender {
/// @dev Interest accrual happens on each deposit/withdraw/borrow/repay. View methods work on storage that might be
/// outdate. Some calculations require accrued interest to return current state of Silo. This struct is used
/// to make a decision inside functions if interest should be accrued in memory to work on updated values.
enum AccrueInterestInMemory {
No,
Yes
}
/// @dev Silo has two separate oracles for solvency and maxLtv calculations. MaxLtv oracle is optional. Solvency
/// oracle can also be optional if asset is used as denominator in Silo config. For example, in ETH/USDC Silo
/// one could setup only solvency oracle for ETH that returns price in USDC. Then USDC does not need an oracle
/// because it's used as denominator for ETH and it's "price" can be assume as 1.
enum OracleType {
Solvency,
MaxLtv
}
/// @dev There are 3 types of accounting in the system: for non-borrowable collateral deposit called "protected",
/// for borrowable collateral deposit called "collateral" and for borrowed tokens called "debt". System does
/// identical calculations for each type of accounting but it uses different data. To avoid code duplication
/// this enum is used to decide which data should be read.
enum AssetType {
Protected, // default
Collateral,
Debt
}
/// @dev There are 2 types of accounting in the system: for non-borrowable collateral deposit called "protected" and
/// for borrowable collateral deposit called "collateral". System does
/// identical calculations for each type of accounting but it uses different data. To avoid code duplication
/// this enum is used to decide which data should be read.
enum CollateralType {
Protected, // default
Collateral
}
/// @dev Types of calls that can be made by the hook receiver on behalf of Silo via `callOnBehalfOfSilo` fn
enum CallType {
Call, // default
Delegatecall
}
/// @param _assets Amount of assets the user wishes to withdraw. Use 0 if shares are provided.
/// @param _shares Shares the user wishes to burn in exchange for the withdrawal. Use 0 if assets are provided.
/// @param _receiver Address receiving the withdrawn assets
/// @param _owner Address of the owner of the shares being burned
/// @param _spender Address executing the withdrawal; may be different than `_owner` if an allowance was set
/// @param _collateralType Type of the asset being withdrawn (Collateral or Protected)
struct WithdrawArgs {
uint256 assets;
uint256 shares;
address receiver;
address owner;
address spender;
ISilo.CollateralType collateralType;
}
/// @param assets Number of assets the borrower intends to borrow. Use 0 if shares are provided.
/// @param shares Number of shares corresponding to the assets that the borrower intends to borrow. Use 0 if
/// assets are provided.
/// @param receiver Address that will receive the borrowed assets
/// @param borrower The user who is borrowing the assets
struct BorrowArgs {
uint256 assets;
uint256 shares;
address receiver;
address borrower;
}
/// @param shares Amount of shares the user wishes to transit.
/// @param owner owner of the shares after transition.
/// @param transitionFrom type of collateral that will be transitioned.
struct TransitionCollateralArgs {
uint256 shares;
address owner;
ISilo.CollateralType transitionFrom;
}
struct UtilizationData {
/// @dev COLLATERAL: Amount of asset token that has been deposited to Silo plus interest earned by depositors.
/// It also includes token amount that has been borrowed.
uint256 collateralAssets;
/// @dev DEBT: Amount of asset token that has been borrowed plus accrued interest.
uint256 debtAssets;
/// @dev timestamp of the last interest accrual
uint64 interestRateTimestamp;
}
struct SiloStorage {
/// @param daoAndDeployerRevenue Current amount of assets (fees) accrued by DAO and Deployer
/// but not yet withdrawn
uint192 daoAndDeployerRevenue;
/// @dev timestamp of the last interest accrual
uint64 interestRateTimestamp;
/// @dev silo is just for one asset,
/// but this one asset can be of three types: mapping key is uint256(AssetType), so we store `assets` by type.
/// Assets based on type:
/// - PROTECTED COLLATERAL: Amount of asset token that has been deposited to Silo that can be ONLY used
/// as collateral. These deposits do NOT earn interest and CANNOT be borrowed.
/// - COLLATERAL: Amount of asset token that has been deposited to Silo plus interest earned by depositors.
/// It also includes token amount that has been borrowed.
/// - DEBT: Amount of asset token that has been borrowed plus accrued interest.
/// `totalAssets` can have outdated value (without interest), if you doing view call (of off-chain call)
/// please use getters eg `getCollateralAssets()` to fetch value that includes interest.
mapping(AssetType assetType => uint256 assets) totalAssets;
}
/// @notice Emitted on protected deposit
/// @param sender wallet address that deposited asset
/// @param owner wallet address that received shares in Silo
/// @param assets amount of asset that was deposited
/// @param shares amount of shares that was minted
event DepositProtected(address indexed sender, address indexed owner, uint256 assets, uint256 shares);
/// @notice Emitted on protected withdraw
/// @param sender wallet address that sent transaction
/// @param receiver wallet address that received asset
/// @param owner wallet address that owned asset
/// @param assets amount of asset that was withdrew
/// @param shares amount of shares that was burn
event WithdrawProtected(
address indexed sender, address indexed receiver, address indexed owner, uint256 assets, uint256 shares
);
/// @notice Emitted on borrow
/// @param sender wallet address that sent transaction
/// @param receiver wallet address that received asset
/// @param owner wallet address that owes assets
/// @param assets amount of asset that was borrowed
/// @param shares amount of shares that was minted
event Borrow(
address indexed sender, address indexed receiver, address indexed owner, uint256 assets, uint256 shares
);
/// @notice Emitted on repayment
/// @param sender wallet address that repaid asset
/// @param owner wallet address that owed asset
/// @param assets amount of asset that was repaid
/// @param shares amount of shares that was burn
event Repay(address indexed sender, address indexed owner, uint256 assets, uint256 shares);
/// @notice emitted only when collateral has been switched to other one
event CollateralTypeChanged(address indexed borrower);
event HooksUpdated(uint24 hooksBefore, uint24 hooksAfter);
event AccruedInterest(uint256 hooksBefore);
event FlashLoan(uint256 amount);
event WithdrawnFeed(uint256 daoFees, uint256 deployerFees);
error Unsupported();
error NothingToWithdraw();
error NotEnoughLiquidity();
error NotSolvent();
error BorrowNotPossible();
error EarnedZero();
error FlashloanFailed();
error AboveMaxLtv();
error SiloInitialized();
error OnlyHookReceiver();
error NoLiquidity();
error InputCanBeAssetsOrShares();
error CollateralSiloAlreadySet();
error RepayTooHigh();
error ZeroAmount();
error InputZeroShares();
error ReturnZeroAssets();
error ReturnZeroShares();
/// @return siloFactory The associated factory of the silo
function factory() external view returns (ISiloFactory siloFactory);
/// @notice Method for HookReceiver only to call on behalf of Silo
/// @param _target address of the contract to call
/// @param _value amount of ETH to send
/// @param _callType type of the call (Call or Delegatecall)
/// @param _input calldata for the call
function callOnBehalfOfSilo(address _target, uint256 _value, CallType _callType, bytes calldata _input)
external
payable
returns (bool success, bytes memory result);
/// @notice Initialize Silo
/// @param _siloConfig address of ISiloConfig with full config for this Silo
function initialize(ISiloConfig _siloConfig) external;
/// @notice Update hooks configuration for Silo
/// @dev This function must be called after the hooks configuration is changed in the hook receiver
function updateHooks() external;
/// @notice Fetches the silo configuration contract
/// @return siloConfig Address of the configuration contract associated with the silo
function config() external view returns (ISiloConfig siloConfig);
/// @notice Fetches the utilization data of the silo used by IRM
function utilizationData() external view returns (UtilizationData memory utilizationData);
/// @notice Fetches the real (available to borrow) liquidity in the silo, it does include interest
/// @return liquidity The amount of liquidity
function getLiquidity() external view returns (uint256 liquidity);
/// @notice Determines if a borrower is solvent
/// @param _borrower Address of the borrower to check for solvency
/// @return True if the borrower is solvent, otherwise false
function isSolvent(address _borrower) external view returns (bool);
/// @notice Retrieves the raw total amount of assets based on provided type (direct storage access)
function getTotalAssetsStorage(AssetType _assetType) external view returns (uint256);
/// @notice Direct storage access to silo storage
/// @dev See struct `SiloStorage` for more details
function getSiloStorage()
external
view
returns (
uint192 daoAndDeployerRevenue,
uint64 interestRateTimestamp,
uint256 protectedAssets,
uint256 collateralAssets,
uint256 debtAssets
);
/// @notice Retrieves the total amount of collateral (borrowable) assets with interest
/// @return totalCollateralAssets The total amount of assets of type 'Collateral'
function getCollateralAssets() external view returns (uint256 totalCollateralAssets);
/// @notice Retrieves the total amount of debt assets with interest
/// @return totalDebtAssets The total amount of assets of type 'Debt'
function getDebtAssets() external view returns (uint256 totalDebtAssets);
/// @notice Retrieves the total amounts of collateral and protected (non-borrowable) assets
/// @return totalCollateralAssets The total amount of assets of type 'Collateral'
/// @return totalProtectedAssets The total amount of protected (non-borrowable) assets
function getCollateralAndProtectedTotalsStorage()
external
view
returns (uint256 totalCollateralAssets, uint256 totalProtectedAssets);
/// @notice Retrieves the total amounts of collateral and debt assets
/// @return totalCollateralAssets The total amount of assets of type 'Collateral'
/// @return totalDebtAssets The total amount of debt assets of type 'Debt'
function getCollateralAndDebtTotalsStorage()
external
view
returns (uint256 totalCollateralAssets, uint256 totalDebtAssets);
/// @notice Implements IERC4626.convertToShares for each asset type
function convertToShares(uint256 _assets, AssetType _assetType) external view returns (uint256 shares);
/// @notice Implements IERC4626.convertToAssets for each asset type
function convertToAssets(uint256 _shares, AssetType _assetType) external view returns (uint256 assets);
/// @notice Implements IERC4626.previewDeposit for protected (non-borrowable) collateral and collateral
/// @dev Reverts for debt asset type
function previewDeposit(uint256 _assets, CollateralType _collateralType) external view returns (uint256 shares);
/// @notice Implements IERC4626.deposit for protected (non-borrowable) collateral and collateral
/// @dev Reverts for debt asset type
function deposit(uint256 _assets, address _receiver, CollateralType _collateralType)
external
returns (uint256 shares);
/// @notice Implements IERC4626.previewMint for protected (non-borrowable) collateral and collateral
/// @dev Reverts for debt asset type
function previewMint(uint256 _shares, CollateralType _collateralType) external view returns (uint256 assets);
/// @notice Implements IERC4626.mint for protected (non-borrowable) collateral and collateral
/// @dev Reverts for debt asset type
function mint(uint256 _shares, address _receiver, CollateralType _collateralType) external returns (uint256 assets);
/// @notice Implements IERC4626.maxWithdraw for protected (non-borrowable) collateral and collateral
/// @dev Reverts for debt asset type
function maxWithdraw(address _owner, CollateralType _collateralType) external view returns (uint256 maxAssets);
/// @notice Implements IERC4626.previewWithdraw for protected (non-borrowable) collateral and collateral
/// @dev Reverts for debt asset type
function previewWithdraw(uint256 _assets, CollateralType _collateralType) external view returns (uint256 shares);
/// @notice Implements IERC4626.withdraw for protected (non-borrowable) collateral and collateral
/// @dev Reverts for debt asset type
function withdraw(uint256 _assets, address _receiver, address _owner, CollateralType _collateralType)
external
returns (uint256 shares);
/// @notice Implements IERC4626.maxRedeem for protected (non-borrowable) collateral and collateral
/// @dev Reverts for debt asset type
function maxRedeem(address _owner, CollateralType _collateralType) external view returns (uint256 maxShares);
/// @notice Implements IERC4626.previewRedeem for protected (non-borrowable) collateral and collateral
/// @dev Reverts for debt asset type
function previewRedeem(uint256 _shares, CollateralType _collateralType) external view returns (uint256 assets);
/// @notice Implements IERC4626.redeem for protected (non-borrowable) collateral and collateral
/// @dev Reverts for debt asset type
function redeem(uint256 _shares, address _receiver, address _owner, CollateralType _collateralType)
external
returns (uint256 assets);
/// @notice Calculates the maximum amount of assets that can be borrowed by the given address
/// @param _borrower Address of the potential borrower
/// @return maxAssets Maximum amount of assets that the borrower can borrow, this value is underestimated
/// That means, in some cases when you borrow maxAssets, you will be able to borrow again eg. up to 2wei
/// Reason for underestimation is to return value that will not cause borrow revert
function maxBorrow(address _borrower) external view returns (uint256 maxAssets);
/// @notice Previews the amount of shares equivalent to the given asset amount for borrowing
/// @param _assets Amount of assets to preview the equivalent shares for
/// @return shares Amount of shares equivalent to the provided asset amount
function previewBorrow(uint256 _assets) external view returns (uint256 shares);
/// @notice Allows an address to borrow a specified amount of assets
/// @param _assets Amount of assets to borrow
/// @param _receiver Address receiving the borrowed assets
/// @param _borrower Address responsible for the borrowed assets
/// @return shares Amount of shares equivalent to the borrowed assets
function borrow(uint256 _assets, address _receiver, address _borrower)
external returns (uint256 shares);
/// @notice Calculates the maximum amount of shares that can be borrowed by the given address
/// @param _borrower Address of the potential borrower
/// @return maxShares Maximum number of shares that the borrower can borrow
function maxBorrowShares(address _borrower) external view returns (uint256 maxShares);
/// @notice Previews the amount of assets equivalent to the given share amount for borrowing
/// @param _shares Amount of shares to preview the equivalent assets for
/// @return assets Amount of assets equivalent to the provided share amount
function previewBorrowShares(uint256 _shares) external view returns (uint256 assets);
/// @notice Calculates the maximum amount of assets that can be borrowed by the given address
/// @param _borrower Address of the potential borrower
/// @return maxAssets Maximum amount of assets that the borrower can borrow, this value is underestimated
/// That means, in some cases when you borrow maxAssets, you will be able to borrow again eg. up to 2wei
/// Reason for underestimation is to return value that will not cause borrow revert
function maxBorrowSameAsset(address _borrower) external view returns (uint256 maxAssets);
/// @notice Allows an address to borrow a specified amount of assets that will be back up with deposit made with the
/// same asset
/// @param _assets Amount of assets to borrow
/// @param _receiver Address receiving the borrowed assets
/// @param _borrower Address responsible for the borrowed assets
/// @return shares Amount of shares equivalent to the borrowed assets
function borrowSameAsset(uint256 _assets, address _receiver, address _borrower)
external returns (uint256 shares);
/// @notice Allows a user to borrow assets based on the provided share amount
/// @param _shares Amount of shares to borrow against
/// @param _receiver Address to receive the borrowed assets
/// @param _borrower Address responsible for the borrowed assets
/// @return assets Amount of assets borrowed
function borrowShares(uint256 _shares, address _receiver, address _borrower)
external
returns (uint256 assets);
/// @notice Calculates the maximum amount an address can repay based on their debt shares
/// @param _borrower Address of the borrower
/// @return assets Maximum amount of assets the borrower can repay
function maxRepay(address _borrower) external view returns (uint256 assets);
/// @notice Provides an estimation of the number of shares equivalent to a given asset amount for repayment
/// @param _assets Amount of assets to be repaid
/// @return shares Estimated number of shares equivalent to the provided asset amount
function previewRepay(uint256 _assets) external view returns (uint256 shares);
/// @notice Repays a given asset amount and returns the equivalent number of shares
/// @param _assets Amount of assets to be repaid
/// @param _borrower Address of the borrower whose debt is being repaid
/// @return shares The equivalent number of shares for the provided asset amount
function repay(uint256 _assets, address _borrower) external returns (uint256 shares);
/// @notice Calculates the maximum number of shares that can be repaid for a given borrower
/// @param _borrower Address of the borrower
/// @return shares The maximum number of shares that can be repaid for the borrower
function maxRepayShares(address _borrower) external view returns (uint256 shares);
/// @notice Provides a preview of the equivalent assets for a given number of shares to repay
/// @param _shares Number of shares to preview repayment for
/// @return assets Equivalent assets for the provided shares
function previewRepayShares(uint256 _shares) external view returns (uint256 assets);
/// @notice Allows a user to repay a loan using shares instead of assets
/// @param _shares The number of shares the borrower wants to repay with
/// @param _borrower The address of the borrower for whom to repay the loan
/// @return assets The equivalent assets amount for the provided shares
function repayShares(uint256 _shares, address _borrower) external returns (uint256 assets);
/// @notice Transitions assets between borrowable (collateral) and non-borrowable (protected) states
/// @dev This function allows assets to move between collateral and protected (non-borrowable) states without
/// leaving the protocol
/// @param _shares Amount of shares to be transitioned
/// @param _owner Owner of the assets being transitioned
/// @param _transitionFrom Specifies if the transition is from collateral or protected assets
/// @return assets Amount of assets transitioned
function transitionCollateral(uint256 _shares, address _owner, CollateralType _transitionFrom)
external
returns (uint256 assets);
/// @notice Switches the collateral silo to this silo
/// @dev Revert if the collateral silo is already set
function switchCollateralToThisSilo() external;
/// @notice Accrues interest for the asset and returns the accrued interest amount
/// @return accruedInterest The total interest accrued during this operation
function accrueInterest() external returns (uint256 accruedInterest);
/// @notice only for SiloConfig
function accrueInterestForConfig(
address _interestRateModel,
uint256 _daoFee,
uint256 _deployerFee
) external;
/// @notice Withdraws earned fees and distributes them to the DAO and deployer fee receivers
function withdrawFees() external;
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0;
import {ISilo} from "./ISilo.sol";
import {ICrossReentrancyGuard} from "./ICrossReentrancyGuard.sol";
interface ISiloConfig is ICrossReentrancyGuard {
struct InitData {
/// @notice Can be address zero if deployer fees are not to be collected. If deployer address is zero then
/// deployer fee must be zero as well. Deployer will be minted an NFT that gives the right to claim deployer
/// fees. NFT can be transferred with the right to claim.
address deployer;
/// @notice Address of the hook receiver called on every before/after action on Silo. Hook contract also
/// implements liquidation logic and veSilo gauge connection.
address hookReceiver;
/// @notice Deployer's fee in 18 decimals points. Deployer will earn this fee based on the interest earned
/// by the Silo. Max deployer fee is set by the DAO. At deployment it is 15%.
uint256 deployerFee;
/// @notice DAO's fee in 18 decimals points. DAO will earn this fee based on the interest earned
/// by the Silo. Acceptable fee range fee is set by the DAO. Default at deployment is 5% - 50%.
uint256 daoFee;
/// @notice Address of the first token
address token0;
/// @notice Address of the solvency oracle. Solvency oracle is used to calculate LTV when deciding if borrower
/// is solvent or should be liquidated. Solvency oracle is optional and if not set price of 1 will be assumed.
address solvencyOracle0;
/// @notice Address of the maxLtv oracle. Max LTV oracle is used to calculate LTV when deciding if borrower
/// can borrow given amount of assets. Max LTV oracle is optional and if not set it defaults to solvency
/// oracle. If neither is set price of 1 will be assumed.
address maxLtvOracle0;
/// @notice Address of the interest rate model
address interestRateModel0;
/// @notice Maximum LTV for first token. maxLTV is in 18 decimals points and is used to determine, if borrower
/// can borrow given amount of assets. MaxLtv is in 18 decimals points. MaxLtv must be lower or equal to LT.
uint256 maxLtv0;
/// @notice Liquidation threshold for first token. LT is used to calculate solvency. LT is in 18 decimals
/// points. LT must not be lower than maxLTV.
uint256 lt0;
/// @notice minimal acceptable LTV after liquidation, in 18 decimals points
uint256 liquidationTargetLtv0;
/// @notice Liquidation fee for the first token in 18 decimals points. Liquidation fee is what liquidator earns
/// for repaying insolvent loan.
uint256 liquidationFee0;
/// @notice Flashloan fee sets the cost of taking a flashloan in 18 decimals points
uint256 flashloanFee0;
/// @notice Indicates if a beforeQuote on oracle contract should be called before quoting price
bool callBeforeQuote0;
/// @notice Address of the second token
address token1;
/// @notice Address of the solvency oracle. Solvency oracle is used to calculate LTV when deciding if borrower
/// is solvent or should be liquidated. Solvency oracle is optional and if not set price of 1 will be assumed.
address solvencyOracle1;
/// @notice Address of the maxLtv oracle. Max LTV oracle is used to calculate LTV when deciding if borrower
/// can borrow given amount of assets. Max LTV oracle is optional and if not set it defaults to solvency
/// oracle. If neither is set price of 1 will be assumed.
address maxLtvOracle1;
/// @notice Address of the interest rate model
address interestRateModel1;
/// @notice Maximum LTV for first token. maxLTV is in 18 decimals points and is used to determine,
/// if borrower can borrow given amount of assets. maxLtv is in 18 decimals points
uint256 maxLtv1;
/// @notice Liquidation threshold for first token. LT is used to calculate solvency. LT is in 18 decimals points
uint256 lt1;
/// @notice minimal acceptable LTV after liquidation, in 18 decimals points
uint256 liquidationTargetLtv1;
/// @notice Liquidation fee is what liquidator earns for repaying insolvent loan.
uint256 liquidationFee1;
/// @notice Flashloan fee sets the cost of taking a flashloan in 18 decimals points
uint256 flashloanFee1;
/// @notice Indicates if a beforeQuote on oracle contract should be called before quoting price
bool callBeforeQuote1;
}
struct ConfigData {
uint256 daoFee;
uint256 deployerFee;
address silo;
address token;
address protectedShareToken;
address collateralShareToken;
address debtShareToken;
address solvencyOracle;
address maxLtvOracle;
address interestRateModel;
uint256 maxLtv;
uint256 lt;
uint256 liquidationTargetLtv;
uint256 liquidationFee;
uint256 flashloanFee;
address hookReceiver;
bool callBeforeQuote;
}
struct DepositConfig {
address silo;
address token;
address collateralShareToken;
address protectedShareToken;
uint256 daoFee;
uint256 deployerFee;
address interestRateModel;
}
error OnlySilo();
error OnlySiloOrTokenOrHookReceiver();
error WrongSilo();
error OnlyDebtShareToken();
error DebtExistInOtherSilo();
error FeeTooHigh();
/// @dev It should be called on debt transfer (debt share token transfer).
/// In the case if the`_recipient` doesn't have configured a collateral silo,
/// it will be set to the collateral silo of the `_sender`.
/// @param _sender sender address
/// @param _recipient recipient address
function onDebtTransfer(address _sender, address _recipient) external;
/// @notice Set collateral silo.
/// @dev Revert if msg.sender is not a SILO_0 or SILO_1.
/// @dev Always set collateral silo the same as msg.sender.
/// @param _borrower borrower address
function setThisSiloAsCollateralSilo(address _borrower) external;
/// @notice Set collateral silo
/// @dev Revert if msg.sender is not a SILO_0 or SILO_1.
/// @dev Always set collateral silo opposite to the msg.sender.
/// @param _borrower borrower address
function setOtherSiloAsCollateralSilo(address _borrower) external;
/// @notice Accrue interest for the silo
/// @param _silo silo for which accrue interest
function accrueInterestForSilo(address _silo) external;
/// @notice Accrue interest for both silos (SILO_0 and SILO_1 in a config)
function accrueInterestForBothSilos() external;
/// @notice Retrieves the collateral silo for a specific borrower.
/// @dev As a user can deposit into `Silo0` and `Silo1`, this property specifies which Silo
/// will be used as collateral for the debt. Later on, it will be used for max LTV and solvency checks.
/// After being set, the collateral silo is never set to `address(0)` again but such getters as
/// `getConfigsForSolvency`, `getConfigsForBorrow`, `getConfigsForWithdraw` will return empty
/// collateral silo config if borrower doesn't have debt.
///
/// In the SiloConfig collateral silo is set by the following functions:
/// `onDebtTransfer` - only if the recipient doesn't have collateral silo set (inherits it from the sender)
/// This function is called on debt share token transfer (debt transfer).
/// `setThisSiloAsCollateralSilo` - sets the same silo as the one that calls the function.
/// `setOtherSiloAsCollateralSilo` - sets the opposite silo as collateral from the one that calls the function.
///
/// In the Silo collateral silo is set by the following functions:
/// `borrow` - always sets opposite silo as collateral.
/// If Silo0 borrows, then Silo1 will be collateral and vice versa.
/// `borrowSameAsset` - always sets the same silo as collateral.
/// `switchCollateralToThisSilo` - always sets the same silo as collateral.
/// @param _borrower The address of the borrower for which the collateral silo is being retrieved
/// @return collateralSilo The address of the collateral silo for the specified borrower
function borrowerCollateralSilo(address _borrower) external view returns (address collateralSilo);
/// @notice Retrieves the silo ID
/// @dev Each silo is assigned a unique ID. ERC-721 token is minted with identical ID to deployer.
/// An owner of that token receives the deployer fees.
/// @return siloId The ID of the silo
function SILO_ID() external view returns (uint256 siloId); // solhint-disable-line func-name-mixedcase
/// @notice Retrieves the addresses of the two silos
/// @return silo0 The address of the first silo
/// @return silo1 The address of the second silo
function getSilos() external view returns (address silo0, address silo1);
/// @notice Retrieves the asset associated with a specific silo
/// @dev This function reverts for incorrect silo address input
/// @param _silo The address of the silo for which the associated asset is being retrieved
/// @return asset The address of the asset associated with the specified silo
function getAssetForSilo(address _silo) external view returns (address asset);
/// @notice Verifies if the borrower has debt in other silo by checking the debt share token balance
/// @param _thisSilo The address of the silo in respect of which the debt is checked
/// @param _borrower The address of the borrower for which the debt is checked
/// @return hasDebt true if the borrower has debt in other silo
function hasDebtInOtherSilo(address _thisSilo, address _borrower) external view returns (bool hasDebt);
/// @notice Retrieves the debt silo associated with a specific borrower
/// @dev This function reverts if debt present in two silo (should not happen)
/// @param _borrower The address of the borrower for which the debt silo is being retrieved
function getDebtSilo(address _borrower) external view returns (address debtSilo);
/// @notice Retrieves configuration data for both silos. First config is for the silo that is asking for configs.
/// @param borrower borrower address for which debtConfig will be returned
/// @return collateralConfig The configuration data for collateral silo (empty if there is no debt).
/// @return debtConfig The configuration data for debt silo (empty if there is no debt).
function getConfigsForSolvency(address borrower)
external
view
returns (ConfigData memory collateralConfig, ConfigData memory debtConfig);
/// @notice Retrieves configuration data for a specific silo
/// @dev This function reverts for incorrect silo address input.
/// @param _silo The address of the silo for which configuration data is being retrieved
/// @return config The configuration data for the specified silo
function getConfig(address _silo) external view returns (ConfigData memory config);
/// @notice Retrieves configuration data for a specific silo for withdraw fn.
/// @dev This function reverts for incorrect silo address input.
/// @param _silo The address of the silo for which configuration data is being retrieved
/// @return depositConfig The configuration data for the specified silo (always config for `_silo`)
/// @return collateralConfig The configuration data for the collateral silo (empty if there is no debt)
/// @return debtConfig The configuration data for the debt silo (empty if there is no debt)
function getConfigsForWithdraw(address _silo, address _borrower) external view returns (
DepositConfig memory depositConfig,
ConfigData memory collateralConfig,
ConfigData memory debtConfig
);
/// @notice Retrieves configuration data for a specific silo for borrow fn.
/// @dev This function reverts for incorrect silo address input.
/// @param _debtSilo The address of the silo for which configuration data is being retrieved
/// @return collateralConfig The configuration data for the collateral silo (always other than `_debtSilo`)
/// @return debtConfig The configuration data for the debt silo (always config for `_debtSilo`)
function getConfigsForBorrow(address _debtSilo)
external
view
returns (ConfigData memory collateralConfig, ConfigData memory debtConfig);
/// @notice Retrieves fee-related information for a specific silo
/// @dev This function reverts for incorrect silo address input
/// @param _silo The address of the silo for which fee-related information is being retrieved.
/// @return daoFee The DAO fee percentage in 18 decimals points.
/// @return deployerFee The deployer fee percentage in 18 decimals points.
/// @return flashloanFee The flashloan fee percentage in 18 decimals points.
/// @return asset The address of the asset associated with the specified silo.
function getFeesWithAsset(address _silo)
external
view
returns (uint256 daoFee, uint256 deployerFee, uint256 flashloanFee, address asset);
/// @notice Retrieves share tokens associated with a specific silo
/// @dev This function reverts for incorrect silo address input
/// @param _silo The address of the silo for which share tokens are being retrieved
/// @return protectedShareToken The address of the protected (non-borrowable) share token
/// @return collateralShareToken The address of the collateral share token
/// @return debtShareToken The address of the debt share token
function getShareTokens(address _silo)
external
view
returns (address protectedShareToken, address collateralShareToken, address debtShareToken);
/// @notice Retrieves the share token and the silo token associated with a specific silo
/// @param _silo The address of the silo for which the share token and silo token are being retrieved
/// @param _collateralType The type of collateral
/// @return shareToken The address of the share token (collateral or protected collateral)
/// @return asset The address of the silo token
function getCollateralShareTokenAndAsset(address _silo, ISilo.CollateralType _collateralType)
external
view
returns (address shareToken, address asset);
/// @notice Retrieves the share token and the silo token associated with a specific silo
/// @param _silo The address of the silo for which the share token and silo token are being retrieved
/// @return shareToken The address of the share token (debt)
/// @return asset The address of the silo token
function getDebtShareTokenAndAsset(address _silo)
external
view
returns (address shareToken, address asset);
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0;
import {IERC20} from "openzeppelin5/token/ERC20/IERC20.sol";
interface IWrappedNativeToken is IERC20 {
function deposit() external payable;
function withdraw(uint256 amount) external;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {IERC20} from "openzeppelin5/token/ERC20/IERC20.sol";
import {ILiquidationHelper} from "../../interfaces/ILiquidationHelper.sol";
import {RevertLib} from "../../lib/RevertLib.sol";
/// @dev Based on demo contract that swaps its ERC20 balance for another ERC20.
/// demo source: https://github.com/0xProject/0x-api-starter-guide-code/blob/master/contracts/SimpleTokenSwap.sol
contract DexSwap {
using RevertLib for bytes;
/// @dev 0x ExchangeProxy address.
/// See https://docs.0x.org/developer-resources/contract-addresses
/// The `to` field from the API response, but at the same time,
/// TODO: maybe unit test that will check, if it does not changed?
// solhint-disable-next-line var-name-mixedcase
address public immutable EXCHANGE_PROXY;
error AddressZero();
constructor(address _exchangeProxy) {
if (_exchangeProxy == address(0)) revert AddressZero();
EXCHANGE_PROXY = _exchangeProxy;
}
/// @dev Swaps ERC20->ERC20 tokens held by this contract using a 0x-API quote.
/// Must attach ETH equal to the `value` field from the API response.
/// @param _sellToken The `sellTokenAddress` field from the API response.
/// @param _spender The `allowanceTarget` field from the API response.
/// @param _swapCallData The `data` field from the API response.
function fillQuote(address _sellToken, address _spender, bytes memory _swapCallData) public virtual {
IERC20(_sellToken).approve(_spender, type(uint256).max);
// Call the encoded swap function call on the contract at `swapTarget`
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory data) = EXCHANGE_PROXY.call(_swapCallData);
if (!success) data.revertBytes("SWAP_CALL_FAILED");
IERC20(_sellToken).approve(_spender, 0);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/**
* @dev Collection of common custom errors used in multiple contracts
*
* IMPORTANT: Backwards compatibility is not guaranteed in future versions of the library.
* It is recommended to avoid relying on the error API for critical functionality.
*/
library Errors {
/**
* @dev The ETH balance of the account is not enough to perform the operation.
*/
error InsufficientBalance(uint256 balance, uint256 needed);
/**
* @dev A call to an address target failed. The target may have reverted.
*/
error FailedCall();
/**
* @dev The deployment failed.
*/
error FailedDeployment();
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC4626.sol)
pragma solidity ^0.8.20;
import {IERC20} from "../token/ERC20/IERC20.sol";
import {IERC20Metadata} from "../token/ERC20/extensions/IERC20Metadata.sol";
/**
* @dev Interface of the ERC-4626 "Tokenized Vault Standard", as defined in
* https://eips.ethereum.org/EIPS/eip-4626[ERC-4626].
*/
interface IERC4626 is IERC20, IERC20Metadata {
event Deposit(address indexed sender, address indexed owner, uint256 assets, uint256 shares);
event Withdraw(
address indexed sender,
address indexed receiver,
address indexed owner,
uint256 assets,
uint256 shares
);
/**
* @dev Returns the address of the underlying token used for the Vault for accounting, depositing, and withdrawing.
*
* - MUST be an ERC-20 token contract.
* - MUST NOT revert.
*/
function asset() external view returns (address assetTokenAddress);
/**
* @dev Returns the total amount of the underlying asset that is “managed” by Vault.
*
* - SHOULD include any compounding that occurs from yield.
* - MUST be inclusive of any fees that are charged against assets in the Vault.
* - MUST NOT revert.
*/
function totalAssets() external view returns (uint256 totalManagedAssets);
/**
* @dev Returns the amount of shares that the Vault would exchange for the amount of assets provided, in an ideal
* scenario where all the conditions are met.
*
* - MUST NOT be inclusive of any fees that are charged against assets in the Vault.
* - MUST NOT show any variations depending on the caller.
* - MUST NOT reflect slippage or other on-chain conditions, when performing the actual exchange.
* - MUST NOT revert.
*
* NOTE: This calculation MAY NOT reflect the “per-user” price-per-share, and instead should reflect the
* “average-user’s” price-per-share, meaning what the average user should expect to see when exchanging to and
* from.
*/
function convertToShares(uint256 assets) external view returns (uint256 shares);
/**
* @dev Returns the amount of assets that the Vault would exchange for the amount of shares provided, in an ideal
* scenario where all the conditions are met.
*
* - MUST NOT be inclusive of any fees that are charged against assets in the Vault.
* - MUST NOT show any variations depending on the caller.
* - MUST NOT reflect slippage or other on-chain conditions, when performing the actual exchange.
* - MUST NOT revert.
*
* NOTE: This calculation MAY NOT reflect the “per-user” price-per-share, and instead should reflect the
* “average-user’s” price-per-share, meaning what the average user should expect to see when exchanging to and
* from.
*/
function convertToAssets(uint256 shares) external view returns (uint256 assets);
/**
* @dev Returns the maximum amount of the underlying asset that can be deposited into the Vault for the receiver,
* through a deposit call.
*
* - MUST return a limited value if receiver is subject to some deposit limit.
* - MUST return 2 ** 256 - 1 if there is no limit on the maximum amount of assets that may be deposited.
* - MUST NOT revert.
*/
function maxDeposit(address receiver) external view returns (uint256 maxAssets);
/**
* @dev Allows an on-chain or off-chain user to simulate the effects of their deposit at the current block, given
* current on-chain conditions.
*
* - MUST return as close to and no more than the exact amount of Vault shares that would be minted in a deposit
* call in the same transaction. I.e. deposit should return the same or more shares as previewDeposit if called
* in the same transaction.
* - MUST NOT account for deposit limits like those returned from maxDeposit and should always act as though the
* deposit would be accepted, regardless if the user has enough tokens approved, etc.
* - MUST be inclusive of deposit fees. Integrators should be aware of the existence of deposit fees.
* - MUST NOT revert.
*
* NOTE: any unfavorable discrepancy between convertToShares and previewDeposit SHOULD be considered slippage in
* share price or some other type of condition, meaning the depositor will lose assets by depositing.
*/
function previewDeposit(uint256 assets) external view returns (uint256 shares);
/**
* @dev Mints shares Vault shares to receiver by depositing exactly amount of underlying tokens.
*
* - MUST emit the Deposit event.
* - MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the
* deposit execution, and are accounted for during deposit.
* - MUST revert if all of assets cannot be deposited (due to deposit limit being reached, slippage, the user not
* approving enough underlying tokens to the Vault contract, etc).
*
* NOTE: most implementations will require pre-approval of the Vault with the Vault’s underlying asset token.
*/
function deposit(uint256 assets, address receiver) external returns (uint256 shares);
/**
* @dev Returns the maximum amount of the Vault shares that can be minted for the receiver, through a mint call.
* - MUST return a limited value if receiver is subject to some mint limit.
* - MUST return 2 ** 256 - 1 if there is no limit on the maximum amount of shares that may be minted.
* - MUST NOT revert.
*/
function maxMint(address receiver) external view returns (uint256 maxShares);
/**
* @dev Allows an on-chain or off-chain user to simulate the effects of their mint at the current block, given
* current on-chain conditions.
*
* - MUST return as close to and no fewer than the exact amount of assets that would be deposited in a mint call
* in the same transaction. I.e. mint should return the same or fewer assets as previewMint if called in the
* same transaction.
* - MUST NOT account for mint limits like those returned from maxMint and should always act as though the mint
* would be accepted, regardless if the user has enough tokens approved, etc.
* - MUST be inclusive of deposit fees. Integrators should be aware of the existence of deposit fees.
* - MUST NOT revert.
*
* NOTE: any unfavorable discrepancy between convertToAssets and previewMint SHOULD be considered slippage in
* share price or some other type of condition, meaning the depositor will lose assets by minting.
*/
function previewMint(uint256 shares) external view returns (uint256 assets);
/**
* @dev Mints exactly shares Vault shares to receiver by depositing amount of underlying tokens.
*
* - MUST emit the Deposit event.
* - MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the mint
* execution, and are accounted for during mint.
* - MUST revert if all of shares cannot be minted (due to deposit limit being reached, slippage, the user not
* approving enough underlying tokens to the Vault contract, etc).
*
* NOTE: most implementations will require pre-approval of the Vault with the Vault’s underlying asset token.
*/
function mint(uint256 shares, address receiver) external returns (uint256 assets);
/**
* @dev Returns the maximum amount of the underlying asset that can be withdrawn from the owner balance in the
* Vault, through a withdraw call.
*
* - MUST return a limited value if owner is subject to some withdrawal limit or timelock.
* - MUST NOT revert.
*/
function maxWithdraw(address owner) external view returns (uint256 maxAssets);
/**
* @dev Allows an on-chain or off-chain user to simulate the effects of their withdrawal at the current block,
* given current on-chain conditions.
*
* - MUST return as close to and no fewer than the exact amount of Vault shares that would be burned in a withdraw
* call in the same transaction. I.e. withdraw should return the same or fewer shares as previewWithdraw if
* called
* in the same transaction.
* - MUST NOT account for withdrawal limits like those returned from maxWithdraw and should always act as though
* the withdrawal would be accepted, regardless if the user has enough shares, etc.
* - MUST be inclusive of withdrawal fees. Integrators should be aware of the existence of withdrawal fees.
* - MUST NOT revert.
*
* NOTE: any unfavorable discrepancy between convertToShares and previewWithdraw SHOULD be considered slippage in
* share price or some other type of condition, meaning the depositor will lose assets by depositing.
*/
function previewWithdraw(uint256 assets) external view returns (uint256 shares);
/**
* @dev Burns shares from owner and sends exactly assets of underlying tokens to receiver.
*
* - MUST emit the Withdraw event.
* - MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the
* withdraw execution, and are accounted for during withdraw.
* - MUST revert if all of assets cannot be withdrawn (due to withdrawal limit being reached, slippage, the owner
* not having enough shares, etc).
*
* Note that some implementations will require pre-requesting to the Vault before a withdrawal may be performed.
* Those methods should be performed separately.
*/
function withdraw(uint256 assets, address receiver, address owner) external returns (uint256 shares);
/**
* @dev Returns the maximum amount of Vault shares that can be redeemed from the owner balance in the Vault,
* through a redeem call.
*
* - MUST return a limited value if owner is subject to some withdrawal limit or timelock.
* - MUST return balanceOf(owner) if owner is not subject to any withdrawal limit or timelock.
* - MUST NOT revert.
*/
function maxRedeem(address owner) external view returns (uint256 maxShares);
/**
* @dev Allows an on-chain or off-chain user to simulate the effects of their redeemption at the current block,
* given current on-chain conditions.
*
* - MUST return as close to and no more than the exact amount of assets that would be withdrawn in a redeem call
* in the same transaction. I.e. redeem should return the same or more assets as previewRedeem if called in the
* same transaction.
* - MUST NOT account for redemption limits like those returned from maxRedeem and should always act as though the
* redemption would be accepted, regardless if the user has enough shares, etc.
* - MUST be inclusive of withdrawal fees. Integrators should be aware of the existence of withdrawal fees.
* - MUST NOT revert.
*
* NOTE: any unfavorable discrepancy between convertToAssets and previewRedeem SHOULD be considered slippage in
* share price or some other type of condition, meaning the depositor will lose assets by redeeming.
*/
function previewRedeem(uint256 shares) external view returns (uint256 assets);
/**
* @dev Burns exactly shares from owner and sends assets of underlying tokens to receiver.
*
* - MUST emit the Withdraw event.
* - MAY support an additional flow in which the underlying tokens are owned by the Vault contract before the
* redeem execution, and are accounted for during redeem.
* - MUST revert if all of shares cannot be redeemed (due to withdrawal limit being reached, slippage, the owner
* not having enough shares, etc).
*
* NOTE: some implementations will require pre-requesting to the Vault before a withdrawal may be performed.
* Those methods should be performed separately.
*/
function redeem(uint256 shares, address receiver, address owner) external returns (uint256 assets);
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0;
import {IERC3156FlashBorrower} from "./IERC3156FlashBorrower.sol";
/// @notice https://eips.ethereum.org/EIPS/eip-3156
interface IERC3156FlashLender {
/// @notice Protected deposits are not available for a flash loan.
/// During the execution of the flashloan, Silo methods are not taking into consideration the fact,
/// that some (or all) tokens were transferred as flashloan, therefore some methods can return invalid state
/// eg. maxWithdraw can return amount that are not available to withdraw during flashlon.
/// @dev Initiate a flash loan.
/// @param _receiver The receiver of the tokens in the loan, and the receiver of the callback.
/// @param _token The loan currency.
/// @param _amount The amount of tokens lent.
/// @param _data Arbitrary data structure, intended to contain user-defined parameters.
function flashLoan(IERC3156FlashBorrower _receiver, address _token, uint256 _amount, bytes calldata _data)
external
returns (bool);
/// @dev The amount of currency available to be lent.
/// @param _token The loan currency.
/// @return The amount of `token` that can be borrowed.
function maxFlashLoan(address _token) external view returns (uint256);
/// @dev The fee to be charged for a given loan.
/// @param _token The loan currency.
/// @param _amount The amount of tokens lent.
/// @return The amount of `token` to be charged for the loan, on top of the returned principal.
function flashFee(address _token, uint256 _amount) external view returns (uint256);
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0;
import {IERC721} from "openzeppelin5/interfaces/IERC721.sol";
import {ISiloConfig} from "./ISiloConfig.sol";
interface ISiloFactory is IERC721 {
struct Range {
uint128 min;
uint128 max;
}
/// @notice Emitted on the creation of a Silo.
/// @param implementation Address of the Silo implementation.
/// @param token0 Address of the first Silo token.
/// @param token1 Address of the second Silo token.
/// @param silo0 Address of the first Silo.
/// @param silo1 Address of the second Silo.
/// @param siloConfig Address of the SiloConfig.
event NewSilo(
address indexed implementation,
address indexed token0,
address indexed token1,
address silo0,
address silo1,
address siloConfig
);
event BaseURI(string newBaseURI);
/// @notice Emitted on the update of DAO fee.
/// @param minDaoFee Value of the new minimal DAO fee.
/// @param maxDaoFee Value of the new maximal DAO fee.
event DaoFeeChanged(uint128 minDaoFee, uint128 maxDaoFee);
/// @notice Emitted on the update of max deployer fee.
/// @param maxDeployerFee Value of the new max deployer fee.
event MaxDeployerFeeChanged(uint256 maxDeployerFee);
/// @notice Emitted on the update of max flashloan fee.
/// @param maxFlashloanFee Value of the new max flashloan fee.
event MaxFlashloanFeeChanged(uint256 maxFlashloanFee);
/// @notice Emitted on the update of max liquidation fee.
/// @param maxLiquidationFee Value of the new max liquidation fee.
event MaxLiquidationFeeChanged(uint256 maxLiquidationFee);
/// @notice Emitted on the change of DAO fee receiver.
/// @param daoFeeReceiver Address of the new DAO fee receiver.
event DaoFeeReceiverChanged(address daoFeeReceiver);
error MissingHookReceiver();
error ZeroAddress();
error DaoFeeReceiverZeroAddress();
error EmptyToken0();
error EmptyToken1();
error MaxFeeExceeded();
error InvalidFeeRange();
error SameAsset();
error SameRange();
error InvalidIrm();
error InvalidMaxLtv();
error InvalidLt();
error InvalidDeployer();
error DaoMinRangeExceeded();
error DaoMaxRangeExceeded();
error MaxDeployerFeeExceeded();
error MaxFlashloanFeeExceeded();
error MaxLiquidationFeeExceeded();
error InvalidCallBeforeQuote();
error OracleMisconfiguration();
error InvalidQuoteToken();
error HookIsZeroAddress();
error LiquidationTargetLtvTooHigh();
/// @notice Create a new Silo.
/// @param _initData Silo initialization data.
/// @param _siloConfig Silo configuration.
/// @param _siloImpl Address of the `Silo` implementation.
/// @param _shareProtectedCollateralTokenImpl Address of the `ShareProtectedCollateralToken` implementation.
/// @param _shareDebtTokenImpl Address of the `ShareDebtToken` implementation.
function createSilo(
ISiloConfig.InitData memory _initData,
ISiloConfig _siloConfig,
address _siloImpl,
address _shareProtectedCollateralTokenImpl,
address _shareDebtTokenImpl
)
external;
/// @notice NFT ownership represents the deployer fee receiver for the each Silo ID. After burning,
/// the deployer fee is sent to the DAO. Burning doesn't affect Silo's behavior. It is only about fee distribution.
/// @param _siloIdToBurn silo ID to burn.
function burn(uint256 _siloIdToBurn) external;
/// @notice Update the value of DAO fee. Updated value will be used only for a new Silos.
/// Previously deployed SiloConfigs are immutable.
/// @param _minFee Value of the new DAO minimal fee.
/// @param _maxFee Value of the new DAO maximal fee.
function setDaoFee(uint128 _minFee, uint128 _maxFee) external;
/// @notice Set the new DAO fee receiver.
/// @param _newDaoFeeReceiver Address of the new DAO fee receiver.
function setDaoFeeReceiver(address _newDaoFeeReceiver) external;
/// @notice Update the value of max deployer fee. Updated value will be used only for a new Silos max deployer
/// fee validation. Previously deployed SiloConfigs are immutable.
/// @param _newMaxDeployerFee Value of the new max deployer fee.
function setMaxDeployerFee(uint256 _newMaxDeployerFee) external;
/// @notice Update the value of max flashloan fee. Updated value will be used only for a new Silos max flashloan
/// fee validation. Previously deployed SiloConfigs are immutable.
/// @param _newMaxFlashloanFee Value of the new max flashloan fee.
function setMaxFlashloanFee(uint256 _newMaxFlashloanFee) external;
/// @notice Update the value of max liquidation fee. Updated value will be used only for a new Silos max
/// liquidation fee validation. Previously deployed SiloConfigs are immutable.
/// @param _newMaxLiquidationFee Value of the new max liquidation fee.
function setMaxLiquidationFee(uint256 _newMaxLiquidationFee) external;
/// @notice Update the base URI.
/// @param _newBaseURI Value of the new base URI.
function setBaseURI(string calldata _newBaseURI) external;
/// @notice Acceptable DAO fee range for new Silos. Denominated in 18 decimals points. 1e18 == 100%.
function daoFeeRange() external view returns (Range memory);
/// @notice Max deployer fee for a new Silos. Denominated in 18 decimals points. 1e18 == 100%.
function maxDeployerFee() external view returns (uint256);
/// @notice Max flashloan fee for a new Silos. Denominated in 18 decimals points. 1e18 == 100%.
function maxFlashloanFee() external view returns (uint256);
/// @notice Max liquidation fee for a new Silos. Denominated in 18 decimals points. 1e18 == 100%.
function maxLiquidationFee() external view returns (uint256);
/// @notice The recipient of DAO fees.
function daoFeeReceiver() external view returns (address);
/// @notice Get SiloConfig address by Silo id.
function idToSiloConfig(uint256 _id) external view returns (address);
/// @notice Do not use this method to check if silo is secure. Anyone can deploy silo with any configuration
/// and implementation. Most critical part of verification would be to check who deployed it.
/// @dev True if the address was deployed using SiloFactory.
function isSilo(address _silo) external view returns (bool);
/// @notice Id of a next Silo to be deployed. This is an ID of non-existing Silo outside of createSilo
/// function call. ID of a first Silo is 1.
function getNextSiloId() external view returns (uint256);
/// @notice Get the DAO and deployer fee receivers for a particular Silo address.
/// @param _silo Silo address.
/// @return dao DAO fee receiver.
/// @return deployer Deployer fee receiver.
function getFeeReceivers(address _silo) external view returns (address dao, address deployer);
/// @notice Validate InitData for a new Silo. Config will be checked for the fee limits, missing parameters.
/// @param _initData Silo init data.
function validateSiloInitData(ISiloConfig.InitData memory _initData) external view returns (bool);
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0;
import {ISiloConfig} from "./ISiloConfig.sol";
interface IHookReceiver {
struct HookConfig {
uint24 hooksBefore;
uint24 hooksAfter;
}
event HookConfigured(address silo, uint24 hooksBefore, uint24 hooksAfter);
/// @notice Initialize a hook receiver
/// @param _siloConfig Silo configuration with all the details about the silo
/// @param _data Data to initialize the hook receiver (if needed)
function initialize(ISiloConfig _siloConfig, bytes calldata _data) external;
/// @notice state of Silo before action, can be also without interest, if you need them, call silo.accrueInterest()
function beforeAction(address _silo, uint256 _action, bytes calldata _input) external;
function afterAction(address _silo, uint256 _action, bytes calldata _inputAndOutput) external;
/// @notice return hooksBefore and hooksAfter configuration
function hookReceiverConfig(address _silo) external view returns (uint24 hooksBefore, uint24 hooksAfter);
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0;
interface ICrossReentrancyGuard {
error CrossReentrantCall();
error CrossReentrancyNotActive();
/// @notice only silo method for cross Silo reentrancy
function turnOnReentrancyProtection() external;
/// @notice only silo method for cross Silo reentrancy
function turnOffReentrancyProtection() external;
/// @notice view method for checking cross Silo reentrancy flag
/// @return entered true if the reentrancy guard is currently set to "entered", which indicates there is a
/// `nonReentrant` function in the call stack.
function reentrancyGuardEntered() external view returns (bool entered);
}
// SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.7.6 <=0.9.0;
library RevertLib {
function revertBytes(bytes memory _errMsg, string memory _customErr) internal pure {
if (_errMsg.length > 0) {
assembly { // solhint-disable-line no-inline-assembly
revert(add(32, _errMsg), mload(_errMsg))
}
}
revert(_customErr);
}
function revertIfError(bytes4 _errorSelector) internal pure {
if (_errorSelector == 0) return;
bytes memory customError = abi.encodeWithSelector(_errorSelector);
assembly {
revert(add(32, customError), mload(customError))
}
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/extensions/IERC20Metadata.sol)
pragma solidity ^0.8.20;
import {IERC20} from "../IERC20.sol";
/**
* @dev Interface for the optional metadata functions from the ERC-20 standard.
*/
interface IERC20Metadata is IERC20 {
/**
* @dev Returns the name of the token.
*/
function name() external view returns (string memory);
/**
* @dev Returns the symbol of the token.
*/
function symbol() external view returns (string memory);
/**
* @dev Returns the decimals places of the token.
*/
function decimals() external view returns (uint8);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC721.sol)
pragma solidity ^0.8.20;
import {IERC721} from "../token/ERC721/IERC721.sol";
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC721/IERC721.sol)
pragma solidity ^0.8.20;
import {IERC165} from "../../utils/introspection/IERC165.sol";
/**
* @dev Required interface of an ERC-721 compliant contract.
*/
interface IERC721 is IERC165 {
/**
* @dev Emitted when `tokenId` token is transferred from `from` to `to`.
*/
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
*/
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
*/
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
/**
* @dev Returns the number of tokens in ``owner``'s account.
*/
function balanceOf(address owner) external view returns (uint256 balance);
/**
* @dev Returns the owner of the `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function ownerOf(uint256 tokenId) external view returns (address owner);
/**
* @dev Safely transfers `tokenId` token from `from` to `to`.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon
* a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
/**
* @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
* are aware of the ERC-721 protocol to prevent tokens from being forever locked.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must have been allowed to move this token by either {approve} or
* {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon
* a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(address from, address to, uint256 tokenId) external;
/**
* @dev Transfers `tokenId` token from `from` to `to`.
*
* WARNING: Note that the caller is responsible to confirm that the recipient is capable of receiving ERC-721
* or else they may be permanently lost. Usage of {safeTransferFrom} prevents loss, though the caller must
* understand this adds an external call which potentially creates a reentrancy vulnerability.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 tokenId) external;
/**
* @dev Gives permission to `to` to transfer `tokenId` token to another account.
* The approval is cleared when the token is transferred.
*
* Only a single account can be approved at a time, so approving the zero address clears previous approvals.
*
* Requirements:
*
* - The caller must own the token or be an approved operator.
* - `tokenId` must exist.
*
* Emits an {Approval} event.
*/
function approve(address to, uint256 tokenId) external;
/**
* @dev Approve or remove `operator` as an operator for the caller.
* Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
*
* Requirements:
*
* - The `operator` cannot be the address zero.
*
* Emits an {ApprovalForAll} event.
*/
function setApprovalForAll(address operator, bool approved) external;
/**
* @dev Returns the account approved for `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function getApproved(uint256 tokenId) external view returns (address operator);
/**
* @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
*
* See {setApprovalForAll}
*/
function isApprovedForAll(address owner, address operator) external view returns (bool);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/introspection/IERC165.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC-165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[ERC].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[ERC section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}