Verified contractExactBUSL-1.1
WeightedRateCollectionPool
Address0x89710b9b4076345bc89cfc6d5a0bb10b73f15c7b
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.20
- Same code
Only this contract
Details
- Compiler
- solc 0.8.20+commit.a1b79de6
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- shanghai
- Via IR
- yes
- License
- BUSL-1.1
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 18,032,857
- Creation transaction
- 0x4993ffa04e…8e657e3730
27 files
- contracts/configurations/WeightedRateCollectionPool.solmain2.83 KB
- @openzeppelin/contracts/security/ReentrancyGuard.sol3.13 KB
- @openzeppelin/contracts/token/ERC20/IERC20.sol2.74 KB
- @openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol683 B
- @openzeppelin/contracts/token/ERC20/extensions/IERC20Permit.sol2.26 KB
- @openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol6.94 KB
- @openzeppelin/contracts/token/ERC721/IERC721.sol4.88 KB
- @openzeppelin/contracts/utils/Address.sol9.36 KB
- @openzeppelin/contracts/utils/Multicall.sol781 B
- @openzeppelin/contracts/utils/introspection/ERC165.sol987 B
- @openzeppelin/contracts/utils/introspection/IERC165.sol853 B
- @openzeppelin/contracts/utils/math/Math.sol12.79 KB
- @openzeppelin/contracts/utils/math/SafeCast.sol35.08 KB
- contracts/CollateralFilter.sol1.17 KB
- contracts/InterestRateModel.sol1.46 KB
- contracts/LiquidityManager.sol18.87 KB
- contracts/LoanReceipt.sol7.81 KB
- contracts/Pool.sol42.36 KB
- contracts/Tick.sol5.12 KB
- contracts/filters/CollectionCollateralFilter.sol1.78 KB
- contracts/integrations/DelegateCash/IDelegationRegistry.sol7.33 KB
- contracts/interfaces/ICollateralLiquidationReceiver.sol543 B
- contracts/interfaces/ICollateralLiquidator.sol911 B
- contracts/interfaces/ICollateralWrapper.sol1.09 KB
- contracts/interfaces/ILiquidity.sol1.78 KB
- contracts/interfaces/IPool.sol10.93 KB
- contracts/rates/WeightedInterestRateModel.sol6.09 KB
contracts/configurations/WeightedRateCollectionPool.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.20;
import "../Pool.sol";
import "../rates/WeightedInterestRateModel.sol";
import "../filters/CollectionCollateralFilter.sol";
/**
* @title Pool Configuration with a Weighted Interest Rate Model and Collection
* Collateral Filter
* @author MetaStreet Labs
*/
contract WeightedRateCollectionPool is Pool, WeightedInterestRateModel, CollectionCollateralFilter {
/**************************************************************************/
/* State */
/**************************************************************************/
/**
* @notice Initialized boolean
*/
bool private _initialized;
/**************************************************************************/
/* Constructor */
/**************************************************************************/
/**
* @notice Pool constructor
* @param collateralLiquidator_ Collateral liquidator
* @param delegationRegistry_ Delegation registry contract
* @param collateralWrappers Collateral wrappers
* @param parameters WeightedInterestRateModel parameters
*/
constructor(
address collateralLiquidator_,
address delegationRegistry_,
address[] memory collateralWrappers,
WeightedInterestRateModel.Parameters memory parameters
) Pool(collateralLiquidator_, delegationRegistry_, collateralWrappers) WeightedInterestRateModel(parameters) {
/* Disable initialization of implementation contract */
_initialized = true;
}
/**************************************************************************/
/* Initializer */
/**************************************************************************/
/**
* @notice Initializer
* @dev Fee-on-transfer currency tokens are not supported
* @param params ABI-encoded parameters
*/
function initialize(bytes memory params) external {
require(!_initialized, "Already initialized");
_initialized = true;
/* Decode parameters */
(address collateralToken_, address currencyToken_, uint64[] memory durations_, uint64[] memory rates_) = abi
.decode(params, (address, address, uint64[], uint64[]));
/* Initialize Collateral Filter */
CollectionCollateralFilter._initialize(collateralToken_);
/* Initialize Pool */
Pool._initialize(currencyToken_, durations_, rates_);
}
/**************************************************************************/
/* Name */
/**************************************************************************/
/**
* @inheritdoc Pool
*/
function IMPLEMENTATION_NAME() external pure override returns (string memory) {
return "WeightedRateCollectionPool";
}
}
@openzeppelin/contracts/security/ReentrancyGuard.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (security/ReentrancyGuard.sol)
pragma solidity ^0.8.0;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be _NOT_ENTERED
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == _ENTERED;
}
}
@openzeppelin/contracts/token/ERC20/IERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 amount) external returns (bool);
}
@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)
pragma solidity ^0.8.0;
import "../IERC20.sol";
/**
* @dev Interface for the optional metadata functions from the ERC20 standard.
*
* _Available since v4.1._
*/
interface IERC20Metadata is IERC20 {
/**
* @dev Returns the name of the token.
*/
function name() external view returns (string memory);
/**
* @dev Returns the symbol of the token.
*/
function symbol() external view returns (string memory);
/**
* @dev Returns the decimals places of the token.
*/
function decimals() external view returns (uint8);
}
@openzeppelin/contracts/token/ERC20/extensions/IERC20Permit.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/extensions/IERC20Permit.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
* https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
*
* Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
* presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
* need to send a transaction, and thus is not required to hold Ether at all.
*/
interface IERC20Permit {
/**
* @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
* given ``owner``'s signed approval.
*
* IMPORTANT: The same issues {IERC20-approve} has related to transaction
* ordering also apply here.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `deadline` must be a timestamp in the future.
* - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
* over the EIP712-formatted function arguments.
* - the signature must use ``owner``'s current nonce (see {nonces}).
*
* For more information on the signature format, see the
* https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
* section].
*/
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
/**
* @dev Returns the current nonce for `owner`. This value must be
* included whenever a signature is generated for {permit}.
*
* Every successful call to {permit} increases ``owner``'s nonce by one. This
* prevents a signature from being used multiple times.
*/
function nonces(address owner) external view returns (uint256);
/**
* @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
*/
// solhint-disable-next-line func-name-mixedcase
function DOMAIN_SEPARATOR() external view returns (bytes32);
}
@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.3) (token/ERC20/utils/SafeERC20.sol)
pragma solidity ^0.8.0;
import "../IERC20.sol";
import "../extensions/IERC20Permit.sol";
import "../../../utils/Address.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20 {
using Address for address;
/**
* @dev 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.encodeWithSelector(token.transfer.selector, 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.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
/**
* @dev Deprecated. This function has issues similar to the ones found in
* {IERC20-approve}, and its usage is discouraged.
*
* Whenever possible, use {safeIncreaseAllowance} and
* {safeDecreaseAllowance} instead.
*/
function safeApprove(IERC20 token, address spender, uint256 value) internal {
// safeApprove should only be called when setting an initial allowance,
// or when resetting it to zero. To increase and decrease it, use
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
require(
(value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
/**
* @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 oldAllowance = token.allowance(address(this), spender);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance + value));
}
/**
* @dev Decrease the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance - value));
}
}
/**
* @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful. Meant to be used with tokens that require the approval
* to be set to zero before setting it to a non-zero value, such as USDT.
*/
function forceApprove(IERC20 token, address spender, uint256 value) internal {
bytes memory approvalCall = abi.encodeWithSelector(token.approve.selector, spender, value);
if (!_callOptionalReturnBool(token, approvalCall)) {
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, 0));
_callOptionalReturn(token, approvalCall);
}
}
/**
* @dev Use a ERC-2612 signature to set the `owner` approval toward `spender` on `token`.
* Revert on invalid signature.
*/
function safePermit(
IERC20Permit token,
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
uint256 nonceBefore = token.nonces(owner);
token.permit(owner, spender, value, deadline, v, r, s);
uint256 nonceAfter = token.nonces(owner);
require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/
function _callOptionalReturn(IERC20 token, bytes memory data) private {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that
// the target address contains contract code and also asserts for success in the low-level call.
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
require(returndata.length == 0 || abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*
* This is a variant of {_callOptionalReturn} that silents catches all reverts and returns a bool instead.
*/
function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We cannot use {Address-functionCall} here since this should return false
// and not revert is the subcall reverts.
(bool success, bytes memory returndata) = address(token).call(data);
return
success && (returndata.length == 0 || abi.decode(returndata, (bool))) && Address.isContract(address(token));
}
}
@openzeppelin/contracts/token/ERC721/IERC721.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC721/IERC721.sol)
pragma solidity ^0.8.0;
import "../../utils/introspection/IERC165.sol";
/**
* @dev Required interface of an ERC721 compliant contract.
*/
interface IERC721 is IERC165 {
/**
* @dev Emitted when `tokenId` token is transferred from `from` to `to`.
*/
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
*/
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
*/
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
/**
* @dev Returns the number of tokens in ``owner``'s account.
*/
function balanceOf(address owner) external view returns (uint256 balance);
/**
* @dev Returns the owner of the `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function ownerOf(uint256 tokenId) external view returns (address owner);
/**
* @dev Safely transfers `tokenId` token from `from` to `to`.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
/**
* @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
* are aware of the ERC721 protocol to prevent tokens from being forever locked.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must have been allowed to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(address from, address to, uint256 tokenId) external;
/**
* @dev Transfers `tokenId` token from `from` to `to`.
*
* WARNING: Note that the caller is responsible to confirm that the recipient is capable of receiving ERC721
* or else they may be permanently lost. Usage of {safeTransferFrom} prevents loss, though the caller must
* understand this adds an external call which potentially creates a reentrancy vulnerability.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 tokenId) external;
/**
* @dev Gives permission to `to` to transfer `tokenId` token to another account.
* The approval is cleared when the token is transferred.
*
* Only a single account can be approved at a time, so approving the zero address clears previous approvals.
*
* Requirements:
*
* - The caller must own the token or be an approved operator.
* - `tokenId` must exist.
*
* Emits an {Approval} event.
*/
function approve(address to, uint256 tokenId) external;
/**
* @dev Approve or remove `operator` as an operator for the caller.
* Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
*
* Requirements:
*
* - The `operator` cannot be the caller.
*
* 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);
}
@openzeppelin/contracts/utils/Address.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/Address.sol)
pragma solidity ^0.8.1;
/**
* @dev Collection of functions related to the address type
*/
library Address {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
*
* Furthermore, `isContract` will also return true if the target contract within
* the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
* which only has an effect at the end of a transaction.
* ====
*
* [IMPORTANT]
* ====
* You shouldn't rely on `isContract` to protect against flash loan attacks!
*
* Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
* like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
* constructor.
* ====
*/
function isContract(address account) internal view returns (bool) {
// This method relies on extcodesize/address.code.length, which returns 0
// for contracts in construction, since the code is only stored at the end
// of the constructor execution.
return account.code.length > 0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.8.0/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(
address target,
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
* the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
*
* _Available since v4.8._
*/
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata,
string memory errorMessage
) internal view returns (bytes memory) {
if (success) {
if (returndata.length == 0) {
// only check isContract if the call was successful and the return data is empty
// otherwise we already know that it was a contract
require(isContract(target), "Address: call to non-contract");
}
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
/**
* @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
* revert reason or using the provided one.
*
* _Available since v4.3._
*/
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function _revert(bytes memory returndata, string memory errorMessage) private pure {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
/// @solidity memory-safe-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
@openzeppelin/contracts/utils/Multicall.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/Multicall.sol)
pragma solidity ^0.8.0;
import "./Address.sol";
/**
* @dev Provides a function to batch together multiple calls in a single external call.
*
* _Available since v4.1._
*/
abstract contract Multicall {
/**
* @dev Receives and executes a batch of function calls on this contract.
* @custom:oz-upgrades-unsafe-allow-reachable delegatecall
*/
function multicall(bytes[] calldata data) external virtual returns (bytes[] memory results) {
results = new bytes[](data.length);
for (uint256 i = 0; i < data.length; i++) {
results[i] = Address.functionDelegateCall(address(this), data[i]);
}
return results;
}
}
@openzeppelin/contracts/utils/introspection/ERC165.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/ERC165.sol)
pragma solidity ^0.8.0;
import "./IERC165.sol";
/**
* @dev Implementation of the {IERC165} interface.
*
* Contracts that want to implement ERC165 should inherit from this contract and override {supportsInterface} to check
* for the additional interface id that will be supported. For example:
*
* ```solidity
* function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
* return interfaceId == type(MyInterface).interfaceId || super.supportsInterface(interfaceId);
* }
* ```
*
* Alternatively, {ERC165Storage} provides an easier to use but more expensive implementation.
*/
abstract contract ERC165 is IERC165 {
/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return interfaceId == type(IERC165).interfaceId;
}
}
@openzeppelin/contracts/utils/introspection/IERC165.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[EIP].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
@openzeppelin/contracts/utils/math/Math.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/math/Math.sol)
pragma solidity ^0.8.0;
/**
* @dev Standard math utilities missing in the Solidity language.
*/
library Math {
enum Rounding {
Down, // Toward negative infinity
Up, // Toward infinity
Zero // Toward zero
}
/**
* @dev Returns the largest of two numbers.
*/
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a > b ? a : b;
}
/**
* @dev Returns the smallest of two numbers.
*/
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/
function average(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b) / 2 can overflow.
return (a & b) + (a ^ b) / 2;
}
/**
* @dev Returns the ceiling of the division of two numbers.
*
* This differs from standard division with `/` in that it rounds up instead
* of rounding down.
*/
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b - 1) / b can overflow on addition, so we distribute.
return a == 0 ? 0 : (a - 1) / b + 1;
}
/**
* @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
* @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
* with further edits by Uniswap Labs also under MIT license.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
// 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
// use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
// variables such that product = prod1 * 2^256 + prod0.
uint256 prod0; // Least significant 256 bits of the product
uint256 prod1; // Most significant 256 bits of the product
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division.
if (prod1 == 0) {
// Solidity will revert if denominator == 0, unlike the div opcode on its own.
// The surrounding unchecked block does not change this fact.
// See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
return prod0 / denominator;
}
// Make sure the result is less than 2^256. Also prevents denominator == 0.
require(denominator > prod1, "Math: mulDiv overflow");
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [prod1 prod0].
uint256 remainder;
assembly {
// Compute remainder using mulmod.
remainder := mulmod(x, y, denominator)
// Subtract 256 bit number from 512 bit number.
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
// Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
// See https://cs.stackexchange.com/q/138556/92363.
// Does not overflow because the denominator cannot be zero at this stage in the function.
uint256 twos = denominator & (~denominator + 1);
assembly {
// Divide denominator by twos.
denominator := div(denominator, twos)
// Divide [prod1 prod0] by twos.
prod0 := div(prod0, twos)
// Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
twos := add(div(sub(0, twos), twos), 1)
}
// Shift in bits from prod1 into prod0.
prod0 |= prod1 * twos;
// Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
// that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
// four bits. That is, denominator * inv = 1 mod 2^4.
uint256 inverse = (3 * denominator) ^ 2;
// Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
// in modular arithmetic, doubling the correct bits in each step.
inverse *= 2 - denominator * inverse; // inverse mod 2^8
inverse *= 2 - denominator * inverse; // inverse mod 2^16
inverse *= 2 - denominator * inverse; // inverse mod 2^32
inverse *= 2 - denominator * inverse; // inverse mod 2^64
inverse *= 2 - denominator * inverse; // inverse mod 2^128
inverse *= 2 - denominator * inverse; // inverse mod 2^256
// Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
// This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
// less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
// is no longer required.
result = prod0 * inverse;
return result;
}
}
/**
* @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
uint256 result = mulDiv(x, y, denominator);
if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
result += 1;
}
return result;
}
/**
* @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
*
* Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
*/
function sqrt(uint256 a) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
// For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
//
// We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
// `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
//
// This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
// → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
// → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
//
// Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
uint256 result = 1 << (log2(a) >> 1);
// At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
// since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
// every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
// into the expected uint128 result.
unchecked {
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
return min(result, a / result);
}
}
/**
* @notice Calculates sqrt(a), following the selected rounding direction.
*/
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
}
}
/**
* @dev Return the log in base 2, rounded down, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 128;
}
if (value >> 64 > 0) {
value >>= 64;
result += 64;
}
if (value >> 32 > 0) {
value >>= 32;
result += 32;
}
if (value >> 16 > 0) {
value >>= 16;
result += 16;
}
if (value >> 8 > 0) {
value >>= 8;
result += 8;
}
if (value >> 4 > 0) {
value >>= 4;
result += 4;
}
if (value >> 2 > 0) {
value >>= 2;
result += 2;
}
if (value >> 1 > 0) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 2, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log2(value);
return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 10, rounded down, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >= 10 ** 64) {
value /= 10 ** 64;
result += 64;
}
if (value >= 10 ** 32) {
value /= 10 ** 32;
result += 32;
}
if (value >= 10 ** 16) {
value /= 10 ** 16;
result += 16;
}
if (value >= 10 ** 8) {
value /= 10 ** 8;
result += 8;
}
if (value >= 10 ** 4) {
value /= 10 ** 4;
result += 4;
}
if (value >= 10 ** 2) {
value /= 10 ** 2;
result += 2;
}
if (value >= 10 ** 1) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 10, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log10(value);
return result + (rounding == Rounding.Up && 10 ** result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 256, rounded down, of a positive value.
* Returns 0 if given 0.
*
* Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
*/
function log256(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 16;
}
if (value >> 64 > 0) {
value >>= 64;
result += 8;
}
if (value >> 32 > 0) {
value >>= 32;
result += 4;
}
if (value >> 16 > 0) {
value >>= 16;
result += 2;
}
if (value >> 8 > 0) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 256, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log256(value);
return result + (rounding == Rounding.Up && 1 << (result << 3) < value ? 1 : 0);
}
}
}
@openzeppelin/contracts/utils/math/SafeCast.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/math/SafeCast.sol)
// This file was procedurally generated from scripts/generate/templates/SafeCast.js.
pragma solidity ^0.8.0;
/**
* @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
* checks.
*
* Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
* easily result in undesired exploitation or bugs, since developers usually
* assume that overflows raise errors. `SafeCast` restores this intuition by
* reverting the transaction when such an operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*
* Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing
* all math on `uint256` and `int256` and then downcasting.
*/
library SafeCast {
/**
* @dev Returns the downcasted uint248 from uint256, reverting on
* overflow (when the input is greater than largest uint248).
*
* Counterpart to Solidity's `uint248` operator.
*
* Requirements:
*
* - input must fit into 248 bits
*
* _Available since v4.7._
*/
function toUint248(uint256 value) internal pure returns (uint248) {
require(value <= type(uint248).max, "SafeCast: value doesn't fit in 248 bits");
return uint248(value);
}
/**
* @dev Returns the downcasted uint240 from uint256, reverting on
* overflow (when the input is greater than largest uint240).
*
* Counterpart to Solidity's `uint240` operator.
*
* Requirements:
*
* - input must fit into 240 bits
*
* _Available since v4.7._
*/
function toUint240(uint256 value) internal pure returns (uint240) {
require(value <= type(uint240).max, "SafeCast: value doesn't fit in 240 bits");
return uint240(value);
}
/**
* @dev Returns the downcasted uint232 from uint256, reverting on
* overflow (when the input is greater than largest uint232).
*
* Counterpart to Solidity's `uint232` operator.
*
* Requirements:
*
* - input must fit into 232 bits
*
* _Available since v4.7._
*/
function toUint232(uint256 value) internal pure returns (uint232) {
require(value <= type(uint232).max, "SafeCast: value doesn't fit in 232 bits");
return uint232(value);
}
/**
* @dev Returns the downcasted uint224 from uint256, reverting on
* overflow (when the input is greater than largest uint224).
*
* Counterpart to Solidity's `uint224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*
* _Available since v4.2._
*/
function toUint224(uint256 value) internal pure returns (uint224) {
require(value <= type(uint224).max, "SafeCast: value doesn't fit in 224 bits");
return uint224(value);
}
/**
* @dev Returns the downcasted uint216 from uint256, reverting on
* overflow (when the input is greater than largest uint216).
*
* Counterpart to Solidity's `uint216` operator.
*
* Requirements:
*
* - input must fit into 216 bits
*
* _Available since v4.7._
*/
function toUint216(uint256 value) internal pure returns (uint216) {
require(value <= type(uint216).max, "SafeCast: value doesn't fit in 216 bits");
return uint216(value);
}
/**
* @dev Returns the downcasted uint208 from uint256, reverting on
* overflow (when the input is greater than largest uint208).
*
* Counterpart to Solidity's `uint208` operator.
*
* Requirements:
*
* - input must fit into 208 bits
*
* _Available since v4.7._
*/
function toUint208(uint256 value) internal pure returns (uint208) {
require(value <= type(uint208).max, "SafeCast: value doesn't fit in 208 bits");
return uint208(value);
}
/**
* @dev Returns the downcasted uint200 from uint256, reverting on
* overflow (when the input is greater than largest uint200).
*
* Counterpart to Solidity's `uint200` operator.
*
* Requirements:
*
* - input must fit into 200 bits
*
* _Available since v4.7._
*/
function toUint200(uint256 value) internal pure returns (uint200) {
require(value <= type(uint200).max, "SafeCast: value doesn't fit in 200 bits");
return uint200(value);
}
/**
* @dev Returns the downcasted uint192 from uint256, reverting on
* overflow (when the input is greater than largest uint192).
*
* Counterpart to Solidity's `uint192` operator.
*
* Requirements:
*
* - input must fit into 192 bits
*
* _Available since v4.7._
*/
function toUint192(uint256 value) internal pure returns (uint192) {
require(value <= type(uint192).max, "SafeCast: value doesn't fit in 192 bits");
return uint192(value);
}
/**
* @dev Returns the downcasted uint184 from uint256, reverting on
* overflow (when the input is greater than largest uint184).
*
* Counterpart to Solidity's `uint184` operator.
*
* Requirements:
*
* - input must fit into 184 bits
*
* _Available since v4.7._
*/
function toUint184(uint256 value) internal pure returns (uint184) {
require(value <= type(uint184).max, "SafeCast: value doesn't fit in 184 bits");
return uint184(value);
}
/**
* @dev Returns the downcasted uint176 from uint256, reverting on
* overflow (when the input is greater than largest uint176).
*
* Counterpart to Solidity's `uint176` operator.
*
* Requirements:
*
* - input must fit into 176 bits
*
* _Available since v4.7._
*/
function toUint176(uint256 value) internal pure returns (uint176) {
require(value <= type(uint176).max, "SafeCast: value doesn't fit in 176 bits");
return uint176(value);
}
/**
* @dev Returns the downcasted uint168 from uint256, reverting on
* overflow (when the input is greater than largest uint168).
*
* Counterpart to Solidity's `uint168` operator.
*
* Requirements:
*
* - input must fit into 168 bits
*
* _Available since v4.7._
*/
function toUint168(uint256 value) internal pure returns (uint168) {
require(value <= type(uint168).max, "SafeCast: value doesn't fit in 168 bits");
return uint168(value);
}
/**
* @dev Returns the downcasted uint160 from uint256, reverting on
* overflow (when the input is greater than largest uint160).
*
* Counterpart to Solidity's `uint160` operator.
*
* Requirements:
*
* - input must fit into 160 bits
*
* _Available since v4.7._
*/
function toUint160(uint256 value) internal pure returns (uint160) {
require(value <= type(uint160).max, "SafeCast: value doesn't fit in 160 bits");
return uint160(value);
}
/**
* @dev Returns the downcasted uint152 from uint256, reverting on
* overflow (when the input is greater than largest uint152).
*
* Counterpart to Solidity's `uint152` operator.
*
* Requirements:
*
* - input must fit into 152 bits
*
* _Available since v4.7._
*/
function toUint152(uint256 value) internal pure returns (uint152) {
require(value <= type(uint152).max, "SafeCast: value doesn't fit in 152 bits");
return uint152(value);
}
/**
* @dev Returns the downcasted uint144 from uint256, reverting on
* overflow (when the input is greater than largest uint144).
*
* Counterpart to Solidity's `uint144` operator.
*
* Requirements:
*
* - input must fit into 144 bits
*
* _Available since v4.7._
*/
function toUint144(uint256 value) internal pure returns (uint144) {
require(value <= type(uint144).max, "SafeCast: value doesn't fit in 144 bits");
return uint144(value);
}
/**
* @dev Returns the downcasted uint136 from uint256, reverting on
* overflow (when the input is greater than largest uint136).
*
* Counterpart to Solidity's `uint136` operator.
*
* Requirements:
*
* - input must fit into 136 bits
*
* _Available since v4.7._
*/
function toUint136(uint256 value) internal pure returns (uint136) {
require(value <= type(uint136).max, "SafeCast: value doesn't fit in 136 bits");
return uint136(value);
}
/**
* @dev Returns the downcasted uint128 from uint256, reverting on
* overflow (when the input is greater than largest uint128).
*
* Counterpart to Solidity's `uint128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*
* _Available since v2.5._
*/
function toUint128(uint256 value) internal pure returns (uint128) {
require(value <= type(uint128).max, "SafeCast: value doesn't fit in 128 bits");
return uint128(value);
}
/**
* @dev Returns the downcasted uint120 from uint256, reverting on
* overflow (when the input is greater than largest uint120).
*
* Counterpart to Solidity's `uint120` operator.
*
* Requirements:
*
* - input must fit into 120 bits
*
* _Available since v4.7._
*/
function toUint120(uint256 value) internal pure returns (uint120) {
require(value <= type(uint120).max, "SafeCast: value doesn't fit in 120 bits");
return uint120(value);
}
/**
* @dev Returns the downcasted uint112 from uint256, reverting on
* overflow (when the input is greater than largest uint112).
*
* Counterpart to Solidity's `uint112` operator.
*
* Requirements:
*
* - input must fit into 112 bits
*
* _Available since v4.7._
*/
function toUint112(uint256 value) internal pure returns (uint112) {
require(value <= type(uint112).max, "SafeCast: value doesn't fit in 112 bits");
return uint112(value);
}
/**
* @dev Returns the downcasted uint104 from uint256, reverting on
* overflow (when the input is greater than largest uint104).
*
* Counterpart to Solidity's `uint104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*
* _Available since v4.7._
*/
function toUint104(uint256 value) internal pure returns (uint104) {
require(value <= type(uint104).max, "SafeCast: value doesn't fit in 104 bits");
return uint104(value);
}
/**
* @dev Returns the downcasted uint96 from uint256, reverting on
* overflow (when the input is greater than largest uint96).
*
* Counterpart to Solidity's `uint96` operator.
*
* Requirements:
*
* - input must fit into 96 bits
*
* _Available since v4.2._
*/
function toUint96(uint256 value) internal pure returns (uint96) {
require(value <= type(uint96).max, "SafeCast: value doesn't fit in 96 bits");
return uint96(value);
}
/**
* @dev Returns the downcasted uint88 from uint256, reverting on
* overflow (when the input is greater than largest uint88).
*
* Counterpart to Solidity's `uint88` operator.
*
* Requirements:
*
* - input must fit into 88 bits
*
* _Available since v4.7._
*/
function toUint88(uint256 value) internal pure returns (uint88) {
require(value <= type(uint88).max, "SafeCast: value doesn't fit in 88 bits");
return uint88(value);
}
/**
* @dev Returns the downcasted uint80 from uint256, reverting on
* overflow (when the input is greater than largest uint80).
*
* Counterpart to Solidity's `uint80` operator.
*
* Requirements:
*
* - input must fit into 80 bits
*
* _Available since v4.7._
*/
function toUint80(uint256 value) internal pure returns (uint80) {
require(value <= type(uint80).max, "SafeCast: value doesn't fit in 80 bits");
return uint80(value);
}
/**
* @dev Returns the downcasted uint72 from uint256, reverting on
* overflow (when the input is greater than largest uint72).
*
* Counterpart to Solidity's `uint72` operator.
*
* Requirements:
*
* - input must fit into 72 bits
*
* _Available since v4.7._
*/
function toUint72(uint256 value) internal pure returns (uint72) {
require(value <= type(uint72).max, "SafeCast: value doesn't fit in 72 bits");
return uint72(value);
}
/**
* @dev Returns the downcasted uint64 from uint256, reverting on
* overflow (when the input is greater than largest uint64).
*
* Counterpart to Solidity's `uint64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*
* _Available since v2.5._
*/
function toUint64(uint256 value) internal pure returns (uint64) {
require(value <= type(uint64).max, "SafeCast: value doesn't fit in 64 bits");
return uint64(value);
}
/**
* @dev Returns the downcasted uint56 from uint256, reverting on
* overflow (when the input is greater than largest uint56).
*
* Counterpart to Solidity's `uint56` operator.
*
* Requirements:
*
* - input must fit into 56 bits
*
* _Available since v4.7._
*/
function toUint56(uint256 value) internal pure returns (uint56) {
require(value <= type(uint56).max, "SafeCast: value doesn't fit in 56 bits");
return uint56(value);
}
/**
* @dev Returns the downcasted uint48 from uint256, reverting on
* overflow (when the input is greater than largest uint48).
*
* Counterpart to Solidity's `uint48` operator.
*
* Requirements:
*
* - input must fit into 48 bits
*
* _Available since v4.7._
*/
function toUint48(uint256 value) internal pure returns (uint48) {
require(value <= type(uint48).max, "SafeCast: value doesn't fit in 48 bits");
return uint48(value);
}
/**
* @dev Returns the downcasted uint40 from uint256, reverting on
* overflow (when the input is greater than largest uint40).
*
* Counterpart to Solidity's `uint40` operator.
*
* Requirements:
*
* - input must fit into 40 bits
*
* _Available since v4.7._
*/
function toUint40(uint256 value) internal pure returns (uint40) {
require(value <= type(uint40).max, "SafeCast: value doesn't fit in 40 bits");
return uint40(value);
}
/**
* @dev Returns the downcasted uint32 from uint256, reverting on
* overflow (when the input is greater than largest uint32).
*
* Counterpart to Solidity's `uint32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*
* _Available since v2.5._
*/
function toUint32(uint256 value) internal pure returns (uint32) {
require(value <= type(uint32).max, "SafeCast: value doesn't fit in 32 bits");
return uint32(value);
}
/**
* @dev Returns the downcasted uint24 from uint256, reverting on
* overflow (when the input is greater than largest uint24).
*
* Counterpart to Solidity's `uint24` operator.
*
* Requirements:
*
* - input must fit into 24 bits
*
* _Available since v4.7._
*/
function toUint24(uint256 value) internal pure returns (uint24) {
require(value <= type(uint24).max, "SafeCast: value doesn't fit in 24 bits");
return uint24(value);
}
/**
* @dev Returns the downcasted uint16 from uint256, reverting on
* overflow (when the input is greater than largest uint16).
*
* Counterpart to Solidity's `uint16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*
* _Available since v2.5._
*/
function toUint16(uint256 value) internal pure returns (uint16) {
require(value <= type(uint16).max, "SafeCast: value doesn't fit in 16 bits");
return uint16(value);
}
/**
* @dev Returns the downcasted uint8 from uint256, reverting on
* overflow (when the input is greater than largest uint8).
*
* Counterpart to Solidity's `uint8` operator.
*
* Requirements:
*
* - input must fit into 8 bits
*
* _Available since v2.5._
*/
function toUint8(uint256 value) internal pure returns (uint8) {
require(value <= type(uint8).max, "SafeCast: value doesn't fit in 8 bits");
return uint8(value);
}
/**
* @dev Converts a signed int256 into an unsigned uint256.
*
* Requirements:
*
* - input must be greater than or equal to 0.
*
* _Available since v3.0._
*/
function toUint256(int256 value) internal pure returns (uint256) {
require(value >= 0, "SafeCast: value must be positive");
return uint256(value);
}
/**
* @dev Returns the downcasted int248 from int256, reverting on
* overflow (when the input is less than smallest int248 or
* greater than largest int248).
*
* Counterpart to Solidity's `int248` operator.
*
* Requirements:
*
* - input must fit into 248 bits
*
* _Available since v4.7._
*/
function toInt248(int256 value) internal pure returns (int248 downcasted) {
downcasted = int248(value);
require(downcasted == value, "SafeCast: value doesn't fit in 248 bits");
}
/**
* @dev Returns the downcasted int240 from int256, reverting on
* overflow (when the input is less than smallest int240 or
* greater than largest int240).
*
* Counterpart to Solidity's `int240` operator.
*
* Requirements:
*
* - input must fit into 240 bits
*
* _Available since v4.7._
*/
function toInt240(int256 value) internal pure returns (int240 downcasted) {
downcasted = int240(value);
require(downcasted == value, "SafeCast: value doesn't fit in 240 bits");
}
/**
* @dev Returns the downcasted int232 from int256, reverting on
* overflow (when the input is less than smallest int232 or
* greater than largest int232).
*
* Counterpart to Solidity's `int232` operator.
*
* Requirements:
*
* - input must fit into 232 bits
*
* _Available since v4.7._
*/
function toInt232(int256 value) internal pure returns (int232 downcasted) {
downcasted = int232(value);
require(downcasted == value, "SafeCast: value doesn't fit in 232 bits");
}
/**
* @dev Returns the downcasted int224 from int256, reverting on
* overflow (when the input is less than smallest int224 or
* greater than largest int224).
*
* Counterpart to Solidity's `int224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*
* _Available since v4.7._
*/
function toInt224(int256 value) internal pure returns (int224 downcasted) {
downcasted = int224(value);
require(downcasted == value, "SafeCast: value doesn't fit in 224 bits");
}
/**
* @dev Returns the downcasted int216 from int256, reverting on
* overflow (when the input is less than smallest int216 or
* greater than largest int216).
*
* Counterpart to Solidity's `int216` operator.
*
* Requirements:
*
* - input must fit into 216 bits
*
* _Available since v4.7._
*/
function toInt216(int256 value) internal pure returns (int216 downcasted) {
downcasted = int216(value);
require(downcasted == value, "SafeCast: value doesn't fit in 216 bits");
}
/**
* @dev Returns the downcasted int208 from int256, reverting on
* overflow (when the input is less than smallest int208 or
* greater than largest int208).
*
* Counterpart to Solidity's `int208` operator.
*
* Requirements:
*
* - input must fit into 208 bits
*
* _Available since v4.7._
*/
function toInt208(int256 value) internal pure returns (int208 downcasted) {
downcasted = int208(value);
require(downcasted == value, "SafeCast: value doesn't fit in 208 bits");
}
/**
* @dev Returns the downcasted int200 from int256, reverting on
* overflow (when the input is less than smallest int200 or
* greater than largest int200).
*
* Counterpart to Solidity's `int200` operator.
*
* Requirements:
*
* - input must fit into 200 bits
*
* _Available since v4.7._
*/
function toInt200(int256 value) internal pure returns (int200 downcasted) {
downcasted = int200(value);
require(downcasted == value, "SafeCast: value doesn't fit in 200 bits");
}
/**
* @dev Returns the downcasted int192 from int256, reverting on
* overflow (when the input is less than smallest int192 or
* greater than largest int192).
*
* Counterpart to Solidity's `int192` operator.
*
* Requirements:
*
* - input must fit into 192 bits
*
* _Available since v4.7._
*/
function toInt192(int256 value) internal pure returns (int192 downcasted) {
downcasted = int192(value);
require(downcasted == value, "SafeCast: value doesn't fit in 192 bits");
}
/**
* @dev Returns the downcasted int184 from int256, reverting on
* overflow (when the input is less than smallest int184 or
* greater than largest int184).
*
* Counterpart to Solidity's `int184` operator.
*
* Requirements:
*
* - input must fit into 184 bits
*
* _Available since v4.7._
*/
function toInt184(int256 value) internal pure returns (int184 downcasted) {
downcasted = int184(value);
require(downcasted == value, "SafeCast: value doesn't fit in 184 bits");
}
/**
* @dev Returns the downcasted int176 from int256, reverting on
* overflow (when the input is less than smallest int176 or
* greater than largest int176).
*
* Counterpart to Solidity's `int176` operator.
*
* Requirements:
*
* - input must fit into 176 bits
*
* _Available since v4.7._
*/
function toInt176(int256 value) internal pure returns (int176 downcasted) {
downcasted = int176(value);
require(downcasted == value, "SafeCast: value doesn't fit in 176 bits");
}
/**
* @dev Returns the downcasted int168 from int256, reverting on
* overflow (when the input is less than smallest int168 or
* greater than largest int168).
*
* Counterpart to Solidity's `int168` operator.
*
* Requirements:
*
* - input must fit into 168 bits
*
* _Available since v4.7._
*/
function toInt168(int256 value) internal pure returns (int168 downcasted) {
downcasted = int168(value);
require(downcasted == value, "SafeCast: value doesn't fit in 168 bits");
}
/**
* @dev Returns the downcasted int160 from int256, reverting on
* overflow (when the input is less than smallest int160 or
* greater than largest int160).
*
* Counterpart to Solidity's `int160` operator.
*
* Requirements:
*
* - input must fit into 160 bits
*
* _Available since v4.7._
*/
function toInt160(int256 value) internal pure returns (int160 downcasted) {
downcasted = int160(value);
require(downcasted == value, "SafeCast: value doesn't fit in 160 bits");
}
/**
* @dev Returns the downcasted int152 from int256, reverting on
* overflow (when the input is less than smallest int152 or
* greater than largest int152).
*
* Counterpart to Solidity's `int152` operator.
*
* Requirements:
*
* - input must fit into 152 bits
*
* _Available since v4.7._
*/
function toInt152(int256 value) internal pure returns (int152 downcasted) {
downcasted = int152(value);
require(downcasted == value, "SafeCast: value doesn't fit in 152 bits");
}
/**
* @dev Returns the downcasted int144 from int256, reverting on
* overflow (when the input is less than smallest int144 or
* greater than largest int144).
*
* Counterpart to Solidity's `int144` operator.
*
* Requirements:
*
* - input must fit into 144 bits
*
* _Available since v4.7._
*/
function toInt144(int256 value) internal pure returns (int144 downcasted) {
downcasted = int144(value);
require(downcasted == value, "SafeCast: value doesn't fit in 144 bits");
}
/**
* @dev Returns the downcasted int136 from int256, reverting on
* overflow (when the input is less than smallest int136 or
* greater than largest int136).
*
* Counterpart to Solidity's `int136` operator.
*
* Requirements:
*
* - input must fit into 136 bits
*
* _Available since v4.7._
*/
function toInt136(int256 value) internal pure returns (int136 downcasted) {
downcasted = int136(value);
require(downcasted == value, "SafeCast: value doesn't fit in 136 bits");
}
/**
* @dev Returns the downcasted int128 from int256, reverting on
* overflow (when the input is less than smallest int128 or
* greater than largest int128).
*
* Counterpart to Solidity's `int128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*
* _Available since v3.1._
*/
function toInt128(int256 value) internal pure returns (int128 downcasted) {
downcasted = int128(value);
require(downcasted == value, "SafeCast: value doesn't fit in 128 bits");
}
/**
* @dev Returns the downcasted int120 from int256, reverting on
* overflow (when the input is less than smallest int120 or
* greater than largest int120).
*
* Counterpart to Solidity's `int120` operator.
*
* Requirements:
*
* - input must fit into 120 bits
*
* _Available since v4.7._
*/
function toInt120(int256 value) internal pure returns (int120 downcasted) {
downcasted = int120(value);
require(downcasted == value, "SafeCast: value doesn't fit in 120 bits");
}
/**
* @dev Returns the downcasted int112 from int256, reverting on
* overflow (when the input is less than smallest int112 or
* greater than largest int112).
*
* Counterpart to Solidity's `int112` operator.
*
* Requirements:
*
* - input must fit into 112 bits
*
* _Available since v4.7._
*/
function toInt112(int256 value) internal pure returns (int112 downcasted) {
downcasted = int112(value);
require(downcasted == value, "SafeCast: value doesn't fit in 112 bits");
}
/**
* @dev Returns the downcasted int104 from int256, reverting on
* overflow (when the input is less than smallest int104 or
* greater than largest int104).
*
* Counterpart to Solidity's `int104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*
* _Available since v4.7._
*/
function toInt104(int256 value) internal pure returns (int104 downcasted) {
downcasted = int104(value);
require(downcasted == value, "SafeCast: value doesn't fit in 104 bits");
}
/**
* @dev Returns the downcasted int96 from int256, reverting on
* overflow (when the input is less than smallest int96 or
* greater than largest int96).
*
* Counterpart to Solidity's `int96` operator.
*
* Requirements:
*
* - input must fit into 96 bits
*
* _Available since v4.7._
*/
function toInt96(int256 value) internal pure returns (int96 downcasted) {
downcasted = int96(value);
require(downcasted == value, "SafeCast: value doesn't fit in 96 bits");
}
/**
* @dev Returns the downcasted int88 from int256, reverting on
* overflow (when the input is less than smallest int88 or
* greater than largest int88).
*
* Counterpart to Solidity's `int88` operator.
*
* Requirements:
*
* - input must fit into 88 bits
*
* _Available since v4.7._
*/
function toInt88(int256 value) internal pure returns (int88 downcasted) {
downcasted = int88(value);
require(downcasted == value, "SafeCast: value doesn't fit in 88 bits");
}
/**
* @dev Returns the downcasted int80 from int256, reverting on
* overflow (when the input is less than smallest int80 or
* greater than largest int80).
*
* Counterpart to Solidity's `int80` operator.
*
* Requirements:
*
* - input must fit into 80 bits
*
* _Available since v4.7._
*/
function toInt80(int256 value) internal pure returns (int80 downcasted) {
downcasted = int80(value);
require(downcasted == value, "SafeCast: value doesn't fit in 80 bits");
}
/**
* @dev Returns the downcasted int72 from int256, reverting on
* overflow (when the input is less than smallest int72 or
* greater than largest int72).
*
* Counterpart to Solidity's `int72` operator.
*
* Requirements:
*
* - input must fit into 72 bits
*
* _Available since v4.7._
*/
function toInt72(int256 value) internal pure returns (int72 downcasted) {
downcasted = int72(value);
require(downcasted == value, "SafeCast: value doesn't fit in 72 bits");
}
/**
* @dev Returns the downcasted int64 from int256, reverting on
* overflow (when the input is less than smallest int64 or
* greater than largest int64).
*
* Counterpart to Solidity's `int64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*
* _Available since v3.1._
*/
function toInt64(int256 value) internal pure returns (int64 downcasted) {
downcasted = int64(value);
require(downcasted == value, "SafeCast: value doesn't fit in 64 bits");
}
/**
* @dev Returns the downcasted int56 from int256, reverting on
* overflow (when the input is less than smallest int56 or
* greater than largest int56).
*
* Counterpart to Solidity's `int56` operator.
*
* Requirements:
*
* - input must fit into 56 bits
*
* _Available since v4.7._
*/
function toInt56(int256 value) internal pure returns (int56 downcasted) {
downcasted = int56(value);
require(downcasted == value, "SafeCast: value doesn't fit in 56 bits");
}
/**
* @dev Returns the downcasted int48 from int256, reverting on
* overflow (when the input is less than smallest int48 or
* greater than largest int48).
*
* Counterpart to Solidity's `int48` operator.
*
* Requirements:
*
* - input must fit into 48 bits
*
* _Available since v4.7._
*/
function toInt48(int256 value) internal pure returns (int48 downcasted) {
downcasted = int48(value);
require(downcasted == value, "SafeCast: value doesn't fit in 48 bits");
}
/**
* @dev Returns the downcasted int40 from int256, reverting on
* overflow (when the input is less than smallest int40 or
* greater than largest int40).
*
* Counterpart to Solidity's `int40` operator.
*
* Requirements:
*
* - input must fit into 40 bits
*
* _Available since v4.7._
*/
function toInt40(int256 value) internal pure returns (int40 downcasted) {
downcasted = int40(value);
require(downcasted == value, "SafeCast: value doesn't fit in 40 bits");
}
/**
* @dev Returns the downcasted int32 from int256, reverting on
* overflow (when the input is less than smallest int32 or
* greater than largest int32).
*
* Counterpart to Solidity's `int32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*
* _Available since v3.1._
*/
function toInt32(int256 value) internal pure returns (int32 downcasted) {
downcasted = int32(value);
require(downcasted == value, "SafeCast: value doesn't fit in 32 bits");
}
/**
* @dev Returns the downcasted int24 from int256, reverting on
* overflow (when the input is less than smallest int24 or
* greater than largest int24).
*
* Counterpart to Solidity's `int24` operator.
*
* Requirements:
*
* - input must fit into 24 bits
*
* _Available since v4.7._
*/
function toInt24(int256 value) internal pure returns (int24 downcasted) {
downcasted = int24(value);
require(downcasted == value, "SafeCast: value doesn't fit in 24 bits");
}
/**
* @dev Returns the downcasted int16 from int256, reverting on
* overflow (when the input is less than smallest int16 or
* greater than largest int16).
*
* Counterpart to Solidity's `int16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*
* _Available since v3.1._
*/
function toInt16(int256 value) internal pure returns (int16 downcasted) {
downcasted = int16(value);
require(downcasted == value, "SafeCast: value doesn't fit in 16 bits");
}
/**
* @dev Returns the downcasted int8 from int256, reverting on
* overflow (when the input is less than smallest int8 or
* greater than largest int8).
*
* Counterpart to Solidity's `int8` operator.
*
* Requirements:
*
* - input must fit into 8 bits
*
* _Available since v3.1._
*/
function toInt8(int256 value) internal pure returns (int8 downcasted) {
downcasted = int8(value);
require(downcasted == value, "SafeCast: value doesn't fit in 8 bits");
}
/**
* @dev Converts an unsigned uint256 into a signed int256.
*
* Requirements:
*
* - input must be less than or equal to maxInt256.
*
* _Available since v3.0._
*/
function toInt256(uint256 value) internal pure returns (int256) {
// Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
require(value <= uint256(type(int256).max), "SafeCast: value doesn't fit in an int256");
return int256(value);
}
}
contracts/CollateralFilter.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title Collateral Filter API
* @author MetaStreet Labs
*/
abstract contract CollateralFilter {
/**
* @notice Get collateral filter name
* @return Collateral filter name
*/
function COLLATERAL_FILTER_NAME() external view virtual returns (string memory);
/**
* @notice Get collateral filter version
* @return Collateral filter version
*/
function COLLATERAL_FILTER_VERSION() external view virtual returns (string memory);
/**
* @notice Get collateral token
* @return Collateral token contract
*/
function collateralToken() external view virtual returns (address);
/**
* Query if collateral token is supported
* @param token Collateral token contract
* @param tokenId Collateral Token ID
* @param index Collateral Token ID index
* @param context ABI-encoded context
* @return True if supported, otherwise false
*/
function _collateralSupported(
address token,
uint256 tokenId,
uint256 index,
bytes calldata context
) internal view virtual returns (bool);
}
contracts/InterestRateModel.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./interfaces/ILiquidity.sol";
/**
* @title Interest Rate Model API
* @author MetaStreet Labs
*/
abstract contract InterestRateModel {
/**
* @notice Get interest rate model name
* @return Interest rate model name
*/
function INTEREST_RATE_MODEL_NAME() external view virtual returns (string memory);
/**
* @notice Get interest rate model version
* @return Interest rate model version
*/
function INTEREST_RATE_MODEL_VERSION() external view virtual returns (string memory);
/**
* Get interest rate for liquidity
* @param amount Liquidity amount
* @param rates Rates
* @param nodes Liquidity nodes
* @param count Liquidity node count
* @return Interest per second
*/
function _rate(
uint256 amount,
uint64[] memory rates,
ILiquidity.NodeSource[] memory nodes,
uint16 count
) internal view virtual returns (uint256);
/**
* Distribute interest to liquidity
* @param amount Liquidity amount
* @param interest Interest to distribute
* @param nodes Liquidity nodes
* @param count Liquidity node count
* @return Interest distribution
*/
function _distribute(
uint256 amount,
uint256 interest,
ILiquidity.NodeSource[] memory nodes,
uint16 count
) internal view virtual returns (uint128[] memory);
}
contracts/LiquidityManager.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.20;
import "@openzeppelin/contracts/utils/math/Math.sol";
import "@openzeppelin/contracts/utils/math/SafeCast.sol";
import "./interfaces/ILiquidity.sol";
import "./Tick.sol";
/**
* @title LiquidityManager
* @author MetaStreet Labs
*/
library LiquidityManager {
using SafeCast for uint256;
/**************************************************************************/
/* Constants */
/**************************************************************************/
/**
* @notice Tick limit spacing basis points (10%)
*/
uint256 internal constant TICK_LIMIT_SPACING_BASIS_POINTS = 1000;
/**
* @notice Fixed point scale
*/
uint256 internal constant FIXED_POINT_SCALE = 1e18;
/**
* @notice Basis points scale
*/
uint256 internal constant BASIS_POINTS_SCALE = 10_000;
/**
* @notice Impaired price threshold (5%)
*/
uint256 internal constant IMPAIRED_PRICE_THRESHOLD = 0.05 * 1e18;
/**
* @notice Max redemption queue scan count
*/
uint256 private constant MAX_REDEMPTION_QUEUE_SCAN_COUNT = 150;
/**************************************************************************/
/* Errors */
/**************************************************************************/
/**
* @notice Insufficient liquidity
*/
error InsufficientLiquidity();
/**
* @notice Inactive liquidity
*/
error InactiveLiquidity();
/**
* @notice Insufficient tick spacing
*/
error InsufficientTickSpacing();
/**************************************************************************/
/* Structures */
/**************************************************************************/
/**
* @notice Fulfilled redemption
* @param shares Shares redeemed
* @param amount Amount redeemed
*/
struct FulfilledRedemption {
uint128 shares;
uint128 amount;
}
/**
* @notice Redemption state
* @param pending Pending shares
* @param index Current index
* @param fulfilled Fulfilled redemptions
*/
struct Redemptions {
uint128 pending;
uint128 index;
mapping(uint128 => FulfilledRedemption) fulfilled;
}
/**
* @notice Liquidity node
* @param value Liquidity value
* @param shares Liquidity shares outstanding
* @param available Liquidity available
* @param pending Liquidity pending (with interest)
* @param redemption Redemption state
* @param prev Previous liquidity node
* @param next Next liquidity node
*/
struct Node {
uint128 value;
uint128 shares;
uint128 available;
uint128 pending;
uint128 prev;
uint128 next;
Redemptions redemptions;
}
/**
* @notice Liquidity state
* @param nodes Liquidity nodes
*/
struct Liquidity {
mapping(uint256 => Node) nodes;
}
/**************************************************************************/
/* Getters */
/**************************************************************************/
/**
* Get liquidity node at tick
* @param liquidity Liquidity state
* @param tick Tick
* @return Liquidity node
*/
function liquidityNode(
Liquidity storage liquidity,
uint128 tick
) internal view returns (ILiquidity.NodeInfo memory) {
Node storage node = liquidity.nodes[tick];
return
ILiquidity.NodeInfo({
tick: tick,
value: node.value,
shares: node.shares,
available: node.available,
pending: node.pending,
redemptions: node.redemptions.pending,
prev: node.prev,
next: node.next
});
}
/**
* Get liquidity nodes spanning [startTick, endTick] range where startTick
* must be 0 or an instantiated tick
* @param startTick Start tick
* @param endTick End tick
* @return Liquidity nodes
*/
function liquidityNodes(
Liquidity storage liquidity,
uint128 startTick,
uint128 endTick
) internal view returns (ILiquidity.NodeInfo[] memory) {
/* Validate start tick has active liquidity */
if (liquidity.nodes[startTick].next == 0) revert InactiveLiquidity();
/* Count nodes first to figure out how to size liquidity nodes array */
uint256 i;
uint128 t = startTick;
while (t != type(uint128).max && t <= endTick) {
t = liquidity.nodes[t].next;
i++;
}
ILiquidity.NodeInfo[] memory nodes = new ILiquidity.NodeInfo[](i);
/* Populate nodes */
i = 0;
t = startTick;
while (t != type(uint128).max && t <= endTick) {
nodes[i] = liquidityNode(liquidity, t);
t = nodes[i].next;
i++;
}
return nodes;
}
/**
* @notice Get redemption available amount
* @param liquidity Liquidity state
* @param tick Tick
* @param pending Redemption pending
* @param index Redemption index
* @param target Redemption target
* @return redeemedShares Redeemed shares
* @return redeemedAmount Redeemed amount
* @return processedIndices Processed indices
* @return processedShares Processed shares
*/
function redemptionAvailable(
Liquidity storage liquidity,
uint128 tick,
uint128 pending,
uint128 index,
uint128 target
)
internal
view
returns (uint128 redeemedShares, uint128 redeemedAmount, uint128 processedIndices, uint128 processedShares)
{
Node storage node = liquidity.nodes[tick];
uint256 stopIndex = index + MAX_REDEMPTION_QUEUE_SCAN_COUNT;
for (; processedShares < target + pending && index < stopIndex; index++) {
if (index == node.redemptions.index) {
/* Reached pending unfulfilled redemption */
break;
}
/* Look up the next fulfilled redemption */
FulfilledRedemption storage redemption = node.redemptions.fulfilled[index];
/* Update processed count */
processedIndices += 1;
processedShares += redemption.shares;
if (processedShares <= target) {
/* Have not reached the redemption queue position yet */
continue;
} else {
/* Compute number of shares to redeem in range of this
* redemption batch */
uint128 shares = (((processedShares > target + pending) ? pending : (processedShares - target))) -
redeemedShares;
/* Compute price of shares in this redemption batch */
uint256 price = (redemption.amount * FIXED_POINT_SCALE) / redemption.shares;
/* Accumulate redeemed shares and corresponding amount */
redeemedShares += shares;
redeemedAmount += Math.mulDiv(shares, price, FIXED_POINT_SCALE).toUint128();
}
}
}
/**************************************************************************/
/* Internal Helpers */
/**************************************************************************/
/**
* @dev Check if tick is reserved
* @param tick Tick
* @return True if reserved, otherwise false
*/
function _isReserved(uint128 tick) internal pure returns (bool) {
return tick == 0 || tick == type(uint128).max;
}
/**
* @dev Check if liquidity node is empty
* @param node Liquidity node
* @return True if empty, otherwise false
*/
function _isEmpty(Node storage node) internal view returns (bool) {
return node.shares == 0 && node.pending == 0;
}
/**
* @dev Check if liquidity node is active
* @param node Liquidity node
* @return True if active, otherwise false
*/
function _isActive(Node storage node) internal view returns (bool) {
return node.prev != 0 || node.next != 0;
}
/**
* @dev Check if liquidity node is impaired
* @param node Liquidity node
* @return True if impaired, otherwise false
*/
function _isImpaired(Node storage node) internal view returns (bool) {
/* If there's shares, but insufficient value for a stable share price */
return node.shares != 0 && node.value * FIXED_POINT_SCALE < node.shares * IMPAIRED_PRICE_THRESHOLD;
}
/**
* @notice Instantiate liquidity
* @param liquidity Liquidity state
* @param tick Tick
*/
function _instantiate(Liquidity storage liquidity, Node storage node, uint128 tick) internal {
/* If node is active, do nothing */
if (_isActive(node)) return;
/* If node is inactive and not empty, revert */
if (!_isEmpty(node)) revert InactiveLiquidity();
/* Find prior node to new tick */
uint128 prevTick;
Node storage prevNode = liquidity.nodes[prevTick];
while (prevNode.next < tick) {
prevTick = prevNode.next;
prevNode = liquidity.nodes[prevTick];
}
/* Decode limits from previous tick, new tick, and next tick */
(uint256 prevLimit, , , ) = Tick.decode(prevTick);
(uint256 newLimit, , , ) = Tick.decode(tick);
(uint256 nextLimit, , , ) = Tick.decode(prevNode.next);
/* Validate tick limit spacing */
if (
newLimit != prevLimit &&
newLimit < (prevLimit * (BASIS_POINTS_SCALE + TICK_LIMIT_SPACING_BASIS_POINTS)) / BASIS_POINTS_SCALE
) revert InsufficientTickSpacing();
if (
newLimit != nextLimit &&
nextLimit < (newLimit * (BASIS_POINTS_SCALE + TICK_LIMIT_SPACING_BASIS_POINTS)) / BASIS_POINTS_SCALE
) revert InsufficientTickSpacing();
/* Link new node */
node.prev = prevTick;
node.next = prevNode.next;
liquidity.nodes[prevNode.next].prev = tick;
prevNode.next = tick;
}
/**
* @dev Garbage collect an impaired or empty node, unlinking it from active
* liquidity
* @param liquidity Liquidity state
* @param node Liquidity node
*/
function _garbageCollect(Liquidity storage liquidity, Node storage node) internal {
/* If node is not impaired and not empty, or already inactive, do nothing */
if ((!_isImpaired(node) && !_isEmpty(node)) || !_isActive(node)) return;
/* Make node inactive by unlinking it */
liquidity.nodes[node.prev].next = node.next;
liquidity.nodes[node.next].prev = node.prev;
node.next = 0;
node.prev = 0;
}
/**
* @notice Process redemptions from available liquidity
* @param liquidity Liquidity state
* @param node Liquidity node
*/
function _processRedemptions(Liquidity storage liquidity, Node storage node) internal {
/* If there's no pending shares to redeem */
if (node.redemptions.pending == 0) return;
/* Compute redemption price */
uint256 price = (node.value * FIXED_POINT_SCALE) / node.shares;
if (price == 0) {
/* If node has pending interest */
if (node.pending != 0) return;
/* If node is insolvent, redeem all shares for zero amount */
uint128 shares = node.redemptions.pending;
/* Record fulfilled redemption */
node.redemptions.fulfilled[node.redemptions.index++] = FulfilledRedemption({shares: shares, amount: 0});
/* Update node state */
node.shares -= shares;
node.value = 0;
node.available = 0;
node.redemptions.pending = 0;
return;
} else {
/* Node is solvent */
/* If there's no cash to redeem from */
if (node.available == 0) return;
/* Redeem as many shares as possible and pending from available cash */
uint128 shares = uint128(Math.min((node.available * FIXED_POINT_SCALE) / price, node.redemptions.pending));
uint128 amount = Math.mulDiv(shares, price, FIXED_POINT_SCALE).toUint128();
/* If there's insufficient cash to redeem non-zero pending shares
* at current price */
if (shares == 0) return;
/* Record fulfilled redemption */
node.redemptions.fulfilled[node.redemptions.index++] = FulfilledRedemption({
shares: shares,
amount: amount
});
/* Update node state */
node.shares -= shares;
node.value -= amount;
node.available -= amount;
node.redemptions.pending -= shares;
/* Garbage collect node if it is now empty */
_garbageCollect(liquidity, node);
return;
}
}
/**************************************************************************/
/* Primary API */
/**************************************************************************/
/**
* @notice Initialize liquidity state
* @param liquidity Liquidity state
*/
function initialize(Liquidity storage liquidity) internal {
/* Liquidity state defaults to zero, but need to make head and tail nodes */
liquidity.nodes[0].next = type(uint128).max;
/* liquidity.nodes[type(uint128).max].prev = 0 by default */
}
/**
* @notice Deposit liquidity
* @param liquidity Liquidity state
* @param tick Tick
* @param amount Amount
* @return Number of shares
*/
function deposit(Liquidity storage liquidity, uint128 tick, uint128 amount) internal returns (uint128) {
Node storage node = liquidity.nodes[tick];
/* If tick is reserved */
if (_isReserved(tick)) revert InactiveLiquidity();
/* Instantiate node, if necessary */
_instantiate(liquidity, node, tick);
/* Compute deposit price as current value + 50% of pending returns */
uint256 price = node.shares == 0
? FIXED_POINT_SCALE
: ((node.value + (node.available + node.pending - node.value) / 2) * FIXED_POINT_SCALE) / node.shares;
uint128 shares = ((amount * FIXED_POINT_SCALE) / price).toUint128();
node.value += amount;
node.shares += shares;
node.available += amount;
/* Process any pending redemptions from available cash */
_processRedemptions(liquidity, node);
return shares;
}
/**
* @notice Use liquidity from node
* @param liquidity Liquidity state
* @param tick Tick
* @param used Used amount
* @param pending Pending Amount
*/
function use(Liquidity storage liquidity, uint128 tick, uint128 used, uint128 pending) internal {
Node storage node = liquidity.nodes[tick];
node.available -= used;
node.pending += pending;
}
/**
* @notice Restore liquidity and process pending redemptions
* @param liquidity Liquidity state
* @param tick Tick
* @param used Used amount
* @param pending Pending amount
* @param restored Restored amount
*/
function restore(
Liquidity storage liquidity,
uint128 tick,
uint128 used,
uint128 pending,
uint128 restored
) internal {
Node storage node = liquidity.nodes[tick];
node.value = node.value - used + restored;
node.available += restored;
node.pending -= pending;
/* Garbage collect node if it is now impaired */
_garbageCollect(liquidity, node);
/* Process any pending redemptions */
_processRedemptions(liquidity, node);
}
/**
* @notice Redeem liquidity
* @param liquidity Liquidity state
* @param tick Tick
* @param shares Shares
* @return Redemption index, Redemption target
*/
function redeem(Liquidity storage liquidity, uint128 tick, uint128 shares) internal returns (uint128, uint128) {
Node storage node = liquidity.nodes[tick];
/* Redemption from inactive liquidity nodes is allowed to facilitate
* restoring garbage collected nodes */
/* Snapshot redemption target */
uint128 redemptionIndex = node.redemptions.index;
uint128 redemptionTarget = node.redemptions.pending;
/* Add shares to pending redemptions */
node.redemptions.pending += shares;
/* Initialize redemption record to save gas in loan callbacks */
if (node.redemptions.fulfilled[redemptionIndex].shares != type(uint128).max) {
node.redemptions.fulfilled[redemptionIndex] = FulfilledRedemption({shares: type(uint128).max, amount: 0});
}
/* Process any pending redemptions from available cash */
_processRedemptions(liquidity, node);
return (redemptionIndex, redemptionTarget);
}
/**
* @notice Source liquidity from nodes
* @param liquidity Liquidity state
* @param amount Amount
* @param ticks Ticks to source from
* @param multiplier Multiplier for amount
* @param durationIndex Duration index for amount
* @return Sourced liquidity nodes, count of nodes
*/
function source(
Liquidity storage liquidity,
uint256 amount,
uint128[] calldata ticks,
uint256 multiplier,
uint256 durationIndex
) internal view returns (ILiquidity.NodeSource[] memory, uint16) {
ILiquidity.NodeSource[] memory sources = new ILiquidity.NodeSource[](ticks.length);
uint256 prevTick;
uint256 taken;
uint256 count;
for (; count < ticks.length && taken != amount; count++) {
uint128 tick = ticks[count];
/* Validate tick and decode limit */
uint256 limit = Tick.validate(tick, prevTick, durationIndex);
/* Look up liquidity node */
Node storage node = liquidity.nodes[tick];
/* Consume as much as possible up to the tick limit, amount available, and amount remaining */
uint128 take = uint128(Math.min(Math.min(limit * multiplier - taken, node.available), amount - taken));
/* Record the liquidity allocation in our sources list */
sources[count] = ILiquidity.NodeSource({tick: tick, used: take});
taken += take;
prevTick = tick;
}
/* If unable to source required liquidity amount from provided ticks */
if (taken < amount) revert InsufficientLiquidity();
return (sources, count.toUint16());
}
}
contracts/LoanReceipt.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.20;
/**
* @title LoanReceipt
* @author MetaStreet Labs
*/
library LoanReceipt {
/**************************************************************************/
/* Errors */
/**************************************************************************/
/**
* @notice Invalid receipt encoding
*/
error InvalidReceiptEncoding();
/**************************************************************************/
/* Constants */
/**************************************************************************/
/**
* @notice LoanReceiptV1 version
*/
uint8 internal constant LOAN_RECEIPT_V1_VERSION = 1;
/**
* @notice LoanReceiptV1 header size in bytes
* @dev Header excludes borrow options byte array
*/
uint256 internal constant LOAN_RECEIPT_V1_HEADER_SIZE = 155;
/**
* @notice LoanReceiptV1 node receipt size in bytes
*/
uint256 internal constant LOAN_RECEIPT_V1_NODE_RECEIPT_SIZE = 48;
/**************************************************************************/
/* Structures */
/**************************************************************************/
/**
* @notice LoanReceiptV1
* @param version Version (1)
* @param principal Principal amount in currency tokens
* @param repayment Repayment amount in currency tokens
* @param borrower Borrower
* @param maturity Loan maturity timestamp
* @param duration Loan duration
* @param collateralToken Collateral token
* @param collateralTokenId Collateral token ID
* @param collateralWrapperContextLen Collateral wrapper context length
* @param collateralWrapperContext Collateral wrapper context data
* @param nodeReceipts Node receipts
*/
struct LoanReceiptV1 {
uint8 version;
uint256 principal;
uint256 repayment;
address borrower;
uint64 maturity;
uint64 duration;
address collateralToken;
uint256 collateralTokenId;
uint16 collateralWrapperContextLen;
bytes collateralWrapperContext;
NodeReceipt[] nodeReceipts;
}
/**
* @notice Node receipt
* @param tick Tick
* @param used Used amount
* @param pending Pending amount
*/
struct NodeReceipt {
uint128 tick;
uint128 used;
uint128 pending;
}
/**************************************************************************/
/* Tightly packed format */
/**************************************************************************/
/*
Header (155 bytes)
1 uint8 version 0:1
32 uint256 principal 1:33
32 uint256 repayment 33:65
20 address borrower 65:85
8 uint64 maturity 85:93
8 uint64 duration 93:101
20 address collateralToken 101:121
32 uint256 collateralTokenId 121:153
2 uint16 collateralWrapperContextLen 153:155
Collateral Wrapper Context Data (M bytes) 155:---
Node Receipts (48 * N bytes)
N NodeReceipts[] nodeReceipts
16 uint128 tick
16 uint128 used
16 uint128 pending
*/
/**************************************************************************/
/* API */
/**************************************************************************/
/**
* @dev Compute loan receipt hash
* @param encodedReceipt Encoded loan receipt
* @return Loan Receipt hash
*/
function hash(bytes memory encodedReceipt) internal view returns (bytes32) {
/* Take hash of chain ID (32 bytes) concatenated with encoded loan receipt */
return keccak256(abi.encodePacked(block.chainid, encodedReceipt));
}
/**
* @dev Encode a loan receipt into bytes
* @param receipt Loan Receipt
* @return Encoded loan receipt
*/
function encode(LoanReceiptV1 memory receipt) internal pure returns (bytes memory) {
/* Encode header */
bytes memory header = abi.encodePacked(
receipt.version,
receipt.principal,
receipt.repayment,
receipt.borrower,
receipt.maturity,
receipt.duration,
receipt.collateralToken,
receipt.collateralTokenId,
receipt.collateralWrapperContextLen,
receipt.collateralWrapperContext
);
/* Encode node receipts */
bytes memory nodeReceipts;
for (uint256 i; i < receipt.nodeReceipts.length; i++) {
nodeReceipts = abi.encodePacked(
nodeReceipts,
receipt.nodeReceipts[i].tick,
receipt.nodeReceipts[i].used,
receipt.nodeReceipts[i].pending
);
}
return abi.encodePacked(header, nodeReceipts);
}
/**
* @dev Decode a loan receipt from bytes
* @param encodedReceipt Encoded loan Receipt
* @return Decoded loan receipt
*/
function decode(bytes calldata encodedReceipt) internal pure returns (LoanReceiptV1 memory) {
/* Validate encoded receipt length */
if (encodedReceipt.length < LOAN_RECEIPT_V1_HEADER_SIZE) revert InvalidReceiptEncoding();
uint16 collateralWrapperContextLen = uint16(bytes2(encodedReceipt[153:155]));
/* Validate length with collateral wrapper context */
if (encodedReceipt.length < LOAN_RECEIPT_V1_HEADER_SIZE + collateralWrapperContextLen)
revert InvalidReceiptEncoding();
/* Validate length with node receipts */
if (
(encodedReceipt.length - LOAN_RECEIPT_V1_HEADER_SIZE - collateralWrapperContextLen) %
LOAN_RECEIPT_V1_NODE_RECEIPT_SIZE !=
0
) revert InvalidReceiptEncoding();
/* Validate encoded receipt version */
if (uint8(encodedReceipt[0]) != LOAN_RECEIPT_V1_VERSION) revert InvalidReceiptEncoding();
LoanReceiptV1 memory receipt;
/* Decode header */
receipt.version = uint8(encodedReceipt[0]);
receipt.principal = uint256(bytes32(encodedReceipt[1:33]));
receipt.repayment = uint256(bytes32(encodedReceipt[33:65]));
receipt.borrower = address(uint160(bytes20(encodedReceipt[65:85])));
receipt.maturity = uint64(bytes8(encodedReceipt[85:93]));
receipt.duration = uint64(bytes8(encodedReceipt[93:101]));
receipt.collateralToken = address(uint160(bytes20(encodedReceipt[101:121])));
receipt.collateralTokenId = uint256(bytes32(encodedReceipt[121:153]));
receipt.collateralWrapperContextLen = collateralWrapperContextLen;
receipt.collateralWrapperContext = encodedReceipt[155:155 + collateralWrapperContextLen];
/* Decode node receipts */
uint256 numNodeReceipts = (encodedReceipt.length - LOAN_RECEIPT_V1_HEADER_SIZE - collateralWrapperContextLen) /
LOAN_RECEIPT_V1_NODE_RECEIPT_SIZE;
receipt.nodeReceipts = new NodeReceipt[](numNodeReceipts);
uint256 offset = LOAN_RECEIPT_V1_HEADER_SIZE + collateralWrapperContextLen;
for (uint256 i; i < numNodeReceipts; i++) {
receipt.nodeReceipts[i].tick = uint128(bytes16(encodedReceipt[offset:offset + 16]));
receipt.nodeReceipts[i].used = uint128(bytes16(encodedReceipt[offset + 16:offset + 32]));
receipt.nodeReceipts[i].pending = uint128(bytes16(encodedReceipt[offset + 32:offset + 48]));
offset += LOAN_RECEIPT_V1_NODE_RECEIPT_SIZE;
}
return receipt;
}
}
contracts/Pool.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.20;
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import "@openzeppelin/contracts/utils/Multicall.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "@openzeppelin/contracts/utils/math/SafeCast.sol";
import "./LoanReceipt.sol";
import "./LiquidityManager.sol";
import "./CollateralFilter.sol";
import "./InterestRateModel.sol";
import "./interfaces/IPool.sol";
import "./interfaces/ILiquidity.sol";
import "./interfaces/ICollateralWrapper.sol";
import "./interfaces/ICollateralLiquidator.sol";
import "./interfaces/ICollateralLiquidationReceiver.sol";
import "./integrations/DelegateCash/IDelegationRegistry.sol";
/**
* @title Pool
* @author MetaStreet Labs
*/
abstract contract Pool is
ERC165,
ReentrancyGuard,
Multicall,
CollateralFilter,
InterestRateModel,
IPool,
ILiquidity,
ICollateralLiquidationReceiver
{
using SafeCast for uint256;
using SafeERC20 for IERC20;
using LoanReceipt for LoanReceipt.LoanReceiptV1;
using LiquidityManager for LiquidityManager.Liquidity;
/**************************************************************************/
/* Constants */
/**************************************************************************/
/**
* @notice Tick spacing basis points
*/
uint256 public constant TICK_LIMIT_SPACING_BASIS_POINTS = LiquidityManager.TICK_LIMIT_SPACING_BASIS_POINTS;
/**
* @notice Borrower's split of liquidation proceed surplus in basis points
*/
uint256 public constant BORROWER_SURPLUS_SPLIT_BASIS_POINTS = 9_500;
/**
* @notice Basis points scale
*/
uint256 internal constant BASIS_POINTS_SCALE = 10_000;
/**
* @notice Borrow options tag size in bytes
*/
uint256 internal constant BORROW_OPTIONS_TAG_SIZE = 2;
/**
* @notice Borrow options length size in bytes
*/
uint256 internal constant BORROW_OPTIONS_LENGTH_SIZE = 2;
/**************************************************************************/
/* Errors */
/**************************************************************************/
/**
* @notice Invalid address
*/
error InvalidAddress();
/**
* @notice Parameter out of bounds
*/
error ParameterOutOfBounds();
/**************************************************************************/
/* Structures */
/**************************************************************************/
/**
* @notice Deposit
* @param shares Shares
* @param redemptionPending Redemption shares pending
* @param redemptionIndex Redemption queue index
* @param redemptionTarget Redemption queue target
*/
struct Deposit {
uint128 shares;
uint128 redemptionPending;
uint128 redemptionIndex;
uint128 redemptionTarget;
}
/**
* @notice Loan status
*/
enum LoanStatus {
Uninitialized,
Active,
Repaid,
Liquidated,
CollateralLiquidated
}
/**
* @notice Borrow function options
*/
enum BorrowOptions {
None,
CollateralWrapperContext,
CollateralFilterContext,
DelegateCash
}
/**************************************************************************/
/* Immutable State */
/**************************************************************************/
/**
* @notice Collateral wrappers (max 3)
*/
address internal immutable _collateralWrapper1;
address internal immutable _collateralWrapper2;
address internal immutable _collateralWrapper3;
/**
* @notice Collateral liquidator
*/
ICollateralLiquidator internal immutable _collateralLiquidator;
/**
* @notice Delegation registry contract
*/
IDelegationRegistry internal immutable _delegationRegistry;
/**************************************************************************/
/* State */
/**************************************************************************/
/**
* @notice Currency token contract
*/
IERC20 internal _currencyToken;
/**
* @notice Admin fee rate in basis points
*/
uint32 internal _adminFeeRate;
/**
* @notice Durations
*/
uint64[] internal _durations;
/**
* @notice Rates
*/
uint64[] internal _rates;
/**
* @notice Admin
*/
address private _admin;
/**
* @notice Total admin fee balance
*/
uint256 internal _adminFeeBalance;
/**
* @notice Liquidity
*/
LiquidityManager.Liquidity internal _liquidity;
/**
* @notice Mapping of account to tick to deposit
*/
mapping(address => mapping(uint128 => Deposit)) internal _deposits;
/**
* @notice Mapping of loan receipt hash to loan status
*/
mapping(bytes32 => LoanStatus) internal _loans;
/**************************************************************************/
/* Constructor */
/**************************************************************************/
/**
* @notice Pool constructor
* @param collateralLiquidator_ Collateral liquidator
* @param delegationRegistry_ Delegation registry contract
* @param collateralWrappers_ Collateral wrappers
*/
constructor(address collateralLiquidator_, address delegationRegistry_, address[] memory collateralWrappers_) {
_collateralLiquidator = ICollateralLiquidator(collateralLiquidator_);
_delegationRegistry = IDelegationRegistry(delegationRegistry_);
if (collateralWrappers_.length > 3) revert ParameterOutOfBounds();
_collateralWrapper1 = (collateralWrappers_.length > 0) ? collateralWrappers_[0] : address(0);
_collateralWrapper2 = (collateralWrappers_.length > 1) ? collateralWrappers_[1] : address(0);
_collateralWrapper3 = (collateralWrappers_.length > 2) ? collateralWrappers_[2] : address(0);
}
/**************************************************************************/
/* Initializer */
/**************************************************************************/
/**
* @notice Pool initializer
* @dev Fee-on-transfer currency tokens are not supported
* @param currencyToken_ Currency token contract
* @param durations_ Duration tiers
* @param rates_ Interest rate tiers
*/
function _initialize(address currencyToken_, uint64[] memory durations_, uint64[] memory rates_) internal {
if (IERC20Metadata(currencyToken_).decimals() != 18) revert ParameterOutOfBounds();
_currencyToken = IERC20(currencyToken_);
_admin = msg.sender;
/* Assign durations */
if (durations_.length > Tick.MAX_NUM_DURATIONS) revert ParameterOutOfBounds();
for (uint256 i; i < durations_.length; i++) {
/* Check duration is monotonic */
if (i != 0 && durations_[i] <= durations_[i - 1]) revert ParameterOutOfBounds();
_durations.push(durations_[i]);
}
/* Assign rates */
if (rates_.length > Tick.MAX_NUM_RATES) revert ParameterOutOfBounds();
for (uint256 i; i < rates_.length; i++) {
/* Check rate is monotonic */
if (i != 0 && rates_[i] <= rates_[i - 1]) revert ParameterOutOfBounds();
_rates.push(rates_[i]);
}
/* Initialize liquidity */
_liquidity.initialize();
}
/**************************************************************************/
/* Getters */
/**************************************************************************/
/**
* @notice Get implementation name
* @return Implementation name
*/
function IMPLEMENTATION_NAME() external pure virtual returns (string memory);
/**
* @notice Get implementation version
* @return Implementation version
*/
function IMPLEMENTATION_VERSION() external pure returns (string memory) {
return "1.4";
}
/**
* @inheritdoc IPool
*/
function currencyToken() external view returns (address) {
return address(_currencyToken);
}
/**
* @inheritdoc IPool
*/
function durations() external view returns (uint64[] memory) {
return _durations;
}
/**
* @inheritdoc IPool
*/
function rates() external view returns (uint64[] memory) {
return _rates;
}
/**
* @inheritdoc IPool
*/
function admin() external view returns (address) {
return _admin;
}
/**
* @inheritdoc IPool
*/
function adminFeeRate() external view returns (uint32) {
return _adminFeeRate;
}
/**
* @inheritdoc IPool
*/
function collateralWrappers() external view returns (address[] memory) {
address[] memory collateralWrappers_ = new address[](3);
collateralWrappers_[0] = _collateralWrapper1;
collateralWrappers_[1] = _collateralWrapper2;
collateralWrappers_[2] = _collateralWrapper3;
return collateralWrappers_;
}
/**
* @inheritdoc IPool
*/
function collateralLiquidator() external view returns (address) {
return address(_collateralLiquidator);
}
/**
* @inheritdoc IPool
*/
function delegationRegistry() external view returns (address) {
return address(_delegationRegistry);
}
/**
* @notice Get deposit
* @param account Account
* @param tick Tick
* @return Deposit information
*/
function deposits(address account, uint128 tick) external view returns (Deposit memory) {
return _deposits[account][tick];
}
/**
* @notice Get loan status
* @param receiptHash Loan receipt hash
* @return Loan status
*/
function loans(bytes32 receiptHash) external view returns (LoanStatus) {
return _loans[receiptHash];
}
/**
* @notice Get total admin fee balance
* @return Total admin fee balance
*/
function adminFeeBalance() external view returns (uint256) {
return _adminFeeBalance;
}
/**************************************************************************/
/* Loan Receipt External Helpers */
/**************************************************************************/
/**
* @notice Decode loan receipt
* @param loanReceipt Loan receipt
* @return Decoded loan receipt
*/
function decodeLoanReceipt(bytes calldata loanReceipt) external pure returns (LoanReceipt.LoanReceiptV1 memory) {
return LoanReceipt.decode(loanReceipt);
}
/**************************************************************************/
/* ILiquidity Getters */
/**************************************************************************/
/**
* @inheritdoc ILiquidity
*/
function liquidityNodes(uint128 startTick, uint128 endTick) external view returns (NodeInfo[] memory) {
return _liquidity.liquidityNodes(startTick, endTick);
}
/**
* @inheritdoc ILiquidity
*/
function liquidityNode(uint128 tick) external view returns (NodeInfo memory) {
return _liquidity.liquidityNode(tick);
}
/**************************************************************************/
/* Helper Functions */
/**************************************************************************/
/**
* @notice Helper function to extract specified option tag from options
* data
*
* @dev Options are encoded as:
* 2 byte uint16 tag
* 2 byte uint16 length
* n byte bytes data
* The first matching tag is returned.
*
* @param options Encoded options
* @param tag Tag to find
* @return Options data
*/
function _getOptionsData(bytes calldata options, uint16 tag) internal pure returns (bytes calldata) {
uint256 offsetTag = 0;
/* Scan the options for the tag */
while (offsetTag < options.length) {
/* Compute offsets with for tag length and data */
uint256 offsetLength = offsetTag + BORROW_OPTIONS_TAG_SIZE;
uint256 offsetData = offsetTag + BORROW_OPTIONS_TAG_SIZE + BORROW_OPTIONS_LENGTH_SIZE;
/* The tag is in the first 2 bytes of each options item */
uint16 currentTag = uint16(bytes2(options[offsetTag:offsetLength]));
/* The length of the options data is in the second 2 bytes of each options item, after the tag */
uint256 dataLength = uint16(bytes2(options[offsetLength:offsetData]));
/* Return the offset and length if the tag is found */
if (currentTag == tag) {
return options[offsetData:offsetData + dataLength];
}
/* Increment to next options item */
offsetTag = offsetData + dataLength;
}
/* Return empty slice if no tag is found */
return options[0:0];
}
/**
* @notice Helper function that returns underlying collateral in (address,
* uint256[]) shape
* @param collateralToken Collateral token, either underlying token or collateral wrapper
* @param collateralTokenId Collateral token ID
* @param collateralWrapperContext Collateral wrapper context
* @return Underlying collateral token and token IDs
*/
function _getUnderlyingCollateral(
address collateralToken,
uint256 collateralTokenId,
bytes memory collateralWrapperContext
) internal view returns (address, uint256[] memory) {
/* Enumerate bundle if collateral token is a collateral wrapper */
if (
collateralToken == _collateralWrapper1 ||
collateralToken == _collateralWrapper2 ||
collateralToken == _collateralWrapper3
) {
return ICollateralWrapper(collateralToken).enumerate(collateralTokenId, collateralWrapperContext);
}
/* If single asset, convert to length one token ID array */
uint256[] memory underlyingCollateralTokenIds = new uint256[](1);
underlyingCollateralTokenIds[0] = collateralTokenId;
return (collateralToken, underlyingCollateralTokenIds);
}
/**
* @notice Helper function that calls delegate.cash registry to delegate
* token
* @param collateralToken Collateral token
* @param collateralTokenId Collateral token ID
* @param options Options data
*/
function _optionDelegateCash(address collateralToken, uint256 collateralTokenId, bytes calldata options) internal {
/* Find delegate.cash tagged data in options */
bytes calldata delegateData = _getOptionsData(options, uint16(BorrowOptions.DelegateCash));
if (delegateData.length != 0) {
if (address(_delegationRegistry) == address(0) || delegateData.length != 20) revert InvalidBorrowOptions();
/* Delegate token */
_delegationRegistry.delegateForToken(
address(uint160(bytes20(delegateData))),
collateralToken,
collateralTokenId,
true
);
}
}
/**
* @dev Helper function to revoke token delegate
* @param collateralToken Contract address of token that delegation is being removed from
* @param collateralTokenId Token id of token that delegation is being removed from
*/
function _revokeDelegates(address collateralToken, uint256 collateralTokenId) internal {
/* No operation if _delegationRegistry not set */
if (address(_delegationRegistry) == address(0)) return;
/* Get delegates for collateral token and id */
address[] memory delegates = _delegationRegistry.getDelegatesForToken(
address(this),
collateralToken,
collateralTokenId
);
for (uint256 i; i < delegates.length; i++) {
/* Revoke by setting value to false */
_delegationRegistry.delegateForToken(delegates[i], collateralToken, collateralTokenId, false);
}
}
/**
* @dev Helper function to quote a loan
* @param principal Principal amount in currency tokens
* @param duration Duration in seconds
* @param collateralToken Collateral token address
* @param collateralTokenIds List of collateral token ids
* @param ticks Liquidity node ticks
* @param collateralFilterContext Collateral filter context
* @return Repayment amount in currency tokens, liquidity nodes, liquidity
* node count
*/
function _quote(
uint256 principal,
uint64 duration,
address collateralToken,
uint256[] memory collateralTokenIds,
uint128[] calldata ticks,
bytes calldata collateralFilterContext
) internal view returns (uint256, ILiquidity.NodeSource[] memory, uint16) {
/* Verify collateral is supported */
for (uint256 i; i < collateralTokenIds.length; i++) {
if (!_collateralSupported(collateralToken, collateralTokenIds[i], i, collateralFilterContext))
revert UnsupportedCollateral(i);
}
/* Cache durations */
uint64[] memory durations_ = _durations;
/* Lookup duration index */
uint256 durationIndex;
for (; durationIndex < durations_.length; durationIndex++) {
if (duration <= durations_[durationIndex]) break;
}
/* Validate duration index */
if (durationIndex == durations_.length) revert UnsupportedLoanDuration();
/* Source liquidity nodes */
(ILiquidity.NodeSource[] memory nodes, uint16 count) = _liquidity.source(
principal,
ticks,
collateralTokenIds.length,
durationIndex
);
/* Calculate repayment from principal, rate, and duration */
uint256 repayment = (principal *
(LiquidityManager.FIXED_POINT_SCALE + (_rate(principal, _rates, nodes, count) * duration))) /
LiquidityManager.FIXED_POINT_SCALE;
return (repayment, nodes, count);
}
/**
* @dev Helper function to calculated prorated repayment
* @param loanReceipt Decoded loan receipt
* @return repayment amount in currency tokens
* @return proration based on elapsed duration
*/
function _prorateRepayment(
LoanReceipt.LoanReceiptV1 memory loanReceipt
) internal view returns (uint256 repayment, uint256 proration) {
/* Minimum of proration and 1.0 */
proration = Math.min(
((block.timestamp - (loanReceipt.maturity - loanReceipt.duration)) * LiquidityManager.FIXED_POINT_SCALE) /
loanReceipt.duration,
LiquidityManager.FIXED_POINT_SCALE
);
/* Compute repayment using prorated interest */
repayment =
loanReceipt.principal +
(((loanReceipt.repayment - loanReceipt.principal) * proration) / LiquidityManager.FIXED_POINT_SCALE);
}
/**
* @dev Helper function to handle borrow accounting
* @param principal Principal amount in currency tokens
* @param duration Duration in seconds
* @param collateralToken Collateral token address
* @param collateralTokenId Collateral token ID
* @param maxRepayment Maximum repayment amount in currency tokens
* @param ticks Liquidity node ticks
* @param collateralWrapperContext Collateral wrapper context data
* @param collateralFilterContext Collateral filter context data
* @return Repayment amount in currency tokens, encoded loan receipt, loan
* receipt hash
*/
function _borrow(
uint256 principal,
uint64 duration,
address collateralToken,
uint256 collateralTokenId,
uint256 maxRepayment,
uint128[] calldata ticks,
bytes memory collateralWrapperContext,
bytes calldata collateralFilterContext
) internal returns (uint256, bytes memory, bytes32) {
/* Validate duration is non-zero */
if (duration == 0) revert UnsupportedLoanDuration();
/* Get underlying collateral */
(address underlyingCollateralToken, uint256[] memory underlyingCollateralTokenIds) = _getUnderlyingCollateral(
collateralToken,
collateralTokenId,
collateralWrapperContext
);
/* Quote repayment and liquidity nodes */
(uint256 repayment, ILiquidity.NodeSource[] memory nodes, uint16 count) = _quote(
principal,
duration,
underlyingCollateralToken,
underlyingCollateralTokenIds,
ticks,
collateralFilterContext
);
/* Validate repayment */
if (repayment > maxRepayment) revert RepaymentTooHigh();
/* Compute total fee */
uint256 totalFee = repayment - principal;
/* Compute admin fee */
uint256 adminFee = (_adminFeeRate * totalFee) / BASIS_POINTS_SCALE;
/* Distribute interest */
uint128[] memory interest = _distribute(principal, totalFee - adminFee, nodes, count);
/* Build the loan receipt */
LoanReceipt.LoanReceiptV1 memory receipt = LoanReceipt.LoanReceiptV1({
version: 1,
principal: principal,
repayment: repayment,
borrower: msg.sender,
maturity: uint64(block.timestamp + duration),
duration: duration,
collateralToken: collateralToken,
collateralTokenId: collateralTokenId,
collateralWrapperContextLen: collateralWrapperContext.length.toUint16(),
collateralWrapperContext: collateralWrapperContext,
nodeReceipts: new LoanReceipt.NodeReceipt[](count)
});
/* Use liquidity nodes */
for (uint256 i; i < count; i++) {
/* Compute pending */
uint128 pending = nodes[i].used + interest[i];
/* Use node */
_liquidity.use(nodes[i].tick, nodes[i].used, pending);
/* Construct node receipt */
receipt.nodeReceipts[i] = LoanReceipt.NodeReceipt({
tick: nodes[i].tick,
used: nodes[i].used,
pending: pending
});
}
/* Encode and hash the loan receipt */
bytes memory encodedLoanReceipt = receipt.encode();
bytes32 loanReceiptHash = LoanReceipt.hash(encodedLoanReceipt);
/* Validate no loan receipt hash collision */
if (_loans[loanReceiptHash] != LoanStatus.Uninitialized) revert InvalidLoanReceipt();
/* Store loan status */
_loans[loanReceiptHash] = LoanStatus.Active;
return (repayment, encodedLoanReceipt, loanReceiptHash);
}
/**
* @dev Helper function to handle repay accounting
* @param encodedLoanReceipt Encoded loan receipt
* @return Repayment amount in currency tokens, decoded loan receipt, loan
* receipt hash
*/
function _repay(
bytes calldata encodedLoanReceipt
) internal returns (uint256, LoanReceipt.LoanReceiptV1 memory, bytes32) {
/* Compute loan receipt hash */
bytes32 loanReceiptHash = LoanReceipt.hash(encodedLoanReceipt);
/* Validate loan receipt */
if (_loans[loanReceiptHash] != LoanStatus.Active) revert InvalidLoanReceipt();
/* Decode loan receipt */
LoanReceipt.LoanReceiptV1 memory loanReceipt = LoanReceipt.decode(encodedLoanReceipt);
/* Validate borrow and repay is not in same block */
if (loanReceipt.maturity - loanReceipt.duration == block.timestamp) revert InvalidLoanReceipt();
/* Validate caller is borrower */
if (msg.sender != loanReceipt.borrower) revert InvalidCaller();
/* Compute proration and repayment using prorated interest */
(uint256 repayment, uint256 proration) = _prorateRepayment(loanReceipt);
/* Restore liquidity nodes */
uint128 totalPending;
for (uint256 i; i < loanReceipt.nodeReceipts.length; i++) {
/* Restore node */
_liquidity.restore(
loanReceipt.nodeReceipts[i].tick,
loanReceipt.nodeReceipts[i].used,
loanReceipt.nodeReceipts[i].pending,
loanReceipt.nodeReceipts[i].used +
uint128(
((loanReceipt.nodeReceipts[i].pending - loanReceipt.nodeReceipts[i].used) * proration) /
LiquidityManager.FIXED_POINT_SCALE
)
);
/* Accumulate pending */
totalPending += loanReceipt.nodeReceipts[i].pending;
}
/* Update admin fee total balance with prorated admin fee */
_adminFeeBalance += ((loanReceipt.repayment - totalPending) * proration) / LiquidityManager.FIXED_POINT_SCALE;
/* Mark loan status repaid */
_loans[loanReceiptHash] = LoanStatus.Repaid;
return (repayment, loanReceipt, loanReceiptHash);
}
/**
* @dev Helper function to handle deposit accounting
* @param tick Tick
* @param amount Amount
* @param minShares Minimum shares
* @return Deposit shares
*/
function _deposit(uint128 tick, uint128 amount, uint128 minShares) internal returns (uint128) {
/* Validate tick */
Tick.validate(tick, 0, 0, _durations.length - 1, 0, _rates.length - 1);
/* Deposit into liquidity node */
uint128 shares = _liquidity.deposit(tick, amount);
/* Validate shares received is sufficient */
if (shares == 0 || shares < minShares) revert InsufficientShares();
/* Add to deposit */
_deposits[msg.sender][tick].shares += shares;
return shares;
}
/**
* @dev Helper function to handle redeem accounting
* @param tick Tick
* @param shares Shares
*/
function _redeem(uint128 tick, uint128 shares) internal {
/* Look up Deposit */
Deposit storage dep = _deposits[msg.sender][tick];
/* Validate shares */
if (shares == 0 || shares > dep.shares) revert InsufficientShares();
/* Validate redemption isn't pending */
if (dep.redemptionPending != 0) revert InvalidRedemptionStatus();
/* Redeem shares in tick with liquidity manager */
(uint128 redemptionIndex, uint128 redemptionTarget) = _liquidity.redeem(tick, shares);
/* Update deposit state */
dep.redemptionPending = shares;
dep.redemptionIndex = redemptionIndex;
dep.redemptionTarget = redemptionTarget;
}
/**
* @dev Helper function to handle withdraw accounting
* @param tick Tick
* @return Withdrawn shares and withdrawn amount
*/
function _withdraw(uint128 tick) internal returns (uint128, uint128) {
/* Look up Deposit */
Deposit storage dep = _deposits[msg.sender][tick];
/* If no redemption is pending */
if (dep.redemptionPending == 0) revert InvalidRedemptionStatus();
/* Look up redemption available */
(uint128 shares, uint128 amount, uint128 processedIndices, uint128 processedShares) = _liquidity
.redemptionAvailable(tick, dep.redemptionPending, dep.redemptionIndex, dep.redemptionTarget);
/* If the entire redemption is ready */
if (shares == dep.redemptionPending) {
dep.redemptionPending = 0;
dep.redemptionIndex = 0;
dep.redemptionTarget = 0;
} else {
dep.redemptionPending -= shares;
dep.redemptionIndex += processedIndices;
dep.redemptionTarget = (processedShares < dep.redemptionTarget)
? dep.redemptionTarget - processedShares
: 0;
}
/* Decrement deposit shares */
dep.shares -= shares;
return (shares, amount);
}
/**************************************************************************/
/* Lend API */
/**************************************************************************/
/**
* @inheritdoc IPool
*/
function quote(
uint256 principal,
uint64 duration,
address collateralToken,
uint256[] calldata collateralTokenIds,
uint128[] calldata ticks,
bytes calldata options
) external view returns (uint256) {
/* Quote repayment */
(uint256 repayment, , ) = _quote(
principal,
duration,
collateralToken,
collateralTokenIds,
ticks,
_getOptionsData(options, uint16(BorrowOptions.CollateralFilterContext))
);
return repayment;
}
/**
* @inheritdoc IPool
*/
function quoteRefinance(
bytes calldata encodedLoanReceipt,
uint256 principal,
uint64 duration,
uint128[] calldata ticks
) external view returns (int256, uint256) {
/* Decode loan receipt */
LoanReceipt.LoanReceiptV1 memory loanReceipt = LoanReceipt.decode(encodedLoanReceipt);
/* Get underlying collateral */
(address underlyingCollateralToken, uint256[] memory underlyingCollateralTokenIds) = _getUnderlyingCollateral(
loanReceipt.collateralToken,
loanReceipt.collateralTokenId,
loanReceipt.collateralWrapperContext
);
/* Quote repayment */
(uint256 newRepayment, , ) = _quote(
principal,
duration,
underlyingCollateralToken,
underlyingCollateralTokenIds,
ticks,
encodedLoanReceipt[0:0]
);
/* Compute repayment using prorated interest */
(uint256 proratedRepayment, ) = _prorateRepayment(loanReceipt);
return (int256(proratedRepayment) - int256(principal), newRepayment);
}
/**
* @inheritdoc IPool
*/
function borrow(
uint256 principal,
uint64 duration,
address collateralToken,
uint256 collateralTokenId,
uint256 maxRepayment,
uint128[] calldata ticks,
bytes calldata options
) external nonReentrant returns (uint256) {
/* Handle borrow accounting */
(uint256 repayment, bytes memory encodedLoanReceipt, bytes32 loanReceiptHash) = _borrow(
principal,
duration,
collateralToken,
collateralTokenId,
maxRepayment,
ticks,
_getOptionsData(options, uint16(BorrowOptions.CollateralWrapperContext)),
_getOptionsData(options, uint16(BorrowOptions.CollateralFilterContext))
);
/* Handle delegate.cash option */
_optionDelegateCash(collateralToken, collateralTokenId, options);
/* Transfer collateral from borrower to pool */
IERC721(collateralToken).transferFrom(msg.sender, address(this), collateralTokenId);
/* Transfer principal from pool to borrower */
_currencyToken.safeTransfer(msg.sender, principal);
/* Emit LoanOriginated */
emit LoanOriginated(loanReceiptHash, encodedLoanReceipt);
return repayment;
}
/**
* @inheritdoc IPool
*/
function repay(bytes calldata encodedLoanReceipt) external nonReentrant returns (uint256) {
/* Handle repay accounting */
(uint256 repayment, LoanReceipt.LoanReceiptV1 memory loanReceipt, bytes32 loanReceiptHash) = _repay(
encodedLoanReceipt
);
/* Revoke delegates */
_revokeDelegates(loanReceipt.collateralToken, loanReceipt.collateralTokenId);
/* Transfer repayment from borrower to pool */
_currencyToken.safeTransferFrom(loanReceipt.borrower, address(this), repayment);
/* Transfer collateral from pool to borrower */
IERC721(loanReceipt.collateralToken).transferFrom(
address(this),
loanReceipt.borrower,
loanReceipt.collateralTokenId
);
/* Emit Loan Repaid */
emit LoanRepaid(loanReceiptHash, repayment);
return repayment;
}
/**
* @inheritdoc IPool
*/
function refinance(
bytes calldata encodedLoanReceipt,
uint256 principal,
uint64 duration,
uint256 maxRepayment,
uint128[] calldata ticks
) external nonReentrant returns (uint256) {
/* Handle repay accounting */
(uint256 repayment, LoanReceipt.LoanReceiptV1 memory loanReceipt, bytes32 loanReceiptHash) = _repay(
encodedLoanReceipt
);
/* Handle borrow accounting */
(uint256 newRepayment, bytes memory newEncodedLoanReceipt, bytes32 newLoanReceiptHash) = _borrow(
principal,
duration,
loanReceipt.collateralToken,
loanReceipt.collateralTokenId,
maxRepayment,
ticks,
loanReceipt.collateralWrapperContext,
encodedLoanReceipt[0:0]
);
/* Determine transfer direction */
if (principal < repayment) {
/* Transfer prorated repayment less principal from borrower to pool */
_currencyToken.safeTransferFrom(loanReceipt.borrower, address(this), repayment - principal);
} else {
/* Transfer principal less prorated repayment from pool to borrower */
_currencyToken.safeTransfer(msg.sender, principal - repayment);
}
/* Emit Loan Repaid */
emit LoanRepaid(loanReceiptHash, repayment);
/* Emit LoanOriginated */
emit LoanOriginated(newLoanReceiptHash, newEncodedLoanReceipt);
return newRepayment;
}
/**
* @inheritdoc IPool
*/
function liquidate(bytes calldata encodedLoanReceipt) external nonReentrant {
/* Compute loan receipt hash */
bytes32 loanReceiptHash = LoanReceipt.hash(encodedLoanReceipt);
/* Validate loan status is active */
if (_loans[loanReceiptHash] != LoanStatus.Active) revert InvalidLoanReceipt();
/* Decode loan receipt */
LoanReceipt.LoanReceiptV1 memory loanReceipt = LoanReceipt.decode(encodedLoanReceipt);
/* Validate loan is expired */
if (block.timestamp <= loanReceipt.maturity) revert LoanNotExpired();
/* Approve collateral for transfer to _collateralLiquidator */
IERC721(loanReceipt.collateralToken).approve(address(_collateralLiquidator), loanReceipt.collateralTokenId);
/* Mark loan status liquidated */
_loans[loanReceiptHash] = LoanStatus.Liquidated;
/* Revoke delegates */
_revokeDelegates(loanReceipt.collateralToken, loanReceipt.collateralTokenId);
/* Start liquidation with collateral liquidator */
_collateralLiquidator.liquidate(
address(_currencyToken),
loanReceipt.collateralToken,
loanReceipt.collateralTokenId,
loanReceipt.collateralWrapperContext,
encodedLoanReceipt
);
/* Emit Loan Liquidated */
emit LoanLiquidated(loanReceiptHash);
}
/**************************************************************************/
/* Callbacks */
/**************************************************************************/
/**
* @inheritdoc ICollateralLiquidationReceiver
*/
function onCollateralLiquidated(bytes calldata encodedLoanReceipt, uint256 proceeds) external nonReentrant {
/* Validate caller is collateral liquidator */
if (msg.sender != address(_collateralLiquidator)) revert InvalidCaller();
/* Compute loan receipt hash */
bytes32 loanReceiptHash = LoanReceipt.hash(encodedLoanReceipt);
/* Validate loan status is liquidated */
if (_loans[loanReceiptHash] != LoanStatus.Liquidated) revert InvalidLoanReceipt();
/* Decode loan receipt */
LoanReceipt.LoanReceiptV1 memory loanReceipt = LoanReceipt.decode(encodedLoanReceipt);
/* Compute borrower's share of liquidation surplus */
uint256 borrowerSurplus = proceeds > loanReceipt.repayment
? Math.mulDiv(proceeds - loanReceipt.repayment, BORROWER_SURPLUS_SPLIT_BASIS_POINTS, BASIS_POINTS_SCALE)
: 0;
/* Compute remaining proceeds */
uint128 proceedsRemaining = (proceeds - borrowerSurplus).toUint128();
/* Restore liquidity nodes */
for (uint256 i; i < loanReceipt.nodeReceipts.length; i++) {
/* Restore node */
uint128 restored = (i == loanReceipt.nodeReceipts.length - 1)
? proceedsRemaining
: uint128(Math.min(loanReceipt.nodeReceipts[i].pending, proceedsRemaining));
_liquidity.restore(
loanReceipt.nodeReceipts[i].tick,
loanReceipt.nodeReceipts[i].used,
loanReceipt.nodeReceipts[i].pending,
restored
);
/* Update proceeds remaining */
proceedsRemaining -= restored;
}
/* Mark loan status collateral liquidated */
_loans[loanReceiptHash] = LoanStatus.CollateralLiquidated;
/* Transfer surplus to borrower */
if (borrowerSurplus != 0) IERC20(_currencyToken).safeTransfer(loanReceipt.borrower, borrowerSurplus);
/* Emit Collateral Liquidated */
emit CollateralLiquidated(loanReceiptHash, proceeds, borrowerSurplus);
}
/**************************************************************************/
/* Deposit API */
/**************************************************************************/
/**
* @inheritdoc IPool
*/
function deposit(uint128 tick, uint256 amount, uint256 minShares) external nonReentrant returns (uint256) {
/* Handle deposit accounting and compute shares */
uint128 shares = _deposit(tick, amount.toUint128(), minShares.toUint128());
/* Transfer deposit amount */
_currencyToken.safeTransferFrom(msg.sender, address(this), amount);
/* Emit Deposited */
emit Deposited(msg.sender, tick, amount, shares);
return shares;
}
/**
* @inheritdoc IPool
*/
function redeem(uint128 tick, uint256 shares) external nonReentrant {
/* Handle redeem accounting */
_redeem(tick, shares.toUint128());
/* Emit Redeemed event */
emit Redeemed(msg.sender, tick, shares);
}
/**
* @inheritdoc IPool
*/
function redemptionAvailable(address account, uint128 tick) external view returns (uint256 shares, uint256 amount) {
/* Look up Deposit */
Deposit storage dep = _deposits[account][tick];
/* If no redemption is pending */
if (dep.redemptionPending == 0) return (0, 0);
(shares, amount, , ) = _liquidity.redemptionAvailable(
tick,
dep.redemptionPending,
dep.redemptionIndex,
dep.redemptionTarget
);
}
/**
* @inheritdoc IPool
*/
function withdraw(uint128 tick) external nonReentrant returns (uint256, uint256) {
/* Handle withdraw accounting and compute both shares and amount */
(uint128 shares, uint128 amount) = _withdraw(tick);
/* Transfer withdrawal amount */
_currencyToken.safeTransfer(msg.sender, amount);
/* Emit Withdrawn */
emit Withdrawn(msg.sender, tick, shares, amount);
return (shares, amount);
}
/**
* @inheritdoc IPool
*/
function rebalance(
uint128 srcTick,
uint128 dstTick,
uint256 minShares
) external nonReentrant returns (uint256, uint256, uint256) {
/* Handle withdraw accounting and compute both shares and amount */
(uint128 oldShares, uint128 amount) = _withdraw(srcTick);
/* Handle deposit accounting and compute new shares */
uint128 newShares = _deposit(dstTick, amount, minShares.toUint128());
/* Emit Withdrawn */
emit Withdrawn(msg.sender, srcTick, oldShares, amount);
/* Emit Deposited */
emit Deposited(msg.sender, dstTick, amount, newShares);
return (oldShares, newShares, amount);
}
/**************************************************************************/
/* Admin Fees API */
/**************************************************************************/
/**
* @notice Set the admin fee rate
*
* Emits a {AdminFeeRateUpdated} event.
*
* @param rate Rate is the admin fee in basis points
*/
function setAdminFeeRate(uint32 rate) external {
if (msg.sender != _admin) revert InvalidCaller();
if (rate == 0 || rate >= BASIS_POINTS_SCALE) revert ParameterOutOfBounds();
_adminFeeRate = rate;
emit AdminFeeRateUpdated(rate);
}
/**
* @notice Withdraw admin fees
*
* Emits a {AdminFeesWithdrawn} event.
*
* @param recipient Recipient account
* @param amount Amount to withdraw
*/
function withdrawAdminFees(address recipient, uint256 amount) external nonReentrant {
if (msg.sender != _admin) revert InvalidCaller();
if (recipient == address(0)) revert InvalidAddress();
if (amount > _adminFeeBalance) revert ParameterOutOfBounds();
/* Update admin fees balance */
_adminFeeBalance -= amount;
/* Transfer cash from Pool to recipient */
_currencyToken.safeTransfer(recipient, amount);
emit AdminFeesWithdrawn(recipient, amount);
}
/******************************************************/
/* ERC165 interface */
/******************************************************/
/**
* @inheritdoc IERC165
*/
function supportsInterface(bytes4 interfaceId) public view override returns (bool) {
return interfaceId == type(ICollateralLiquidationReceiver).interfaceId || super.supportsInterface(interfaceId);
}
}
contracts/Tick.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.20;
/**
* @title Tick
* @author MetaStreet Labs
*/
library Tick {
/*
* A tick encodes three conditions on liquidity: limit, duration, and rate.
* Limit is the maximum depth that liquidity sourced from the node can be
* used in. Duration is the maximum allowed duration for that liquidity.
* Rate is the interest rate associated with that liquidity. Duration and
* rates are encoded as indexes into predetermined, discrete tiers.
*
* +-----------------------------------------------------------------------+
* | 128 |
* +--------------------------------------|----------|----------|----------+
* | 120 | 3 | 3 | 2 |
* | Limit | Dur. Idx | Rate Idx | Reserved |
* +-----------------------------------------------------------------------+
*
* Duration Index is ordered from shortest duration to longest, e.g. 7
* days, 14 days, 30 days.
*
* Rate Index is ordered from lowest rate to highest rate, e.g. 10%, 30%,
* 50%.
*/
/**************************************************************************/
/* Constants */
/**************************************************************************/
/**
* @notice Tick limit mask
*/
uint256 internal constant TICK_LIMIT_MASK = 0xffffffffffffffffffffffffffffff;
/**
* @notice Tick limit shift
*/
uint256 internal constant TICK_LIMIT_SHIFT = 8;
/**
* @notice Tick duration index mask
*/
uint256 internal constant TICK_DURATION_MASK = 0x7;
/**
* @notice Tick duration index shift
*/
uint256 internal constant TICK_DURATION_SHIFT = 5;
/**
* @notice Tick rate index mask
*/
uint256 internal constant TICK_RATE_MASK = 0x7;
/**
* @notice Tick rate index shift
*/
uint256 internal constant TICK_RATE_SHIFT = 2;
/**
* @notice Tick reserved mask
*/
uint256 internal constant TICK_RESERVED_MASK = 0x3;
/**
* @notice Tick reserved shift
*/
uint256 internal constant TICK_RESERVED_SHIFT = 0;
/**
* @notice Maximum number of durations supported
*/
uint256 internal constant MAX_NUM_DURATIONS = TICK_DURATION_MASK + 1;
/**
* @notice Maximum number of rates supported
*/
uint256 internal constant MAX_NUM_RATES = TICK_RATE_MASK + 1;
/**************************************************************************/
/* Errors */
/**************************************************************************/
/**
* @notice Invalid tick
*/
error InvalidTick();
/**************************************************************************/
/* Helper Functions */
/**************************************************************************/
/**
* @dev Decode a Tick
* @param tick Tick
* @return limit Limit field
* @return duration Duration field
* @return rate Rate field
* @return reserved Reserved field
*/
function decode(
uint128 tick
) internal pure returns (uint256 limit, uint256 duration, uint256 rate, uint256 reserved) {
limit = ((tick >> TICK_LIMIT_SHIFT) & TICK_LIMIT_MASK);
duration = ((tick >> TICK_DURATION_SHIFT) & TICK_DURATION_MASK);
rate = ((tick >> TICK_RATE_SHIFT) & TICK_RATE_MASK);
reserved = ((tick >> TICK_RESERVED_SHIFT) & TICK_RESERVED_MASK);
}
/**
* @dev Validate a Tick (fast)
* @param tick Tick
* @param prevTick Previous tick
* @param minDurationIndex Minimum Duration Index (inclusive)
* @return Limit field
*/
function validate(uint128 tick, uint256 prevTick, uint256 minDurationIndex) internal pure returns (uint256) {
(uint256 limit, uint256 duration, , ) = decode(tick);
if (tick <= prevTick) revert InvalidTick();
if (duration < minDurationIndex) revert InvalidTick();
return limit;
}
/**
* @dev Validate a Tick (slow)
* @param tick Tick
* @param minLimit Minimum Limit (exclusive)
* @param minDurationIndex Minimum Duration Index (inclusive)
* @param maxDurationIndex Maximum Duration Index (inclusive)
* @param minRateIndex Minimum Rate Index (inclusive)
* @param maxRateIndex Maximum Rate Index (inclusive)
*/
function validate(
uint128 tick,
uint256 minLimit,
uint256 minDurationIndex,
uint256 maxDurationIndex,
uint256 minRateIndex,
uint256 maxRateIndex
) internal pure {
(uint256 limit, uint256 duration, uint256 rate, uint256 reserved) = decode(tick);
if (limit <= minLimit) revert InvalidTick();
if (duration < minDurationIndex || duration > maxDurationIndex) revert InvalidTick();
if (rate < minRateIndex || rate > maxRateIndex) revert InvalidTick();
if (reserved != 0) revert InvalidTick();
}
}
contracts/filters/CollectionCollateralFilter.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.20;
import "../CollateralFilter.sol";
/**
* @title Collection Collateral Filter
* @author MetaStreet Labs
*/
contract CollectionCollateralFilter is CollateralFilter {
/**************************************************************************/
/* State */
/**************************************************************************/
/**
* @notice Supported token
*/
address private _token;
/**************************************************************************/
/* Initializer */
/**************************************************************************/
/**
* @notice CollectionCollateralFilter initializer
*/
function _initialize(address token) internal {
_token = token;
}
/**************************************************************************/
/* Implementation */
/**************************************************************************/
/**
* @inheritdoc CollateralFilter
*/
function COLLATERAL_FILTER_NAME() external pure override returns (string memory) {
return "CollectionCollateralFilter";
}
/**
* @inheritdoc CollateralFilter
*/
function COLLATERAL_FILTER_VERSION() external pure override returns (string memory) {
return "1.0";
}
/**
* @inheritdoc CollateralFilter
*/
function collateralToken() external view override returns (address) {
return _token;
}
/**
* @inheritdoc CollateralFilter
*/
function _collateralSupported(
address token,
uint256,
uint256,
bytes calldata
) internal view override returns (bool) {
return token == _token;
}
}
contracts/integrations/DelegateCash/IDelegationRegistry.sol
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.19;
/**
* @title An immutable registry contract to be deployed as a standalone primitive
* @dev See EIP-5639, new project launches can read previous cold wallet -> hot wallet delegations
* from here and integrate those permissions into their flow
*/
interface IDelegationRegistry {
/// @notice Delegation type
enum DelegationType {
NONE,
ALL,
CONTRACT,
TOKEN
}
/// @notice Info about a single delegation, used for onchain enumeration
struct DelegationInfo {
DelegationType type_;
address vault;
address delegate;
address contract_;
uint256 tokenId;
}
/// @notice Info about a single contract-level delegation
struct ContractDelegation {
address contract_;
address delegate;
}
/// @notice Info about a single token-level delegation
struct TokenDelegation {
address contract_;
uint256 tokenId;
address delegate;
}
/// @notice Emitted when a user delegates their entire wallet
event DelegateForAll(address vault, address delegate, bool value);
/// @notice Emitted when a user delegates a specific contract
event DelegateForContract(address vault, address delegate, address contract_, bool value);
/// @notice Emitted when a user delegates a specific token
event DelegateForToken(address vault, address delegate, address contract_, uint256 tokenId, bool value);
/// @notice Emitted when a user revokes all delegations
event RevokeAllDelegates(address vault);
/// @notice Emitted when a user revoes all delegations for a given delegate
event RevokeDelegate(address vault, address delegate);
/**
* ----------- WRITE -----------
*/
/**
* @notice Allow the delegate to act on your behalf for all contracts
* @param delegate The hotwallet to act on your behalf
* @param value Whether to enable or disable delegation for this address, true for setting and false for revoking
*/
function delegateForAll(address delegate, bool value) external;
/**
* @notice Allow the delegate to act on your behalf for a specific contract
* @param delegate The hotwallet to act on your behalf
* @param contract_ The address for the contract you're delegating
* @param value Whether to enable or disable delegation for this address, true for setting and false for revoking
*/
function delegateForContract(address delegate, address contract_, bool value) external;
/**
* @notice Allow the delegate to act on your behalf for a specific token
* @param delegate The hotwallet to act on your behalf
* @param contract_ The address for the contract you're delegating
* @param tokenId The token id for the token you're delegating
* @param value Whether to enable or disable delegation for this address, true for setting and false for revoking
*/
function delegateForToken(address delegate, address contract_, uint256 tokenId, bool value) external;
/**
* @notice Revoke all delegates
*/
function revokeAllDelegates() external;
/**
* @notice Revoke a specific delegate for all their permissions
* @param delegate The hotwallet to revoke
*/
function revokeDelegate(address delegate) external;
/**
* @notice Remove yourself as a delegate for a specific vault
* @param vault The vault which delegated to the msg.sender, and should be removed
*/
function revokeSelf(address vault) external;
/**
* ----------- READ -----------
*/
/**
* @notice Returns all active delegations a given delegate is able to claim on behalf of
* @param delegate The delegate that you would like to retrieve delegations for
* @return info Array of DelegationInfo structs
*/
function getDelegationsByDelegate(address delegate) external view returns (DelegationInfo[] memory);
/**
* @notice Returns an array of wallet-level delegates for a given vault
* @param vault The cold wallet who issued the delegation
* @return addresses Array of wallet-level delegates for a given vault
*/
function getDelegatesForAll(address vault) external view returns (address[] memory);
/**
* @notice Returns an array of contract-level delegates for a given vault and contract
* @param vault The cold wallet who issued the delegation
* @param contract_ The address for the contract you're delegating
* @return addresses Array of contract-level delegates for a given vault and contract
*/
function getDelegatesForContract(address vault, address contract_) external view returns (address[] memory);
/**
* @notice Returns an array of contract-level delegates for a given vault's token
* @param vault The cold wallet who issued the delegation
* @param contract_ The address for the contract holding the token
* @param tokenId The token id for the token you're delegating
* @return addresses Array of contract-level delegates for a given vault's token
*/
function getDelegatesForToken(
address vault,
address contract_,
uint256 tokenId
) external view returns (address[] memory);
/**
* @notice Returns all contract-level delegations for a given vault
* @param vault The cold wallet who issued the delegations
* @return delegations Array of ContractDelegation structs
*/
function getContractLevelDelegations(address vault) external view returns (ContractDelegation[] memory delegations);
/**
* @notice Returns all token-level delegations for a given vault
* @param vault The cold wallet who issued the delegations
* @return delegations Array of TokenDelegation structs
*/
function getTokenLevelDelegations(address vault) external view returns (TokenDelegation[] memory delegations);
/**
* @notice Returns true if the address is delegated to act on the entire vault
* @param delegate The hotwallet to act on your behalf
* @param vault The cold wallet who issued the delegation
*/
function checkDelegateForAll(address delegate, address vault) external view returns (bool);
/**
* @notice Returns true if the address is delegated to act on your behalf for a token contract or an entire vault
* @param delegate The hotwallet to act on your behalf
* @param contract_ The address for the contract you're delegating
* @param vault The cold wallet who issued the delegation
*/
function checkDelegateForContract(address delegate, address vault, address contract_) external view returns (bool);
/**
* @notice Returns true if the address is delegated to act on your behalf for a specific token, the token's contract or an entire vault
* @param delegate The hotwallet to act on your behalf
* @param contract_ The address for the contract you're delegating
* @param tokenId The token id for the token you're delegating
* @param vault The cold wallet who issued the delegation
*/
function checkDelegateForToken(
address delegate,
address vault,
address contract_,
uint256 tokenId
) external view returns (bool);
}
contracts/interfaces/ICollateralLiquidationReceiver.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title Interface to a Collateral Liquidation Receiver
*/
interface ICollateralLiquidationReceiver {
/**
* @notice Callback on collateral liquidated
* @dev Pre-conditions: 1) proceeds were transferred, and 2) transferred amount >= proceeds
* @param liquidationContext Liquidation context
* @param proceeds Liquidation proceeds in currency tokens
*/
function onCollateralLiquidated(bytes calldata liquidationContext, uint256 proceeds) external;
}
contracts/interfaces/ICollateralLiquidator.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title Interface to a Collateral Liquidator
*/
interface ICollateralLiquidator {
/**
* @notice Get collateral liquidator name
* @return Collateral liquidator name
*/
function name() external view returns (string memory);
/**
* @notice Liquidate collateral
* @param currencyToken Currency token
* @param collateralToken Collateral token, either underlying token or collateral wrapper
* @param collateralTokenId Collateral token ID
* @param collateralWrapperContext Collateral wrapper context
* @param liquidationContext Liquidation callback context
*/
function liquidate(
address currencyToken,
address collateralToken,
uint256 collateralTokenId,
bytes calldata collateralWrapperContext,
bytes calldata liquidationContext
) external;
}
contracts/interfaces/ICollateralWrapper.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title Interface to a Collateral Wrapper
*/
interface ICollateralWrapper {
/**************************************************************************/
/* API */
/**************************************************************************/
/**
* @notice Get collateral wrapper name
* @return Collateral wrapper name
*/
function name() external view returns (string memory);
/**
* @notice Enumerate wrapped collateral
* @param tokenId Collateral wrapper token ID
* @param context Implementation-specific context
* @return token Token address
* @return tokenIds List of token ids
*/
function enumerate(
uint256 tokenId,
bytes calldata context
) external view returns (address token, uint256[] memory tokenIds);
/*
* Unwrap collateral
* @param tokenId Collateral wrapper token ID
* @param context Implementation-specific context
*/
function unwrap(uint256 tokenId, bytes calldata context) external;
}
contracts/interfaces/ILiquidity.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title Interface to Liquidity state
*/
interface ILiquidity {
/**************************************************************************/
/* Structures */
/**************************************************************************/
/**
* @notice Node source
* @param tick Tick
* @param used Amount used
*/
struct NodeSource {
uint128 tick;
uint128 used;
}
/**
* @notice Flattened liquidity node returned by getter
* @param tick Tick
* @param value Liquidity value
* @param shares Liquidity shares outstanding
* @param available Liquidity available
* @param pending Liquidity pending (with interest)
* @param redemptions Total pending redemptions
* @param prev Previous liquidity node
* @param next Next liquidity node
*/
struct NodeInfo {
uint128 tick;
uint128 value;
uint128 shares;
uint128 available;
uint128 pending;
uint128 redemptions;
uint128 prev;
uint128 next;
}
/**************************************************************************/
/* API */
/**************************************************************************/
/**
* Get liquidity nodes spanning [startTick, endTick] range
* @param startTick Start tick
* @param endTick End tick
* @return Liquidity nodes
*/
function liquidityNodes(uint128 startTick, uint128 endTick) external view returns (NodeInfo[] memory);
/**
* Get liquidity node at tick
* @param tick Tick
* @return Liquidity node
*/
function liquidityNode(uint128 tick) external view returns (NodeInfo memory);
}
contracts/interfaces/IPool.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title Interface to a Pool
*/
interface IPool {
/**************************************************************************/
/* Errors */
/**************************************************************************/
/**
* @notice Invalid caller
*/
error InvalidCaller();
/**
* @notice Insufficient shares
*/
error InsufficientShares();
/**
* @notice Invalid redemption status
*/
error InvalidRedemptionStatus();
/**
* @notice Invalid loan receipt
*/
error InvalidLoanReceipt();
/**
* @notice Invalid borrow options
*/
error InvalidBorrowOptions();
/**
* @notice Unsupported collateral
* @param index Index of unsupported asset
*/
error UnsupportedCollateral(uint256 index);
/**
* @notice Unsupported loan duration
*/
error UnsupportedLoanDuration();
/**
* @notice Repayment too high
*/
error RepaymentTooHigh();
/**
* @notice Loan not expired
*/
error LoanNotExpired();
/**************************************************************************/
/* Events */
/**************************************************************************/
/**
* @notice Emitted when currency is deposited
* @param account Account
* @param tick Tick
* @param amount Amount of currency tokens
* @param shares Amount of shares allocated
*/
event Deposited(address indexed account, uint128 indexed tick, uint256 amount, uint256 shares);
/**
* @notice Emitted when deposit shares are redeemed
* @param account Account
* @param tick Tick
* @param shares Amount of shares to be redeemed
*/
event Redeemed(address indexed account, uint128 indexed tick, uint256 shares);
/**
* @notice Emitted when redeemed currency tokens are withdrawn
* @param account Account
* @param tick Tick
* @param shares Amount of shares redeemed
* @param amount Amount of currency tokens withdrawn
*/
event Withdrawn(address indexed account, uint128 indexed tick, uint256 shares, uint256 amount);
/**
* @notice Emitted when a loan is originated
* @param loanReceiptHash Loan receipt hash
* @param loanReceipt Loan receipt
*/
event LoanOriginated(bytes32 indexed loanReceiptHash, bytes loanReceipt);
/**
* @notice Emitted when a loan is repaid
* @param loanReceiptHash Loan receipt hash
* @param repayment Repayment amount in currency tokens
*/
event LoanRepaid(bytes32 indexed loanReceiptHash, uint256 repayment);
/**
* @notice Emitted when a loan is liquidated
* @param loanReceiptHash Loan receipt hash
*/
event LoanLiquidated(bytes32 indexed loanReceiptHash);
/**
* @notice Emitted when loan collateral is liquidated
* @param loanReceiptHash Loan receipt hash
* @param proceeds Total liquidation proceeds in currency tokens
* @param borrowerProceeds Borrower's share of liquidation proceeds in
* currency tokens
*/
event CollateralLiquidated(bytes32 indexed loanReceiptHash, uint256 proceeds, uint256 borrowerProceeds);
/**
* @notice Emitted when admin fee rate is updated
* @param rate New admin fee rate in basis points
*/
event AdminFeeRateUpdated(uint256 rate);
/**
* @notice Emitted when admin fees are withdrawn
* @param account Recipient account
* @param amount Amount of currency tokens withdrawn
*/
event AdminFeesWithdrawn(address indexed account, uint256 amount);
/**************************************************************************/
/* Getters */
/**************************************************************************/
/**
* @notice Get currency token
* @return Currency token contract
*/
function currencyToken() external view returns (address);
/**
* @notice Get supported durations
* @return List of loan durations in second
*/
function durations() external view returns (uint64[] memory);
/**
* @notice Get supported rates
* @return List of rates in interest per second
*/
function rates() external view returns (uint64[] memory);
/**
* @notice Get admin
* @return Admin
*/
function admin() external view returns (address);
/**
* @notice Get admin fee rate
* @return Admin fee rate in basis points
*/
function adminFeeRate() external view returns (uint32);
/**
* @notice Get list of supported collateral wrappers
* @return Collateral wrappers
*/
function collateralWrappers() external view returns (address[] memory);
/**
* @notice Get collateral liquidator contract
* @return Collateral liquidator contract
*/
function collateralLiquidator() external view returns (address);
/**
* @notice Get delegation registry contract
* @return Delegation registry contract
*/
function delegationRegistry() external view returns (address);
/**************************************************************************/
/* Deposit API */
/**************************************************************************/
/**
* @notice Deposit amount at tick
*
* Emits a {Deposited} event.
*
* @param tick Tick
* @param amount Amount of currency tokens
* @param minShares Minimum amount of shares to receive
* @return shares Amount of shares minted
*/
function deposit(uint128 tick, uint256 amount, uint256 minShares) external returns (uint256 shares);
/**
* @notice Redeem deposit shares for currency tokens. Currency tokens can
* be withdrawn with the `withdraw()` method once the redemption is
* processed.
*
* Emits a {Redeemed} event.
*
* @param tick Tick
* @param shares Amount of deposit shares to redeem
*/
function redeem(uint128 tick, uint256 shares) external;
/**
* @notice Get redemption available
*
* @param account Account
* @param tick Tick
* @return shares Amount of deposit shares available for redemption
* @return amount Amount of currency tokens available for withdrawal
*/
function redemptionAvailable(address account, uint128 tick) external view returns (uint256 shares, uint256 amount);
/**
* @notice Withdraw a redemption that is available
*
* Emits a {Withdrawn} event.
*
* @param tick Tick
* @return shares Amount of deposit shares burned
* @return amount Amount of currency tokens withdrawn
*/
function withdraw(uint128 tick) external returns (uint256 shares, uint256 amount);
/**
* @notice Rebalance a redemption that is available to a new tick
*
* Emits {Withdrawn} and {Deposited} events.
*
* @param srcTick Source tick
* @param dstTick Destination Tick
* @param minShares Minimum amount of destination shares to receive
* @return oldShares Amount of source deposit shares burned
* @return newShares Amount of destination deposit shares minted
* @return amount Amount of currency tokens redeposited
*/
function rebalance(
uint128 srcTick,
uint128 dstTick,
uint256 minShares
) external returns (uint256 oldShares, uint256 newShares, uint256 amount);
/**************************************************************************/
/* Lend API */
/**************************************************************************/
/**
* @notice Quote repayment for a loan
* @param principal Principal amount in currency tokens
* @param duration Duration in seconds
* @param collateralToken Collateral token
* @param collateralTokenIds List of collateral token IDs
* @param ticks Liquidity ticks
* @param options Encoded options
* @return Repayment amount in currency tokens
*/
function quote(
uint256 principal,
uint64 duration,
address collateralToken,
uint256[] calldata collateralTokenIds,
uint128[] calldata ticks,
bytes calldata options
) external view returns (uint256);
/**
* @notice Quote refinancing for a loan
*
* @param encodedLoanReceipt Encoded loan receipt
* @param principal New principal amount in currency tokens
* @param duration Duration in seconds
* @param ticks Liquidity ticks
* @return downpayment Downpayment in currency tokens (positive for downpayment, negative for credit)
* @return repayment Repayment amount in currency tokens for new loan
*/
function quoteRefinance(
bytes calldata encodedLoanReceipt,
uint256 principal,
uint64 duration,
uint128[] calldata ticks
) external view returns (int256 downpayment, uint256 repayment);
/**
* @notice Originate a loan
*
* Emits a {LoanOriginated} event.
*
* @param principal Principal amount in currency tokens
* @param duration Duration in seconds
* @param collateralToken Collateral token address
* @param collateralTokenId Collateral token ID
* @param maxRepayment Maximum repayment amount in currency tokens
* @param ticks Liquidity ticks
* @param options Encoded options
* @return Repayment amount in currency tokens
*/
function borrow(
uint256 principal,
uint64 duration,
address collateralToken,
uint256 collateralTokenId,
uint256 maxRepayment,
uint128[] calldata ticks,
bytes calldata options
) external returns (uint256);
/**
* @notice Repay a loan
*
* Emits a {LoanRepaid} event.
*
* @param encodedLoanReceipt Encoded loan receipt
* @return Repayment amount in currency tokens
*/
function repay(bytes calldata encodedLoanReceipt) external returns (uint256);
/**
* @notice Refinance a loan
*
* Emits a {LoanRepaid} event and a {LoanOriginated} event.
*
* @param encodedLoanReceipt Encoded loan receipt
* @param principal Principal amount in currency tokens
* @param duration Duration in seconds
* @param maxRepayment Maximum repayment amount in currency tokens
* @param ticks Liquidity ticks
* @return Repayment amount in currency tokens
*/
function refinance(
bytes calldata encodedLoanReceipt,
uint256 principal,
uint64 duration,
uint256 maxRepayment,
uint128[] calldata ticks
) external returns (uint256);
/**
* @notice Liquidate an expired loan
*
* Emits a {LoanLiquidated} event.
*
* @param loanReceipt Loan receipt
*/
function liquidate(bytes calldata loanReceipt) external;
}
contracts/rates/WeightedInterestRateModel.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.20;
import "@openzeppelin/contracts/utils/math/Math.sol";
import "@openzeppelin/contracts/utils/math/SafeCast.sol";
import "../InterestRateModel.sol";
import "../Tick.sol";
/**
* @title Weighted Interest Rate Model
* @author MetaStreet Labs
*/
contract WeightedInterestRateModel is InterestRateModel {
using SafeCast for uint256;
/**************************************************************************/
/* Constants */
/**************************************************************************/
/**
* @notice Fixed point scale
*/
uint256 internal constant FIXED_POINT_SCALE = 1e18;
/**
* @notice Maximum tick threshold (0.5)
*/
uint256 internal constant MAX_TICK_THRESHOLD = 0.5 * 1e18;
/**
* @notice Minimum tick exponential (0.25)
*/
uint256 internal constant MIN_TICK_EXPONENTIAL = 0.25 * 1e18;
/**
* @notice Maximum tick exponential (4.0)
*/
uint256 internal constant MAX_TICK_EXPONENTIAL = 4.0 * 1e18;
/**************************************************************************/
/* Structures */
/**************************************************************************/
/**
* @notice Parameters
* @param tickThreshold Tick interest threshold
* @param tickExponential Tick exponential base
*/
struct Parameters {
uint64 tickThreshold;
uint64 tickExponential;
}
/**************************************************************************/
/* Errors */
/**************************************************************************/
/**
* @notice Invalid Tick Parameter
*/
error InvalidParameters();
/**
* @notice Insufficient utilization
*/
error InsufficientUtilization();
/**************************************************************************/
/* Immutable State */
/**************************************************************************/
/**
* @notice Tick interest threshold
*/
uint64 internal immutable _tickThreshold;
/**
* @notice Tick exponential base
*/
uint64 internal immutable _tickExponential;
/**************************************************************************/
/* Constructor */
/**************************************************************************/
/**
* @notice WeightedInterestRateModel constructor
*/
constructor(Parameters memory parameters) {
if (parameters.tickThreshold > MAX_TICK_THRESHOLD) revert InvalidParameters();
if (parameters.tickExponential < MIN_TICK_EXPONENTIAL || parameters.tickExponential > MAX_TICK_EXPONENTIAL)
revert InvalidParameters();
_tickThreshold = parameters.tickThreshold;
_tickExponential = parameters.tickExponential;
}
/**************************************************************************/
/* Implementation */
/**************************************************************************/
/**
* @inheritdoc InterestRateModel
*/
function INTEREST_RATE_MODEL_NAME() external pure override returns (string memory) {
return "WeightedInterestRateModel";
}
/**
* @inheritdoc InterestRateModel
*/
function INTEREST_RATE_MODEL_VERSION() external pure override returns (string memory) {
return "1.0";
}
/**
* @inheritdoc InterestRateModel
*/
function _rate(
uint256 amount,
uint64[] memory rates,
ILiquidity.NodeSource[] memory nodes,
uint16 count
) internal pure override returns (uint256) {
uint256 weightedRate;
/* Accumulate weighted rate */
for (uint256 i; i < count; i++) {
(, , uint256 rateIndex, ) = Tick.decode(nodes[i].tick);
weightedRate += (uint256(nodes[i].used) * rates[rateIndex]) / FIXED_POINT_SCALE;
}
/* Return normalized weighted rate */
return Math.mulDiv(weightedRate, FIXED_POINT_SCALE, amount);
}
/**
* @inheritdoc InterestRateModel
*/
function _distribute(
uint256 amount,
uint256 interest,
ILiquidity.NodeSource[] memory nodes,
uint16 count
) internal view override returns (uint128[] memory) {
/* Interest threshold for tick to receive interest */
uint256 threshold = Math.mulDiv(_tickThreshold, amount, FIXED_POINT_SCALE);
/* Interest weight starting at final tick */
uint256 base = _tickExponential;
uint256 weight = (FIXED_POINT_SCALE * FIXED_POINT_SCALE) / base;
/* Assign weighted interest to ticks backwards */
uint128[] memory pending = new uint128[](count);
uint256 normalization;
uint256 index = count;
for (uint256 i; i < count; i++) {
/* Skip tick if it's below threshold */
if (nodes[--index].used <= threshold) continue;
/* Compute scaled weight */
uint256 scaledWeight = Math.mulDiv(weight, nodes[index].used, amount);
/* Assign weighted interest */
pending[index] = Math.mulDiv(scaledWeight, interest, FIXED_POINT_SCALE).toUint128();
/* Accumulate scaled weight for later normalization */
normalization += scaledWeight;
/* Adjust interest weight for next tick */
weight = Math.mulDiv(weight, FIXED_POINT_SCALE, base);
}
/* Validate normalization is non-zero */
if (normalization == 0) revert InsufficientUtilization();
/* Normalize weighted interest */
for (uint256 i; i < count; i++) {
/* Calculate normalized interest to tick */
pending[i] = ((pending[i] * FIXED_POINT_SCALE) / normalization).toUint128();
/* Track remaining interest */
interest -= pending[i];
}
/* Drop off remaining dust at lowest tick */
pending[0] += interest.toUint128();
return pending;
}
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"bytecodeHash": "ipfs"
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "shanghai",
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.