Verified contractExactBUSL-1.1
FluidSmartLendingResolver
Address0x3e69a3af4305b65598b228d3da70786bd9cfeb0e
- Balance
0 ETH
- Transactions
0
- Compiler
solc 0.8.21
- Same code
Only this contract
Details
- Compiler
- solc 0.8.21+commit.d9974bed
- Language
- solidity
- Optimization
- Yes, 10,000,000 runs
- EVM version
- paris
- License
- BUSL-1.1
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 23,881,753
- Creation transaction
- 0x4b7a518890…72178a33a7
32 files
- contracts/periphery/resolvers/smartLending/main.solmain11.37 KB
- @openzeppelin/contracts/token/ERC20/ERC20.sol13.01 KB
- @openzeppelin/contracts/token/ERC20/IERC20.sol2.77 KB
- @openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol683 B
- @openzeppelin/contracts/utils/Context.sol844 B
- contracts/libraries/addressCalcs.sol1.76 KB
- contracts/libraries/bigMathMinified.sol9.82 KB
- contracts/libraries/bytesSliceAndConcat.sol6.05 KB
- contracts/libraries/dexCalcs.sol15.7 KB
- contracts/libraries/dexSlotsLink.sol3.35 KB
- contracts/libraries/errorTypes.sol1.41 KB
- contracts/libraries/safeTransfer.sol5.62 KB
- contracts/libraries/storageRead.sol386 B
- contracts/liquidity/adminModule/structs.sol6.62 KB
- contracts/periphery/resolvers/dex/main.sol47.96 KB
- contracts/periphery/resolvers/dex/structs.sol7.26 KB
- contracts/periphery/resolvers/dex/variables.sol2.17 KB
- contracts/periphery/resolvers/liquidity/iLiquidityResolver.sol6.82 KB
- contracts/periphery/resolvers/liquidity/structs.sol2.92 KB
- contracts/periphery/resolvers/smartLending/structs.sol3.55 KB
- contracts/protocols/dex/error.sol737 B
- contracts/protocols/dex/errorTypes.sol8.5 KB
- contracts/protocols/dex/factory/main.sol15.26 KB
- contracts/protocols/dex/interfaces/iDexFactory.sol707 B
- contracts/protocols/dex/interfaces/iDexT1.sol15.14 KB
- contracts/protocols/dex/poolT1/coreModule/structs.sol4.63 KB
- contracts/protocols/dex/smartLending/factory/main.sol11.14 KB
- contracts/protocols/dex/smartLending/main.sol22.7 KB
- solmate/src/auth/Owned.sol1.48 KB
- solmate/src/utils/Bytes32AddressLib.sol560 B
- solmate/src/utils/CREATE3.sol4.18 KB
- solmate/src/utils/SSTORE2.sol5.46 KB
contracts/periphery/resolvers/smartLending/main.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { FluidDexResolver } from "../dex/main.sol";
import { FluidSmartLendingFactory } from "../../../protocols/dex/smartLending/factory/main.sol";
import { FluidSmartLending } from "../../../protocols/dex/smartLending/main.sol";
import { Structs } from "./structs.sol";
import { Structs as DexResolverStructs } from "../dex/structs.sol";
/// @notice Fluid Smart Lending resolver
/// Implements various view-only methods to give easy access to Smart Lending protocol data.
contract FluidSmartLendingResolver is Structs {
uint256 internal constant SECONDS_PER_YEAR = 365 days;
FluidDexResolver public immutable DEX_RESOLVER;
FluidSmartLendingFactory public immutable SMART_LENDING_FACTORY;
address internal constant _NATIVE_TOKEN_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
/// @notice thrown if an input param address is zero
error FluidSmartLendingResolver__AddressZero();
constructor(FluidDexResolver dexResolver_, FluidSmartLendingFactory smartLendingFactory_) {
if (address(dexResolver_) == address(0) || address(smartLendingFactory_) == address(0)) {
revert FluidSmartLendingResolver__AddressZero();
}
DEX_RESOLVER = dexResolver_;
SMART_LENDING_FACTORY = smartLendingFactory_;
}
/// @notice List of all existing SmartLending tokens
function getAllSmartLendingAddresses() public view returns (address[] memory) {
return SMART_LENDING_FACTORY.allTokens();
}
/// @notice Get the address of a SmartLending for a certain dexId. address zero if no SmartLending exists for the dex.
function getSmartLendingAddress(uint dexId_) public view returns (address smartLending) {
return SMART_LENDING_FACTORY.getSmartLendingAddress(dexId_);
}
/// @notice Get the entire data for a SmartLending, EXCEPT underlying DexEntireData. use write method for that.
/// @param smartLending_ The address of the SmartLending
/// @return data_ A struct containing all the data for the SmartLending
function getSmartLendingEntireViewData(
address payable smartLending_
) public view returns (SmartLendingEntireData memory data_) {
data_.smartLending = smartLending_;
data_.name = FluidSmartLending(smartLending_).name();
data_.symbol = FluidSmartLending(smartLending_).symbol();
data_.decimals = 18;
data_.totalSupply = uint256(FluidSmartLending(smartLending_).totalSupply());
data_.token0 = FluidSmartLending(smartLending_).TOKEN0();
data_.token1 = FluidSmartLending(smartLending_).TOKEN1();
data_.dex = address(FluidSmartLending(smartLending_).DEX());
data_.lastTimestamp = uint256(FluidSmartLending(smartLending_).lastTimestamp());
data_.feeOrReward = int256(FluidSmartLending(smartLending_).feeOrReward());
(data_.exchangePrice, ) = FluidSmartLending(smartLending_).getUpdateExchangePrice();
// exchangePrice is in 1e18, shares are in 1e18, SmartLending is in 1e18
data_.assetsPerShare = (1e18 * 1e18) / data_.exchangePrice;
data_.sharesPerAsset = data_.exchangePrice; // just providing same value for extra clarity. would be 1e18 * exchangePrice / 1e18
data_.totalUnderlyingShares = (data_.totalSupply * data_.exchangePrice) / 1e18;
data_.rebalancer = FluidSmartLending(smartLending_).rebalancer();
data_.rebalanceDiff = uint256(FluidSmartLending(smartLending_).rebalanceDiff());
data_.dexUserSupplyData = DEX_RESOLVER.getUserSupplyData(data_.dex, smartLending_);
}
/// @notice Get the entire view data for multiple SmartLendings, EXCEPT underlying DexEntireData. use write method for that.
/// @param smartLendings_ An array of SmartLending addresses
/// @return datas_ An array of structs containing all the data for each SmartLending
function getSmartLendingEntireViewDatas(
address[] memory smartLendings_
) public view returns (SmartLendingEntireData[] memory datas_) {
uint256 length_ = smartLendings_.length;
datas_ = new SmartLendingEntireData[](length_);
for (uint256 i; i < length_; i++) {
datas_[i] = getSmartLendingEntireViewData(payable(smartLendings_[i]));
}
}
/// @notice Get the entire data for all SmartLendings, EXCEPT underlying DexEntireData. use write method for that.
/// @return datas_ An array of structs containing all the data for all SmartLendings
function getAllSmartLendingEntireViewDatas() public view returns (SmartLendingEntireData[] memory datas_) {
return getSmartLendingEntireViewDatas(getAllSmartLendingAddresses());
}
/// @notice Get the entire data for a SmartLending, incl. underlying DexEntireData and totalUnderlyingAssets for each token
/// @param smartLending_ The address of the SmartLending
/// @return data_ A struct containing all the data for the SmartLending
/// @dev expected to be called via callStatic
function getSmartLendingEntireData(
address payable smartLending_
) public returns (SmartLendingEntireData memory data_) {
data_ = getSmartLendingEntireViewData(smartLending_);
data_.dexEntireData = DEX_RESOLVER.getDexEntireData(data_.dex);
data_.totalUnderlyingAssetsToken0 =
(data_.totalUnderlyingShares * data_.dexEntireData.dexState.token0PerSupplyShare) /
1e18;
data_.totalUnderlyingAssetsToken1 =
(data_.totalUnderlyingShares * data_.dexEntireData.dexState.token1PerSupplyShare) /
1e18;
}
/// @notice Get the entire data for multiple SmartLendings
/// @param smartLendings_ An array of SmartLending addresses
/// @return datas_ An array of structs containing all the data for each SmartLending
/// @dev expected to be called via callStatic
function getSmartLendingEntireDatas(
address[] memory smartLendings_
) public returns (SmartLendingEntireData[] memory datas_) {
uint256 length_ = smartLendings_.length;
datas_ = new SmartLendingEntireData[](length_);
for (uint256 i; i < length_; i++) {
datas_[i] = getSmartLendingEntireData(payable(smartLendings_[i]));
}
}
/// @notice Get the entire data for all SmartLendings
/// @return datas_ An array of structs containing all the data for all SmartLendings
/// @dev expected to be called via callStatic
function getAllSmartLendingEntireDatas() public returns (SmartLendingEntireData[] memory datas_) {
return getSmartLendingEntireDatas(getAllSmartLendingAddresses());
}
/// @notice gets a user position at a certain SmartLending. EXCLUDING underlyingAssetsToken0 and underlyingAssetsToken1.
/// use write method for that.
function getUserPositionView(
address payable smartLending_,
address user_
) public view returns (UserPosition memory userPosition_) {
userPosition_.user = user_;
userPosition_.smartLendingAssets = FluidSmartLending(payable(smartLending_)).balanceOf(user_);
{
(uint256 exchangePrice_, ) = FluidSmartLending(smartLending_).getUpdateExchangePrice();
userPosition_.underlyingShares = (userPosition_.smartLendingAssets * exchangePrice_) / 1e18;
}
{
IERC20 token0_ = IERC20(FluidSmartLending(smartLending_).TOKEN0());
IERC20 token1_ = IERC20(FluidSmartLending(smartLending_).TOKEN1());
if (address(token0_) == _NATIVE_TOKEN_ADDRESS) {
userPosition_.underlyingBalanceToken0 = address(user_).balance;
} else {
userPosition_.underlyingBalanceToken0 = token0_.balanceOf(user_);
userPosition_.allowanceToken0 = token0_.allowance(user_, address(smartLending_));
}
if (address(token1_) == _NATIVE_TOKEN_ADDRESS) {
userPosition_.underlyingBalanceToken1 = address(user_).balance;
} else {
userPosition_.underlyingBalanceToken1 = token1_.balanceOf(user_);
userPosition_.allowanceToken1 = token1_.allowance(user_, address(smartLending_));
}
}
}
/// @notice gets a user position at a certain SmartLending incl. underlyingAssetsToken0 and underlyingAssetsToken1
/// @dev expected to be called via callStatic
function getUserPosition(
address payable smartLending_,
address user_
) public returns (UserPosition memory userPosition_) {
userPosition_ = getUserPositionView(smartLending_, user_);
{
DexResolverStructs.DexState memory dexState_ = DEX_RESOLVER.getDexState(
address(FluidSmartLending(smartLending_).DEX())
);
userPosition_.underlyingAssetsToken0 =
(userPosition_.underlyingShares * dexState_.token0PerSupplyShare) /
1e18;
userPosition_.underlyingAssetsToken1 =
(userPosition_.underlyingShares * dexState_.token1PerSupplyShare) /
1e18;
}
}
/// @notice gets all Smart lendings entire data and all user positions for each.
/// Excluding underlying DexEntireData and underlyingAssetsToken0 and underlyingAssetsToken1. use write method for that.
function getUserPositionsView(address user_) external view returns (SmartLendingEntireDataUserPosition[] memory) {
SmartLendingEntireData[] memory smartLendingsEntireData_ = getAllSmartLendingEntireViewDatas();
SmartLendingEntireDataUserPosition[] memory userPositionArr_ = new SmartLendingEntireDataUserPosition[](
smartLendingsEntireData_.length
);
for (uint256 i = 0; i < smartLendingsEntireData_.length; ) {
userPositionArr_[i].smartLendingEntireData = smartLendingsEntireData_[i];
userPositionArr_[i].userPosition = getUserPositionView(
payable(smartLendingsEntireData_[i].smartLending),
user_
);
unchecked {
i++;
}
}
return userPositionArr_;
}
/// @notice gets all Smart lendings entire data and all user positions for each.
/// incl. underlying Dex (=`getSmartLendingEntireViewData()` + DexEntireData) and underlyingAssetsToken0 and underlyingAssetsToken1.
/// @dev expected to be called via callStatic
function getUserPositions(address user_) external returns (SmartLendingEntireDataUserPosition[] memory) {
SmartLendingEntireData[] memory smartLendingsEntireData_ = getAllSmartLendingEntireDatas();
SmartLendingEntireDataUserPosition[] memory userPositionArr_ = new SmartLendingEntireDataUserPosition[](
smartLendingsEntireData_.length
);
for (uint256 i = 0; i < smartLendingsEntireData_.length; ) {
userPositionArr_[i].smartLendingEntireData = smartLendingsEntireData_[i];
userPositionArr_[i].userPosition = getUserPosition(
payable(smartLendingsEntireData_[i].smartLending),
user_
);
unchecked {
i++;
}
}
return userPositionArr_;
}
}
@openzeppelin/contracts/token/ERC20/ERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (token/ERC20/ERC20.sol)
pragma solidity ^0.8.0;
import "./IERC20.sol";
import "./extensions/IERC20Metadata.sol";
import "../../utils/Context.sol";
/**
* @dev Implementation of the {IERC20} interface.
*
* This implementation is agnostic to the way tokens are created. This means
* that a supply mechanism has to be added in a derived contract using {_mint}.
* For a generic mechanism see {ERC20PresetMinterPauser}.
*
* TIP: For a detailed writeup see our guide
* https://forum.openzeppelin.com/t/how-to-implement-erc20-supply-mechanisms/226[How
* to implement supply mechanisms].
*
* We have followed general OpenZeppelin Contracts guidelines: functions revert
* instead returning `false` on failure. This behavior is nonetheless
* conventional and does not conflict with the expectations of ERC20
* applications.
*
* Additionally, an {Approval} event is emitted on calls to {transferFrom}.
* This allows applications to reconstruct the allowance for all accounts just
* by listening to said events. Other implementations of the EIP may not emit
* these events, as it isn't required by the specification.
*
* Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
* functions have been added to mitigate the well-known issues around setting
* allowances. See {IERC20-approve}.
*/
contract ERC20 is Context, IERC20, IERC20Metadata {
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
/**
* @dev Sets the values for {name} and {symbol}.
*
* The default value of {decimals} is 18. To select a different value for
* {decimals} you should overload it.
*
* All two of these values are immutable: they can only be set once during
* construction.
*/
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
}
/**
* @dev Returns the name of the token.
*/
function name() public view virtual override returns (string memory) {
return _name;
}
/**
* @dev Returns the symbol of the token, usually a shorter version of the
* name.
*/
function symbol() public view virtual override returns (string memory) {
return _symbol;
}
/**
* @dev Returns the number of decimals used to get its user representation.
* For example, if `decimals` equals `2`, a balance of `505` tokens should
* be displayed to a user as `5.05` (`505 / 10 ** 2`).
*
* Tokens usually opt for a value of 18, imitating the relationship between
* Ether and Wei. This is the value {ERC20} uses, unless this function is
* overridden;
*
* NOTE: This information is only used for _display_ purposes: it in
* no way affects any of the arithmetic of the contract, including
* {IERC20-balanceOf} and {IERC20-transfer}.
*/
function decimals() public view virtual override returns (uint8) {
return 18;
}
/**
* @dev See {IERC20-totalSupply}.
*/
function totalSupply() public view virtual override returns (uint256) {
return _totalSupply;
}
/**
* @dev See {IERC20-balanceOf}.
*/
function balanceOf(address account) public view virtual override returns (uint256) {
return _balances[account];
}
/**
* @dev See {IERC20-transfer}.
*
* Requirements:
*
* - `to` cannot be the zero address.
* - the caller must have a balance of at least `amount`.
*/
function transfer(address to, uint256 amount) public virtual override returns (bool) {
address owner = _msgSender();
_transfer(owner, to, amount);
return true;
}
/**
* @dev See {IERC20-allowance}.
*/
function allowance(address owner, address spender) public view virtual override returns (uint256) {
return _allowances[owner][spender];
}
/**
* @dev See {IERC20-approve}.
*
* NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on
* `transferFrom`. This is semantically equivalent to an infinite approval.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function approve(address spender, uint256 amount) public virtual override returns (bool) {
address owner = _msgSender();
_approve(owner, spender, amount);
return true;
}
/**
* @dev See {IERC20-transferFrom}.
*
* Emits an {Approval} event indicating the updated allowance. This is not
* required by the EIP. See the note at the beginning of {ERC20}.
*
* NOTE: Does not update the allowance if the current allowance
* is the maximum `uint256`.
*
* Requirements:
*
* - `from` and `to` cannot be the zero address.
* - `from` must have a balance of at least `amount`.
* - the caller must have allowance for ``from``'s tokens of at least
* `amount`.
*/
function transferFrom(
address from,
address to,
uint256 amount
) public virtual override returns (bool) {
address spender = _msgSender();
_spendAllowance(from, spender, amount);
_transfer(from, to, amount);
return true;
}
/**
* @dev Atomically increases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
address owner = _msgSender();
_approve(owner, spender, allowance(owner, spender) + addedValue);
return true;
}
/**
* @dev Atomically decreases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `spender` must have allowance for the caller of at least
* `subtractedValue`.
*/
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
address owner = _msgSender();
uint256 currentAllowance = allowance(owner, spender);
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
unchecked {
_approve(owner, spender, currentAllowance - subtractedValue);
}
return true;
}
/**
* @dev Moves `amount` of tokens from `from` to `to`.
*
* This internal function is equivalent to {transfer}, and can be used to
* e.g. implement automatic token fees, slashing mechanisms, etc.
*
* Emits a {Transfer} event.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `from` must have a balance of at least `amount`.
*/
function _transfer(
address from,
address to,
uint256 amount
) internal virtual {
require(from != address(0), "ERC20: transfer from the zero address");
require(to != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(from, to, amount);
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[from] = fromBalance - amount;
// Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
// decrementing then incrementing.
_balances[to] += amount;
}
emit Transfer(from, to, amount);
_afterTokenTransfer(from, to, amount);
}
/** @dev Creates `amount` tokens and assigns them to `account`, increasing
* the total supply.
*
* Emits a {Transfer} event with `from` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
*/
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply += amount;
unchecked {
// Overflow not possible: balance + amount is at most totalSupply + amount, which is checked above.
_balances[account] += amount;
}
emit Transfer(address(0), account, amount);
_afterTokenTransfer(address(0), account, amount);
}
/**
* @dev Destroys `amount` tokens from `account`, reducing the
* total supply.
*
* Emits a {Transfer} event with `to` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens.
*/
function _burn(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: burn from the zero address");
_beforeTokenTransfer(account, address(0), amount);
uint256 accountBalance = _balances[account];
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
unchecked {
_balances[account] = accountBalance - amount;
// Overflow not possible: amount <= accountBalance <= totalSupply.
_totalSupply -= amount;
}
emit Transfer(account, address(0), amount);
_afterTokenTransfer(account, address(0), amount);
}
/**
* @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
*
* This internal function is equivalent to `approve`, and can be used to
* e.g. set automatic allowances for certain subsystems, etc.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `owner` cannot be the zero address.
* - `spender` cannot be the zero address.
*/
function _approve(
address owner,
address spender,
uint256 amount
) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
/**
* @dev Updates `owner` s allowance for `spender` based on spent `amount`.
*
* Does not update the allowance amount in case of infinite allowance.
* Revert if not enough allowance is available.
*
* Might emit an {Approval} event.
*/
function _spendAllowance(
address owner,
address spender,
uint256 amount
) internal virtual {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, "ERC20: insufficient allowance");
unchecked {
_approve(owner, spender, currentAllowance - amount);
}
}
}
/**
* @dev Hook that is called before any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* will be transferred to `to`.
* - when `from` is zero, `amount` tokens will be minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens will be burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
/**
* @dev Hook that is called after any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* has been transferred to `to`.
* - when `from` is zero, `amount` tokens have been minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens have been burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _afterTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
}
@openzeppelin/contracts/token/ERC20/IERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.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/utils/Context.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)
pragma solidity ^0.8.0;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
contracts/libraries/addressCalcs.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.21 <=0.8.29;
/// @notice implements calculation of address for contracts deployed through CREATE.
/// Accepts contract deployed from which address & nonce
library AddressCalcs {
/// @notice Computes the address of a contract based
/// @param deployedFrom_ Address from which the contract was deployed
/// @param nonce_ Nonce at which the contract was deployed
/// @return contract_ Address of deployed contract
function addressCalc(address deployedFrom_, uint nonce_) internal pure returns (address contract_) {
// @dev based on https://ethereum.stackexchange.com/a/61413
// nonce of smart contract always starts with 1. so, with nonce 0 there won't be any deployment
// hence, nonce of vault deployment starts with 1.
bytes memory data;
if (nonce_ == 0x00) {
return address(0);
} else if (nonce_ <= 0x7f) {
data = abi.encodePacked(bytes1(0xd6), bytes1(0x94), deployedFrom_, uint8(nonce_));
} else if (nonce_ <= 0xff) {
data = abi.encodePacked(bytes1(0xd7), bytes1(0x94), deployedFrom_, bytes1(0x81), uint8(nonce_));
} else if (nonce_ <= 0xffff) {
data = abi.encodePacked(bytes1(0xd8), bytes1(0x94), deployedFrom_, bytes1(0x82), uint16(nonce_));
} else if (nonce_ <= 0xffffff) {
data = abi.encodePacked(bytes1(0xd9), bytes1(0x94), deployedFrom_, bytes1(0x83), uint24(nonce_));
} else {
data = abi.encodePacked(bytes1(0xda), bytes1(0x94), deployedFrom_, bytes1(0x84), uint32(nonce_));
}
return address(uint160(uint256(keccak256(data))));
}
}contracts/libraries/bigMathMinified.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.21 <=0.8.29;
/// @title library that represents a number in BigNumber(coefficient and exponent) format to store in smaller bits.
/// @notice the number is divided into two parts: a coefficient and an exponent. This comes at a cost of losing some precision
/// at the end of the number because the exponent simply fills it with zeroes. This precision is oftentimes negligible and can
/// result in significant gas cost reduction due to storage space reduction.
/// Also note, a valid big number is as follows: if the exponent is > 0, then coefficient last bits should be occupied to have max precision.
/// @dev roundUp is more like a increase 1, which happens everytime for the same number.
/// roundDown simply sets trailing digits after coefficientSize to zero (floor), only once for the same number.
library BigMathMinified {
/// @dev constants to use for `roundUp` input param to increase readability
bool internal constant ROUND_DOWN = false;
bool internal constant ROUND_UP = true;
/// @dev converts `normal` number to BigNumber with `exponent` and `coefficient` (or precision).
/// e.g.:
/// 5035703444687813576399599 (normal) = (coefficient[32bits], exponent[8bits])[40bits]
/// 5035703444687813576399599 (decimal) => 10000101010010110100000011111011110010100110100000000011100101001101001101011101111 (binary)
/// => 10000101010010110100000011111011000000000000000000000000000000000000000000000000000
/// ^-------------------- 51(exponent) -------------- ^
/// coefficient = 1000,0101,0100,1011,0100,0000,1111,1011 (2236301563)
/// exponent = 0011,0011 (51)
/// bigNumber = 1000,0101,0100,1011,0100,0000,1111,1011,0011,0011 (572493200179)
///
/// @param normal number which needs to be converted into Big Number
/// @param coefficientSize at max how many bits of precision there should be (64 = uint64 (64 bits precision))
/// @param exponentSize at max how many bits of exponent there should be (8 = uint8 (8 bits exponent))
/// @param roundUp signals if result should be rounded down or up
/// @return bigNumber converted bigNumber (coefficient << exponent)
function toBigNumber(
uint256 normal,
uint256 coefficientSize,
uint256 exponentSize,
bool roundUp
) internal pure returns (uint256 bigNumber) {
assembly {
let lastBit_
let number_ := normal
if gt(number_, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) {
number_ := shr(0x80, number_)
lastBit_ := 0x80
}
if gt(number_, 0xFFFFFFFFFFFFFFFF) {
number_ := shr(0x40, number_)
lastBit_ := add(lastBit_, 0x40)
}
if gt(number_, 0xFFFFFFFF) {
number_ := shr(0x20, number_)
lastBit_ := add(lastBit_, 0x20)
}
if gt(number_, 0xFFFF) {
number_ := shr(0x10, number_)
lastBit_ := add(lastBit_, 0x10)
}
if gt(number_, 0xFF) {
number_ := shr(0x8, number_)
lastBit_ := add(lastBit_, 0x8)
}
if gt(number_, 0xF) {
number_ := shr(0x4, number_)
lastBit_ := add(lastBit_, 0x4)
}
if gt(number_, 0x3) {
number_ := shr(0x2, number_)
lastBit_ := add(lastBit_, 0x2)
}
if gt(number_, 0x1) {
lastBit_ := add(lastBit_, 1)
}
if gt(number_, 0) {
lastBit_ := add(lastBit_, 1)
}
if lt(lastBit_, coefficientSize) {
// for throw exception
lastBit_ := coefficientSize
}
let exponent := sub(lastBit_, coefficientSize)
let coefficient := shr(exponent, normal)
if and(roundUp, gt(exponent, 0)) {
// rounding up is only needed if exponent is > 0, as otherwise the coefficient fully holds the original number
coefficient := add(coefficient, 1)
if eq(shl(coefficientSize, 1), coefficient) {
// case were coefficient was e.g. 111, with adding 1 it became 1000 (in binary) and coefficientSize 3 bits
// final coefficient would exceed it's size. -> reduce coefficent to 100 and increase exponent by 1.
coefficient := shl(sub(coefficientSize, 1), 1)
exponent := add(exponent, 1)
}
}
if iszero(lt(exponent, shl(exponentSize, 1))) {
// if exponent is >= exponentSize, the normal number is too big to fit within
// BigNumber with too small sizes for coefficient and exponent
revert(0, 0)
}
bigNumber := shl(exponentSize, coefficient)
bigNumber := add(bigNumber, exponent)
}
}
/// @dev get `normal` number from `bigNumber`, `exponentSize` and `exponentMask`
function fromBigNumber(
uint256 bigNumber,
uint256 exponentSize,
uint256 exponentMask
) internal pure returns (uint256 normal) {
assembly {
let coefficient := shr(exponentSize, bigNumber)
let exponent := and(bigNumber, exponentMask)
normal := shl(exponent, coefficient)
}
}
/// @dev gets the most significant bit `lastBit` of a `normal` number (length of given number of binary format).
/// e.g.
/// 5035703444687813576399584 = 10000101010010110100000011111011110010100110100000000011100101001101001101011100000
/// lastBit = ^--------------------------------- 83 ----------------------------------------^
function mostSignificantBit(uint256 normal) internal pure returns (uint lastBit) {
assembly {
let number_ := normal
if gt(normal, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) {
number_ := shr(0x80, number_)
lastBit := 0x80
}
if gt(number_, 0xFFFFFFFFFFFFFFFF) {
number_ := shr(0x40, number_)
lastBit := add(lastBit, 0x40)
}
if gt(number_, 0xFFFFFFFF) {
number_ := shr(0x20, number_)
lastBit := add(lastBit, 0x20)
}
if gt(number_, 0xFFFF) {
number_ := shr(0x10, number_)
lastBit := add(lastBit, 0x10)
}
if gt(number_, 0xFF) {
number_ := shr(0x8, number_)
lastBit := add(lastBit, 0x8)
}
if gt(number_, 0xF) {
number_ := shr(0x4, number_)
lastBit := add(lastBit, 0x4)
}
if gt(number_, 0x3) {
number_ := shr(0x2, number_)
lastBit := add(lastBit, 0x2)
}
if gt(number_, 0x1) {
lastBit := add(lastBit, 1)
}
if gt(number_, 0) {
lastBit := add(lastBit, 1)
}
}
}
/// @dev gets the least significant bit `firstBit` of a `normal` number (position of rightmost 1 in binary format).
/// e.g.
/// 5035703444687813576399584 = 10000101010010110100000011111011110010100110100000000011100101001101001101011100000
/// firstBit = ^-6--^
function leastSignificantBit(uint256 normal) internal pure returns (uint firstBit) {
assembly {
// If number is 0, revert as there is no least significant bit
if iszero(normal) {
revert(0, 0)
}
// Find first set bit using binary search
let number_ := normal
firstBit := 0
// Check if lower 128 bits are all zero
if iszero(and(number_, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)) {
number_ := shr(0x80, number_)
firstBit := 0x80
}
// Check if lower 64 bits are all zero
if iszero(and(number_, 0xFFFFFFFFFFFFFFFF)) {
number_ := shr(0x40, number_)
firstBit := add(firstBit, 0x40)
}
// Check if lower 32 bits are all zero
if iszero(and(number_, 0xFFFFFFFF)) {
number_ := shr(0x20, number_)
firstBit := add(firstBit, 0x20)
}
// Check if lower 16 bits are all zero
if iszero(and(number_, 0xFFFF)) {
number_ := shr(0x10, number_)
firstBit := add(firstBit, 0x10)
}
// Check if lower 8 bits are all zero
if iszero(and(number_, 0xFF)) {
number_ := shr(0x8, number_)
firstBit := add(firstBit, 0x8)
}
// Check if lower 4 bits are all zero
if iszero(and(number_, 0xF)) {
number_ := shr(0x4, number_)
firstBit := add(firstBit, 0x4)
}
// Check if lower 2 bits are all zero
if iszero(and(number_, 0x3)) {
number_ := shr(0x2, number_)
firstBit := add(firstBit, 0x2)
}
// Check if lowest bit is zero
if iszero(and(number_, 0x1)) {
firstBit := add(firstBit, 1)
}
// Add 1 to match the 1-based position counting
firstBit := add(firstBit, 1)
}
}
}
contracts/libraries/bytesSliceAndConcat.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.21 <=0.8.29;
library BytesSliceAndConcat {
// @dev taken from https://github.com/GNSPS/solidity-bytes-utils/blob/master/contracts/BytesLib.sol
function bytesConcat(
bytes memory _preBytes,
bytes memory _postBytes
) internal pure returns (bytes memory tempBytes) {
assembly {
// Get a location of some free memory and store it in tempBytes as
// Solidity does for memory variables.
tempBytes := mload(0x40)
// Store the length of the first bytes array at the beginning of
// the memory for tempBytes.
let length := mload(_preBytes)
mstore(tempBytes, length)
// Maintain a memory counter for the current write location in the
// temp bytes array by adding the 32 bytes for the array length to
// the starting location.
let mc := add(tempBytes, 0x20)
// Stop copying when the memory counter reaches the length of the
// first bytes array.
let end := add(mc, length)
for {
// Initialize a copy counter to the start of the _preBytes data,
// 32 bytes into its memory.
let cc := add(_preBytes, 0x20)
} lt(mc, end) {
// Increase both counters by 32 bytes each iteration.
mc := add(mc, 0x20)
cc := add(cc, 0x20)
} {
// Write the _preBytes data into the tempBytes memory 32 bytes
// at a time.
mstore(mc, mload(cc))
}
// Add the length of _postBytes to the current length of tempBytes
// and store it as the new length in the first 32 bytes of the
// tempBytes memory.
length := mload(_postBytes)
mstore(tempBytes, add(length, mload(tempBytes)))
// Move the memory counter back from a multiple of 0x20 to the
// actual end of the _preBytes data.
mc := end
// Stop copying when the memory counter reaches the new combined
// length of the arrays.
end := add(mc, length)
for {
let cc := add(_postBytes, 0x20)
} lt(mc, end) {
mc := add(mc, 0x20)
cc := add(cc, 0x20)
} {
mstore(mc, mload(cc))
}
// Update the free-memory pointer by padding our last write location
// to 32 bytes: add 31 bytes to the end of tempBytes to move to the
// next 32 byte block, then round down to the nearest multiple of
// 32. If the sum of the length of the two arrays is zero then add
// one before rounding down to leave a blank 32 bytes (the length block with 0).
mstore(
0x40,
and(
add(add(end, iszero(add(length, mload(_preBytes)))), 31),
not(31) // Round down to the nearest 32 bytes.
)
)
}
return tempBytes;
}
// @dev taken from https://github.com/GNSPS/solidity-bytes-utils/blob/master/contracts/BytesLib.sol
function bytesSlice(
bytes memory _bytes,
uint256 _start,
uint256 _length
) internal pure returns (bytes memory tempBytes) {
require(_length + 31 >= _length, "slice_overflow");
require(_bytes.length >= _start + _length, "slice_outOfBounds");
assembly {
switch iszero(_length)
case 0 {
// Get a location of some free memory and store it in tempBytes as
// Solidity does for memory variables.
tempBytes := mload(0x40)
// The first word of the slice result is potentially a partial
// word read from the original array. To read it, we calculate
// the length of that partial word and start copying that many
// bytes into the array. The first word we copy will start with
// data we don't care about, but the last `lengthmod` bytes will
// land at the beginning of the contents of the new array. When
// we're done copying, we overwrite the full first word with
// the actual length of the slice.
let lengthmod := and(_length, 31)
// The multiplication in the next line is necessary
// because when slicing multiples of 32 bytes (lengthmod == 0)
// the following copy loop was copying the origin's length
// and then ending prematurely not copying everything it should.
let mc := add(add(tempBytes, lengthmod), mul(0x20, iszero(lengthmod)))
let end := add(mc, _length)
for {
// The multiplication in the next line has the same exact purpose
// as the one above.
let cc := add(add(add(_bytes, lengthmod), mul(0x20, iszero(lengthmod))), _start)
} lt(mc, end) {
mc := add(mc, 0x20)
cc := add(cc, 0x20)
} {
mstore(mc, mload(cc))
}
mstore(tempBytes, _length)
//update free-memory pointer
//allocating the array padded to 32 bytes like the compiler does now
mstore(0x40, and(add(mc, 31), not(31)))
}
//if we want a zero-length slice let's just return a zero-length array
default {
tempBytes := mload(0x40)
//zero out the 32 bytes slice we are about to return
//we need to do it because Solidity does not garbage collect
mstore(tempBytes, 0)
mstore(0x40, add(tempBytes, 0x20))
}
}
return tempBytes;
}
}
contracts/libraries/dexCalcs.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.21 <=0.8.29;
import { BigMathMinified } from "./bigMathMinified.sol";
import { DexSlotsLink } from "./dexSlotsLink.sol";
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// @DEV ATTENTION: ON ANY CHANGES HERE, MAKE SURE THAT LOGIC IN VAULTS WILL STILL BE VALID.
// SOME CODE THERE ASSUMES DEXCALCS == LIQUIDITYCALCS.
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/// @notice implements calculation methods used for Fluid Dex such as updated withdrawal / borrow limits.
library DexCalcs {
// constants used for BigMath conversion from and to storage
uint256 internal constant DEFAULT_EXPONENT_SIZE = 8;
uint256 internal constant DEFAULT_EXPONENT_MASK = 0xFF;
uint256 internal constant FOUR_DECIMALS = 1e4;
uint256 internal constant X14 = 0x3fff;
uint256 internal constant X18 = 0x3ffff;
uint256 internal constant X24 = 0xffffff;
uint256 internal constant X33 = 0x1ffffffff;
uint256 internal constant X64 = 0xffffffffffffffff;
///////////////////////////////////////////////////////////////////////////
////////// CALC LIMITS /////////
///////////////////////////////////////////////////////////////////////////
/// @dev calculates withdrawal limit before an operate execution:
/// amount of user supply that must stay supplied (not amount that can be withdrawn).
/// i.e. if user has supplied 100m and can withdraw 5M, this method returns the 95M, not the withdrawable amount 5M
/// @param userSupplyData_ user supply data packed uint256 from storage
/// @param userSupply_ current user supply amount already extracted from `userSupplyData_` and converted from BigMath
/// @return currentWithdrawalLimit_ current withdrawal limit updated for expansion since last interaction.
/// returned value is in raw for with interest mode, normal amount for interest free mode!
function calcWithdrawalLimitBeforeOperate(
uint256 userSupplyData_,
uint256 userSupply_
) internal view returns (uint256 currentWithdrawalLimit_) {
// @dev must support handling the case where timestamp is 0 (config is set but no interactions yet).
// first tx where timestamp is 0 will enter `if (lastWithdrawalLimit_ == 0)` because lastWithdrawalLimit_ is not set yet.
// returning max withdrawal allowed, which is not exactly right but doesn't matter because the first interaction must be
// a deposit anyway. Important is that it would not revert.
// Note the first time a deposit brings the user supply amount to above the base withdrawal limit, the active limit
// is the fully expanded limit immediately.
// extract last set withdrawal limit
uint256 lastWithdrawalLimit_ = (userSupplyData_ >> DexSlotsLink.BITS_USER_SUPPLY_PREVIOUS_WITHDRAWAL_LIMIT) &
X64;
lastWithdrawalLimit_ =
(lastWithdrawalLimit_ >> DEFAULT_EXPONENT_SIZE) <<
(lastWithdrawalLimit_ & DEFAULT_EXPONENT_MASK);
if (lastWithdrawalLimit_ == 0) {
// withdrawal limit is not activated. Max withdrawal allowed
return 0;
}
uint256 maxWithdrawableLimit_;
uint256 temp_;
unchecked {
// extract max withdrawable percent of user supply and
// calculate maximum withdrawable amount expandPercentage of user supply at full expansion duration elapsed
// e.g.: if 10% expandPercentage, meaning 10% is withdrawable after full expandDuration has elapsed.
// userSupply_ needs to be at least 1e73 to overflow max limit of ~1e77 in uint256 (no token in existence where this is possible).
maxWithdrawableLimit_ =
(((userSupplyData_ >> DexSlotsLink.BITS_USER_SUPPLY_EXPAND_PERCENT) & X14) * userSupply_) /
FOUR_DECIMALS;
// time elapsed since last withdrawal limit was set (in seconds)
// @dev last process timestamp is guaranteed to exist for withdrawal, as a supply must have happened before.
// last timestamp can not be > current timestamp
temp_ = block.timestamp - ((userSupplyData_ >> DexSlotsLink.BITS_USER_SUPPLY_LAST_UPDATE_TIMESTAMP) & X33);
}
// calculate withdrawable amount of expandPercent that is elapsed of expandDuration.
// e.g. if 60% of expandDuration has elapsed, then user should be able to withdraw 6% of user supply, down to 94%.
// Note: no explicit check for this needed, it is covered by setting minWithdrawalLimit_ if needed.
temp_ =
(maxWithdrawableLimit_ * temp_) /
// extract expand duration: After this, decrement won't happen (user can withdraw 100% of withdraw limit)
((userSupplyData_ >> DexSlotsLink.BITS_USER_SUPPLY_EXPAND_DURATION) & X24); // expand duration can never be 0
// calculate expanded withdrawal limit: last withdrawal limit - withdrawable amount.
// Note: withdrawable amount here can grow bigger than userSupply if timeElapsed is a lot bigger than expandDuration,
// which would cause the subtraction `lastWithdrawalLimit_ - withdrawableAmount_` to revert. In that case, set 0
// which will cause minimum (fully expanded) withdrawal limit to be set in lines below.
unchecked {
// underflow explicitly checked & handled
currentWithdrawalLimit_ = lastWithdrawalLimit_ > temp_ ? lastWithdrawalLimit_ - temp_ : 0;
// calculate minimum withdrawal limit: minimum amount of user supply that must stay supplied at full expansion.
// subtraction can not underflow as maxWithdrawableLimit_ is a percentage amount (<=100%) of userSupply_
temp_ = userSupply_ - maxWithdrawableLimit_;
}
// if withdrawal limit is decreased below minimum then set minimum
// (e.g. when more than expandDuration time has elapsed)
if (temp_ > currentWithdrawalLimit_) {
currentWithdrawalLimit_ = temp_;
}
}
/// @dev calculates withdrawal limit after an operate execution:
/// amount of user supply that must stay supplied (not amount that can be withdrawn).
/// i.e. if user has supplied 100m and can withdraw 5M, this method returns the 95M, not the withdrawable amount 5M
/// @param userSupplyData_ user supply data packed uint256 from storage
/// @param userSupply_ current user supply amount already extracted from `userSupplyData_` and added / subtracted with the executed operate amount
/// @param newWithdrawalLimit_ current withdrawal limit updated for expansion since last interaction, result from `calcWithdrawalLimitBeforeOperate`
/// @return withdrawalLimit_ updated withdrawal limit that should be written to storage. returned value is in
/// raw for with interest mode, normal amount for interest free mode!
function calcWithdrawalLimitAfterOperate(
uint256 userSupplyData_,
uint256 userSupply_,
uint256 newWithdrawalLimit_
) internal pure returns (uint256) {
// temp_ => base withdrawal limit. below this, maximum withdrawals are allowed
uint256 temp_ = (userSupplyData_ >> DexSlotsLink.BITS_USER_SUPPLY_BASE_WITHDRAWAL_LIMIT) & X18;
temp_ = (temp_ >> DEFAULT_EXPONENT_SIZE) << (temp_ & DEFAULT_EXPONENT_MASK);
// if user supply is below base limit then max withdrawals are allowed
if (userSupply_ < temp_) {
return 0;
}
// temp_ => withdrawal limit expandPercent (is in 1e2 decimals)
temp_ = (userSupplyData_ >> DexSlotsLink.BITS_USER_SUPPLY_EXPAND_PERCENT) & X14;
unchecked {
// temp_ => minimum withdrawal limit: userSupply - max withdrawable limit (userSupply * expandPercent))
// userSupply_ needs to be at least 1e73 to overflow max limit of ~1e77 in uint256 (no token in existence where this is possible).
// subtraction can not underflow as maxWithdrawableLimit_ is a percentage amount (<=100%) of userSupply_
temp_ = userSupply_ - ((userSupply_ * temp_) / FOUR_DECIMALS);
}
// if new (before operation) withdrawal limit is less than minimum limit then set minimum limit.
// e.g. can happen on new deposits. withdrawal limit is instantly fully expanded in a scenario where
// increased deposit amount outpaces withrawals.
if (temp_ > newWithdrawalLimit_) {
return temp_;
}
return newWithdrawalLimit_;
}
/// @dev calculates borrow limit before an operate execution:
/// total amount user borrow can reach (not borrowable amount in current operation).
/// i.e. if user has borrowed 50M and can still borrow 5M, this method returns the total 55M, not the borrowable amount 5M
/// @param userBorrowData_ user borrow data packed uint256 from storage
/// @param userBorrow_ current user borrow amount already extracted from `userBorrowData_`
/// @return currentBorrowLimit_ current borrow limit updated for expansion since last interaction. returned value is in
/// raw for with interest mode, normal amount for interest free mode!
function calcBorrowLimitBeforeOperate(
uint256 userBorrowData_,
uint256 userBorrow_
) internal view returns (uint256 currentBorrowLimit_) {
// @dev must support handling the case where timestamp is 0 (config is set but no interactions yet) -> base limit.
// first tx where timestamp is 0 will end up with maxExpansionLimit_ = 0 because userBorrow = 0, so maxExpandedBorrowLimit_ = 0,
// so currentBorrowLimit_ = 0, so it will enter currentBorrowLimit_ < temp_ when checking against base limit as expected.
// temp_ = extract borrow expand percent (is in 1e2 decimals)
uint256 temp_ = (userBorrowData_ >> DexSlotsLink.BITS_USER_BORROW_EXPAND_PERCENT) & X14;
uint256 maxExpansionLimit_;
uint256 maxExpandedBorrowLimit_;
unchecked {
// calculate max expansion limit: Max amount limit can expand to since last interaction
// userBorrow_ needs to be at least 1e73 to overflow max limit of ~1e77 in uint256 (no token in existence where this is possible).
maxExpansionLimit_ = ((userBorrow_ * temp_) / FOUR_DECIMALS);
// calculate max borrow limit: Max point limit can increase to since last interaction
maxExpandedBorrowLimit_ = userBorrow_ + maxExpansionLimit_;
}
// time elapsed since last borrow limit was set (in seconds)
unchecked {
// temp_ = timeElapsed_ (last timestamp can not be > current timestamp)
temp_ = block.timestamp - ((userBorrowData_ >> DexSlotsLink.BITS_USER_BORROW_LAST_UPDATE_TIMESTAMP) & X33); // extract last update timestamp
}
// currentBorrowLimit_ = expandedBorrowableAmount + extract last set borrow limit
currentBorrowLimit_ =
// calculate borrow limit expansion since last interaction for `expandPercent` that is elapsed of `expandDuration`.
// divisor is extract expand duration (after this, full expansion to expandPercentage happened).
((maxExpansionLimit_ * temp_) /
((userBorrowData_ >> DexSlotsLink.BITS_USER_BORROW_EXPAND_DURATION) & X24)) + // expand duration can never be 0
// extract last set borrow limit
BigMathMinified.fromBigNumber(
(userBorrowData_ >> DexSlotsLink.BITS_USER_BORROW_PREVIOUS_BORROW_LIMIT) & X64,
DEFAULT_EXPONENT_SIZE,
DEFAULT_EXPONENT_MASK
);
// if timeElapsed is bigger than expandDuration, new borrow limit would be > max expansion,
// so set to `maxExpandedBorrowLimit_` in that case.
// also covers the case where last process timestamp = 0 (timeElapsed would simply be very big)
if (currentBorrowLimit_ > maxExpandedBorrowLimit_) {
currentBorrowLimit_ = maxExpandedBorrowLimit_;
}
// temp_ = extract base borrow limit, if current limit is below this, then set base limit as current limit
temp_ = (userBorrowData_ >> DexSlotsLink.BITS_USER_BORROW_BASE_BORROW_LIMIT) & X18;
temp_ = (temp_ >> DEFAULT_EXPONENT_SIZE) << (temp_ & DEFAULT_EXPONENT_MASK);
if (currentBorrowLimit_ < temp_) {
return temp_;
}
// temp_ = extract hard max borrow limit. Above this user can never borrow (not expandable above)
temp_ = (userBorrowData_ >> DexSlotsLink.BITS_USER_BORROW_MAX_BORROW_LIMIT) & X18;
temp_ = (temp_ >> DEFAULT_EXPONENT_SIZE) << (temp_ & DEFAULT_EXPONENT_MASK);
if (currentBorrowLimit_ > temp_) {
return temp_;
}
}
/// @dev calculates borrow limit after an operate execution:
/// total amount user borrow can reach (not borrowable amount in current operation).
/// i.e. if user has borrowed 50M and can still borrow 5M, this method returns the total 55M, not the borrowable amount 5M
/// @param userBorrowData_ user borrow data packed uint256 from storage
/// @param userBorrow_ current user borrow amount already extracted from `userBorrowData_` and added / subtracted with the executed operate amount
/// @param newBorrowLimit_ current borrow limit updated for expansion since last interaction, result from `calcBorrowLimitBeforeOperate`
/// @return borrowLimit_ updated borrow limit that should be written to storage.
/// returned value is in raw for with interest mode, normal amount for interest free mode!
function calcBorrowLimitAfterOperate(
uint256 userBorrowData_,
uint256 userBorrow_,
uint256 newBorrowLimit_
) internal pure returns (uint256 borrowLimit_) {
// temp_ = extract borrow expand percent
uint256 temp_ = (userBorrowData_ >> DexSlotsLink.BITS_USER_BORROW_EXPAND_PERCENT) & X14; // (is in 1e2 decimals)
unchecked {
// borrowLimit_ = calculate maximum borrow limit at full expansion.
// userBorrow_ needs to be at least 1e73 to overflow max limit of ~1e77 in uint256 (no token in existence where this is possible).
borrowLimit_ = userBorrow_ + ((userBorrow_ * temp_) / FOUR_DECIMALS);
}
// temp_ = extract base borrow limit
temp_ = (userBorrowData_ >> DexSlotsLink.BITS_USER_BORROW_BASE_BORROW_LIMIT) & X18;
temp_ = (temp_ >> DEFAULT_EXPONENT_SIZE) << (temp_ & DEFAULT_EXPONENT_MASK);
if (borrowLimit_ < temp_) {
// below base limit, borrow limit is always base limit
return temp_;
}
// temp_ = extract hard max borrow limit. Above this user can never borrow (not expandable above)
temp_ = (userBorrowData_ >> DexSlotsLink.BITS_USER_BORROW_MAX_BORROW_LIMIT) & X18;
temp_ = (temp_ >> DEFAULT_EXPONENT_SIZE) << (temp_ & DEFAULT_EXPONENT_MASK);
// make sure fully expanded borrow limit is not above hard max borrow limit
if (borrowLimit_ > temp_) {
borrowLimit_ = temp_;
}
// if new borrow limit (from before operate) is > max borrow limit, set max borrow limit.
// (e.g. on a repay shrinking instantly to fully expanded borrow limit from new borrow amount. shrinking is instant)
if (newBorrowLimit_ > borrowLimit_) {
return borrowLimit_;
}
return newBorrowLimit_;
}
}
contracts/libraries/dexSlotsLink.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.21 <=0.8.29;
/// @notice library that helps in reading / working with storage slot data of Fluid Dex.
/// @dev as all data for Fluid Dex is internal, any data must be fetched directly through manual
/// slot reading through this library or, if gas usage is less important, through the FluidDexResolver.
library DexSlotsLink {
/// @dev storage slot for variables at Dex
uint256 internal constant DEX_VARIABLES_SLOT = 0;
/// @dev storage slot for variables2 at Dex
uint256 internal constant DEX_VARIABLES2_SLOT = 1;
/// @dev storage slot for total supply shares at Dex
uint256 internal constant DEX_TOTAL_SUPPLY_SHARES_SLOT = 2;
/// @dev storage slot for user supply mapping at Dex
uint256 internal constant DEX_USER_SUPPLY_MAPPING_SLOT = 3;
/// @dev storage slot for total borrow shares at Dex
uint256 internal constant DEX_TOTAL_BORROW_SHARES_SLOT = 4;
/// @dev storage slot for user borrow mapping at Dex
uint256 internal constant DEX_USER_BORROW_MAPPING_SLOT = 5;
/// @dev storage slot for oracle mapping at Dex
uint256 internal constant DEX_ORACLE_MAPPING_SLOT = 6;
/// @dev storage slot for range and threshold shifts at Dex
uint256 internal constant DEX_RANGE_THRESHOLD_SHIFTS_SLOT = 7;
/// @dev storage slot for center price shift at Dex
uint256 internal constant DEX_CENTER_PRICE_SHIFT_SLOT = 8;
// --------------------------------
// @dev stacked uint256 storage slots bits position data for each:
// UserSupplyData
uint256 internal constant BITS_USER_SUPPLY_ALLOWED = 0;
uint256 internal constant BITS_USER_SUPPLY_AMOUNT = 1;
uint256 internal constant BITS_USER_SUPPLY_PREVIOUS_WITHDRAWAL_LIMIT = 65;
uint256 internal constant BITS_USER_SUPPLY_LAST_UPDATE_TIMESTAMP = 129;
uint256 internal constant BITS_USER_SUPPLY_EXPAND_PERCENT = 162;
uint256 internal constant BITS_USER_SUPPLY_EXPAND_DURATION = 176;
uint256 internal constant BITS_USER_SUPPLY_BASE_WITHDRAWAL_LIMIT = 200;
// UserBorrowData
uint256 internal constant BITS_USER_BORROW_ALLOWED = 0;
uint256 internal constant BITS_USER_BORROW_AMOUNT = 1;
uint256 internal constant BITS_USER_BORROW_PREVIOUS_BORROW_LIMIT = 65;
uint256 internal constant BITS_USER_BORROW_LAST_UPDATE_TIMESTAMP = 129;
uint256 internal constant BITS_USER_BORROW_EXPAND_PERCENT = 162;
uint256 internal constant BITS_USER_BORROW_EXPAND_DURATION = 176;
uint256 internal constant BITS_USER_BORROW_BASE_BORROW_LIMIT = 200;
uint256 internal constant BITS_USER_BORROW_MAX_BORROW_LIMIT = 218;
// --------------------------------
/// @notice Calculating the slot ID for Dex contract for single mapping at `slot_` for `key_`
function calculateMappingStorageSlot(uint256 slot_, address key_) internal pure returns (bytes32) {
return keccak256(abi.encode(key_, slot_));
}
/// @notice Calculating the slot ID for Dex contract for double mapping at `slot_` for `key1_` and `key2_`
function calculateDoubleMappingStorageSlot(
uint256 slot_,
address key1_,
address key2_
) internal pure returns (bytes32) {
bytes32 intermediateSlot_ = keccak256(abi.encode(key1_, slot_));
return keccak256(abi.encode(key2_, intermediateSlot_));
}
}
contracts/libraries/errorTypes.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.21 <=0.8.29;
library LibsErrorTypes {
/***********************************|
| LiquidityCalcs |
|__________________________________*/
/// @notice thrown when supply or borrow exchange price is zero at calc token data (token not configured yet)
uint256 internal constant LiquidityCalcs__ExchangePriceZero = 70001;
/// @notice thrown when rate data is set to a version that is not implemented
uint256 internal constant LiquidityCalcs__UnsupportedRateVersion = 70002;
/// @notice thrown when the calculated borrow rate turns negative. This should never happen.
uint256 internal constant LiquidityCalcs__BorrowRateNegative = 70003;
/***********************************|
| SafeTransfer |
|__________________________________*/
/// @notice thrown when safe transfer from for an ERC20 fails
uint256 internal constant SafeTransfer__TransferFromFailed = 71001;
/// @notice thrown when safe transfer for an ERC20 fails
uint256 internal constant SafeTransfer__TransferFailed = 71002;
/***********************************|
| SafeApprove |
|__________________________________*/
/// @notice thrown when safe approve from for an ERC20 fails
uint256 internal constant SafeApprove__ApproveFailed = 81001;
}
contracts/libraries/safeTransfer.sol
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.21 <=0.8.29;
import { LibsErrorTypes as ErrorTypes } from "./errorTypes.sol";
/// @notice provides minimalistic methods for safe transfers, e.g. ERC20 safeTransferFrom
library SafeTransfer {
// pass max. 50k gas for native transfers. This ensures support for some realistic
// scenarios like WETH deposits in receive() but as restrictive as possible.
uint256 internal constant MAX_NATIVE_TRANSFER_GAS = 50000;
error FluidSafeTransferError(uint256 errorId_);
/// @dev Transfer `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.
/// Minimally modified from Solmate SafeTransferLib (address as input param for token, Custom Error):
/// https://github.com/transmissions11/solmate/blob/50e15bb566f98b7174da9b0066126a4c3e75e0fd/src/utils/SafeTransferLib.sol#L31-L63
function safeTransferFrom(address token_, address from_, address to_, uint256 amount_) internal {
bool success_;
/// @solidity memory-safe-assembly
assembly {
// Get a pointer to some free memory.
let freeMemoryPointer := mload(0x40)
// Write the abi-encoded calldata into memory, beginning with the function selector.
mstore(freeMemoryPointer, 0x23b872dd00000000000000000000000000000000000000000000000000000000)
mstore(add(freeMemoryPointer, 4), and(from_, 0xffffffffffffffffffffffffffffffffffffffff)) // Append and mask the "from_" argument.
mstore(add(freeMemoryPointer, 36), and(to_, 0xffffffffffffffffffffffffffffffffffffffff)) // Append and mask the "to_" argument.
mstore(add(freeMemoryPointer, 68), amount_) // Append the "amount_" argument. Masking not required as it's a full 32 byte type.
success_ := and(
// Set success to whether the call reverted, if not we check it either
// returned exactly 1 (can't just be non-zero data), or had no return data.
or(and(eq(mload(0), 1), gt(returndatasize(), 31)), iszero(returndatasize())),
// We use 100 because the length of our calldata totals up like so: 4 + 32 * 3.
// We use 0 and 32 to copy up to 32 bytes of return data into the scratch space.
// Counterintuitively, this call must be positioned second to the or() call in the
// surrounding and() call or else returndatasize() will be zero during the computation.
call(gas(), token_, 0, freeMemoryPointer, 100, 0, 32)
)
}
if (!success_) {
revert FluidSafeTransferError(ErrorTypes.SafeTransfer__TransferFromFailed);
}
}
/// @dev Transfer `amount_` of `token_` to `to_`.
/// If `token_` returns no value, non-reverting calls are assumed to be successful.
/// Minimally modified from Solmate SafeTransferLib (address as input param for token, Custom Error):
/// https://github.com/transmissions11/solmate/blob/50e15bb566f98b7174da9b0066126a4c3e75e0fd/src/utils/SafeTransferLib.sol#L65-L95
function safeTransfer(address token_, address to_, uint256 amount_) internal {
bool success_;
/// @solidity memory-safe-assembly
assembly {
// Get a pointer to some free memory.
let freeMemoryPointer := mload(0x40)
// Write the abi-encoded calldata into memory, beginning with the function selector.
mstore(freeMemoryPointer, 0xa9059cbb00000000000000000000000000000000000000000000000000000000)
mstore(add(freeMemoryPointer, 4), and(to_, 0xffffffffffffffffffffffffffffffffffffffff)) // Append and mask the "to_" argument.
mstore(add(freeMemoryPointer, 36), amount_) // Append the "amount_" argument. Masking not required as it's a full 32 byte type.
success_ := and(
// Set success to whether the call reverted, if not we check it either
// returned exactly 1 (can't just be non-zero data), or had no return data.
or(and(eq(mload(0), 1), gt(returndatasize(), 31)), iszero(returndatasize())),
// We use 68 because the length of our calldata totals up like so: 4 + 32 * 2.
// We use 0 and 32 to copy up to 32 bytes of return data into the scratch space.
// Counterintuitively, this call must be positioned second to the or() call in the
// surrounding and() call or else returndatasize() will be zero during the computation.
call(gas(), token_, 0, freeMemoryPointer, 68, 0, 32)
)
}
if (!success_) {
revert FluidSafeTransferError(ErrorTypes.SafeTransfer__TransferFailed);
}
}
/// @dev Transfer `amount_` of ` native token to `to_`.
/// Minimally modified from Solmate SafeTransferLib (Custom Error):
/// https://github.com/transmissions11/solmate/blob/50e15bb566f98b7174da9b0066126a4c3e75e0fd/src/utils/SafeTransferLib.sol#L15-L25
function safeTransferNative(address to_, uint256 amount_) internal {
bool success_;
/// @solidity memory-safe-assembly
assembly {
// Transfer the ETH and store if it succeeded or not. Pass limited gas
success_ := call(MAX_NATIVE_TRANSFER_GAS, to_, amount_, 0, 0, 0, 0)
}
if (!success_) {
revert FluidSafeTransferError(ErrorTypes.SafeTransfer__TransferFailed);
}
}
}
contracts/libraries/storageRead.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.21 <=0.8.29;
/// @notice implements a method to read uint256 data from storage at a bytes32 storage slot key.
contract StorageRead {
function readFromStorage(bytes32 slot_) public view returns (uint256 result_) {
assembly {
result_ := sload(slot_) // read value from the storage slot
}
}
}
contracts/liquidity/adminModule/structs.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.21 <=0.8.29;
abstract contract Structs {
struct AddressBool {
address addr;
bool value;
}
struct AddressUint256 {
address addr;
uint256 value;
}
/// @notice struct to set borrow rate data for version 1
struct RateDataV1Params {
///
/// @param token for rate data
address token;
///
/// @param kink in borrow rate. in 1e2: 100% = 10_000; 1% = 100
/// utilization below kink usually means slow increase in rate, once utilization is above kink borrow rate increases fast
uint256 kink;
///
/// @param rateAtUtilizationZero desired borrow rate when utilization is zero. in 1e2: 100% = 10_000; 1% = 100
/// i.e. constant minimum borrow rate
/// e.g. at utilization = 0.01% rate could still be at least 4% (rateAtUtilizationZero would be 400 then)
uint256 rateAtUtilizationZero;
///
/// @param rateAtUtilizationKink borrow rate when utilization is at kink. in 1e2: 100% = 10_000; 1% = 100
/// e.g. when rate should be 7% at kink then rateAtUtilizationKink would be 700
uint256 rateAtUtilizationKink;
///
/// @param rateAtUtilizationMax borrow rate when utilization is maximum at 100%. in 1e2: 100% = 10_000; 1% = 100
/// e.g. when rate should be 125% at 100% then rateAtUtilizationMax would be 12_500
uint256 rateAtUtilizationMax;
}
/// @notice struct to set borrow rate data for version 2
struct RateDataV2Params {
///
/// @param token for rate data
address token;
///
/// @param kink1 first kink in borrow rate. in 1e2: 100% = 10_000; 1% = 100
/// utilization below kink 1 usually means slow increase in rate, once utilization is above kink 1 borrow rate increases faster
uint256 kink1;
///
/// @param kink2 second kink in borrow rate. in 1e2: 100% = 10_000; 1% = 100
/// utilization below kink 2 usually means slow / medium increase in rate, once utilization is above kink 2 borrow rate increases fast
uint256 kink2;
///
/// @param rateAtUtilizationZero desired borrow rate when utilization is zero. in 1e2: 100% = 10_000; 1% = 100
/// i.e. constant minimum borrow rate
/// e.g. at utilization = 0.01% rate could still be at least 4% (rateAtUtilizationZero would be 400 then)
uint256 rateAtUtilizationZero;
///
/// @param rateAtUtilizationKink1 desired borrow rate when utilization is at first kink. in 1e2: 100% = 10_000; 1% = 100
/// e.g. when rate should be 7% at first kink then rateAtUtilizationKink would be 700
uint256 rateAtUtilizationKink1;
///
/// @param rateAtUtilizationKink2 desired borrow rate when utilization is at second kink. in 1e2: 100% = 10_000; 1% = 100
/// e.g. when rate should be 7% at second kink then rateAtUtilizationKink would be 1_200
uint256 rateAtUtilizationKink2;
///
/// @param rateAtUtilizationMax desired borrow rate when utilization is maximum at 100%. in 1e2: 100% = 10_000; 1% = 100
/// e.g. when rate should be 125% at 100% then rateAtUtilizationMax would be 12_500
uint256 rateAtUtilizationMax;
}
/// @notice struct to set token config
struct TokenConfig {
///
/// @param token address
address token;
///
/// @param fee charges on borrower's interest. in 1e2: 100% = 10_000; 1% = 100
uint256 fee;
///
/// @param threshold on when to update the storage slot. in 1e2: 100% = 10_000; 1% = 100
uint256 threshold;
///
/// @param maxUtilization maximum allowed utilization. in 1e2: 100% = 10_000; 1% = 100
/// set to 100% to disable and have default limit of 100% (avoiding SLOAD).
uint256 maxUtilization;
}
/// @notice struct to set user supply & withdrawal config
struct UserSupplyConfig {
///
/// @param user address
address user;
///
/// @param token address
address token;
///
/// @param mode: 0 = without interest. 1 = with interest
uint8 mode;
///
/// @param expandPercent withdrawal limit expand percent. in 1e2: 100% = 10_000; 1% = 100
/// Also used to calculate rate at which withdrawal limit should decrease (instant).
uint256 expandPercent;
///
/// @param expandDuration withdrawal limit expand duration in seconds.
/// used to calculate rate together with expandPercent
uint256 expandDuration;
///
/// @param baseWithdrawalLimit base limit, below this, user can withdraw the entire amount.
/// amount in raw (to be multiplied with exchange price) or normal depends on configured mode in user config for the token:
/// with interest -> raw, without interest -> normal
uint256 baseWithdrawalLimit;
}
/// @notice struct to set user borrow & payback config
struct UserBorrowConfig {
///
/// @param user address
address user;
///
/// @param token address
address token;
///
/// @param mode: 0 = without interest. 1 = with interest
uint8 mode;
///
/// @param expandPercent debt limit expand percent. in 1e2: 100% = 10_000; 1% = 100
/// Also used to calculate rate at which debt limit should decrease (instant).
uint256 expandPercent;
///
/// @param expandDuration debt limit expand duration in seconds.
/// used to calculate rate together with expandPercent
uint256 expandDuration;
///
/// @param baseDebtCeiling base borrow limit. until here, borrow limit remains as baseDebtCeiling
/// (user can borrow until this point at once without stepped expansion). Above this, automated limit comes in place.
/// amount in raw (to be multiplied with exchange price) or normal depends on configured mode in user config for the token:
/// with interest -> raw, without interest -> normal
uint256 baseDebtCeiling;
///
/// @param maxDebtCeiling max borrow ceiling, maximum amount the user can borrow.
/// amount in raw (to be multiplied with exchange price) or normal depends on configured mode in user config for the token:
/// with interest -> raw, without interest -> normal
uint256 maxDebtCeiling;
}
}
contracts/periphery/resolvers/dex/main.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
import { AddressCalcs } from "../../../libraries/addressCalcs.sol";
import { DexSlotsLink } from "../../../libraries/dexSlotsLink.sol";
import { DexCalcs } from "../../../libraries/dexCalcs.sol";
import { BigMathMinified } from "../../../libraries/bigMathMinified.sol";
import { BytesSliceAndConcat } from "../../../libraries/bytesSliceAndConcat.sol";
import { IFluidDexT1 } from "../../../protocols/dex/interfaces/iDexT1.sol";
import { Structs as FluidLiquidityResolverStructs } from "../liquidity/structs.sol";
import { Structs } from "./structs.sol";
import { Variables } from "./variables.sol";
/// @title DexFactoryViews
/// @notice Abstract contract providing view functions for DEX factory-related operations
abstract contract DexFactoryViews is Variables {
/// @notice Get the address of a DEX given its ID
/// @param dexId_ The ID of the DEX
/// @return dex_ The address of the DEX
function getDexAddress(uint256 dexId_) public view returns (address dex_) {
return AddressCalcs.addressCalc(address(FACTORY), dexId_);
}
/// @notice Get the ID of a DEX given its address
/// @param dex_ The address of the DEX
/// @return id_ The ID of the DEX
function getDexId(address dex_) public view returns (uint id_) {
id_ = IFluidDexT1(dex_).DEX_ID();
}
/// @notice Get the total number of DEXes
/// @return The total number of DEXes
function getTotalDexes() public view returns (uint) {
return FACTORY.totalDexes();
}
/// @notice Get an array of all DEX addresses
/// @return dexes_ An array containing all DEX addresses
function getAllDexAddresses() public view returns (address[] memory dexes_) {
uint totalDexes_ = getTotalDexes();
dexes_ = new address[](totalDexes_);
for (uint i = 0; i < totalDexes_; i++) {
dexes_[i] = getDexAddress((i + 1));
}
}
}
/// @title DexStorageVars
/// @notice Abstract contract providing view functions for DEX storage variables
abstract contract DexStorageVars is Variables {
/// @notice Get the raw DEX variables
/// @param dex_ The address of the DEX
/// @return The raw DEX variables
function getDexVariablesRaw(address dex_) public view returns (uint) {
return IFluidDexT1(dex_).readFromStorage(bytes32(DexSlotsLink.DEX_VARIABLES_SLOT));
}
/// @notice Get the raw DEX variables2
/// @param dex_ The address of the DEX
/// @return The raw DEX variables2
function getDexVariables2Raw(address dex_) public view returns (uint) {
return IFluidDexT1(dex_).readFromStorage(bytes32(DexSlotsLink.DEX_VARIABLES2_SLOT));
}
/// @notice Get the total supply shares slot data of a DEX
/// @param dex_ The address of the DEX
/// @return The total supply shares
function getTotalSupplySharesRaw(address dex_) public view returns (uint) {
return IFluidDexT1(dex_).readFromStorage(bytes32(DexSlotsLink.DEX_TOTAL_SUPPLY_SHARES_SLOT));
}
/// @notice Get the raw user supply data for a specific user and DEX
/// @param dex_ The address of the DEX
/// @param user_ The address of the user
/// @return The raw user supply data
function getUserSupplyDataRaw(address dex_, address user_) public view returns (uint) {
return
IFluidDexT1(dex_).readFromStorage(
DexSlotsLink.calculateMappingStorageSlot(DexSlotsLink.DEX_USER_SUPPLY_MAPPING_SLOT, user_)
);
}
/// @notice Get the total borrow shares slot data of a DEX
/// @param dex_ The address of the DEX
/// @return The total borrow shares
function getTotalBorrowSharesRaw(address dex_) public view returns (uint) {
return IFluidDexT1(dex_).readFromStorage(bytes32(DexSlotsLink.DEX_TOTAL_BORROW_SHARES_SLOT));
}
/// @notice Get the raw user borrow data for a specific user and DEX
/// @param dex_ The address of the DEX
/// @param user_ The address of the user
/// @return The raw user borrow data
function getUserBorrowDataRaw(address dex_, address user_) public view returns (uint) {
return
IFluidDexT1(dex_).readFromStorage(
DexSlotsLink.calculateMappingStorageSlot(DexSlotsLink.DEX_USER_BORROW_MAPPING_SLOT, user_)
);
}
/// @notice Get the raw oracle data for a specific DEX and index
/// @param dex_ The address of the DEX
/// @param index_ The index of the oracle data
/// @return The raw oracle data
function getOracleRaw(address dex_, uint index_) public view returns (uint) {
return
IFluidDexT1(dex_).readFromStorage(
_calculateStorageSlotUintMapping(DexSlotsLink.DEX_ORACLE_MAPPING_SLOT, index_)
);
}
/// @notice Get the raw range shift for a DEX
/// @param dex_ The address of the DEX
/// @return The raw range shift
function getRangeShiftRaw(address dex_) public view returns (uint) {
return
IFluidDexT1(dex_).readFromStorage(bytes32(DexSlotsLink.DEX_RANGE_THRESHOLD_SHIFTS_SLOT)) &
type(uint128).max;
}
/// @notice Get the raw threshold shift for a DEX
/// @param dex_ The address of the DEX
/// @return The raw threshold shift
function getThresholdShiftRaw(address dex_) public view returns (uint) {
return IFluidDexT1(dex_).readFromStorage(bytes32(DexSlotsLink.DEX_RANGE_THRESHOLD_SHIFTS_SLOT)) >> 128;
}
/// @notice Get the raw center price shift for a DEX
/// @param dex_ The address of the DEX
/// @return The raw center price shift
function getCenterPriceShiftRaw(address dex_) public view returns (uint) {
return IFluidDexT1(dex_).readFromStorage(bytes32(DexSlotsLink.DEX_CENTER_PRICE_SHIFT_SLOT));
}
/// @dev Calculate the storage slot for a uint mapping
/// @param slot_ The base slot of the mapping
/// @param key_ The key of the mapping
/// @return The calculated storage slot
function _calculateStorageSlotUintMapping(uint256 slot_, uint key_) internal pure returns (bytes32) {
return keccak256(abi.encode(key_, slot_));
}
}
abstract contract DexActionEstimates {
address private constant ADDRESS_DEAD = 0x000000000000000000000000000000000000dEaD;
/// @notice estimates swap IN tokens execution
/// @param dex_ Dex pool
/// @param swap0to1_ Direction of swap. If true, swaps token0 for token1; if false, swaps token1 for token0
/// @param amountIn_ The exact amount of input tokens to swap
/// @param amountOutMin_ The minimum amount of output tokens the user is willing to accept
/// @return amountOut_ The amount of output tokens received from the swap
function estimateSwapIn(
address dex_,
bool swap0to1_,
uint256 amountIn_,
uint256 amountOutMin_
) public payable returns (uint256 amountOut_) {
try IFluidDexT1(dex_).swapIn{ value: msg.value }(swap0to1_, amountIn_, amountOutMin_, ADDRESS_DEAD) {} catch (
bytes memory lowLevelData_
) {
(amountOut_) = _decodeLowLevelUint1x(lowLevelData_, IFluidDexT1.FluidDexSwapResult.selector);
}
}
/// @notice estimates swap OUT tokens execution
/// @param dex_ Dex pool
/// @param swap0to1_ Direction of swap. If true, swaps token0 for token1; if false, swaps token1 for token0
/// @param amountOut_ The exact amount of tokens to receive after swap
/// @param amountInMax_ Maximum amount of tokens to swap in
/// @return amountIn_ The amount of input tokens used for the swap
function estimateSwapOut(
address dex_,
bool swap0to1_,
uint256 amountOut_,
uint256 amountInMax_
) public payable returns (uint256 amountIn_) {
try IFluidDexT1(dex_).swapOut{ value: msg.value }(swap0to1_, amountOut_, amountInMax_, ADDRESS_DEAD) {} catch (
bytes memory lowLevelData_
) {
(amountIn_) = _decodeLowLevelUint1x(lowLevelData_, IFluidDexT1.FluidDexSwapResult.selector);
}
}
/// @dev Estimate deposit tokens in equal proportion to the current pool ratio
/// @param dex_ The address of the DEX contract
/// @param shares_ The number of shares to mint
/// @param maxToken0Deposit_ Maximum amount of token0 to deposit
/// @param maxToken1Deposit_ Maximum amount of token1 to deposit
/// @return token0Amt_ Estimated amount of token0 to deposit
/// @return token1Amt_ Estimated amount of token1 to deposit
function estimateDepositPerfect(
address dex_,
uint shares_,
uint maxToken0Deposit_,
uint maxToken1Deposit_
) public payable returns (uint token0Amt_, uint token1Amt_) {
try
IFluidDexT1(dex_).depositPerfect{ value: msg.value }(shares_, maxToken0Deposit_, maxToken1Deposit_, true)
{} catch (bytes memory lowLevelData_) {
(token0Amt_, token1Amt_) = _decodeLowLevelUint2x(
lowLevelData_,
IFluidDexT1.FluidDexPerfectLiquidityOutput.selector
);
}
}
/// @dev Estimate withdrawal of a perfect amount of collateral liquidity
/// @param dex_ The address of the DEX contract
/// @param shares_ The number of shares to withdraw
/// @param minToken0Withdraw_ The minimum amount of token0 the user is willing to accept
/// @param minToken1Withdraw_ The minimum amount of token1 the user is willing to accept
/// @return token0Amt_ Estimated amount of token0 to be withdrawn
/// @return token1Amt_ Estimated amount of token1 to be withdrawn
function estimateWithdrawPerfect(
address dex_,
uint shares_,
uint minToken0Withdraw_,
uint minToken1Withdraw_
) public returns (uint token0Amt_, uint token1Amt_) {
try IFluidDexT1(dex_).withdrawPerfect(shares_, minToken0Withdraw_, minToken1Withdraw_, ADDRESS_DEAD) {} catch (
bytes memory lowLevelData_
) {
(token0Amt_, token1Amt_) = _decodeLowLevelUint2x(
lowLevelData_,
IFluidDexT1.FluidDexPerfectLiquidityOutput.selector
);
}
}
/// @dev Estimate borrowing tokens in equal proportion to the current debt pool ratio
/// @param dex_ The address of the DEX contract
/// @param shares_ The number of shares to borrow
/// @param minToken0Borrow_ Minimum amount of token0 to borrow
/// @param minToken1Borrow_ Minimum amount of token1 to borrow
/// @return token0Amt_ Estimated amount of token0 to be borrowed
/// @return token1Amt_ Estimated amount of token1 to be borrowed
function estimateBorrowPerfect(
address dex_,
uint shares_,
uint minToken0Borrow_,
uint minToken1Borrow_
) public returns (uint token0Amt_, uint token1Amt_) {
try IFluidDexT1(dex_).borrowPerfect(shares_, minToken0Borrow_, minToken1Borrow_, ADDRESS_DEAD) {} catch (
bytes memory lowLevelData_
) {
(token0Amt_, token1Amt_) = _decodeLowLevelUint2x(
lowLevelData_,
IFluidDexT1.FluidDexPerfectLiquidityOutput.selector
);
}
}
/// @dev Estimate paying back borrowed tokens in equal proportion to the current debt pool ratio
/// @param dex_ The address of the DEX contract
/// @param shares_ The number of shares to pay back
/// @param maxToken0Payback_ Maximum amount of token0 to pay back
/// @param maxToken1Payback_ Maximum amount of token1 to pay back
/// @return token0Amt_ Estimated amount of token0 to be paid back
/// @return token1Amt_ Estimated amount of token1 to be paid back
function estimatePaybackPerfect(
address dex_,
uint shares_,
uint maxToken0Payback_,
uint maxToken1Payback_
) public payable returns (uint token0Amt_, uint token1Amt_) {
try
IFluidDexT1(dex_).paybackPerfect{ value: msg.value }(shares_, maxToken0Payback_, maxToken1Payback_, true)
{} catch (bytes memory lowLevelData_) {
(token0Amt_, token1Amt_) = _decodeLowLevelUint2x(
lowLevelData_,
IFluidDexT1.FluidDexPerfectLiquidityOutput.selector
);
}
}
/// @dev Estimate deposit of tokens
/// @param dex_ The address of the DEX contract
/// @param token0Amt_ Amount of token0 to deposit
/// @param token1Amt_ Amount of token1 to deposit
/// @param minSharesAmt_ Minimum amount of shares to receive
/// @return shares_ Estimated amount of shares to be minted
function estimateDeposit(
address dex_,
uint token0Amt_,
uint token1Amt_,
uint minSharesAmt_
) public payable returns (uint shares_) {
try IFluidDexT1(dex_).deposit{ value: msg.value }(token0Amt_, token1Amt_, minSharesAmt_, true) {} catch (
bytes memory lowLevelData_
) {
(shares_) = _decodeLowLevelUint1x(lowLevelData_, IFluidDexT1.FluidDexLiquidityOutput.selector);
}
}
/// @dev Estimate withdrawal of tokens
/// @param dex_ The address of the DEX contract
/// @param token0Amt_ Amount of token0 to withdraw
/// @param token1Amt_ Amount of token1 to withdraw
/// @param maxSharesAmt_ Maximum amount of shares to burn
/// @return shares_ Estimated amount of shares to be burned
function estimateWithdraw(
address dex_,
uint token0Amt_,
uint token1Amt_,
uint maxSharesAmt_
) public returns (uint shares_) {
try IFluidDexT1(dex_).withdraw(token0Amt_, token1Amt_, maxSharesAmt_, ADDRESS_DEAD) {} catch (
bytes memory lowLevelData_
) {
(shares_) = _decodeLowLevelUint1x(lowLevelData_, IFluidDexT1.FluidDexLiquidityOutput.selector);
}
}
/// @dev Estimate borrowing of tokens
/// @param dex_ The address of the DEX contract
/// @param token0Amt_ Amount of token0 to borrow
/// @param token1Amt_ Amount of token1 to borrow
/// @param maxSharesAmt_ Maximum amount of shares to mint
/// @return shares_ Estimated amount of shares to be minted
function estimateBorrow(
address dex_,
uint token0Amt_,
uint token1Amt_,
uint maxSharesAmt_
) public returns (uint shares_) {
try IFluidDexT1(dex_).borrow(token0Amt_, token1Amt_, maxSharesAmt_, ADDRESS_DEAD) {} catch (
bytes memory lowLevelData_
) {
(shares_) = _decodeLowLevelUint1x(lowLevelData_, IFluidDexT1.FluidDexLiquidityOutput.selector);
}
}
/// @dev Estimate paying back of borrowed tokens
/// @param dex_ The address of the DEX contract
/// @param token0Amt_ Amount of token0 to pay back
/// @param token1Amt_ Amount of token1 to pay back
/// @param minSharesAmt_ Minimum amount of shares to burn
/// @return shares_ Estimated amount of shares to be burned
function estimatePayback(
address dex_,
uint token0Amt_,
uint token1Amt_,
uint minSharesAmt_
) public payable returns (uint shares_) {
try IFluidDexT1(dex_).payback{ value: msg.value }(token0Amt_, token1Amt_, minSharesAmt_, true) {} catch (
bytes memory lowLevelData_
) {
(shares_) = _decodeLowLevelUint1x(lowLevelData_, IFluidDexT1.FluidDexLiquidityOutput.selector);
}
}
/// @dev Estimate withdrawal of a perfect amount of collateral liquidity in one token
/// @param dex_ The address of the DEX contract
/// @param shares_ The number of shares to withdraw
/// @param minToken0_ The minimum amount of token0 the user is willing to accept
/// @param minToken1_ The minimum amount of token1 the user is willing to accept
/// @return withdrawAmt_ Estimated amount of tokens to be withdrawn
function estimateWithdrawPerfectInOneToken(
address dex_,
uint shares_,
uint minToken0_,
uint minToken1_
) public returns (uint withdrawAmt_) {
try IFluidDexT1(dex_).withdrawPerfectInOneToken(shares_, minToken0_, minToken1_, ADDRESS_DEAD) {} catch (
bytes memory lowLevelData_
) {
(withdrawAmt_) = _decodeLowLevelUint1x(lowLevelData_, IFluidDexT1.FluidDexLiquidityOutput.selector);
}
}
/// @dev Estimate paying back of a perfect amount of borrowed tokens in one token
/// @param dex_ The address of the DEX contract
/// @param shares_ The number of shares to pay back
/// @param maxToken0_ Maximum amount of token0 to pay back
/// @param maxToken1_ Maximum amount of token1 to pay back
/// @return paybackAmt_ Estimated amount of tokens to be paid back
function estimatePaybackPerfectInOneToken(
address dex_,
uint shares_,
uint maxToken0_,
uint maxToken1_
) public payable returns (uint paybackAmt_) {
try
IFluidDexT1(dex_).paybackPerfectInOneToken{ value: msg.value }(shares_, maxToken0_, maxToken1_, true)
{} catch (bytes memory lowLevelData_) {
(paybackAmt_) = _decodeLowLevelUint1x(lowLevelData_, IFluidDexT1.FluidDexSingleTokenOutput.selector);
}
}
function _decodeLowLevelUint2x(
bytes memory lowLevelData_,
bytes4 targetErrorSelector_
) internal pure returns (uint value1_, uint value2_) {
if (lowLevelData_.length < 68) {
return (0, 0);
}
bytes4 errorSelector_;
assembly {
// Extract the selector from the error data
errorSelector_ := mload(add(lowLevelData_, 0x20))
}
if (errorSelector_ == targetErrorSelector_) {
assembly {
value1_ := mload(add(lowLevelData_, 36))
value2_ := mload(add(lowLevelData_, 68))
}
}
// else => values remain 0
}
function _decodeLowLevelUint1x(
bytes memory lowLevelData_,
bytes4 targetErrorSelector_
) internal pure returns (uint value1_) {
if (lowLevelData_.length < 36) {
return 0;
}
bytes4 errorSelector_;
assembly {
// Extract the selector from the error data
errorSelector_ := mload(add(lowLevelData_, 0x20))
}
if (errorSelector_ == targetErrorSelector_) {
assembly {
value1_ := mload(add(lowLevelData_, 36))
}
}
// else => values remain 0
}
}
abstract contract DexConstantsViews {
/// @notice returns all Dex constants
function getDexConstantsView(address dex_) public view returns (IFluidDexT1.ConstantViews memory constantsView_) {
return IFluidDexT1(dex_).constantsView();
}
/// @notice returns all Dex constants 2
function getDexConstantsView2(
address dex_
) public view returns (IFluidDexT1.ConstantViews2 memory constantsView2_) {
return IFluidDexT1(dex_).constantsView2();
}
/// @notice Get the addresses of the tokens in a DEX
/// @param dex_ The address of the DEX
/// @return token0_ The address of token0 in the DEX
/// @return token1_ The address of token1 in the DEX
function getDexTokens(address dex_) public view returns (address token0_, address token1_) {
IFluidDexT1.ConstantViews memory constantsView_ = IFluidDexT1(dex_).constantsView();
return (constantsView_.token0, constantsView_.token1);
}
}
abstract contract DexPublicViews is DexStorageVars, DexConstantsViews {
/// @notice Get the prices and exchange prices for a DEX
/// @param dex_ The address of the DEX
/// @return pex_ A struct containing prices and exchange prices
/// @dev expected to be called via callStatic
function getDexPricesAndExchangePrices(
address dex_
) public returns (IFluidDexT1.PricesAndExchangePrice memory pex_) {
try IFluidDexT1(dex_).getPricesAndExchangePrices() {} catch (bytes memory lowLevelData_) {
bytes4 errorSelector_;
assembly {
// Extract the selector from the error data
errorSelector_ := mload(add(lowLevelData_, 0x20))
}
if (errorSelector_ == IFluidDexT1.FluidDexPricesAndExchangeRates.selector) {
pex_ = abi.decode(
BytesSliceAndConcat.bytesSlice(lowLevelData_, 4, lowLevelData_.length - 4),
(IFluidDexT1.PricesAndExchangePrice)
);
}
}
}
/// @notice Get the collateral reserves for a DEX
/// @param dex_ The address of the DEX
/// @return reserves_ A struct containing collateral reserve information
/// @dev expected to be called via callStatic
function getDexCollateralReserves(address dex_) public returns (IFluidDexT1.CollateralReserves memory reserves_) {
return _getDexCollateralReserves(dex_, getDexConstantsView2(dex_));
}
/// @notice Get the debt reserves for a DEX
/// @param dex_ The address of the DEX
/// @return reserves_ A struct containing debt reserve information
/// @dev expected to be called via callStatic
function getDexDebtReserves(address dex_) public returns (IFluidDexT1.DebtReserves memory reserves_) {
return _getDexDebtReserves(dex_, getDexConstantsView2(dex_));
}
/// @notice get Dex oracle price TWAP data
/// @param secondsAgos_ array of seconds ago for which TWAP is needed. If user sends [10, 30, 60] then twaps_ will return [10-0, 30-10, 60-30]
/// @return twaps_ twap price, lowest price (aka minima) & highest price (aka maxima) between secondsAgo checkpoints
/// @return currentPrice_ price of pool after the most recent swap
function getDexOraclePrice(
address dex_,
uint[] memory secondsAgos_
) external view returns (IFluidDexT1.Oracle[] memory twaps_, uint currentPrice_) {
return IFluidDexT1(dex_).oraclePrice(secondsAgos_);
}
/// @dev Get the collateral reserves for a DEX scaled to token decimals
function _getDexCollateralReserves(
address dex_,
IFluidDexT1.ConstantViews2 memory constantsView2_
) internal returns (IFluidDexT1.CollateralReserves memory reserves_) {
uint256 dexVariables2_ = getDexVariables2Raw(dex_);
if ((dexVariables2_ & 1) != 1) {
// smart col not enabled
return IFluidDexT1.CollateralReserves(0, 0, 0, 0);
}
try this.getDexPricesAndExchangePrices(dex_) returns (IFluidDexT1.PricesAndExchangePrice memory pex_) {
try
IFluidDexT1(dex_).getCollateralReserves(
pex_.geometricMean,
pex_.upperRange,
pex_.lowerRange,
pex_.supplyToken0ExchangePrice,
pex_.supplyToken1ExchangePrice
)
returns (IFluidDexT1.CollateralReserves memory colReserves_) {
// returned reserves are in 1e12 decimals -> normalize to token decimals
reserves_.token0RealReserves =
(colReserves_.token0RealReserves * constantsView2_.token0DenominatorPrecision) /
constantsView2_.token0NumeratorPrecision;
reserves_.token0ImaginaryReserves =
(colReserves_.token0ImaginaryReserves * constantsView2_.token0DenominatorPrecision) /
constantsView2_.token0NumeratorPrecision;
reserves_.token1RealReserves =
(colReserves_.token1RealReserves * constantsView2_.token1DenominatorPrecision) /
constantsView2_.token1NumeratorPrecision;
reserves_.token1ImaginaryReserves =
(colReserves_.token1ImaginaryReserves * constantsView2_.token1DenominatorPrecision) /
constantsView2_.token1NumeratorPrecision;
} catch {
reserves_ = IFluidDexT1.CollateralReserves(0, 0, 0, 0);
}
} catch {
reserves_ = IFluidDexT1.CollateralReserves(0, 0, 0, 0);
}
}
/// @dev Get the debt reserves for a DEX scaled to token decimals
function _getDexDebtReserves(
address dex_,
IFluidDexT1.ConstantViews2 memory constantsView2_
) internal returns (IFluidDexT1.DebtReserves memory reserves_) {
uint256 dexVariables2_ = getDexVariables2Raw(dex_);
if ((dexVariables2_ & 2) != 2) {
// smart debt not enabled
return IFluidDexT1.DebtReserves(0, 0, 0, 0, 0, 0);
}
try this.getDexPricesAndExchangePrices(dex_) returns (IFluidDexT1.PricesAndExchangePrice memory pex_) {
try
IFluidDexT1(dex_).getDebtReserves(
pex_.geometricMean,
pex_.upperRange,
pex_.lowerRange,
pex_.borrowToken0ExchangePrice,
pex_.borrowToken1ExchangePrice
)
returns (IFluidDexT1.DebtReserves memory debtReserves_) {
// returned reserves are in 1e12 decimals -> normalize to token decimals
reserves_.token0Debt =
(debtReserves_.token0Debt * constantsView2_.token0DenominatorPrecision) /
constantsView2_.token0NumeratorPrecision;
reserves_.token0RealReserves =
(debtReserves_.token0RealReserves * constantsView2_.token0DenominatorPrecision) /
constantsView2_.token0NumeratorPrecision;
reserves_.token0ImaginaryReserves =
(debtReserves_.token0ImaginaryReserves * constantsView2_.token0DenominatorPrecision) /
constantsView2_.token0NumeratorPrecision;
reserves_.token1Debt =
(debtReserves_.token1Debt * constantsView2_.token1DenominatorPrecision) /
constantsView2_.token1NumeratorPrecision;
reserves_.token1RealReserves =
(debtReserves_.token1RealReserves * constantsView2_.token1DenominatorPrecision) /
constantsView2_.token1NumeratorPrecision;
reserves_.token1ImaginaryReserves =
(debtReserves_.token1ImaginaryReserves * constantsView2_.token1DenominatorPrecision) /
constantsView2_.token1NumeratorPrecision;
} catch {
reserves_ = IFluidDexT1.DebtReserves(0, 0, 0, 0, 0, 0);
}
} catch {
reserves_ = IFluidDexT1.DebtReserves(0, 0, 0, 0, 0, 0);
}
}
}
abstract contract DexUserViews is Variables, Structs, DexConstantsViews, DexPublicViews {
/// @notice Get user supply data for a specific DEX and user
/// @param dex_ The address of the DEX
/// @param user_ The address of the user
/// @return userSupplyData_ Struct containing user supply data
function getUserSupplyData(
address dex_,
address user_
) public view returns (UserSupplyData memory userSupplyData_) {
uint256 userSupply_ = getUserSupplyDataRaw(dex_, user_);
if (userSupply_ > 0) {
// if userSupply_ == 0 -> user not configured yet
userSupplyData_.isAllowed = userSupply_ & 1 == 1;
userSupplyData_.supply = BigMathMinified.fromBigNumber(
(userSupply_ >> DexSlotsLink.BITS_USER_SUPPLY_AMOUNT) & DexCalcs.X64,
DexCalcs.DEFAULT_EXPONENT_SIZE,
DexCalcs.DEFAULT_EXPONENT_MASK
);
// get updated expanded withdrawal limit
userSupplyData_.withdrawalLimit = DexCalcs.calcWithdrawalLimitBeforeOperate(
userSupply_,
userSupplyData_.supply
);
userSupplyData_.lastUpdateTimestamp =
(userSupply_ >> DexSlotsLink.BITS_USER_SUPPLY_LAST_UPDATE_TIMESTAMP) &
DexCalcs.X33;
userSupplyData_.expandPercent =
(userSupply_ >> DexSlotsLink.BITS_USER_SUPPLY_EXPAND_PERCENT) &
DexCalcs.X14;
userSupplyData_.expandDuration =
(userSupply_ >> DexSlotsLink.BITS_USER_SUPPLY_EXPAND_DURATION) &
DexCalcs.X24;
userSupplyData_.baseWithdrawalLimit = BigMathMinified.fromBigNumber(
(userSupply_ >> DexSlotsLink.BITS_USER_SUPPLY_BASE_WITHDRAWAL_LIMIT) & DexCalcs.X18,
DexCalcs.DEFAULT_EXPONENT_SIZE,
DexCalcs.DEFAULT_EXPONENT_MASK
);
userSupplyData_.withdrawableUntilLimit = userSupplyData_.supply > userSupplyData_.withdrawalLimit
? userSupplyData_.supply - userSupplyData_.withdrawalLimit
: 0;
userSupplyData_.withdrawable = userSupplyData_.withdrawableUntilLimit;
(address token0_, address token1_) = getDexTokens(dex_);
(userSupplyData_.liquidityUserSupplyDataToken0, userSupplyData_.liquidityTokenData0) = LIQUIDITY_RESOLVER
.getUserSupplyData(dex_, token0_);
(userSupplyData_.liquidityUserSupplyDataToken1, userSupplyData_.liquidityTokenData1) = LIQUIDITY_RESOLVER
.getUserSupplyData(dex_, token1_);
}
}
/// @notice Get user supply data for multiple users in a specific DEX
/// @param dex_ The address of the DEX
/// @param users_ Array of user addresses
/// @return userSuppliesData_ Array of UserSupplyData structs for each user
function getUserSupplyDatas(
address dex_,
address[] calldata users_
) public view returns (UserSupplyData[] memory userSuppliesData_) {
uint256 length_ = users_.length;
userSuppliesData_ = new UserSupplyData[](length_);
for (uint256 i; i < length_; i++) {
(userSuppliesData_[i]) = getUserSupplyData(dex_, users_[i]);
}
}
/// @notice Get user borrow data for a specific DEX and user
/// @param dex_ The address of the DEX
/// @param user_ The address of the user
/// @return userBorrowData_ Struct containing user borrow data
function getUserBorrowData(
address dex_,
address user_
) public view returns (UserBorrowData memory userBorrowData_) {
uint256 userBorrow_ = getUserBorrowDataRaw(dex_, user_);
if (userBorrow_ > 0) {
// if userBorrow_ == 0 -> user not configured yet
userBorrowData_.isAllowed = userBorrow_ & 1 == 1;
userBorrowData_.borrow = BigMathMinified.fromBigNumber(
(userBorrow_ >> DexSlotsLink.BITS_USER_BORROW_AMOUNT) & DexCalcs.X64,
DexCalcs.DEFAULT_EXPONENT_SIZE,
DexCalcs.DEFAULT_EXPONENT_MASK
);
// get updated expanded borrow limit
userBorrowData_.borrowLimit = DexCalcs.calcBorrowLimitBeforeOperate(userBorrow_, userBorrowData_.borrow);
userBorrowData_.lastUpdateTimestamp =
(userBorrow_ >> DexSlotsLink.BITS_USER_BORROW_LAST_UPDATE_TIMESTAMP) &
DexCalcs.X33;
userBorrowData_.expandPercent =
(userBorrow_ >> DexSlotsLink.BITS_USER_BORROW_EXPAND_PERCENT) &
DexCalcs.X14;
userBorrowData_.expandDuration =
(userBorrow_ >> DexSlotsLink.BITS_USER_BORROW_EXPAND_DURATION) &
DexCalcs.X24;
userBorrowData_.baseBorrowLimit = BigMathMinified.fromBigNumber(
(userBorrow_ >> DexSlotsLink.BITS_USER_BORROW_BASE_BORROW_LIMIT) & DexCalcs.X18,
DexCalcs.DEFAULT_EXPONENT_SIZE,
DexCalcs.DEFAULT_EXPONENT_MASK
);
userBorrowData_.maxBorrowLimit = BigMathMinified.fromBigNumber(
(userBorrow_ >> DexSlotsLink.BITS_USER_BORROW_MAX_BORROW_LIMIT) & DexCalcs.X18,
DexCalcs.DEFAULT_EXPONENT_SIZE,
DexCalcs.DEFAULT_EXPONENT_MASK
);
userBorrowData_.borrowableUntilLimit = userBorrowData_.borrowLimit > userBorrowData_.borrow
? userBorrowData_.borrowLimit - userBorrowData_.borrow
: 0;
userBorrowData_.borrowable = userBorrowData_.borrowableUntilLimit;
(address token0_, address token1_) = getDexTokens(dex_);
(userBorrowData_.liquidityUserBorrowDataToken0, userBorrowData_.liquidityTokenData0) = LIQUIDITY_RESOLVER
.getUserBorrowData(dex_, token0_);
(userBorrowData_.liquidityUserBorrowDataToken1, userBorrowData_.liquidityTokenData1) = LIQUIDITY_RESOLVER
.getUserBorrowData(dex_, token1_);
}
}
/// @notice Get user borrow data for multiple users in a specific DEX
/// @param dex_ The address of the DEX
/// @param users_ Array of user addresses
/// @return userBorrowingsData_ Array of UserBorrowData structs for each user
function getUserBorrowDatas(
address dex_,
address[] calldata users_
) public view returns (UserBorrowData[] memory userBorrowingsData_) {
uint256 length_ = users_.length;
userBorrowingsData_ = new UserBorrowData[](length_);
for (uint256 i; i < length_; i++) {
(userBorrowingsData_[i]) = getUserBorrowData(dex_, users_[i]);
}
}
/// @notice Get both user supply and borrow data for multiple users in a specific DEX
/// @param dex_ The address of the DEX
/// @param users_ Array of user addresses
/// @return userSuppliesData_ Array of UserSupplyData structs for each user
/// @return userBorrowingsData_ Array of UserBorrowData structs for each user
function getUserBorrowSupplyDatas(
address dex_,
address[] calldata users_
) public view returns (UserSupplyData[] memory userSuppliesData_, UserBorrowData[] memory userBorrowingsData_) {
uint256 length_ = users_.length;
userSuppliesData_ = new UserSupplyData[](length_);
userBorrowingsData_ = new UserBorrowData[](length_);
for (uint256 i; i < length_; i++) {
(userSuppliesData_[i]) = getUserSupplyData(dex_, users_[i]);
(userBorrowingsData_[i]) = getUserBorrowData(dex_, users_[i]);
}
}
}
/// @notice Fluid Dex protocol resolver
/// Implements various view-only methods to give easy access to Dex protocol data.
contract FluidDexResolver is Variables, DexFactoryViews, DexActionEstimates, DexUserViews {
constructor(
address factory_,
address liquidity_,
address liquidityResolver_,
address deployer_
) Variables(factory_, liquidity_, liquidityResolver_, deployer_) {}
/// @notice Get the current state of a DEX
/// @param dex_ The address of the DEX
/// @return state_ A struct containing the current state of the DEX
/// @dev expected to be called via callStatic
function getDexState(address dex_) public returns (DexState memory state_) {
return _getDexState(dex_, getDexCollateralReserves(dex_), getDexDebtReserves(dex_));
}
/// @notice Get the current configurations of a DEX
/// @param dex_ The address of the DEX
/// @return configs_ A struct containing the current configurations of the DEX
function getDexConfigs(address dex_) public view returns (Configs memory configs_) {
uint256 dexVariables2_ = getDexVariables2Raw(dex_);
configs_.isSmartCollateralEnabled = (dexVariables2_ & 1) == 1;
configs_.isSmartDebtEnabled = (dexVariables2_ & 2) == 2;
configs_.fee = (dexVariables2_ >> 2) & X17;
configs_.revenueCut = (dexVariables2_ >> 19) & X7;
configs_.upperRange = (dexVariables2_ >> 27) & X20;
configs_.lowerRange = (dexVariables2_ >> 47) & X20;
configs_.upperShiftThreshold = (dexVariables2_ >> 68) & X10;
configs_.lowerShiftThreshold = (dexVariables2_ >> 78) & X10;
configs_.shiftingTime = (dexVariables2_ >> 88) & X24;
configs_.maxSupplyShares = getTotalSupplySharesRaw(dex_) >> 128;
configs_.maxBorrowShares = getTotalBorrowSharesRaw(dex_) >> 128;
uint256 addressNonce_ = (dexVariables2_ >> 112) & X30;
if (addressNonce_ > 0) {
configs_.centerPriceAddress = AddressCalcs.addressCalc(DEPLOYER_CONTRACT, addressNonce_);
}
addressNonce_ = (dexVariables2_ >> 142) & X30;
if (addressNonce_ > 0) {
configs_.hookAddress = AddressCalcs.addressCalc(DEPLOYER_CONTRACT, addressNonce_);
}
configs_.maxCenterPrice = BigMathMinified.fromBigNumber(
(dexVariables2_ >> 172) & X28,
DexCalcs.DEFAULT_EXPONENT_SIZE,
DexCalcs.DEFAULT_EXPONENT_MASK
);
configs_.minCenterPrice = BigMathMinified.fromBigNumber(
(dexVariables2_ >> 200) & X28,
DexCalcs.DEFAULT_EXPONENT_SIZE,
DexCalcs.DEFAULT_EXPONENT_MASK
);
configs_.utilizationLimitToken0 = (dexVariables2_ >> 228) & X10;
configs_.utilizationLimitToken1 = (dexVariables2_ >> 238) & X10;
}
/// @notice Get the swap limits and availability for a DEX
/// @param dex_ The address of the DEX
/// @return limitsAndAvailability_ A struct containing the swap limits and availability for the DEX
function getDexSwapLimitsAndAvailability(
address dex_
) public view returns (SwapLimitsAndAvailability memory limitsAndAvailability_) {
(address token0_, address token1_) = getDexTokens(dex_);
uint256 dexVariables2_ = getDexVariables2Raw(dex_);
uint256 utilizationLimitToken0_ = (dexVariables2_ >> 228) & X10;
uint256 utilizationLimitToken1_ = (dexVariables2_ >> 238) & X10;
return
_getDexSwapLimitsAndAvailability(dex_, token0_, token1_, utilizationLimitToken0_, utilizationLimitToken1_);
}
/// @notice Get the entire data for a DEX
/// @param dex_ The address of the DEX
/// @return data_ A struct containing all the data for the DEX
/// @dev expected to be called via callStatic
function getDexEntireData(address dex_) public returns (DexEntireData memory data_) {
data_.dex = dex_;
data_.constantViews = getDexConstantsView(dex_);
data_.constantViews2 = getDexConstantsView2(dex_);
data_.configs = getDexConfigs(dex_);
data_.pex = getDexPricesAndExchangePrices(dex_);
data_.colReserves = _getDexCollateralReserves(dex_, data_.constantViews2);
data_.debtReserves = _getDexDebtReserves(dex_, data_.constantViews2);
data_.dexState = _getDexState(dex_, data_.colReserves, data_.debtReserves);
data_.limitsAndAvailability = _getDexSwapLimitsAndAvailability(
dex_,
data_.constantViews.token0,
data_.constantViews.token1,
data_.configs.utilizationLimitToken0,
data_.configs.utilizationLimitToken1
);
}
/// @notice Get the entire data for multiple DEXes
/// @param dexes_ An array of DEX addresses
/// @return datas_ An array of structs containing all the data for each DEX
/// @dev expected to be called via callStatic
function getDexEntireDatas(address[] memory dexes_) public returns (DexEntireData[] memory datas_) {
uint256 length_ = dexes_.length;
datas_ = new DexEntireData[](length_);
for (uint256 i; i < length_; i++) {
datas_[i] = getDexEntireData(dexes_[i]);
}
}
/// @notice Get the entire data for all DEXes
/// @return datas_ An array of structs containing all the data for all DEXes
/// @dev expected to be called via callStatic
function getAllDexEntireDatas() external returns (DexEntireData[] memory datas_) {
return getDexEntireDatas(getAllDexAddresses());
}
/// @dev get the swap limits and availability for a DEX
/// @param dex_ The address of the DEX
/// @param token0_ The address of token0
/// @param token1_ The address of token1
/// @param utilizationLimitToken0Percent_ The utilization limit percentage for token0
/// @param utilizationLimitToken1Percent_ The utilization limit percentage for token1
/// @return limitsAndAvailability_ A struct containing the swap limits and availability for the DEX
function _getDexSwapLimitsAndAvailability(
address dex_,
address token0_,
address token1_,
uint256 utilizationLimitToken0Percent_,
uint256 utilizationLimitToken1Percent_
) internal view returns (SwapLimitsAndAvailability memory limitsAndAvailability_) {
(
limitsAndAvailability_.liquidityUserSupplyDataToken0,
limitsAndAvailability_.liquidityTokenData0
) = LIQUIDITY_RESOLVER.getUserSupplyData(dex_, token0_);
(
limitsAndAvailability_.liquidityUserSupplyDataToken1,
limitsAndAvailability_.liquidityTokenData1
) = LIQUIDITY_RESOLVER.getUserSupplyData(dex_, token1_);
(limitsAndAvailability_.liquidityUserBorrowDataToken0, ) = LIQUIDITY_RESOLVER.getUserBorrowData(dex_, token0_);
(limitsAndAvailability_.liquidityUserBorrowDataToken1, ) = LIQUIDITY_RESOLVER.getUserBorrowData(dex_, token1_);
limitsAndAvailability_.liquiditySupplyToken0 = limitsAndAvailability_.liquidityTokenData0.totalSupply;
limitsAndAvailability_.liquiditySupplyToken1 = limitsAndAvailability_.liquidityTokenData1.totalSupply;
limitsAndAvailability_.liquidityBorrowToken0 = limitsAndAvailability_.liquidityTokenData0.totalBorrow;
limitsAndAvailability_.liquidityBorrowToken1 = limitsAndAvailability_.liquidityTokenData1.totalBorrow;
limitsAndAvailability_.liquidityWithdrawableToken0 = limitsAndAvailability_
.liquidityUserSupplyDataToken0
.withdrawable;
limitsAndAvailability_.liquidityWithdrawableToken1 = limitsAndAvailability_
.liquidityUserSupplyDataToken1
.withdrawable;
limitsAndAvailability_.liquidityBorrowableToken0 = limitsAndAvailability_
.liquidityUserBorrowDataToken0
.borrowable;
limitsAndAvailability_.liquidityBorrowableToken1 = limitsAndAvailability_
.liquidityUserBorrowDataToken1
.borrowable;
limitsAndAvailability_.utilizationLimitToken0 =
(limitsAndAvailability_.liquiditySupplyToken0 * utilizationLimitToken0Percent_) /
1e3;
limitsAndAvailability_.utilizationLimitToken1 =
(limitsAndAvailability_.liquiditySupplyToken1 * utilizationLimitToken1Percent_) /
1e3;
if (limitsAndAvailability_.liquidityBorrowToken0 < limitsAndAvailability_.utilizationLimitToken0) {
limitsAndAvailability_.withdrawableUntilUtilizationLimitToken0 =
(1e3 * limitsAndAvailability_.liquidityBorrowToken0) /
utilizationLimitToken0Percent_;
limitsAndAvailability_.withdrawableUntilUtilizationLimitToken0 = limitsAndAvailability_
.liquiditySupplyToken0 > limitsAndAvailability_.withdrawableUntilUtilizationLimitToken0
? limitsAndAvailability_.liquiditySupplyToken0 -
limitsAndAvailability_.withdrawableUntilUtilizationLimitToken0
: 0;
limitsAndAvailability_.borrowableUntilUtilizationLimitToken0 =
limitsAndAvailability_.utilizationLimitToken0 -
limitsAndAvailability_.liquidityBorrowToken0;
}
if (limitsAndAvailability_.liquidityBorrowToken1 < limitsAndAvailability_.utilizationLimitToken1) {
limitsAndAvailability_.withdrawableUntilUtilizationLimitToken1 =
(1e3 * limitsAndAvailability_.liquidityBorrowToken1) /
utilizationLimitToken1Percent_;
limitsAndAvailability_.withdrawableUntilUtilizationLimitToken1 = limitsAndAvailability_
.liquiditySupplyToken1 > limitsAndAvailability_.withdrawableUntilUtilizationLimitToken1
? limitsAndAvailability_.liquiditySupplyToken1 -
limitsAndAvailability_.withdrawableUntilUtilizationLimitToken1
: 0;
limitsAndAvailability_.borrowableUntilUtilizationLimitToken1 =
limitsAndAvailability_.utilizationLimitToken1 -
limitsAndAvailability_.liquidityBorrowToken1;
}
}
/// @dev Get the current state of a DEX
function _getDexState(
address dex_,
IFluidDexT1.CollateralReserves memory colReserves_,
IFluidDexT1.DebtReserves memory debtReserves_
) internal view returns (DexState memory state_) {
uint256 storageVar_ = getDexVariablesRaw(dex_);
/// Next 40 bits => 1-40 => last to last stored price. BigNumber (32 bits precision, 8 bits exponent)
/// Next 40 bits => 41-80 => last stored price of pool. BigNumber (32 bits precision, 8 bits exponent)
/// Next 40 bits => 81-120 => center price. Center price from where the ranges will be calculated. BigNumber (32 bits precision, 8 bits exponent)
state_.lastToLastStoredPrice = (storageVar_ >> 1) & X40;
state_.lastToLastStoredPrice = (state_.lastToLastStoredPrice >> 8) << (state_.lastToLastStoredPrice & X8);
state_.lastStoredPrice = (storageVar_ >> 41) & X40;
state_.lastStoredPrice = (state_.lastStoredPrice >> 8) << (state_.lastStoredPrice & X8);
state_.centerPrice = (storageVar_ >> 81) & X40;
state_.centerPrice = (state_.centerPrice >> 8) << (state_.centerPrice & X8);
state_.lastUpdateTimestamp = (storageVar_ >> 121) & X33;
state_.lastPricesTimeDiff = (storageVar_ >> 154) & X22;
state_.oracleCheckPoint = (storageVar_ >> 176) & X3;
state_.oracleMapping = (storageVar_ >> 179) & X16;
state_.totalSupplyShares = getTotalSupplySharesRaw(dex_) & X128;
state_.totalBorrowShares = getTotalBorrowSharesRaw(dex_) & X128;
storageVar_ = getDexVariables2Raw(dex_);
state_.isSwapAndArbitragePaused = storageVar_ >> 255 == 1;
state_.shifts.isRangeChangeActive = (storageVar_ >> 26) & 1 == 1;
state_.shifts.isThresholdChangeActive = (storageVar_ >> 67) & 1 == 1;
state_.shifts.isCenterPriceShiftActive = (storageVar_ >> 248) & 1 == 1;
storageVar_ = getRangeShiftRaw(dex_);
state_.shifts.rangeShift.oldUpper = storageVar_ & X20;
state_.shifts.rangeShift.oldLower = (storageVar_ >> 20) & X20;
state_.shifts.rangeShift.duration = (storageVar_ >> 40) & X20;
state_.shifts.rangeShift.startTimestamp = (storageVar_ >> 60) & X33;
storageVar_ = getThresholdShiftRaw(dex_);
state_.shifts.thresholdShift.oldUpper = storageVar_ & X10;
state_.shifts.thresholdShift.oldLower = (storageVar_ >> 20) & X10;
state_.shifts.thresholdShift.duration = (storageVar_ >> 40) & X20;
state_.shifts.thresholdShift.startTimestamp = (storageVar_ >> 60) & X33;
state_.shifts.thresholdShift.oldTime = (storageVar_ >> 93) & X24;
storageVar_ = getCenterPriceShiftRaw(dex_);
state_.shifts.centerPriceShift.startTimestamp = storageVar_ & X33;
state_.shifts.centerPriceShift.shiftPercentage = (storageVar_ >> 33) & X20;
state_.shifts.centerPriceShift.duration = (storageVar_ >> 53) & X20;
if (state_.totalSupplyShares > 0) {
state_.token0PerSupplyShare = (colReserves_.token0RealReserves * 1e18) / state_.totalSupplyShares;
state_.token1PerSupplyShare = (colReserves_.token1RealReserves * 1e18) / state_.totalSupplyShares;
}
if (state_.totalBorrowShares > 0) {
state_.token0PerBorrowShare = (debtReserves_.token0Debt * 1e18) / state_.totalBorrowShares;
state_.token1PerBorrowShare = (debtReserves_.token1Debt * 1e18) / state_.totalBorrowShares;
}
}
}
contracts/periphery/resolvers/dex/structs.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
import { IFluidDexT1 } from "../../../protocols/dex/interfaces/iDexT1.sol";
import { Structs as FluidLiquidityResolverStructs } from "../liquidity/structs.sol";
abstract contract Structs {
struct DexState {
uint256 lastToLastStoredPrice;
uint256 lastStoredPrice; // price of pool after the most recent swap
uint256 centerPrice;
uint256 lastUpdateTimestamp;
uint256 lastPricesTimeDiff;
uint256 oracleCheckPoint;
uint256 oracleMapping;
uint256 totalSupplyShares;
uint256 totalBorrowShares;
bool isSwapAndArbitragePaused; // if true, only perfect functions will be usable
ShiftChanges shifts;
// below values have to be combined with Oracle price data at the VaultResolver
uint256 token0PerSupplyShare; // token0 amount per 1e18 supply shares
uint256 token1PerSupplyShare; // token1 amount per 1e18 supply shares
uint256 token0PerBorrowShare; // token0 amount per 1e18 borrow shares
uint256 token1PerBorrowShare; // token1 amount per 1e18 borrow shares
}
struct ShiftData {
uint256 oldUpper;
uint256 oldLower;
uint256 duration;
uint256 startTimestamp;
uint256 oldTime; // only for thresholdShift
}
struct CenterPriceShift {
uint256 shiftPercentage;
uint256 duration;
uint256 startTimestamp;
}
struct ShiftChanges {
bool isRangeChangeActive;
bool isThresholdChangeActive;
bool isCenterPriceShiftActive;
ShiftData rangeShift;
ShiftData thresholdShift;
CenterPriceShift centerPriceShift;
}
struct Configs {
bool isSmartCollateralEnabled;
bool isSmartDebtEnabled;
uint256 fee;
uint256 revenueCut;
uint256 upperRange;
uint256 lowerRange;
uint256 upperShiftThreshold;
uint256 lowerShiftThreshold;
uint256 shiftingTime;
address centerPriceAddress;
address hookAddress;
uint256 maxCenterPrice;
uint256 minCenterPrice;
uint256 utilizationLimitToken0;
uint256 utilizationLimitToken1;
uint256 maxSupplyShares;
uint256 maxBorrowShares;
}
// @dev note there might be other things that act as effective limits which are not fully considered here.
// e.g. such as maximum 5% oracle shift in one swap, withdraws & borrowing together affecting each other,
// shares being below max supply / borrow shares etc.
struct SwapLimitsAndAvailability {
// liquidity total amounts
uint liquiditySupplyToken0;
uint liquiditySupplyToken1;
uint liquidityBorrowToken0;
uint liquidityBorrowToken1;
// liquidity limits
uint liquidityWithdrawableToken0;
uint liquidityWithdrawableToken1;
uint liquidityBorrowableToken0;
uint liquidityBorrowableToken1;
// utilization limits based on config at Dex. (e.g. liquiditySupplyToken0 * Configs.utilizationLimitToken0 / 1e3)
uint utilizationLimitToken0;
uint utilizationLimitToken1;
// swappable amounts until utilization limit.
// In a swap that does both withdraw and borrow, the effective amounts might be less because withdraw / borrow affect each other
// (both increase utilization).
uint withdrawableUntilUtilizationLimitToken0; // x = totalSupply - totalBorrow / maxUtilizationPercentage
uint withdrawableUntilUtilizationLimitToken1;
uint borrowableUntilUtilizationLimitToken0; // x = maxUtilizationPercentage * totalSupply - totalBorrow.
uint borrowableUntilUtilizationLimitToken1;
// additional liquidity related data such as supply amount, limits, expansion etc.
FluidLiquidityResolverStructs.UserSupplyData liquidityUserSupplyDataToken0;
FluidLiquidityResolverStructs.UserSupplyData liquidityUserSupplyDataToken1;
// additional liquidity related data such as borrow amount, limits, expansion etc.
FluidLiquidityResolverStructs.UserBorrowData liquidityUserBorrowDataToken0;
FluidLiquidityResolverStructs.UserBorrowData liquidityUserBorrowDataToken1;
// liquidity token related data
FluidLiquidityResolverStructs.OverallTokenData liquidityTokenData0;
FluidLiquidityResolverStructs.OverallTokenData liquidityTokenData1;
}
struct DexEntireData {
address dex;
IFluidDexT1.ConstantViews constantViews;
IFluidDexT1.ConstantViews2 constantViews2;
Configs configs;
IFluidDexT1.PricesAndExchangePrice pex;
IFluidDexT1.CollateralReserves colReserves;
IFluidDexT1.DebtReserves debtReserves;
DexState dexState;
SwapLimitsAndAvailability limitsAndAvailability;
}
// amounts are always in normal (for withInterest already multiplied with exchange price)
struct UserSupplyData {
bool isAllowed;
uint256 supply; // user supply amount/shares
// the withdrawal limit (e.g. if 10% is the limit, and 100M is supplied, it would be 90M)
uint256 withdrawalLimit;
uint256 lastUpdateTimestamp;
uint256 expandPercent; // withdrawal limit expand percent in 1e2
uint256 expandDuration; // withdrawal limit expand duration in seconds
uint256 baseWithdrawalLimit;
// the current actual max withdrawable amount (e.g. if 10% is the limit, and 100M is supplied, it would be 10M)
uint256 withdrawableUntilLimit;
uint256 withdrawable; // actual currently withdrawable amount (supply - withdrawal Limit) & considering balance
// liquidity related data such as supply amount, limits, expansion etc.
FluidLiquidityResolverStructs.UserSupplyData liquidityUserSupplyDataToken0;
FluidLiquidityResolverStructs.UserSupplyData liquidityUserSupplyDataToken1;
// liquidity token related data
FluidLiquidityResolverStructs.OverallTokenData liquidityTokenData0;
FluidLiquidityResolverStructs.OverallTokenData liquidityTokenData1;
}
// amounts are always in normal (for withInterest already multiplied with exchange price)
struct UserBorrowData {
bool isAllowed;
uint256 borrow; // user borrow amount/shares
uint256 borrowLimit;
uint256 lastUpdateTimestamp;
uint256 expandPercent;
uint256 expandDuration;
uint256 baseBorrowLimit;
uint256 maxBorrowLimit;
uint256 borrowableUntilLimit; // borrowable amount until any borrow limit (incl. max utilization limit)
uint256 borrowable; // actual currently borrowable amount (borrow limit - already borrowed) & considering balance, max utilization
// liquidity related data such as borrow amount, limits, expansion etc.
FluidLiquidityResolverStructs.UserBorrowData liquidityUserBorrowDataToken0;
FluidLiquidityResolverStructs.UserBorrowData liquidityUserBorrowDataToken1;
// liquidity token related data
FluidLiquidityResolverStructs.OverallTokenData liquidityTokenData0;
FluidLiquidityResolverStructs.OverallTokenData liquidityTokenData1;
}
}
contracts/periphery/resolvers/dex/variables.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
import { IFluidLiquidityResolver } from "../liquidity/iLiquidityResolver.sol";
import { IFluidDexFactory } from "../../../protocols/dex/interfaces/iDexFactory.sol";
interface IFluidLiquidity {
function readFromStorage(bytes32 slot_) external view returns (uint256 result_);
}
abstract contract Variables {
IFluidDexFactory public immutable FACTORY;
IFluidLiquidity public immutable LIQUIDITY;
IFluidLiquidityResolver public immutable LIQUIDITY_RESOLVER;
/// @dev Address of contract used for deploying center price & hook related contract
address public immutable DEPLOYER_CONTRACT;
uint256 internal constant X2 = 0x3;
uint256 internal constant X3 = 0x7;
uint256 internal constant X5 = 0x1f;
uint256 internal constant X7 = 0x7f;
uint256 internal constant X8 = 0xff;
uint256 internal constant X9 = 0x1ff;
uint256 internal constant X10 = 0x3ff;
uint256 internal constant X11 = 0x7ff;
uint256 internal constant X14 = 0x3fff;
uint256 internal constant X16 = 0xffff;
uint256 internal constant X17 = 0x1ffff;
uint256 internal constant X20 = 0xfffff;
uint256 internal constant X22 = 0x3fffff;
uint256 internal constant X23 = 0x7fffff;
uint256 internal constant X24 = 0xffffff;
uint256 internal constant X28 = 0xfffffff;
uint256 internal constant X30 = 0x3fffffff;
uint256 internal constant X32 = 0xffffffff;
uint256 internal constant X33 = 0x1ffffffff;
uint256 internal constant X40 = 0xffffffffff;
uint256 internal constant X64 = 0xffffffffffffffff;
uint256 internal constant X128 = 0xffffffffffffffffffffffffffffffff;
/// @dev address that is mapped to the chain native token
address internal constant NATIVE_TOKEN_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
constructor(address factory_, address liquidity_, address liquidityResolver_, address deployer_) {
FACTORY = IFluidDexFactory(factory_);
LIQUIDITY = IFluidLiquidity(liquidity_);
LIQUIDITY_RESOLVER = IFluidLiquidityResolver(liquidityResolver_);
DEPLOYER_CONTRACT = deployer_;
}
}
contracts/periphery/resolvers/liquidity/iLiquidityResolver.sol
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.21 <=0.8.29;
import { Structs as LiquidityStructs } from "../../../periphery/resolvers/liquidity/structs.sol";
interface IFluidLiquidityResolver {
/// @notice gets the `revenueAmount_` for a `token_`.
function getRevenue(address token_) external view returns (uint256 revenueAmount_);
/// @notice address of contract that gets sent the revenue. Configurable by governance
function getRevenueCollector() external view returns (address);
/// @notice Liquidity contract paused status: status = 1 -> normal. status = 2 -> paused.
function getStatus() external view returns (uint256);
/// @notice checks if `auth_` is an allowed auth on Liquidity.
/// Auths can set most config values. E.g. contracts that automate certain flows like e.g. adding a new fToken.
/// Governance can add/remove auths. Governance is auth by default.
function isAuth(address auth_) external view returns (uint256);
/// @notice checks if `guardian_` is an allowed Guardian on Liquidity.
/// Guardians can pause lower class users.
/// Governance can add/remove guardians. Governance is guardian by default.
function isGuardian(address guardian_) external view returns (uint256);
/// @notice gets user class for `user_`. Class defines which protocols can be paused by guardians.
/// Currently there are 2 classes: 0 can be paused by guardians. 1 cannot be paused by guardians.
/// New protocols are added as class 0 and will be upgraded to 1 over time.
function getUserClass(address user_) external view returns (uint256);
/// @notice gets exchangePricesAndConfig packed uint256 storage slot for `token_`.
function getExchangePricesAndConfig(address token_) external view returns (uint256);
/// @notice gets rateConfig packed uint256 storage slot for `token_`.
function getRateConfig(address token_) external view returns (uint256);
/// @notice gets totalAmounts packed uint256 storage slot for `token_`.
function getTotalAmounts(address token_) external view returns (uint256);
/// @notice gets configs2 packed uint256 storage slot for `token_`.
function getConfigs2(address token_) external view returns (uint256);
/// @notice gets userSupply data packed uint256 storage slot for `user_` and `token_`.
function getUserSupply(address user_, address token_) external view returns (uint256);
/// @notice gets userBorrow data packed uint256 storage slot for `user_` and `token_`.
function getUserBorrow(address user_, address token_) external view returns (uint256);
/// @notice returns all `listedTokens_` at the Liquidity contract. Once configured, a token can never be removed.
function listedTokens() external view returns (address[] memory listedTokens_);
/// @notice get the Rate config data `rateData_` for a `token_` compiled from the packed uint256 rateConfig storage slot
function getTokenRateData(address token_) external view returns (LiquidityStructs.RateData memory rateData_);
/// @notice get the Rate config datas `rateDatas_` for multiple `tokens_` compiled from the packed uint256 rateConfig storage slot
function getTokensRateData(
address[] calldata tokens_
) external view returns (LiquidityStructs.RateData[] memory rateDatas_);
/// @notice returns general data for `token_` such as rates, exchange prices, utilization, fee, total amounts etc.
function getOverallTokenData(
address token_
) external view returns (LiquidityStructs.OverallTokenData memory overallTokenData_);
/// @notice returns general data for multiple `tokens_` such as rates, exchange prices, utilization, fee, total amounts etc.
function getOverallTokensData(
address[] calldata tokens_
) external view returns (LiquidityStructs.OverallTokenData[] memory overallTokensData_);
/// @notice returns general data for all `listedTokens()` such as rates, exchange prices, utilization, fee, total amounts etc.
function getAllOverallTokensData()
external
view
returns (LiquidityStructs.OverallTokenData[] memory overallTokensData_);
/// @notice returns `user_` supply data and general data (such as rates, exchange prices, utilization, fee, total amounts etc.) for `token_`
function getUserSupplyData(
address user_,
address token_
)
external
view
returns (
LiquidityStructs.UserSupplyData memory userSupplyData_,
LiquidityStructs.OverallTokenData memory overallTokenData_
);
/// @notice returns `user_` supply data and general data (such as rates, exchange prices, utilization, fee, total amounts etc.) for multiple `tokens_`
function getUserMultipleSupplyData(
address user_,
address[] calldata tokens_
)
external
view
returns (
LiquidityStructs.UserSupplyData[] memory userSuppliesData_,
LiquidityStructs.OverallTokenData[] memory overallTokensData_
);
/// @notice returns `user_` borrow data and general data (such as rates, exchange prices, utilization, fee, total amounts etc.) for `token_`
function getUserBorrowData(
address user_,
address token_
)
external
view
returns (
LiquidityStructs.UserBorrowData memory userBorrowData_,
LiquidityStructs.OverallTokenData memory overallTokenData_
);
/// @notice returns `user_` borrow data and general data (such as rates, exchange prices, utilization, fee, total amounts etc.) for multiple `tokens_`
function getUserMultipleBorrowData(
address user_,
address[] calldata tokens_
)
external
view
returns (
LiquidityStructs.UserBorrowData[] memory userBorrowingsData_,
LiquidityStructs.OverallTokenData[] memory overallTokensData_
);
/// @notice returns `user_` supply data and general data (such as rates, exchange prices, utilization, fee, total amounts etc.) for multiple `supplyTokens_`
/// and returns `user_` borrow data and general data (such as rates, exchange prices, utilization, fee, total amounts etc.) for multiple `borrowTokens_`
function getUserMultipleBorrowSupplyData(
address user_,
address[] calldata supplyTokens_,
address[] calldata borrowTokens_
)
external
view
returns (
LiquidityStructs.UserSupplyData[] memory userSuppliesData_,
LiquidityStructs.OverallTokenData[] memory overallSupplyTokensData_,
LiquidityStructs.UserBorrowData[] memory userBorrowingsData_,
LiquidityStructs.OverallTokenData[] memory overallBorrowTokensData_
);
}
contracts/periphery/resolvers/liquidity/structs.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.21 <=0.8.29;
import { Structs as AdminModuleStructs } from "../../../liquidity/adminModule/structs.sol";
abstract contract Structs {
struct RateData {
uint256 version;
AdminModuleStructs.RateDataV1Params rateDataV1;
AdminModuleStructs.RateDataV2Params rateDataV2;
}
struct OverallTokenData {
uint256 borrowRate;
uint256 supplyRate;
uint256 fee; // revenue fee
uint256 lastStoredUtilization;
uint256 storageUpdateThreshold;
uint256 lastUpdateTimestamp;
uint256 supplyExchangePrice;
uint256 borrowExchangePrice;
uint256 supplyRawInterest;
uint256 supplyInterestFree;
uint256 borrowRawInterest;
uint256 borrowInterestFree;
uint256 totalSupply;
uint256 totalBorrow;
uint256 revenue;
uint256 maxUtilization; // maximum allowed utilization
RateData rateData;
}
// amounts are always in normal (for withInterest already multiplied with exchange price)
struct UserSupplyData {
bool modeWithInterest; // true if mode = with interest, false = without interest
uint256 supply; // user supply amount
// the withdrawal limit (e.g. if 10% is the limit, and 100M is supplied, it would be 90M)
uint256 withdrawalLimit;
uint256 lastUpdateTimestamp;
uint256 expandPercent; // withdrawal limit expand percent in 1e2
uint256 expandDuration; // withdrawal limit expand duration in seconds
uint256 baseWithdrawalLimit;
// the current actual max withdrawable amount (e.g. if 10% is the limit, and 100M is supplied, it would be 10M)
uint256 withdrawableUntilLimit;
uint256 withdrawable; // actual currently withdrawable amount (supply - withdrawal Limit) & considering balance
// internal decay limit values
uint256 decayEndTimestamp; // computed from last update timestamp + decay duration. when decay will fully end.
uint256 decayAmount; // as in storage adjusted for decayed time leftover
}
// amounts are always in normal (for withInterest already multiplied with exchange price)
struct UserBorrowData {
bool modeWithInterest; // true if mode = with interest, false = without interest
uint256 borrow; // user borrow amount
uint256 borrowLimit;
uint256 lastUpdateTimestamp;
uint256 expandPercent;
uint256 expandDuration;
uint256 baseBorrowLimit;
uint256 maxBorrowLimit;
uint256 borrowableUntilLimit; // borrowable amount until any borrow limit (incl. max utilization limit)
uint256 borrowable; // actual currently borrowable amount (borrow limit - already borrowed) & considering balance, max utilization
uint256 borrowLimitUtilization; // borrow limit for `maxUtilization`
}
}
contracts/periphery/resolvers/smartLending/structs.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
import { Structs as DexResolverStructs } from "../dex/structs.sol";
abstract contract Structs {
struct SmartLendingEntireData {
// address of the smart lending
address smartLending;
// The name of the SmartLending token.
string name;
// The symbol of the SmartLending token.
string symbol;
// The number of decimal places for the SmartLending token.
uint8 decimals;
// The total supply of the SmartLending token.
uint256 totalSupply;
// total dex shares according to smart lending exchange rate
uint256 totalUnderlyingShares;
// total token0 amount built with dex token0PerSupplyShare. (!!) this is not set when calling the view method
uint256 totalUnderlyingAssetsToken0;
// total token1 amount built with dex token1PerSupplyShare. (!!) this is not set when calling the view method
uint256 totalUnderlyingAssetsToken1;
// The address of the first token in the underlying dex pool.
address token0;
// The address of the second token in the underlying dex pool.
address token1;
// The address of the underlying dex pool.
address dex;
// The last timestamp when the exchange price was updated in storage.
uint256 lastTimestamp;
// The fee or reward rate for the SmartLending. If positive then rewards, if negative then fee. 1e6 = 100%, 1e4 = 1%, minimum 0.0001% fee or reward.
int256 feeOrReward;
// The current exchange price of the SmartLending updated to block.timestamp.
uint256 exchangePrice;
// The address of the rebalancer.
address rebalancer;
// exchange rate for x assets per 1 underlying pool share
uint256 assetsPerShare;
// exchange rate for x underlying pool shares per 1 SmartLending asset (=exchangePrice)
uint256 sharesPerAsset;
// The difference in balance for rebalancing. difference between the total smart lending shares on the DEX and the total smart lending shares calculated.
// A positive value indicates fees to collect, while a negative value indicates rewards to be rebalanced.
uint256 rebalanceDiff;
// structs fetched directly from DexResolver:
DexResolverStructs.DexEntireData dexEntireData; // (!!) this is not set when calling the view method
DexResolverStructs.UserSupplyData dexUserSupplyData; // supply data of the SmartLending at the dex
}
struct UserPosition {
address user;
uint256 smartLendingAssets; // ERC20 smart lending assets that the user owns
uint256 underlyingShares; // dex shares according to smart lending exchange rate
uint256 underlyingAssetsToken0; // position token0 amount built with dex token0PerSupplyShare. (!!) this is not set when calling the view method
uint256 underlyingAssetsToken1; // position token1 amount built with dex token1PerSupplyShare. (!!) this is not set when calling the view method
uint256 underlyingBalanceToken0; // token0 user balance
uint256 underlyingBalanceToken1; // token1 user balance
uint256 allowanceToken0; // allowance of token0 for user to the smartLending
uint256 allowanceToken1; // allowance of token1 for user to the smartLending
}
struct SmartLendingEntireDataUserPosition {
SmartLendingEntireData smartLendingEntireData;
UserPosition userPosition;
}
}
contracts/protocols/dex/error.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
import { Structs } from "./poolT1/coreModule/structs.sol";
abstract contract Error {
error FluidDexError(uint256 errorId_);
error FluidDexFactoryError(uint256 errorId);
/// @notice used to simulate swap to find the output amount
error FluidDexSwapResult(uint256 amountOut);
error FluidDexPerfectLiquidityOutput(uint256 token0Amt, uint token1Amt);
error FluidDexSingleTokenOutput(uint256 tokenAmt);
error FluidDexLiquidityOutput(uint256 shares_);
error FluidDexPricesAndExchangeRates(Structs.PricesAndExchangePrice pex_);
error FluidSmartLendingError(uint256 errorId_);
error FluidSmartLendingFactoryError(uint256 errorId_);
}
contracts/protocols/dex/errorTypes.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
library ErrorTypes {
/***********************************|
| DexT1 |
|__________________________________*/
/// @notice thrown at reentrancy
uint256 internal constant DexT1__AlreadyEntered = 51001;
uint256 internal constant DexT1__NotAnAuth = 51002;
uint256 internal constant DexT1__SmartColNotEnabled = 51003;
uint256 internal constant DexT1__SmartDebtNotEnabled = 51004;
uint256 internal constant DexT1__PoolNotInitialized = 51005;
uint256 internal constant DexT1__TokenReservesTooLow = 51006;
uint256 internal constant DexT1__EthAndAmountInMisMatch = 51007;
uint256 internal constant DexT1__EthSentForNonNativeSwap = 51008;
uint256 internal constant DexT1__NoSwapRoute = 51009;
uint256 internal constant DexT1__NotEnoughAmountOut = 51010;
uint256 internal constant DexT1__LiquidityLayerTokenUtilizationCapReached = 51011;
uint256 internal constant DexT1__HookReturnedFalse = 51012;
// Either user's config are not set or user is paused
uint256 internal constant DexT1__UserSupplyInNotOn = 51013;
// Either user's config are not set or user is paused
uint256 internal constant DexT1__UserDebtInNotOn = 51014;
// Thrown when contract asks for more token0 or token1 than what user's wants to give on deposit
uint256 internal constant DexT1__AboveDepositMax = 51015;
uint256 internal constant DexT1__MsgValueLowOnDepositOrPayback = 51016;
uint256 internal constant DexT1__WithdrawLimitReached = 51017;
// Thrown when contract gives less token0 or token1 than what user's wants on withdraw
uint256 internal constant DexT1__BelowWithdrawMin = 51018;
uint256 internal constant DexT1__DebtLimitReached = 51019;
// Thrown when contract gives less token0 or token1 than what user's wants on borrow
uint256 internal constant DexT1__BelowBorrowMin = 51020;
// Thrown when contract asks for more token0 or token1 than what user's wants on payback
uint256 internal constant DexT1__AbovePaybackMax = 51021;
uint256 internal constant DexT1__InvalidDepositAmts = 51022;
uint256 internal constant DexT1__DepositAmtsZero = 51023;
uint256 internal constant DexT1__SharesMintedLess = 51024;
uint256 internal constant DexT1__WithdrawalNotEnough = 51025;
uint256 internal constant DexT1__InvalidWithdrawAmts = 51026;
uint256 internal constant DexT1__WithdrawAmtsZero = 51027;
uint256 internal constant DexT1__WithdrawExcessSharesBurn = 51028;
uint256 internal constant DexT1__InvalidBorrowAmts = 51029;
uint256 internal constant DexT1__BorrowAmtsZero = 51030;
uint256 internal constant DexT1__BorrowExcessSharesMinted = 51031;
uint256 internal constant DexT1__PaybackAmtTooHigh = 51032;
uint256 internal constant DexT1__InvalidPaybackAmts = 51033;
uint256 internal constant DexT1__PaybackAmtsZero = 51034;
uint256 internal constant DexT1__PaybackSharedBurnedLess = 51035;
uint256 internal constant DexT1__NothingToArbitrage = 51036;
uint256 internal constant DexT1__MsgSenderNotLiquidity = 51037;
// On liquidity callback reentrancy bit should be on
uint256 internal constant DexT1__ReentrancyBitShouldBeOn = 51038;
// Thrown is reentrancy is already on and someone tries to fetch oracle price. Should not be possible to this
uint256 internal constant DexT1__OraclePriceFetchAlreadyEntered = 51039;
// Thrown when swap changes the current price by more than 5%
uint256 internal constant DexT1__OracleUpdateHugeSwapDiff = 51040;
uint256 internal constant DexT1__Token0ShouldBeSmallerThanToken1 = 51041;
uint256 internal constant DexT1__OracleMappingOverflow = 51042;
/// @notice thrown if governance has paused the swapping & arbitrage so only perfect functions are usable
uint256 internal constant DexT1__SwapAndArbitragePaused = 51043;
uint256 internal constant DexT1__ExceedsAmountInMax = 51044;
/// @notice thrown if amount in is too high or too low
uint256 internal constant DexT1__SwapInLimitingAmounts = 51045;
/// @notice thrown if amount out is too high or too low
uint256 internal constant DexT1__SwapOutLimitingAmounts = 51046;
uint256 internal constant DexT1__MintAmtOverflow = 51047;
uint256 internal constant DexT1__BurnAmtOverflow = 51048;
uint256 internal constant DexT1__LimitingAmountsSwapAndNonPerfectActions = 51049;
uint256 internal constant DexT1__InsufficientOracleData = 51050;
uint256 internal constant DexT1__SharesAmountInsufficient = 51051;
uint256 internal constant DexT1__CenterPriceOutOfRange = 51052;
uint256 internal constant DexT1__DebtReservesTooLow = 51053;
uint256 internal constant DexT1__SwapAndDepositTooLowOrTooHigh = 51054;
uint256 internal constant DexT1__WithdrawAndSwapTooLowOrTooHigh = 51055;
uint256 internal constant DexT1__BorrowAndSwapTooLowOrTooHigh = 51056;
uint256 internal constant DexT1__SwapAndPaybackTooLowOrTooHigh = 51057;
uint256 internal constant DexT1__InvalidImplementation = 51058;
uint256 internal constant DexT1__OnlyDelegateCallAllowed = 51059;
uint256 internal constant DexT1__IncorrectDataLength = 51060;
uint256 internal constant DexT1__AmountToSendLessThanAmount = 51061;
uint256 internal constant DexT1__InvalidCollateralReserves = 51062;
uint256 internal constant DexT1__InvalidDebtReserves = 51063;
uint256 internal constant DexT1__SupplySharesOverflow = 51064;
uint256 internal constant DexT1__BorrowSharesOverflow = 51065;
uint256 internal constant DexT1__OracleNotActive = 51066;
/***********************************|
| DEX Admin |
|__________________________________*/
/// @notice thrown when pool is not initialized
uint256 internal constant DexT1Admin__PoolNotInitialized = 52001;
uint256 internal constant DexT1Admin__SmartColIsAlreadyOn = 52002;
uint256 internal constant DexT1Admin__SmartDebtIsAlreadyOn = 52003;
/// @notice thrown when any of the configs value overflow the maximum limit
uint256 internal constant DexT1Admin__ConfigOverflow = 52004;
uint256 internal constant DexT1Admin__AddressNotAContract = 52005;
uint256 internal constant DexT1Admin__InvalidParams = 52006;
uint256 internal constant DexT1Admin__UserNotDefined = 52007;
uint256 internal constant DexT1Admin__OnlyDelegateCallAllowed = 52008;
uint256 internal constant DexT1Admin__UnexpectedPoolState = 52009;
/// @notice thrown when trying to pause or unpause but user is already in the target pause state
uint256 internal constant DexT1Admin__InvalidPauseToggle = 52009;
/***********************************|
| DEX Factory |
|__________________________________*/
uint256 internal constant DexFactory__InvalidOperation = 53001;
uint256 internal constant DexFactory__Unauthorized = 53002;
uint256 internal constant DexFactory__SameTokenNotAllowed = 53003;
uint256 internal constant DexFactory__TokenConfigNotProper = 53004;
uint256 internal constant DexFactory__InvalidParams = 53005;
uint256 internal constant DexFactory__OnlyDelegateCallAllowed = 53006;
uint256 internal constant DexFactory__InvalidDexAddress = 53007;
/***********************************|
| Smart Lending |
|__________________________________*/
uint256 internal constant SmartLending__ZeroAddress = 54001;
uint256 internal constant SmartLending__Unauthorized = 54002;
uint256 internal constant SmartLending__InvalidMsgValue = 54003;
uint256 internal constant SmartLending__OutOfRange = 54004;
uint256 internal constant SmartLending__InvalidRebalancer = 54005;
uint256 internal constant SmartLending__Reentrancy = 54006;
uint256 internal constant SmartLending__InvalidAmounts = 54007;
/***********************************|
| Smart Lending Factory |
|__________________________________*/
uint256 internal constant SmartLendingFactory__ZeroAddress = 55001;
uint256 internal constant SmartLendingFactory__Unauthorized = 55002;
uint256 internal constant SmartLendingFactory__AlreadyDeployed = 55003;
uint256 internal constant SmartLendingFactory__InvalidParams = 55004;
uint256 internal constant SmartLendingFactory__InvalidOperation = 55005;
}
contracts/protocols/dex/factory/main.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
import { Owned } from "solmate/src/auth/Owned.sol";
import { ErrorTypes } from "../errorTypes.sol";
import { Error } from "../error.sol";
import { AddressCalcs } from "../../../libraries/addressCalcs.sol";
import { StorageRead } from "../../../libraries/storageRead.sol";
abstract contract DexFactoryVariables is Owned, StorageRead, Error {
/*//////////////////////////////////////////////////////////////
STORAGE VARIABLES
//////////////////////////////////////////////////////////////*/
// ------------ storage variables from inherited contracts (Owned) come before vars here --------
// ----------------------- slot 0 ---------------------------
// address public owner; // from Owned
// 12 bytes empty
// ----------------------- slot 1 ---------------------------
/// @dev deployer can deploy new Dex Pool contract
/// owner can add/remove deployer.
/// Owner is deployer by default.
mapping(address => bool) internal _deployers;
// ----------------------- slot 2 ---------------------------
/// @dev global auths can update any dex pool config.
/// owner can add/remove global auths.
/// Owner is global auth by default.
mapping(address => bool) internal _globalAuths;
// ----------------------- slot 3 ---------------------------
/// @dev dex auths can update specific dex config.
/// owner can add/remove dex auths.
/// Owner is dex auth by default.
/// dex => auth => add/remove
mapping(address => mapping(address => bool)) internal _dexAuths;
// ----------------------- slot 4 ---------------------------
/// @dev total no of dexes deployed by the factory
/// only addresses that have deployer role or owner can deploy new dex pool.
uint256 internal _totalDexes;
// ----------------------- slot 5 ---------------------------
/// @dev dex deployment logics for deploying dex pool
/// These logic contracts hold the deployment logics of specific dexes and are called via .delegatecall inside deployDex().
/// only addresses that have owner can add/remove new dex deployment logic.
mapping(address => bool) internal _dexDeploymentLogics;
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor(address owner_) Owned(owner_) {}
}
abstract contract DexFactoryEvents {
/// @dev Emitted when a new dex is deployed.
/// @param dex The address of the newly deployed dex.
/// @param dexId The id of the newly deployed dex.
event LogDexDeployed(address indexed dex, uint256 indexed dexId);
/// @dev Emitted when the deployer is modified by owner.
/// @param deployer Address whose deployer status is updated.
/// @param allowed Indicates whether the address is authorized as a deployer or not.
event LogSetDeployer(address indexed deployer, bool indexed allowed);
/// @dev Emitted when the globalAuth is modified by owner.
/// @param globalAuth Address whose globalAuth status is updated.
/// @param allowed Indicates whether the address is authorized as a deployer or not.
event LogSetGlobalAuth(address indexed globalAuth, bool indexed allowed);
/// @dev Emitted when the dexAuth is modified by owner.
/// @param dexAuth Address whose dexAuth status is updated.
/// @param allowed Indicates whether the address is authorized as a deployer or not.
/// @param dex Address of the specific dex related to the authorization change.
event LogSetDexAuth(address indexed dexAuth, bool indexed allowed, address indexed dex);
/// @dev Emitted when the dex deployment logic is modified by owner.
/// @param dexDeploymentLogic The address of the dex deployment logic contract.
/// @param allowed Indicates whether the address is authorized as a deployer or not.
event LogSetDexDeploymentLogic(address indexed dexDeploymentLogic, bool indexed allowed);
}
abstract contract DexFactoryCore is DexFactoryVariables, DexFactoryEvents {
constructor(address owner_) validAddress(owner_) DexFactoryVariables(owner_) {}
/// @dev validates that an address is not the zero address
modifier validAddress(address value_) {
if (value_ == address(0)) {
revert FluidDexFactoryError(ErrorTypes.DexFactory__InvalidParams);
}
_;
}
}
/// @dev Implements Dex Factory auth-only callable methods. Owner / auths can set various config values and
/// can define the allow-listed deployers.
abstract contract DexFactoryAuth is DexFactoryCore {
/// @notice Sets an address (`deployer_`) as allowed deployer or not.
/// This function can only be called by the owner.
/// @param deployer_ The address to be set as deployer.
/// @param allowed_ A boolean indicating whether the specified address is allowed to deploy dexes.
function setDeployer(address deployer_, bool allowed_) external onlyOwner validAddress(deployer_) {
_deployers[deployer_] = allowed_;
emit LogSetDeployer(deployer_, allowed_);
}
/// @notice Sets an address (`globalAuth_`) as a global authorization or not.
/// This function can only be called by the owner.
/// @param globalAuth_ The address to be set as global authorization.
/// @param allowed_ A boolean indicating whether the specified address is allowed to update any dex config.
function setGlobalAuth(address globalAuth_, bool allowed_) external onlyOwner validAddress(globalAuth_) {
_globalAuths[globalAuth_] = allowed_;
emit LogSetGlobalAuth(globalAuth_, allowed_);
}
/// @notice Sets an address (`dexAuth_`) as allowed dex authorization or not for a specific dex (`dex_`).
/// This function can only be called by the owner.
/// @param dex_ The address of the dex for which the authorization is being set.
/// @param dexAuth_ The address to be set as dex authorization.
/// @param allowed_ A boolean indicating whether the specified address is allowed to update the specific dex config.
function setDexAuth(address dex_, address dexAuth_, bool allowed_) external onlyOwner validAddress(dexAuth_) {
_dexAuths[dex_][dexAuth_] = allowed_;
emit LogSetDexAuth(dexAuth_, allowed_, dex_);
}
/// @notice Sets an address as allowed dex deployment logic (`deploymentLogic_`) contract or not.
/// This function can only be called by the owner.
/// @param deploymentLogic_ The address of the dex deployment logic contract to be set.
/// @param allowed_ A boolean indicating whether the specified address is allowed to deploy new type of dex.
function setDexDeploymentLogic(
address deploymentLogic_,
bool allowed_
) public onlyOwner validAddress(deploymentLogic_) {
_dexDeploymentLogics[deploymentLogic_] = allowed_;
emit LogSetDexDeploymentLogic(deploymentLogic_, allowed_);
}
/// @notice Spell allows owner aka governance to do any arbitrary call on factory
/// @param target_ Address to which the call needs to be delegated
/// @param data_ Data to execute at the delegated address
function spell(address target_, bytes memory data_) external onlyOwner returns (bytes memory response_) {
assembly {
let succeeded := delegatecall(gas(), target_, add(data_, 0x20), mload(data_), 0, 0)
let size := returndatasize()
response_ := mload(0x40)
mstore(0x40, add(response_, and(add(add(size, 0x20), 0x1f), not(0x1f))))
mstore(response_, size)
returndatacopy(add(response_, 0x20), 0, size)
switch iszero(succeeded)
case 1 {
// throw if delegatecall failed
returndatacopy(0x00, 0x00, size)
revert(0x00, size)
}
}
}
/// @notice Checks if the provided address (`deployer_`) is authorized as a deployer.
/// @param deployer_ The address to be checked for deployer authorization.
/// @return Returns `true` if the address is a deployer, otherwise `false`.
function isDeployer(address deployer_) public view returns (bool) {
return _deployers[deployer_] || owner == deployer_;
}
/// @notice Checks if the provided address (`globalAuth_`) has global dex authorization privileges.
/// @param globalAuth_ The address to be checked for global authorization privileges.
/// @return Returns `true` if the given address has global authorization privileges, otherwise `false`.
function isGlobalAuth(address globalAuth_) public view returns (bool) {
return _globalAuths[globalAuth_] || owner == globalAuth_;
}
/// @notice Checks if the provided address (`dexAuth_`) has dex authorization privileges for the specified dex (`dex_`).
/// @param dex_ The address of the dex to check.
/// @param dexAuth_ The address to be checked for dex authorization privileges.
/// @return Returns `true` if the given address has dex authorization privileges for the specified dex, otherwise `false`.
function isDexAuth(address dex_, address dexAuth_) public view returns (bool) {
return _dexAuths[dex_][dexAuth_] || owner == dexAuth_;
}
/// @notice Checks if the provided (`dexDeploymentLogic_`) address has authorization for dex deployment.
/// @param dexDeploymentLogic_ The address of the dex deploy logic to check for authorization privileges.
/// @return Returns `true` if the given address has authorization privileges for dex deployment, otherwise `false`.
function isDexDeploymentLogic(address dexDeploymentLogic_) public view returns (bool) {
return _dexDeploymentLogics[dexDeploymentLogic_];
}
}
/// @dev implements DexFactory deploy dex related methods.
abstract contract DexFactoryDeployment is DexFactoryCore, DexFactoryAuth {
/// @dev Deploys a contract using the CREATE opcode with the provided bytecode (`bytecode_`).
/// This is an internal function, meant to be used within the contract to facilitate the deployment of other contracts.
/// @param bytecode_ The bytecode of the contract to be deployed.
/// @return address_ Returns the address of the deployed contract.
function _deploy(bytes memory bytecode_) internal returns (address address_) {
if (bytecode_.length == 0) {
revert FluidDexError(ErrorTypes.DexFactory__InvalidOperation);
}
/// @solidity memory-safe-assembly
assembly {
address_ := create(0, add(bytecode_, 0x20), mload(bytecode_))
}
if (address_ == address(0)) {
revert FluidDexError(ErrorTypes.DexFactory__InvalidOperation);
}
}
/// @notice Deploys a new dex using the specified deployment logic `dexDeploymentLogic_` and data `dexDeploymentData_`.
/// Only accounts with deployer access or the owner can deploy a new dex.
/// @param dexDeploymentLogic_ The address of the dex deployment logic contract.
/// @param dexDeploymentData_ The data to be used for dex deployment.
/// @return dex_ Returns the address of the newly deployed dex.
function deployDex(address dexDeploymentLogic_, bytes calldata dexDeploymentData_) external returns (address dex_) {
// Revert if msg.sender doesn't have deployer access or is an owner.
if (!isDeployer(msg.sender)) revert FluidDexError(ErrorTypes.DexFactory__Unauthorized);
// Revert if dexDeploymentLogic_ is not whitelisted.
if (!isDexDeploymentLogic(dexDeploymentLogic_)) revert FluidDexError(ErrorTypes.DexFactory__Unauthorized);
// Dex ID for the new dex and also acts as `nonce` for CREATE
uint256 dexId_ = ++_totalDexes;
// compute dex address for dex id.
dex_ = getDexAddress(dexId_);
// deploy the dex using dex deployment logic by making .delegatecall
(bool success_, bytes memory data_) = dexDeploymentLogic_.delegatecall(dexDeploymentData_);
if (!(success_ && dex_ == _deploy(abi.decode(data_, (bytes))) && isDex(dex_))) {
revert FluidDexError(ErrorTypes.DexFactory__InvalidDexAddress);
}
emit LogDexDeployed(dex_, dexId_);
}
/// @notice Computes the address of a dex based on its given ID (`dexId_`).
/// @param dexId_ The ID of the dex.
/// @return dex_ Returns the computed address of the dex.
function getDexAddress(uint256 dexId_) public view returns (address dex_) {
return AddressCalcs.addressCalc(address(this), dexId_);
}
/// @notice Checks if a given address (`dex_`) corresponds to a valid dex.
/// @param dex_ The dex address to check.
/// @return Returns `true` if the given address corresponds to a valid dex, otherwise `false`.
function isDex(address dex_) public view returns (bool) {
if (dex_.code.length == 0) {
return false;
} else {
// DEX_ID() function signature is 0xf4b9a3fb
(bool success_, bytes memory data_) = dex_.staticcall(hex"f4b9a3fb");
return success_ && dex_ == getDexAddress(abi.decode(data_, (uint256)));
}
}
/// @notice Returns the total number of dexes deployed by the factory.
/// @return Returns the total number of dexes.
function totalDexes() external view returns (uint256) {
return _totalDexes;
}
}
/// @title Fluid DexFactory
/// @notice creates Fluid dex protocol dexes, which are interacting with Fluid Liquidity to deposit / borrow funds.
/// Dexes are created at a deterministic address, given an incrementing `dexId` (see `getDexAddress()`).
/// Dexes can only be deployed by allow-listed deployer addresses.
/// @dev Note the deployed dexes start out with no config at Liquidity contract.
/// This must be done by Liquidity auths in a separate step, otherwise no deposits will be possible.
/// This contract is not upgradeable. It supports adding new dex deployment logic contracts for new, future dexes.
contract FluidDexFactory is DexFactoryCore, DexFactoryAuth, DexFactoryDeployment {
constructor(address owner_) DexFactoryCore(owner_) {}
}
contracts/protocols/dex/interfaces/iDexFactory.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;
interface IFluidDexFactory {
/// @notice Global auth is auth for all dexes
function isGlobalAuth(address auth_) external view returns (bool);
/// @notice Dex auth is auth for a specific dex
function isDexAuth(address vault_, address auth_) external view returns (bool);
/// @notice Total dexes deployed.
function totalDexes() external view returns (uint256);
/// @notice Compute dexAddress
function getDexAddress(uint256 dexId_) external view returns (address);
/// @notice read uint256 `result_` for a storage `slot_` key
function readFromStorage(bytes32 slot_) external view returns (uint256 result_);
}
contracts/protocols/dex/interfaces/iDexT1.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;
interface IFluidDexT1 {
error FluidDexError(uint256 errorId);
/// @notice used to simulate swap to find the output amount
error FluidDexSwapResult(uint256 amountOut);
error FluidDexPerfectLiquidityOutput(uint256 token0Amt, uint token1Amt);
error FluidDexSingleTokenOutput(uint256 tokenAmt);
error FluidDexLiquidityOutput(uint256 shares);
error FluidDexPricesAndExchangeRates(PricesAndExchangePrice pex_);
/// @notice returns the dex id
function DEX_ID() external view returns (uint256);
/// @notice reads uint256 data `result_` from storage at a bytes32 storage `slot_` key.
function readFromStorage(bytes32 slot_) external view returns (uint256 result_);
struct Implementations {
address shift;
address admin;
address colOperations;
address debtOperations;
address perfectOperationsAndOracle;
}
struct ConstantViews {
uint256 dexId;
address liquidity;
address factory;
Implementations implementations;
address deployerContract;
address token0;
address token1;
bytes32 supplyToken0Slot;
bytes32 borrowToken0Slot;
bytes32 supplyToken1Slot;
bytes32 borrowToken1Slot;
bytes32 exchangePriceToken0Slot;
bytes32 exchangePriceToken1Slot;
uint256 oracleMapping;
}
struct ConstantViews2 {
uint token0NumeratorPrecision;
uint token0DenominatorPrecision;
uint token1NumeratorPrecision;
uint token1DenominatorPrecision;
}
struct PricesAndExchangePrice {
uint lastStoredPrice; // last stored price in 1e27 decimals
uint centerPrice; // last stored price in 1e27 decimals
uint upperRange; // price at upper range in 1e27 decimals
uint lowerRange; // price at lower range in 1e27 decimals
uint geometricMean; // geometric mean of upper range & lower range in 1e27 decimals
uint supplyToken0ExchangePrice;
uint borrowToken0ExchangePrice;
uint supplyToken1ExchangePrice;
uint borrowToken1ExchangePrice;
}
struct CollateralReserves {
uint token0RealReserves;
uint token1RealReserves;
uint token0ImaginaryReserves;
uint token1ImaginaryReserves;
}
struct DebtReserves {
uint token0Debt;
uint token1Debt;
uint token0RealReserves;
uint token1RealReserves;
uint token0ImaginaryReserves;
uint token1ImaginaryReserves;
}
function getCollateralReserves(
uint geometricMean_,
uint upperRange_,
uint lowerRange_,
uint token0SupplyExchangePrice_,
uint token1SupplyExchangePrice_
) external view returns (CollateralReserves memory c_);
function getDebtReserves(
uint geometricMean_,
uint upperRange_,
uint lowerRange_,
uint token0BorrowExchangePrice_,
uint token1BorrowExchangePrice_
) external view returns (DebtReserves memory d_);
// reverts with FluidDexPricesAndExchangeRates(pex_);
function getPricesAndExchangePrices() external;
function constantsView() external view returns (ConstantViews memory constantsView_);
function constantsView2() external view returns (ConstantViews2 memory constantsView2_);
struct Oracle {
uint twap1by0; // TWAP price
uint lowestPrice1by0; // lowest price point
uint highestPrice1by0; // highest price point
uint twap0by1; // TWAP price
uint lowestPrice0by1; // lowest price point
uint highestPrice0by1; // highest price point
}
/// @dev This function allows users to swap a specific amount of input tokens for output tokens
/// @param swap0to1_ Direction of swap. If true, swaps token0 for token1; if false, swaps token1 for token0
/// @param amountIn_ The exact amount of input tokens to swap
/// @param amountOutMin_ The minimum amount of output tokens the user is willing to accept
/// @param to_ Recipient of swapped tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with amountOut_
/// @return amountOut_ The amount of output tokens received from the swap
function swapIn(
bool swap0to1_,
uint256 amountIn_,
uint256 amountOutMin_,
address to_
) external payable returns (uint256 amountOut_);
/// @dev Swap tokens with perfect amount in and callback functionality
/// @param swap0to1_ Direction of swap. If true, swaps token0 for token1; if false, swaps token1 for token0
/// @param amountIn_ The exact amount of tokens to swap in
/// @param amountOutMin_ The minimum amount of tokens to receive after swap
/// @param to_ Recipient of swapped tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with amountOut_
/// @return amountOut_ The amount of output tokens received from the swap
function swapInWithCallback(
bool swap0to1_,
uint256 amountIn_,
uint256 amountOutMin_,
address to_
) external payable returns (uint256 amountOut_);
/// @dev Swap tokens with perfect amount out
/// @param swap0to1_ Direction of swap. If true, swaps token0 for token1; if false, swaps token1 for token0
/// @param amountOut_ The exact amount of tokens to receive after swap
/// @param amountInMax_ Maximum amount of tokens to swap in
/// @param to_ Recipient of swapped tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with amountIn_
/// @return amountIn_ The amount of input tokens used for the swap
function swapOut(
bool swap0to1_,
uint256 amountOut_,
uint256 amountInMax_,
address to_
) external payable returns (uint256 amountIn_);
/// @dev Swap tokens with perfect amount out and callback functionality
/// @param swap0to1_ Direction of swap. If true, swaps token0 for token1; if false, swaps token1 for token0
/// @param amountOut_ The exact amount of tokens to receive after swap
/// @param amountInMax_ Maximum amount of tokens to swap in
/// @param to_ Recipient of swapped tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with amountIn_
/// @return amountIn_ The amount of input tokens used for the swap
function swapOutWithCallback(
bool swap0to1_,
uint256 amountOut_,
uint256 amountInMax_,
address to_
) external payable returns (uint256 amountIn_);
/// @dev Deposit tokens in equal proportion to the current pool ratio
/// @param shares_ The number of shares to mint
/// @param maxToken0Deposit_ Maximum amount of token0 to deposit
/// @param maxToken1Deposit_ Maximum amount of token1 to deposit
/// @param estimate_ If true, function will revert with estimated deposit amounts without executing the deposit
/// @return token0Amt_ Amount of token0 deposited
/// @return token1Amt_ Amount of token1 deposited
function depositPerfect(
uint shares_,
uint maxToken0Deposit_,
uint maxToken1Deposit_,
bool estimate_
) external payable returns (uint token0Amt_, uint token1Amt_);
/// @dev This function allows users to withdraw a perfect amount of collateral liquidity
/// @param shares_ The number of shares to withdraw
/// @param minToken0Withdraw_ The minimum amount of token0 the user is willing to accept
/// @param minToken1Withdraw_ The minimum amount of token1 the user is willing to accept
/// @param to_ Recipient of swapped tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with token0Amt_ & token1Amt_
/// @return token0Amt_ The amount of token0 withdrawn
/// @return token1Amt_ The amount of token1 withdrawn
function withdrawPerfect(
uint shares_,
uint minToken0Withdraw_,
uint minToken1Withdraw_,
address to_
) external returns (uint token0Amt_, uint token1Amt_);
/// @dev This function allows users to borrow tokens in equal proportion to the current debt pool ratio
/// @param shares_ The number of shares to borrow
/// @param minToken0Borrow_ Minimum amount of token0 to borrow
/// @param minToken1Borrow_ Minimum amount of token1 to borrow
/// @param to_ Recipient of swapped tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with token0Amt_ & token1Amt_
/// @return token0Amt_ Amount of token0 borrowed
/// @return token1Amt_ Amount of token1 borrowed
function borrowPerfect(
uint shares_,
uint minToken0Borrow_,
uint minToken1Borrow_,
address to_
) external returns (uint token0Amt_, uint token1Amt_);
/// @dev This function allows users to pay back borrowed tokens in equal proportion to the current debt pool ratio
/// @param shares_ The number of shares to pay back
/// @param maxToken0Payback_ Maximum amount of token0 to pay back
/// @param maxToken1Payback_ Maximum amount of token1 to pay back
/// @param estimate_ If true, function will revert with estimated payback amounts without executing the payback
/// @return token0Amt_ Amount of token0 paid back
/// @return token1Amt_ Amount of token1 paid back
function paybackPerfect(
uint shares_,
uint maxToken0Payback_,
uint maxToken1Payback_,
bool estimate_
) external payable returns (uint token0Amt_, uint token1Amt_);
/// @dev This function allows users to deposit tokens in any proportion into the col pool
/// @param token0Amt_ The amount of token0 to deposit
/// @param token1Amt_ The amount of token1 to deposit
/// @param minSharesAmt_ The minimum amount of shares the user expects to receive
/// @param estimate_ If true, function will revert with estimated shares without executing the deposit
/// @return shares_ The amount of shares minted for the deposit
function deposit(
uint token0Amt_,
uint token1Amt_,
uint minSharesAmt_,
bool estimate_
) external payable returns (uint shares_);
/// @dev This function allows users to withdraw tokens in any proportion from the col pool
/// @param token0Amt_ The amount of token0 to withdraw
/// @param token1Amt_ The amount of token1 to withdraw
/// @param maxSharesAmt_ The maximum number of shares the user is willing to burn
/// @param to_ Recipient of swapped tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with shares_
/// @return shares_ The number of shares burned for the withdrawal
function withdraw(
uint token0Amt_,
uint token1Amt_,
uint maxSharesAmt_,
address to_
) external returns (uint shares_);
/// @dev This function allows users to borrow tokens in any proportion from the debt pool
/// @param token0Amt_ The amount of token0 to borrow
/// @param token1Amt_ The amount of token1 to borrow
/// @param maxSharesAmt_ The maximum amount of shares the user is willing to receive
/// @param to_ Recipient of swapped tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with shares_
/// @return shares_ The amount of borrow shares minted to represent the borrowed amount
function borrow(uint token0Amt_, uint token1Amt_, uint maxSharesAmt_, address to_) external returns (uint shares_);
/// @dev This function allows users to payback tokens in any proportion to the debt pool
/// @param token0Amt_ The amount of token0 to payback
/// @param token1Amt_ The amount of token1 to payback
/// @param minSharesAmt_ The minimum amount of shares the user expects to burn
/// @param estimate_ If true, function will revert with estimated shares without executing the payback
/// @return shares_ The amount of borrow shares burned for the payback
function payback(
uint token0Amt_,
uint token1Amt_,
uint minSharesAmt_,
bool estimate_
) external payable returns (uint shares_);
/// @dev This function allows users to withdraw their collateral with perfect shares in one token
/// @param shares_ The number of shares to burn for withdrawal
/// @param minToken0_ The minimum amount of token0 the user expects to receive (set to 0 if withdrawing in token1)
/// @param minToken1_ The minimum amount of token1 the user expects to receive (set to 0 if withdrawing in token0)
/// @param to_ Recipient of swapped tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with withdrawAmt_
/// @return withdrawAmt_ The amount of tokens withdrawn in the chosen token
function withdrawPerfectInOneToken(
uint shares_,
uint minToken0_,
uint minToken1_,
address to_
) external returns (uint withdrawAmt_);
/// @dev This function allows users to payback their debt with perfect shares in one token
/// @param shares_ The number of shares to burn for payback
/// @param maxToken0_ The maximum amount of token0 the user is willing to pay (set to 0 if paying back in token1)
/// @param maxToken1_ The maximum amount of token1 the user is willing to pay (set to 0 if paying back in token0)
/// @param estimate_ If true, the function will revert with the estimated payback amount without executing the payback
/// @return paybackAmt_ The amount of tokens paid back in the chosen token
function paybackPerfectInOneToken(
uint shares_,
uint maxToken0_,
uint maxToken1_,
bool estimate_
) external payable returns (uint paybackAmt_);
/// @dev the oracle assumes last set price of pool till the next swap happens.
/// There's a possibility that during that time some interest is generated hence the last stored price is not the 100% correct price for the whole duration
/// but the difference due to interest will be super low so this difference is ignored
/// For example 2 swaps happened 10min (600 seconds) apart and 1 token has 10% higher interest than other.
/// then that token will accrue about 10% * 600 / secondsInAYear = ~0.0002%
/// @param secondsAgos_ array of seconds ago for which TWAP is needed. If user sends [10, 30, 60] then twaps_ will return [10-0, 30-10, 60-30]
/// @return twaps_ twap price, lowest price (aka minima) & highest price (aka maxima) between secondsAgo checkpoints
/// @return currentPrice_ price of pool after the most recent swap
function oraclePrice(uint[] memory secondsAgos_) external view returns (Oracle[] memory twaps_, uint currentPrice_);
}
contracts/protocols/dex/poolT1/coreModule/structs.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
abstract contract Structs {
struct PricesAndExchangePrice {
uint lastStoredPrice; // last stored price in 1e27 decimals
uint centerPrice; // last stored price in 1e27 decimals
uint upperRange; // price at upper range in 1e27 decimals
uint lowerRange; // price at lower range in 1e27 decimals
uint geometricMean; // geometric mean of upper range & lower range in 1e27 decimals
uint supplyToken0ExchangePrice;
uint borrowToken0ExchangePrice;
uint supplyToken1ExchangePrice;
uint borrowToken1ExchangePrice;
}
struct ExchangePrices {
uint supplyToken0ExchangePrice;
uint borrowToken0ExchangePrice;
uint supplyToken1ExchangePrice;
uint borrowToken1ExchangePrice;
}
struct CollateralReserves {
uint token0RealReserves;
uint token1RealReserves;
uint token0ImaginaryReserves;
uint token1ImaginaryReserves;
}
struct CollateralReservesSwap {
uint tokenInRealReserves;
uint tokenOutRealReserves;
uint tokenInImaginaryReserves;
uint tokenOutImaginaryReserves;
}
struct DebtReserves {
uint token0Debt;
uint token1Debt;
uint token0RealReserves;
uint token1RealReserves;
uint token0ImaginaryReserves;
uint token1ImaginaryReserves;
}
struct DebtReservesSwap {
uint tokenInDebt;
uint tokenOutDebt;
uint tokenInRealReserves;
uint tokenOutRealReserves;
uint tokenInImaginaryReserves;
uint tokenOutImaginaryReserves;
}
struct SwapInMemory {
address tokenIn;
address tokenOut;
uint256 amtInAdjusted;
address withdrawTo;
address borrowTo;
uint price; // price of pool after swap
uint fee; // fee of pool
uint revenueCut; // revenue cut of pool
bool swap0to1;
int swapRoutingAmt;
bytes data; // just added to avoid stack-too-deep error
}
struct SwapOutMemory {
address tokenIn;
address tokenOut;
uint256 amtOutAdjusted;
address withdrawTo;
address borrowTo;
uint price; // price of pool after swap
uint fee;
uint revenueCut; // revenue cut of pool
bool swap0to1;
int swapRoutingAmt;
bytes data; // just added to avoid stack-too-deep error
uint msgValue;
}
struct DepositColMemory {
uint256 token0AmtAdjusted;
uint256 token1AmtAdjusted;
uint256 token0ReservesInitial;
uint256 token1ReservesInitial;
}
struct WithdrawColMemory {
uint256 token0AmtAdjusted;
uint256 token1AmtAdjusted;
uint256 token0ReservesInitial;
uint256 token1ReservesInitial;
address to;
}
struct BorrowDebtMemory {
uint256 token0AmtAdjusted;
uint256 token1AmtAdjusted;
uint256 token0DebtInitial;
uint256 token1DebtInitial;
address to;
}
struct PaybackDebtMemory {
uint256 token0AmtAdjusted;
uint256 token1AmtAdjusted;
uint256 token0DebtInitial;
uint256 token1DebtInitial;
}
struct OraclePriceMemory {
uint lowestPrice1by0;
uint highestPrice1by0;
uint oracleSlot;
uint oracleMap;
uint oracle;
}
struct Oracle {
uint twap1by0; // TWAP price
uint lowestPrice1by0; // lowest price point
uint highestPrice1by0; // highest price point
uint twap0by1; // TWAP price
uint lowestPrice0by1; // lowest price point
uint highestPrice0by1; // highest price point
}
struct Implementations {
address shift;
address admin;
address colOperations;
address debtOperations;
address perfectOperationsAndSwapOut;
}
struct ConstantViews {
uint256 dexId;
address liquidity;
address factory;
Implementations implementations;
address deployerContract;
address token0;
address token1;
bytes32 supplyToken0Slot;
bytes32 borrowToken0Slot;
bytes32 supplyToken1Slot;
bytes32 borrowToken1Slot;
bytes32 exchangePriceToken0Slot;
bytes32 exchangePriceToken1Slot;
uint256 oracleMapping;
}
struct ConstantViews2 {
uint token0NumeratorPrecision;
uint token0DenominatorPrecision;
uint token1NumeratorPrecision;
uint token1DenominatorPrecision;
}
}
contracts/protocols/dex/smartLending/factory/main.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
import { Owned } from "solmate/src/auth/Owned.sol";
import { SSTORE2 } from "solmate/src/utils/SSTORE2.sol";
import { CREATE3 } from "solmate/src/utils/CREATE3.sol";
import { ErrorTypes } from "../../errorTypes.sol";
import { Error } from "../../error.sol";
abstract contract Constants {
address public immutable DEX_FACTORY;
address public immutable LIQUIDITY;
}
abstract contract Variables is Owned {
// ------------ storage variables from inherited contracts (Owned) come before vars here --------
// ----------------------- slot 0 ---------------------------
// address public owner;
// 12 bytes empty
// ----------------------- slot 1 ---------------------------
/// @dev smart lending auths can update specific configs.
/// owner can add/remove auths.
/// Owner is auth by default.
mapping(address => mapping(address => uint256)) internal _smartLendingAuths;
// ----------------------- slot 2 ---------------------------
/// @dev deployers can deploy new smartLendings.
/// owner can add/remove deployers.
/// Owner is deployer by default.
mapping(address => uint256) internal _deployers;
// ----------------------- slot 3 ---------------------------
/// @notice list of all created tokens.
/// @dev Solidity creates an automatic getter only to fetch at a certain position, so explicitly define a getter that returns all.
address[] public createdTokens;
// ----------------------- slot 4 ---------------------------
/// @dev smart lending creation code, accessed via SSTORE2.
address internal _smartLendingCreationCodePointer;
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor(address owner_) Owned(owner_) {}
}
abstract contract Events {
/// @dev Emitted when a new smart lending is deployed
/// @param dexId The ID of the deployed DEX
/// @param smartLending The address of the deployed smart lending
event LogSmartLendingDeployed(uint256 dexId, address smartLending);
/// @dev Emitted when a SmartLending auth is updated
/// @param smartLending address of SmartLending
/// @param auth address of auth whose status is being updated
/// @param allowed updated status of auth
event LogAuthUpdated(address smartLending, address auth, bool allowed);
/// @dev Emitted when a deployer is modified by owner
/// @param deployer address of deployer
/// @param allowed updated status of deployer
event LogDeployerUpdated(address deployer, bool allowed);
/// @dev Emitted when the smart lending creation code is modified by owner
/// @param creationCodePointer address of the creation code pointer
event LogSetCreationCode(address creationCodePointer);
}
contract FluidSmartLendingFactory is Constants, Variables, Events, Error {
/// @dev Validates that an address is not the zero address
modifier validAddress(address value_) {
if (value_ == address(0)) {
revert FluidSmartLendingFactoryError(ErrorTypes.SmartLendingFactory__ZeroAddress);
}
_;
}
constructor(
address dexFactory_,
address liquidity_,
address owner_
) validAddress(dexFactory_) validAddress(liquidity_) validAddress(owner_) Variables(owner_) {
LIQUIDITY = liquidity_;
DEX_FACTORY = dexFactory_;
}
/// @dev Validates that msg.sender is deployer or owner
modifier onlyDeployers() {
if (!isDeployer(msg.sender)) {
revert FluidSmartLendingFactoryError(ErrorTypes.SmartLendingFactory__Unauthorized);
}
_;
}
/// @notice List of all created tokens
function allTokens() public view returns (address[] memory) {
return createdTokens;
}
/// @notice Reads if a certain `auth_` address is an allowed auth for `smartLending_` or not. Owner is auth by default.
function isSmartLendingAuth(address smartLending_, address auth_) public view returns (bool) {
return auth_ == owner || _smartLendingAuths[smartLending_][auth_] == 1;
}
/// @notice Reads if a certain `deployer_` address is an allowed deployer or not. Owner is deployer by default.
function isDeployer(address deployer_) public view returns (bool) {
return deployer_ == owner || _deployers[deployer_] == 1;
}
/// @dev Retrieves the creation code for the SmartLending contract
function smartLendingCreationCode() public view returns (bytes memory) {
return SSTORE2.read(_smartLendingCreationCodePointer);
}
/// @notice Sets an address as allowed deployer or not. Only callable by owner.
/// @param deployer_ Address to set deployer value for
/// @param allowed_ Bool flag for whether address is allowed as deployer or not
function updateDeployer(address deployer_, bool allowed_) external onlyOwner validAddress(deployer_) {
_deployers[deployer_] = allowed_ ? 1 : 0;
emit LogDeployerUpdated(deployer_, allowed_);
}
/// @notice Updates the authorization status of an address for a SmartLending contract. Only callable by owner.
/// @param smartLending_ The address of the SmartLending contract
/// @param auth_ The address to be updated
/// @param allowed_ The new authorization status
function updateSmartLendingAuth(
address smartLending_,
address auth_,
bool allowed_
) external validAddress(smartLending_) validAddress(auth_) onlyOwner {
_smartLendingAuths[smartLending_][auth_] = allowed_ ? 1 : 0;
emit LogAuthUpdated(smartLending_, auth_, allowed_);
}
/// @notice Sets the `creationCode_` bytecode for new SmartLending contracts. Only callable by owner.
/// @param creationCode_ New SmartLending contract creation code.
function setSmartLendingCreationCode(bytes calldata creationCode_) external onlyOwner {
if (creationCode_.length == 0) {
revert FluidSmartLendingFactoryError(ErrorTypes.SmartLendingFactory__InvalidParams);
}
// write creation code to SSTORE2 pointer and set in mapping
address creationCodePointer_ = SSTORE2.write(creationCode_);
_smartLendingCreationCodePointer = creationCodePointer_;
emit LogSetCreationCode(creationCodePointer_);
}
/// @notice Spell allows owner aka governance to do any arbitrary call on factory
/// @param target_ Address to which the call needs to be delegated
/// @param data_ Data to execute at the delegated address
function spell(address target_, bytes memory data_) external onlyOwner returns (bytes memory response_) {
assembly {
let succeeded := delegatecall(gas(), target_, add(data_, 0x20), mload(data_), 0, 0)
let size := returndatasize()
response_ := mload(0x40)
mstore(0x40, add(response_, and(add(add(size, 0x20), 0x1f), not(0x1f))))
mstore(response_, size)
returndatacopy(add(response_, 0x20), 0, size)
switch iszero(succeeded)
case 1 {
// throw if delegatecall failed
returndatacopy(0x00, 0x00, size)
revert(0x00, size)
}
}
}
/// @notice Deploys a new SmartLending contract. Only callable by deployers.
/// @param dexId_ The ID of the DEX for which the smart lending wrapper is being deployed
/// @return smartLending_ The newly deployed SmartLending contract
function deploy(uint256 dexId_) public onlyDeployers returns (address smartLending_) {
if (getSmartLendingAddress(dexId_).code.length != 0) {
revert FluidSmartLendingFactoryError(ErrorTypes.SmartLendingFactory__AlreadyDeployed);
}
// Use CREATE3 for deterministic deployments. Unfortunately it has 55k gas overhead
smartLending_ = CREATE3.deploy(
_getSalt(dexId_),
abi.encodePacked(
SSTORE2.read(_smartLendingCreationCodePointer), // creation code
abi.encode(dexId_, LIQUIDITY, DEX_FACTORY, address(this)) // constructor params
),
0
);
createdTokens.push(smartLending_); // Add the created token to the allTokens array
emit LogSmartLendingDeployed(dexId_, smartLending_);
}
/// @notice Computes the address of a SmartLending contract based on a given dexId.
/// @param dexId_ The ID of the DEX for which the SmartLending contract address is being computed.
/// @return The computed address of the SmartLending contract.
function getSmartLendingAddress(uint256 dexId_) public view returns (address) {
return CREATE3.getDeployed(_getSalt(dexId_));
}
/// @notice Returns the total number of SmartLending contracts deployed by the factory.
/// @return The total number of SmartLending contracts deployed.
function totalSmartLendings() external view returns (uint256) {
return createdTokens.length;
}
/// @notice Checks if a given address (`smartLending_`) corresponds to a valid smart lending.
/// @param smartLending_ The smart lending address to check.
/// @return Returns `true` if the given address corresponds to a valid smart lending, otherwise `false`.
function isSmartLending(address smartLending_) public view returns (bool) {
if (smartLending_.code.length == 0) {
return false;
} else {
// DEX() function signature is 0x80935aa9
(bool success_, bytes memory data_) = smartLending_.staticcall(hex"80935aa9");
address dex_ = abi.decode(data_, (address));
// DEX_ID() function signature is 0xf4b9a3fb
(success_, data_) = dex_.staticcall(hex"f4b9a3fb");
return success_ && smartLending_ == getSmartLendingAddress(abi.decode(data_, (uint256)));
}
}
/// @dev unique deployment salt for the smart lending
function _getSalt(uint256 dexId_) internal pure returns (bytes32) {
return keccak256(abi.encode(dexId_));
}
/// @dev Deploys a contract using the CREATE opcode with the provided bytecode (`bytecode_`).
/// This is an internal function, meant to be used within the contract to facilitate the deployment of other contracts.
/// @param bytecode_ The bytecode of the contract to be deployed.
/// @return address_ Returns the address of the deployed contract.
function _deploy(bytes memory bytecode_) internal returns (address address_) {
if (bytecode_.length == 0) {
revert FluidDexError(ErrorTypes.SmartLendingFactory__InvalidOperation);
}
/// @solidity memory-safe-assembly
assembly {
address_ := create(0, add(bytecode_, 0x20), mload(bytecode_))
}
if (address_ == address(0)) {
revert FluidDexError(ErrorTypes.SmartLendingFactory__InvalidOperation);
}
}
}
contracts/protocols/dex/smartLending/main.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import { IFluidDexT1 } from "../interfaces/iDexT1.sol";
import { FluidDexFactory } from "../factory/main.sol";
import { FluidSmartLendingFactory } from "./factory/main.sol";
import { SafeTransfer } from "../../../libraries/safeTransfer.sol";
import { ErrorTypes } from "../errorTypes.sol";
import { Error } from "../error.sol";
import { DexSlotsLink } from "../../../libraries/dexSlotsLink.sol";
import { DexCalcs } from "../../../libraries/dexCalcs.sol";
abstract contract Constants {
/// @dev Ignoring leap years
uint256 internal constant SECONDS_PER_YEAR = 365 days;
address internal constant ETH_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
address internal constant DEAD_ADDRESS = 0x000000000000000000000000000000000000dEaD;
FluidDexFactory public immutable DEX_FACTORY;
FluidSmartLendingFactory public immutable SMART_LENDING_FACTORY;
IFluidDexT1 public immutable DEX;
address public immutable LIQUIDITY;
address public immutable TOKEN0;
address public immutable TOKEN1;
bool public immutable IS_NATIVE_PAIR;
}
abstract contract Variables is ERC20, Constants {
// ------------ storage variables from inherited contracts come before vars here --------
// _________ ERC20 _______________
// ----------------------- slot 0 ---------------------------
// mapping(address => uint256) private _balances;
// ----------------------- slot 1 ---------------------------
// mapping(address => mapping(address => uint256)) private _allowances;
// ----------------------- slot 2 ---------------------------
// uint256 private _totalSupply;
// ----------------------- slot 3 ---------------------------
// string private _name;
// ----------------------- slot 4 ---------------------------
// string private _symbol;
// ------------ storage variables ------------------------------------------------------
// ----------------------- slot 5 ---------------------------
uint40 public lastTimestamp;
/// If positive then rewards, if negative then fee.
/// 1e6 = 100%, 1e4 = 1%, minimum 0.0001% fee or reward.
int32 public feeOrReward;
// Starting from 1e18
// If fees then reduce exchange price
// If reward then increase exchange price
uint184 public exchangePrice;
// ----------------------- slot 6 ---------------------------
address public rebalancer;
// ----------------------- slot 7 ---------------------------
address public dexFromAddress;
/// @dev status for reentrancy guard
uint8 internal _status;
}
abstract contract Events {
/// @dev Emitted when the share to tokens ratio is rebalanced
/// @param shares_ The number of shares rebalanced
/// @param token0Amt_ The amount of token0 rebalanced
/// @param token1Amt_ The amount of token1 rebalanced
/// @param isWithdraw_ Whether the rebalance is a withdrawal or deposit
event LogRebalance(uint256 shares_, uint256 token0Amt_, uint256 token1Amt_, bool isWithdraw_);
/// @dev Emitted when the rebalancer is set
/// @param rebalancer The new rebalancer
event LogRebalancerSet(address rebalancer);
/// @dev Emitted when the fee or reward is set
/// @param feeOrReward The new fee or reward
event LogFeeOrRewardSet(int256 feeOrReward);
}
/// @dev ReentrancyGuard based on OpenZeppelin implementation.
/// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v4.8/contracts/security/ReentrancyGuard.sol
abstract contract ReentrancyGuard is Variables, Error {
uint8 internal constant REENTRANCY_NOT_ENTERED = 1;
uint8 internal constant REENTRANCY_ENTERED = 2;
constructor() {
_status = REENTRANCY_NOT_ENTERED;
}
/// @dev Prevents a contract from calling itself, directly or indirectly.
/// See OpenZeppelin implementation for more info
modifier nonReentrant() {
// On the first call to nonReentrant, _status will be NOT_ENTERED
if (_status != REENTRANCY_NOT_ENTERED) {
revert FluidSmartLendingError(ErrorTypes.SmartLending__Reentrancy);
}
// Any calls to nonReentrant after this point will fail
_status = REENTRANCY_ENTERED;
_;
// storing original value triggers a refund (see https://eips.ethereum.org/EIPS/eip-2200)
_status = REENTRANCY_NOT_ENTERED;
}
}
contract FluidSmartLending is ERC20, Variables, Error, ReentrancyGuard, Events {
/// @dev prefix for token name. constructor appends dex id, e.g. "Fluid Smart Lending 12"
string private constant TOKEN_NAME_PREFIX = "Fluid Smart Lending ";
/// @dev prefix for token symbol. constructor appends dex id, e.g. "fSL12"
string private constant TOKEN_SYMBOL_PREFIX = "fSL";
/// @dev Validates that an address is not the zero address
modifier validAddress(address value_) {
if (value_ == address(0)) {
revert FluidSmartLendingError(ErrorTypes.SmartLending__ZeroAddress);
}
_;
}
constructor(
uint256 dexId_,
address liquidity_,
address dexFactory_,
address smartLendingFactory_
)
ERC20(
string(abi.encodePacked(TOKEN_NAME_PREFIX, _toString(dexId_))),
string(abi.encodePacked(TOKEN_SYMBOL_PREFIX, _toString(dexId_)))
)
validAddress(liquidity_)
validAddress(dexFactory_)
validAddress(smartLendingFactory_)
{
LIQUIDITY = liquidity_;
DEX_FACTORY = FluidDexFactory(dexFactory_);
SMART_LENDING_FACTORY = FluidSmartLendingFactory(smartLendingFactory_);
DEX = IFluidDexT1(DEX_FACTORY.getDexAddress(dexId_));
IFluidDexT1.ConstantViews memory constants_ = DEX.constantsView();
TOKEN0 = constants_.token0;
TOKEN1 = constants_.token1;
IS_NATIVE_PAIR = (TOKEN0 == ETH_ADDRESS) || (TOKEN1 == ETH_ADDRESS);
exchangePrice = uint184(1e18);
feeOrReward = int32(0);
lastTimestamp = uint40(block.timestamp);
dexFromAddress = DEAD_ADDRESS;
}
modifier setDexFrom() {
dexFromAddress = msg.sender;
_;
dexFromAddress = DEAD_ADDRESS;
}
modifier onlyAuth() {
if (!SMART_LENDING_FACTORY.isSmartLendingAuth(address(this), msg.sender)) {
revert FluidSmartLendingError(ErrorTypes.SmartLending__Unauthorized);
}
_;
}
modifier onlyOwner() {
if (msg.sender != SMART_LENDING_FACTORY.owner()) {
revert FluidSmartLendingError(ErrorTypes.SmartLending__Unauthorized);
}
_;
}
modifier _updateExchangePrice() {
bool rewardsOrFeeActive_;
(exchangePrice, rewardsOrFeeActive_) = getUpdateExchangePrice();
if (rewardsOrFeeActive_) {
lastTimestamp = uint40(block.timestamp); // only write to storage if fee or reward is active.
}
_;
}
/// @notice gets updated exchange price
function getUpdateExchangePrice() public view returns (uint184 exchangePrice_, bool rewardsOrFeeActive_) {
int256 feeOrReward_ = feeOrReward;
exchangePrice_ = exchangePrice;
if (feeOrReward_ > 0) {
exchangePrice_ =
exchangePrice_ +
uint184(
(exchangePrice_ * uint256(feeOrReward_) * (block.timestamp - uint256(lastTimestamp))) /
(1e6 * SECONDS_PER_YEAR)
);
rewardsOrFeeActive_ = true;
} else if (feeOrReward_ < 0) {
exchangePrice_ =
exchangePrice_ -
uint184(
(exchangePrice_ * uint256(-feeOrReward_) * (block.timestamp - uint256(lastTimestamp))) /
(1e6 * SECONDS_PER_YEAR)
);
rewardsOrFeeActive_ = true;
}
}
/// @notice triggers updateExchangePrice
function updateExchangePrice() public _updateExchangePrice {}
/// @dev Set the fee or reward. Only callable by auths.
/// @param feeOrReward_ The new fee or reward (1e6 = 100%, 1e4 = 1%, minimum 0.0001% fee or reward). 0 means no fee or reward
function setFeeOrReward(int256 feeOrReward_) external onlyAuth _updateExchangePrice {
if (feeOrReward_ > 1e6 || feeOrReward_ < -1e6) {
revert FluidSmartLendingError(ErrorTypes.SmartLending__OutOfRange);
}
lastTimestamp = uint40(block.timestamp); // current fee or reward setting is applied until exactly now even if previously 0
feeOrReward = int32(feeOrReward_);
emit LogFeeOrRewardSet(feeOrReward_);
}
/// @dev Set the rebalancer. Only callable by auths.
/// @param rebalancer_ The new rebalancer
function setRebalancer(address rebalancer_) external onlyAuth validAddress(rebalancer_) {
rebalancer = rebalancer_;
emit LogRebalancerSet(rebalancer_);
}
/// @notice Spell allows auths (governance) to do any arbitrary call
/// @param target_ Address to which the call needs to be delegated
/// @param data_ Data to execute at the delegated address
function spell(address target_, bytes memory data_) external onlyOwner returns (bytes memory response_) {
assembly {
let succeeded := delegatecall(gas(), target_, add(data_, 0x20), mload(data_), 0, 0)
let size := returndatasize()
response_ := mload(0x40)
mstore(0x40, add(response_, and(add(add(size, 0x20), 0x1f), not(0x1f))))
mstore(response_, size)
returndatacopy(add(response_, 0x20), 0, size)
switch iszero(succeeded)
case 1 {
// throw if delegatecall failed
returndatacopy(0x00, 0x00, size)
revert(0x00, size)
}
}
}
/// @dev Deposit tokens in equal proportion to the current pool ratio
/// @param shares_ The number of shares to mint
/// @param maxToken0Deposit_ Maximum amount of token0 to deposit
/// @param maxToken1Deposit_ Maximum amount of token1 to deposit
/// @param to_ Recipient of minted tokens. If to_ == address(0) then out tokens will be sent to msg.sender.
/// @return amount_ Amount of tokens minted
/// @return token0Amt_ Amount of token0 deposited
/// @return token1Amt_ Amount of token1 deposited
function depositPerfect(
uint256 shares_,
uint256 maxToken0Deposit_,
uint256 maxToken1Deposit_,
address to_
)
external
payable
setDexFrom
_updateExchangePrice
nonReentrant
returns (uint256 amount_, uint256 token0Amt_, uint256 token1Amt_)
{
if (!IS_NATIVE_PAIR) {
if (msg.value > 0) {
revert FluidSmartLendingError(ErrorTypes.SmartLending__InvalidMsgValue);
}
(token0Amt_, token1Amt_) = DEX.depositPerfect(
shares_ + 1, // + 1 rounding up but only minting shares
maxToken0Deposit_,
maxToken1Deposit_,
false
);
} else {
uint256 value_ = TOKEN0 == ETH_ADDRESS ? maxToken0Deposit_ : maxToken1Deposit_;
if (value_ > msg.value) {
revert FluidSmartLendingError(ErrorTypes.SmartLending__InvalidMsgValue);
}
uint256 initialEthAmount_ = address(this).balance - msg.value;
(token0Amt_, token1Amt_) = DEX.depositPerfect{ value: value_ }(
shares_ + 1, // + 1 rounding up but only minting shares
maxToken0Deposit_,
maxToken1Deposit_,
false
);
uint finalEth_ = payable(address(this)).balance;
if (finalEth_ > initialEthAmount_) {
unchecked {
SafeTransfer.safeTransferNative(msg.sender, finalEth_ - initialEthAmount_); // sending back excess ETH
}
}
}
to_ = to_ == address(0) ? msg.sender : to_;
amount_ = (shares_ * 1e18) / exchangePrice;
_mint(to_, amount_);
}
/// @dev This function allows users to deposit tokens in any proportion into the col pool
/// @param token0Amt_ The amount of token0 to deposit
/// @param token1Amt_ The amount of token1 to deposit
/// @param minSharesAmt_ The minimum amount of shares the user expects to receive
/// @param to_ Recipient of minted tokens. If to_ == address(0) then out tokens will be sent to msg.sender.
/// @return amount_ The amount of tokens minted for the deposit
/// @return shares_ The number of dex pool shares deposited
function deposit(
uint256 token0Amt_,
uint256 token1Amt_,
uint256 minSharesAmt_,
address to_
) external payable setDexFrom _updateExchangePrice nonReentrant returns (uint256 amount_, uint256 shares_) {
uint256 value_ = !IS_NATIVE_PAIR
? 0
: (TOKEN0 == ETH_ADDRESS)
? token0Amt_
: token1Amt_;
if (value_ != msg.value) {
revert FluidSmartLendingError(ErrorTypes.SmartLending__InvalidMsgValue);
}
to_ = to_ == address(0) ? msg.sender : to_;
shares_ = DEX.deposit{ value: value_ }(token0Amt_, token1Amt_, minSharesAmt_, false);
amount_ = (shares_ * 1e18) / exchangePrice - 1;
_mint(to_, amount_);
}
/// @dev This function allows users to withdraw a perfect amount of collateral liquidity
/// @param shares_ The number of shares to withdraw. set to type(uint).max to withdraw maximum balance.
/// @param minToken0Withdraw_ The minimum amount of token0 the user is willing to accept
/// @param minToken1Withdraw_ The minimum amount of token1 the user is willing to accept
/// @param to_ Recipient of withdrawn tokens. If to_ == address(0) then out tokens will be sent to msg.sender.
/// @return amount_ amount_ of shares actually burnt
/// @return token0Amt_ The amount of token0 withdrawn
/// @return token1Amt_ The amount of token1 withdrawn
function withdrawPerfect(
uint256 shares_,
uint256 minToken0Withdraw_,
uint256 minToken1Withdraw_,
address to_
) external _updateExchangePrice nonReentrant returns (uint256 amount_, uint256 token0Amt_, uint256 token1Amt_) {
if (shares_ == type(uint).max) {
amount_ = balanceOf(msg.sender);
shares_ = (amount_ * exchangePrice) / 1e18 - 1;
} else {
amount_ = (shares_ * 1e18) / exchangePrice + 1;
}
_burn(msg.sender, amount_);
to_ = to_ == address(0) ? msg.sender : to_;
if (minToken0Withdraw_ > 0 && minToken1Withdraw_ > 0) {
(token0Amt_, token1Amt_) = DEX.withdrawPerfect(shares_, minToken0Withdraw_, minToken1Withdraw_, to_);
} else if (minToken0Withdraw_ > 0 && minToken1Withdraw_ == 0) {
// withdraw only in token0, token1Amt_ remains 0
(token0Amt_) = DEX.withdrawPerfectInOneToken(shares_, minToken0Withdraw_, minToken1Withdraw_, to_);
} else if (minToken0Withdraw_ == 0 && minToken1Withdraw_ > 0) {
// withdraw only in token1, token0Amt_ remains 0
(token1Amt_) = DEX.withdrawPerfectInOneToken(shares_, minToken0Withdraw_, minToken1Withdraw_, to_);
} else {
// meaning user sent both amounts as == 0
revert FluidSmartLendingError(ErrorTypes.SmartLending__InvalidAmounts);
}
}
/// @dev This function allows users to withdraw tokens in any proportion from the col pool
/// @param token0Amt_ The amount of token0 to withdraw
/// @param token1Amt_ The amount of token1 to withdraw
/// @param maxSharesAmt_ The maximum number of shares the user is willing to burn
/// @param to_ Recipient of withdrawn tokens. If to_ == address(0) then out tokens will be sent to msg.sender. If to_ == ADDRESS_DEAD then function will revert with shares_
/// @return amount_ The number of tokens burned for the withdrawal
/// @return shares_ The number of dex pool shares withdrawn
function withdraw(
uint256 token0Amt_,
uint256 token1Amt_,
uint256 maxSharesAmt_,
address to_
) external _updateExchangePrice nonReentrant returns (uint256 amount_, uint256 shares_) {
to_ = to_ == address(0) ? msg.sender : to_;
shares_ = DEX.withdraw(token0Amt_, token1Amt_, maxSharesAmt_, to_);
amount_ = (shares_ * 1e18) / exchangePrice + 1;
_burn(msg.sender, amount_);
}
/// @dev Rebalances the share to tokens ratio to balance out rewards and fees
function rebalance(
uint256 minOrMaxToken0_,
uint256 minOrMaxToken1_
)
public
payable
_updateExchangePrice
nonReentrant
returns (uint256 shares_, uint256 token0Amt_, uint256 token1Amt_, bool isWithdraw_)
{
if (rebalancer != msg.sender) revert FluidSmartLendingError(ErrorTypes.SmartLending__InvalidRebalancer);
int256 rebalanceDiff_ = rebalanceDiff();
if (rebalanceDiff_ > 0) {
// fees (withdraw)
isWithdraw_ = true;
if (msg.value > 0) {
revert FluidSmartLendingError(ErrorTypes.SmartLending__InvalidMsgValue);
}
shares_ = uint256(rebalanceDiff_);
(token0Amt_, token1Amt_) = DEX.withdrawPerfect(shares_, minOrMaxToken0_, minOrMaxToken1_, msg.sender);
} else if (rebalanceDiff_ < 0) {
// rewards (deposit)
isWithdraw_ = false;
uint256 initialEthAmount_ = address(this).balance - msg.value;
uint256 value_ = !IS_NATIVE_PAIR
? 0
: (TOKEN0 == ETH_ADDRESS)
? minOrMaxToken0_
: minOrMaxToken1_;
if (value_ > msg.value) {
revert FluidSmartLendingError(ErrorTypes.SmartLending__InvalidMsgValue);
}
shares_ = uint256(-rebalanceDiff_);
dexFromAddress = msg.sender;
(token0Amt_, token1Amt_) = DEX.depositPerfect{ value: value_ }(
shares_,
minOrMaxToken0_,
minOrMaxToken1_,
false
);
dexFromAddress = DEAD_ADDRESS;
uint finalEth_ = payable(address(this)).balance;
if (finalEth_ > initialEthAmount_) {
unchecked {
SafeTransfer.safeTransferNative(msg.sender, finalEth_ - initialEthAmount_); // sending back excess ETH
}
}
}
emit LogRebalance(shares_, token0Amt_, token1Amt_, isWithdraw_);
}
/// @dev Returns the difference between the total smart lending shares on the DEX and the total smart lending shares calculated.
/// A positive value indicates fees to collect, while a negative value indicates rewards to be rebalanced.
function rebalanceDiff() public view returns (int256) {
uint256 totalSmartLendingSharesOnDex_ = DEX.readFromStorage(
DexSlotsLink.calculateMappingStorageSlot(DexSlotsLink.DEX_USER_SUPPLY_MAPPING_SLOT, address(this))
);
totalSmartLendingSharesOnDex_ =
(totalSmartLendingSharesOnDex_ >> DexSlotsLink.BITS_USER_SUPPLY_AMOUNT) &
DexCalcs.X64;
totalSmartLendingSharesOnDex_ =
(totalSmartLendingSharesOnDex_ >> DexCalcs.DEFAULT_EXPONENT_SIZE) <<
(totalSmartLendingSharesOnDex_ & DexCalcs.DEFAULT_EXPONENT_MASK);
uint256 totalSmartLendingShares_ = (totalSupply() * exchangePrice) / 1e18;
return int256(totalSmartLendingSharesOnDex_) - int256(totalSmartLendingShares_);
}
/// @notice dex liquidity callback
/// @param token_ The token being transferred
/// @param amount_ The amount being transferred
function dexCallback(address token_, uint256 amount_) external {
if (msg.sender != address(DEX)) {
revert FluidSmartLendingError(ErrorTypes.SmartLending__Unauthorized);
}
SafeTransfer.safeTransferFrom(token_, dexFromAddress, LIQUIDITY, amount_);
}
/// @dev for excess eth being sent back from dex to here
receive() external payable {
if (msg.sender != address(DEX)) {
revert FluidSmartLendingError(ErrorTypes.SmartLending__Unauthorized);
}
}
/**
* @dev Return the log in base 10 of a positive value rounded towards zero.
* Returns 0 if given 0.
* taken from https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/Math.sol
*/
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 Converts a `uint256` to its ASCII `string` decimal representation.
* taken from https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Strings.sol
*/
bytes16 private constant HEX_DIGITS = "0123456789abcdef";
function _toString(uint256 value) internal pure returns (string memory) {
unchecked {
uint256 length = _log10(value) + 1;
string memory buffer = new string(length);
uint256 ptr;
assembly ("memory-safe") {
ptr := add(buffer, add(32, length))
}
while (true) {
ptr--;
assembly ("memory-safe") {
mstore8(ptr, byte(mod(value, 10), HEX_DIGITS))
}
value /= 10;
if (value == 0) break;
}
return buffer;
}
}
}
solmate/src/auth/Owned.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;
/// @notice Simple single owner authorization mixin.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/auth/Owned.sol)
abstract contract Owned {
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event OwnershipTransferred(address indexed user, address indexed newOwner);
/*//////////////////////////////////////////////////////////////
OWNERSHIP STORAGE
//////////////////////////////////////////////////////////////*/
address public owner;
modifier onlyOwner() virtual {
require(msg.sender == owner, "UNAUTHORIZED");
_;
}
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor(address _owner) {
owner = _owner;
emit OwnershipTransferred(address(0), _owner);
}
/*//////////////////////////////////////////////////////////////
OWNERSHIP LOGIC
//////////////////////////////////////////////////////////////*/
function transferOwnership(address newOwner) public virtual onlyOwner {
owner = newOwner;
emit OwnershipTransferred(msg.sender, newOwner);
}
}
solmate/src/utils/Bytes32AddressLib.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;
/// @notice Library for converting between addresses and bytes32 values.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/Bytes32AddressLib.sol)
library Bytes32AddressLib {
function fromLast20Bytes(bytes32 bytesValue) internal pure returns (address) {
return address(uint160(uint256(bytesValue)));
}
function fillLast12Bytes(address addressValue) internal pure returns (bytes32) {
return bytes32(bytes20(addressValue));
}
}
solmate/src/utils/CREATE3.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;
import {Bytes32AddressLib} from "./Bytes32AddressLib.sol";
/// @notice Deploy to deterministic addresses without an initcode factor.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/CREATE3.sol)
/// @author Modified from 0xSequence (https://github.com/0xSequence/create3/blob/master/contracts/Create3.sol)
library CREATE3 {
using Bytes32AddressLib for bytes32;
//--------------------------------------------------------------------------------//
// Opcode | Opcode + Arguments | Description | Stack View //
//--------------------------------------------------------------------------------//
// 0x36 | 0x36 | CALLDATASIZE | size //
// 0x3d | 0x3d | RETURNDATASIZE | 0 size //
// 0x3d | 0x3d | RETURNDATASIZE | 0 0 size //
// 0x37 | 0x37 | CALLDATACOPY | //
// 0x36 | 0x36 | CALLDATASIZE | size //
// 0x3d | 0x3d | RETURNDATASIZE | 0 size //
// 0x34 | 0x34 | CALLVALUE | value 0 size //
// 0xf0 | 0xf0 | CREATE | newContract //
//--------------------------------------------------------------------------------//
// Opcode | Opcode + Arguments | Description | Stack View //
//--------------------------------------------------------------------------------//
// 0x67 | 0x67XXXXXXXXXXXXXXXX | PUSH8 bytecode | bytecode //
// 0x3d | 0x3d | RETURNDATASIZE | 0 bytecode //
// 0x52 | 0x52 | MSTORE | //
// 0x60 | 0x6008 | PUSH1 08 | 8 //
// 0x60 | 0x6018 | PUSH1 18 | 24 8 //
// 0xf3 | 0xf3 | RETURN | //
//--------------------------------------------------------------------------------//
bytes internal constant PROXY_BYTECODE = hex"67_36_3d_3d_37_36_3d_34_f0_3d_52_60_08_60_18_f3";
bytes32 internal constant PROXY_BYTECODE_HASH = keccak256(PROXY_BYTECODE);
function deploy(
bytes32 salt,
bytes memory creationCode,
uint256 value
) internal returns (address deployed) {
bytes memory proxyChildBytecode = PROXY_BYTECODE;
address proxy;
/// @solidity memory-safe-assembly
assembly {
// Deploy a new contract with our pre-made bytecode via CREATE2.
// We start 32 bytes into the code to avoid copying the byte length.
proxy := create2(0, add(proxyChildBytecode, 32), mload(proxyChildBytecode), salt)
}
require(proxy != address(0), "DEPLOYMENT_FAILED");
deployed = getDeployed(salt);
(bool success, ) = proxy.call{value: value}(creationCode);
require(success && deployed.code.length != 0, "INITIALIZATION_FAILED");
}
function getDeployed(bytes32 salt) internal view returns (address) {
address proxy = keccak256(
abi.encodePacked(
// Prefix:
bytes1(0xFF),
// Creator:
address(this),
// Salt:
salt,
// Bytecode hash:
PROXY_BYTECODE_HASH
)
).fromLast20Bytes();
return
keccak256(
abi.encodePacked(
// 0xd6 = 0xc0 (short RLP prefix) + 0x16 (length of: 0x94 ++ proxy ++ 0x01)
// 0x94 = 0x80 + 0x14 (0x14 = the length of an address, 20 bytes, in hex)
hex"d6_94",
proxy,
hex"01" // Nonce of the proxy contract (1)
)
).fromLast20Bytes();
}
}
solmate/src/utils/SSTORE2.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;
/// @notice Read and write to persistent storage at a fraction of the cost.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/SSTORE2.sol)
/// @author Modified from 0xSequence (https://github.com/0xSequence/sstore2/blob/master/contracts/SSTORE2.sol)
library SSTORE2 {
uint256 internal constant DATA_OFFSET = 1; // We skip the first byte as it's a STOP opcode to ensure the contract can't be called.
/*//////////////////////////////////////////////////////////////
WRITE LOGIC
//////////////////////////////////////////////////////////////*/
function write(bytes memory data) internal returns (address pointer) {
// Prefix the bytecode with a STOP opcode to ensure it cannot be called.
bytes memory runtimeCode = abi.encodePacked(hex"00", data);
bytes memory creationCode = abi.encodePacked(
//---------------------------------------------------------------------------------------------------------------//
// Opcode | Opcode + Arguments | Description | Stack View //
//---------------------------------------------------------------------------------------------------------------//
// 0x60 | 0x600B | PUSH1 11 | codeOffset //
// 0x59 | 0x59 | MSIZE | 0 codeOffset //
// 0x81 | 0x81 | DUP2 | codeOffset 0 codeOffset //
// 0x38 | 0x38 | CODESIZE | codeSize codeOffset 0 codeOffset //
// 0x03 | 0x03 | SUB | (codeSize - codeOffset) 0 codeOffset //
// 0x80 | 0x80 | DUP | (codeSize - codeOffset) (codeSize - codeOffset) 0 codeOffset //
// 0x92 | 0x92 | SWAP3 | codeOffset (codeSize - codeOffset) 0 (codeSize - codeOffset) //
// 0x59 | 0x59 | MSIZE | 0 codeOffset (codeSize - codeOffset) 0 (codeSize - codeOffset) //
// 0x39 | 0x39 | CODECOPY | 0 (codeSize - codeOffset) //
// 0xf3 | 0xf3 | RETURN | //
//---------------------------------------------------------------------------------------------------------------//
hex"60_0B_59_81_38_03_80_92_59_39_F3", // Returns all code in the contract except for the first 11 (0B in hex) bytes.
runtimeCode // The bytecode we want the contract to have after deployment. Capped at 1 byte less than the code size limit.
);
/// @solidity memory-safe-assembly
assembly {
// Deploy a new contract with the generated creation code.
// We start 32 bytes into the code to avoid copying the byte length.
pointer := create(0, add(creationCode, 32), mload(creationCode))
}
require(pointer != address(0), "DEPLOYMENT_FAILED");
}
/*//////////////////////////////////////////////////////////////
READ LOGIC
//////////////////////////////////////////////////////////////*/
function read(address pointer) internal view returns (bytes memory) {
return readBytecode(pointer, DATA_OFFSET, pointer.code.length - DATA_OFFSET);
}
function read(address pointer, uint256 start) internal view returns (bytes memory) {
start += DATA_OFFSET;
return readBytecode(pointer, start, pointer.code.length - start);
}
function read(
address pointer,
uint256 start,
uint256 end
) internal view returns (bytes memory) {
start += DATA_OFFSET;
end += DATA_OFFSET;
require(pointer.code.length >= end, "OUT_OF_BOUNDS");
return readBytecode(pointer, start, end - start);
}
/*//////////////////////////////////////////////////////////////
INTERNAL HELPER LOGIC
//////////////////////////////////////////////////////////////*/
function readBytecode(
address pointer,
uint256 start,
uint256 size
) private view returns (bytes memory data) {
/// @solidity memory-safe-assembly
assembly {
// Get a pointer to some free memory.
data := mload(0x40)
// Update the free memory pointer to prevent overriding our data.
// We use and(x, not(31)) as a cheaper equivalent to sub(x, mod(x, 32)).
// Adding 31 to size and running the result through the logic above ensures
// the memory pointer remains word-aligned, following the Solidity convention.
mstore(0x40, add(data, and(add(add(size, 32), 31), not(31))))
// Store the size of the data in the first 32 byte chunk of free memory.
mstore(data, size)
// Copy the code into memory right after the 32 bytes we used to store the size.
extcodecopy(pointer, add(data, 32), start, size)
}
}
}
Compiler settingsShow
{
"metadata": {
"useLiteralContent": true
},
"optimizer": {
"runs": 10000000,
"enabled": true
},
"evmVersion": "paris"
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.