Address Contract Verified
Address
0x1FAf33c33ce81DC86fe50ba5b6F3417aBEC01d6E
Balance
0 ETH
Nonce
1
Code Size
5326 bytes
Creator
Create2 Deployer at tx 0x953d0e13...8ed57e
Indexed Transactions
0
Contract Bytecode
5326 bytes
0x608060405234801561001057600080fd5b50600436106101775760003560e01c806380935aa9116100d8578063bc063e1a1161008c578063cae5880711610066578063cae5880714610365578063d50741f71461036d578063f28a169d1461039057600080fd5b8063bc063e1a1461032e578063bc669afb14610355578063be47bd831461035d57600080fd5b8063941da44e116100bd578063941da44e146102ec5780639b5075a4146102f4578063b05f233a1461030757600080fd5b806380935aa9146102bd578063813e6745146102e457600080fd5b80636449b8d71161012f57806376aaa6ce1161011457806376aaa6ce1461027957806376c7a3c71461028c5780637d7c2a1c146102b357600080fd5b80636449b8d714610225578063724d76251461027157600080fd5b80631d156e8b116101605780631d156e8b146101d9578063274d713c146101e15780632e2e5b471461020857600080fd5b806302918d131461017c5780631c37ae38146101a2575b600080fd5b61018f61018a36600461139d565b6103b7565b6040519081526020015b60405180910390f35b6101c97f000000000000000000000000000000000000000000000000000000000000000181565b6040519015158152602001610199565b61018f6103d0565b61018f7f000000000000000000000000000000000000000000002a5a058fc295ed00000081565b6102106103ef565b60408051928352602083019190915201610199565b61024c7f000000000000000000000000264786ef916af64a1db19f513f24a3681734ce9281565b60405173ffffffffffffffffffffffffffffffffffffffff9091168152602001610199565b61018f610417565b61018f61028736600461139d565b610457565b61018f7f000000000000000000000000000000000000000000000000000000000000001481565b6102bb610462565b005b61024c7f0000000000000000000000000b1a513ee24972daef112bc777a5610d4325c9e781565b61018f610722565b61018f61072c565b61018f61030236600461139d565b610736565b61018f7f000000000000000000000000000000000000000000009ed194db19b238c0000081565b61018f7f000000000000000000000000000000000000000000000000000000000000009681565b61018f6107fb565b61018f610811565b61018f610844565b61037561084e565b60408051938452602084019290925290820152606001610199565b61018f7f000000000000000000000000000000000000000000000000000000000000000a81565b60006103ca6103c583610736565b61089d565b92915050565b60006103ea6103dd610ace565b6103e5610811565b610ade565b905090565b60008060006103fc610b33565b600281901c6201ffff169460139190911c607f169350915050565b600080610422610811565b9050600061042e610ace565b90508082116104465761044182826113e5565b610450565b61045081836113e5565b9250505090565b60006103ca8261089d565b6040517f467c9eff0000000000000000000000000000000000000000000000000000000081523360048201527f000000000000000000000000264786ef916af64a1db19f513f24a3681734ce9273ffffffffffffffffffffffffffffffffffffffff169063467c9eff906024016020604051808303816000875af11580156104ee573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061051291906113f8565b610552576040517fd002e25a000000000000000000000000000000000000000000000000000000008152620186f360048201526024015b60405180910390fd5b600061055c610811565b90506000806105696103ef565b9150915060006105798385610ade565b90507f000000000000000000000000000000000000000000000000000000000000000a8111156106e55773ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000000b1a513ee24972daef112bc777a5610d4325c9e716632af69377856105ec6127108661141a565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060e085901b16815260048101929092526024820152604401600060405180830381600087803b15801561064257600080fd5b505af1158015610656573d6000803e3d6000fd5b505050507f05feefc978308fcb0cc1f214adc60cd97475eaeda69c7898697ea00bd1e11f4f7f0000000000000000000000000b1a513ee24972daef112bc777a5610d4325c9e785612710856106ab919061141a565b6040805173ffffffffffffffffffffffffffffffffffffffff909416845260208401929092529082015260600160405180910390a161071c565b6040517fd002e25a000000000000000000000000000000000000000000000000000000008152620186f16004820152602401610549565b50505050565b60006103ea610be6565b60006103ea610811565b60007f0000000000000000000000000000000000000000000000000000000000000001156107b7576000610768610be6565b905060008184116107825761077d84836113e5565b61078c565b61078c82856113e5565b9050816107a56b033b2e3c9fd0803ce80000008361141a565b6107af9190611431565b949350505050565b6b033b2e3c9fd0803ce800000082116107e5576107e0826b033b2e3c9fd0803ce80000006113e5565b6103ca565b6103ca6b033b2e3c9fd0803ce8000000836113e5565b6000607f6013610809610b33565b901c16905090565b60008060008061081f61084e565b925092509250428103610830578291505b61083c6103c583610736565b935050505090565b60006103ea610ace565b60008060008061085c610ca5565b63ffffffff600982901c811660ff600184901c81169190911b97603184901c909216602984901c9091161b955060799190911c6401ffffffff169350915050565b60007f000000000000000000000000000000000000000000002a5a058fc295ed00000082116108ed57507f0000000000000000000000000000000000000000000000000000000000000014919050565b7f000000000000000000000000000000000000000000009ed194db19b238c00000821061093b57507f0000000000000000000000000000000000000000000000000000000000000096919050565b60006109877f000000000000000000000000000000000000000000002a5a058fc295ed0000007f000000000000000000000000000000000000000000009ed194db19b238c000006113e5565b6b033b2e3c9fd0803ce80000006109be7f000000000000000000000000000000000000000000002a5a058fc295ed000000866113e5565b6109c8919061141a565b6109d29190611431565b905060006109e08283610d1b565b905060006109ee8284610d1b565b90506000610a12610a0c6b033b2e3c9fd0803ce8000000600261141a565b83610d1b565b610a32610a2c6b033b2e3c9fd0803ce8000000600361141a565b85610d1b565b610a3c91906113e5565b90506000610a8a7f00000000000000000000000000000000000000000000000000000000000000147f00000000000000000000000000000000000000000000000000000000000000966113e5565b90506000610a988383610d1b565b610ac2907f000000000000000000000000000000000000000000000000000000000000001461146c565b98975050505050505050565b60006201ffff6002610809610b33565b6000818303610aef575060006103ca565b81831115610b0857610b0182846113e5565b9050610b15565b610b1283836113e5565b90505b82610b226127108361141a565b610b2c9190611431565b9392505050565b6040517fb5c736e4000000000000000000000000000000000000000000000000000000008152600160048201526000907f0000000000000000000000000b1a513ee24972daef112bc777a5610d4325c9e773ffffffffffffffffffffffffffffffffffffffff169063b5c736e4906024015b602060405180830381865afa158015610bc2573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906103ea919061147f565b6000806000610bf3610ca5565b610bfb610b33565b91509150633fffffff607082901c16925060f881901c600116600003610c4e5782600003610c3e5763ffffffff605983901c1660ff605184901c161b9250610c5b565b610c4783610d34565b9250610c5b565b610c588284610dd0565b92505b620fffff60b482901c1660ff60ac83901c161b80841115610c7e57809350610c9f565b620fffff60d083901c1660ff60c884901c161b80851015610c9d578094505b505b50505090565b6040517fb5c736e4000000000000000000000000000000000000000000000000000000008152600060048201819052907f0000000000000000000000000b1a513ee24972daef112bc777a5610d4325c9e773ffffffffffffffffffffffffffffffffffffffff169063b5c736e490602401610ba5565b60006b033b2e3c9fd0803ce8000000610b22838561141a565b6000610d607f0000000000000000000000004ec7b668baf70d4a4b0fc7941a7708a07b6d45be83610ebd565b73ffffffffffffffffffffffffffffffffffffffff1663f763f7a66040518163ffffffff1660e01b81526004016020604051808303816000875af1158015610dac573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906103ca919061147f565b600063ffffffff605984901c1660ff605185901c161b81610def611327565b90506401ffffffff80821690620fffff602184901c811691603585901c90911690607989901c16838111610e235783610e25565b805b9050610e3088610d34565b96506000610e41620f42408461141a565b610e4b83426113e5565b610e55868a61141a565b610e5f919061141a565b610e699190611431565b905086881115610e9057610e7d818861146c565b965086881115610e8b578697505b610eb0565b808711610e9e576000610ea2565b8087035b965086881015610eb0578697505b5050505050505092915050565b6000606082600003610ed35760009150506103ca565b607f8311610f9c576040517fd60000000000000000000000000000000000000000000000000000000000000060208201527f940000000000000000000000000000000000000000000000000000000000000060218201527fffffffffffffffffffffffffffffffffffffffff000000000000000000000000606086901b16602282015260f884901b7fff000000000000000000000000000000000000000000000000000000000000001660368201526037015b6040516020818303038152906040529050611318565b60ff8311611079576040517fd70000000000000000000000000000000000000000000000000000000000000060208201527f940000000000000000000000000000000000000000000000000000000000000060218201527fffffffffffffffffffffffffffffffffffffffff000000000000000000000000606086901b1660228201527f8100000000000000000000000000000000000000000000000000000000000000603682015260f884901b7fff00000000000000000000000000000000000000000000000000000000000000166037820152603801610f86565b61ffff8311611157576040517fd80000000000000000000000000000000000000000000000000000000000000060208201527f940000000000000000000000000000000000000000000000000000000000000060218201527fffffffffffffffffffffffffffffffffffffffff000000000000000000000000606086901b1660228201527f820000000000000000000000000000000000000000000000000000000000000060368201527fffff00000000000000000000000000000000000000000000000000000000000060f085901b166037820152603901610f86565b62ffffff8311611236576040517fd90000000000000000000000000000000000000000000000000000000000000060208201527f940000000000000000000000000000000000000000000000000000000000000060218201527fffffffffffffffffffffffffffffffffffffffff000000000000000000000000606086901b1660228201527f830000000000000000000000000000000000000000000000000000000000000060368201527fffffff000000000000000000000000000000000000000000000000000000000060e885901b166037820152603a01610f86565b6040517fda0000000000000000000000000000000000000000000000000000000000000060208201527f940000000000000000000000000000000000000000000000000000000000000060218201527fffffffffffffffffffffffffffffffffffffffff000000000000000000000000606086901b1660228201527f840000000000000000000000000000000000000000000000000000000000000060368201527fffffffff0000000000000000000000000000000000000000000000000000000060e085901b166037820152603b0160405160208183030381529060405290505b80516020909101209392505050565b6040517fb5c736e4000000000000000000000000000000000000000000000000000000008152600860048201526000907f0000000000000000000000000b1a513ee24972daef112bc777a5610d4325c9e773ffffffffffffffffffffffffffffffffffffffff169063b5c736e490602401610ba5565b6000602082840312156113af57600080fd5b5035919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b818103818111156103ca576103ca6113b6565b60006020828403121561140a57600080fd5b81518015158114610b2c57600080fd5b80820281158282048414176103ca576103ca6113b6565b600082611467577f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b500490565b808201808211156103ca576103ca6113b6565b60006020828403121561149157600080fd5b505191905056fea264697066735822122072062e5c59dd2b93702843bed2b27a1a20ad9de614f62f32b4a09f3dfe416c5264736f6c63430008150033
Verified Source Code Full Match
Compiler: v0.8.21+commit.d9974bed
EVM: paris
Optimization: Yes (10000000 runs)
main.sol 410 lines
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
import { Error } from "../error.sol";
import { ErrorTypes } from "../errorTypes.sol";
import { AddressCalcs } from "../../libraries/addressCalcs.sol";
import { DexSlotsLink } from "../../libraries/dexSlotsLink.sol";
import { IFluidDexT1 } from "../../protocols/dex/interfaces/iDexT1.sol";
import { IFluidReserveContract } from "../../reserve/interfaces/iReserveContract.sol";
interface IFluidDexT1Admin {
/// @notice sets a new fee and revenue cut for a certain dex
/// @param fee_ new fee (scaled so that 1% = 10000)
/// @param revenueCut_ new revenue cut
function updateFeeAndRevenueCut(uint fee_, uint revenueCut_) external;
}
interface ICenterPrice {
/// @notice Retrieves the center price for the pool
/// @dev This function is marked as non-constant (potentially state-changing) to allow flexibility in price fetching mechanisms.
/// While typically used as a read-only operation, this design permits write operations if needed for certain token pairs
/// (e.g., fetching up-to-date exchange rates that may require state changes).
/// @return price The current price ratio of token1 to token0, expressed with 27 decimal places
function centerPrice() external returns (uint price);
}
abstract contract Events is Error {
/// @notice emitted when rebalancer successfully changes the fee and revenue cut
event LogRebalanceFeeAndRevenueCut(address dex, uint fee, uint revenueCut);
}
abstract contract Constants is Events {
uint256 internal constant FOUR_DECIMALS = 1e4;
uint256 internal constant SIX_DECIMALS = 1e6;
uint256 internal constant SCALE = 1e27;
/// @notice Whether the center price is active
bool public immutable CENTER_PRICE_ACTIVE;
uint256 internal constant X7 = 0x7f;
uint256 internal constant X17 = 0x1ffff;
uint256 internal constant X20 = 0xfffff;
uint256 internal constant X28 = 0xfffffff;
uint256 internal constant X30 = 0x3fffffff;
uint256 internal constant X33 = 0x1ffffffff;
uint256 internal constant X40 = 0xffffffffff;
uint256 internal constant DEFAULT_EXPONENT_SIZE = 8;
uint256 internal constant DEFAULT_EXPONENT_MASK = 0xFF;
/// @dev Address of contract used for deploying center price & hook related contract
address internal immutable DEPLOYER_CONTRACT;
uint256 public immutable MIN_FEE; // e.g. 10 -> 0.001%
uint256 public immutable MAX_FEE; // e.g. 100 -> 0.01%
uint256 public immutable MIN_DEVIATION; // in 1e27 scale, e.g. 3e23 -> 0.003
uint256 public immutable MAX_DEVIATION; // in 1e27 scale, e.g. 1e24 -> 0.01
uint256 public immutable UPDATE_FEE_TRIGGER_BUFFER = 10; // e.g. 1e4 -> 1%
address public immutable DEX;
IFluidReserveContract public immutable RESERVE_CONTRACT;
}
abstract contract DexHelpers is Constants {
modifier validAddress(address value_) {
if (value_ == address(0)) {
revert FluidConfigError(ErrorTypes.DexFeeHandler__InvalidParams);
}
_;
}
constructor(
address dex_,
address deployerContract_,
bool isCenterPriceActive_
) validAddress(dex_) validAddress(deployerContract_) {
DEX = dex_;
DEPLOYER_CONTRACT = deployerContract_;
CENTER_PRICE_ACTIVE = isCenterPriceActive_;
}
function _getCenterPriceShift() internal view returns (uint256) {
return IFluidDexT1(DEX).readFromStorage(bytes32(DexSlotsLink.DEX_CENTER_PRICE_SHIFT_SLOT));
}
function _getDexVariables() internal view returns (uint256) {
return IFluidDexT1(DEX).readFromStorage(bytes32(DexSlotsLink.DEX_VARIABLES_SLOT));
}
function _getDexVariables2() internal view returns (uint256) {
return IFluidDexT1(DEX).readFromStorage(bytes32(DexSlotsLink.DEX_VARIABLES2_SLOT));
}
function _getCenterPriceFromCenterPriceAddress(uint256 centerPriceNonce_) internal returns (uint256) {
return ICenterPrice(AddressCalcs.addressCalc(DEPLOYER_CONTRACT, centerPriceNonce_)).centerPrice();
}
function _calcCenterPrice(uint dexVariables_, uint centerPriceNonce_) internal returns (uint newCenterPrice_) {
uint oldCenterPrice_ = (dexVariables_ >> 81) & X40;
oldCenterPrice_ = (oldCenterPrice_ >> DEFAULT_EXPONENT_SIZE) << (oldCenterPrice_ & DEFAULT_EXPONENT_MASK);
uint centerPriceShift_ = _getCenterPriceShift();
uint startTimeStamp_ = centerPriceShift_ & X33;
uint percent_ = (centerPriceShift_ >> 33) & X20;
uint time_ = (centerPriceShift_ >> 53) & X20;
uint fromTimeStamp_ = (dexVariables_ >> 121) & X33;
fromTimeStamp_ = fromTimeStamp_ > startTimeStamp_ ? fromTimeStamp_ : startTimeStamp_;
newCenterPrice_ = _getCenterPriceFromCenterPriceAddress(centerPriceNonce_);
uint priceShift_ = (oldCenterPrice_ * percent_ * (block.timestamp - fromTimeStamp_)) / (time_ * SIX_DECIMALS);
if (newCenterPrice_ > oldCenterPrice_) {
// shift on positive side
oldCenterPrice_ += priceShift_;
if (newCenterPrice_ > oldCenterPrice_) {
newCenterPrice_ = oldCenterPrice_;
}
} else {
unchecked {
oldCenterPrice_ = oldCenterPrice_ > priceShift_ ? oldCenterPrice_ - priceShift_ : 0;
// In case of oldCenterPrice_ ending up 0, which could happen when a lot of time has passed (pool has no swaps for many days or weeks)
// then below we get into the else logic which will fully conclude shifting and return newCenterPrice_
// as it was fetched from the external center price source.
// not ideal that this would ever happen unless the pool is not in use and all/most users have left leaving not enough liquidity to trade on
}
if (newCenterPrice_ < oldCenterPrice_) {
newCenterPrice_ = oldCenterPrice_;
}
}
}
function _fetchCenterPrice() internal returns (uint256 centerPrice_) {
(uint256 dexVariables_, uint256 dexVariables2_) = (_getDexVariables(), _getDexVariables2());
// centerPrice_ => center price hook
centerPrice_ = (dexVariables2_ >> 112) & X30;
// whether centerPrice shift is active or not
if (((dexVariables2_ >> 248) & 1) == 0) {
if (centerPrice_ == 0) {
centerPrice_ = (dexVariables_ >> 81) & X40;
centerPrice_ = (centerPrice_ >> DEFAULT_EXPONENT_SIZE) << (centerPrice_ & DEFAULT_EXPONENT_MASK);
} else {
// center price should be fetched from external source. For exmaple, in case of wstETH <> ETH pool,
// we would want the center price to be pegged to wstETH exchange rate into ETH
centerPrice_ = _getCenterPriceFromCenterPriceAddress(centerPrice_);
}
} else {
// an active centerPrice_ shift is going on
centerPrice_ = _calcCenterPrice(dexVariables_, centerPrice_);
}
{
uint maxCenterPrice_ = (dexVariables2_ >> 172) & X28;
maxCenterPrice_ = (maxCenterPrice_ >> DEFAULT_EXPONENT_SIZE) << (maxCenterPrice_ & DEFAULT_EXPONENT_MASK);
if (centerPrice_ > maxCenterPrice_) {
// if center price is greater than max center price
centerPrice_ = maxCenterPrice_;
} else {
// check if center price is less than min center price
uint minCenterPrice_ = (dexVariables2_ >> 200) & X28;
minCenterPrice_ =
(minCenterPrice_ >> DEFAULT_EXPONENT_SIZE) <<
(minCenterPrice_ & DEFAULT_EXPONENT_MASK);
if (centerPrice_ < minCenterPrice_) {
centerPrice_ = minCenterPrice_;
}
}
}
}
function _getDexFee() internal view returns (uint256 fee_) {
return (_getDexVariables2() >> 2) & X17;
}
function getDexCenterPrice() public returns (uint256) {
return _fetchCenterPrice();
}
/// @notice returns the revenue cut for the dex
function getDexRevenueCut() public view returns (uint256 revenueCut_) {
return (_getDexVariables2() >> 19) & X7;
}
/// @notice returns the fee and revenue cut for the dex
function getDexFeeAndRevenueCut() public view returns (uint256 fee_, uint256 revenueCut_) {
uint256 dexVariables2_ = _getDexVariables2();
fee_ = (dexVariables2_ >> 2) & X17;
revenueCut_ = (dexVariables2_ >> 19) & X7;
}
/// @notice returns the last stored prices of the pool and the last interaction time stamp
function getDexVariables()
public
view
returns (uint256 lastToLastStoredPrice_, uint256 lastStoredPriceOfPool_, uint256 lastInteractionTimeStamp_)
{
uint256 dexVariables_ = _getDexVariables();
lastToLastStoredPrice_ = (dexVariables_ >> 1) & X40;
lastToLastStoredPrice_ =
(lastToLastStoredPrice_ >> DEFAULT_EXPONENT_SIZE) <<
(lastToLastStoredPrice_ & DEFAULT_EXPONENT_MASK);
lastStoredPriceOfPool_ = (dexVariables_ >> 41) & X40;
lastStoredPriceOfPool_ =
(lastStoredPriceOfPool_ >> DEFAULT_EXPONENT_SIZE) <<
(lastStoredPriceOfPool_ & DEFAULT_EXPONENT_MASK);
lastInteractionTimeStamp_ = (dexVariables_ >> 121) & X33;
}
}
abstract contract DynamicFee is DexHelpers {
constructor(uint256 _minFee, uint256 _maxFee, uint256 _minDeviation, uint256 _maxDeviation) {
// check for zero values
if (_minFee == 0 || _maxFee == 0 || _minDeviation == 0 || _maxDeviation == 0)
revert FluidConfigError(ErrorTypes.DexFeeHandler__InvalidParams);
// check that max fee is not greater or equal to 1%
if (_maxFee >= 1e4) revert FluidConfigError(ErrorTypes.DexFeeHandler__InvalidParams);
// check that min deviation is not greater than max deviation
if (_minDeviation > _maxDeviation) revert FluidConfigError(ErrorTypes.DexFeeHandler__InvalidParams);
// check that min fee is not greater than max fee
if (_minFee > _maxFee) revert FluidConfigError(ErrorTypes.DexFeeHandler__InvalidParams);
MIN_FEE = _minFee;
MAX_FEE = _maxFee;
MIN_DEVIATION = _minDeviation;
MAX_DEVIATION = _maxDeviation;
}
/// @notice Calculates the deviation from the given price.
function getDeviationFromPrice(uint256 price_) public returns (uint256) {
if (CENTER_PRICE_ACTIVE) {
uint256 centerPrice_ = _fetchCenterPrice();
uint256 deviation_ = price_ > centerPrice_ ? price_ - centerPrice_ : centerPrice_ - price_;
return (deviation_ * SCALE) / centerPrice_;
} else {
return price_ > SCALE ? price_ - SCALE : SCALE - price_;
}
}
/// @notice Calculates the dynamic fee based on the given price.
function dynamicFeeFromPrice(uint256 price) external returns (uint256) {
return _computeDynamicFee(getDeviationFromPrice(price));
}
/// @notice Calculates the dynamic fee based on the given deviation.
function dynamicFeeFromDeviation(uint256 deviation) external view returns (uint256) {
return _computeDynamicFee(deviation);
}
/**
* @dev Internal helper that implements a smooth-step curve for fee calculation
* @param deviation Deviation from the target price in SCALE (1e27)
* @return Fee in basis points (1e4 = 1%)
*/
function _computeDynamicFee(uint256 deviation) internal view returns (uint256) {
if (deviation <= MIN_DEVIATION) {
return MIN_FEE;
} else if (deviation >= MAX_DEVIATION) {
return MAX_FEE;
} else {
// Calculate normalized position between min and max deviation (0 to 1 in SCALE)
uint256 alpha = ((deviation - MIN_DEVIATION) * SCALE) / (MAX_DEVIATION - MIN_DEVIATION);
// Smooth step formula: 3x² - 2x³
// https://en.wikipedia.org/wiki/Smoothstep
uint256 alpha2 = _scaleMul(alpha, alpha);
uint256 alpha3 = _scaleMul(alpha2, alpha);
uint256 smooth = _scaleMul(3 * SCALE, alpha2) - _scaleMul(2 * SCALE, alpha3);
uint256 feeDelta = MAX_FEE - MIN_FEE;
uint256 interpolatedFee = MIN_FEE + (_scaleMul(smooth, feeDelta));
return interpolatedFee;
}
}
function _scaleMul(uint256 a, uint256 b) internal pure returns (uint256) {
return (a * b) / SCALE;
}
}
abstract contract FluidDexFeeHandlerHelpers is DynamicFee {
modifier onlyRebalancer() {
if (!RESERVE_CONTRACT.isRebalancer(msg.sender)) {
revert FluidConfigError(ErrorTypes.DexFeeHandler__Unauthorized);
}
_;
}
constructor(
uint256 minFee_,
uint256 maxFee_,
uint256 minDeviation_,
uint256 maxDeviation_,
address dex_,
address deployerContract_,
IFluidReserveContract reserveContract_,
bool centerPriceActive_
)
validAddress(address(reserveContract_))
DexHelpers(dex_, deployerContract_, centerPriceActive_)
DynamicFee(minFee_, maxFee_, minDeviation_, maxDeviation_)
{
RESERVE_CONTRACT = reserveContract_;
}
/// @notice returns the dynamic fee for the dex based on the last stored price of the pool
function getDexDynamicFee() public returns (uint256) {
(
uint256 lastToLastStoredPrice_,
uint256 lastStoredPriceOfPool_,
uint256 lastInteractionTimeStamp_
) = getDexVariables();
if (lastInteractionTimeStamp_ == block.timestamp) lastStoredPriceOfPool_ = lastToLastStoredPrice_;
return _computeDynamicFee(getDeviationFromPrice(lastStoredPriceOfPool_));
}
function _configPercentDiff(
uint256 currentFee_,
uint256 newFee_
) internal pure returns (uint256 configPercentDiff_) {
if (currentFee_ == newFee_) {
return 0;
}
if (currentFee_ > newFee_) configPercentDiff_ = currentFee_ - newFee_;
else configPercentDiff_ = newFee_ - currentFee_;
return (configPercentDiff_ * FOUR_DECIMALS) / currentFee_;
}
}
contract FluidDexFeeHandler is FluidDexFeeHandlerHelpers {
constructor(
uint256 minFee_,
uint256 maxFee_,
uint256 minDeviation_,
uint256 maxDeviation_,
address dex_,
address deployerContract_,
IFluidReserveContract reserveContract_,
bool centerPriceActive_
)
FluidDexFeeHandlerHelpers(
minFee_,
maxFee_,
minDeviation_,
maxDeviation_,
dex_,
deployerContract_,
reserveContract_,
centerPriceActive_
)
{}
/// @notice rebalances the fee
function rebalance() external onlyRebalancer {
uint256 newFee_ = getDexDynamicFee();
(uint256 currentFee_, uint256 currentRevenueCut_) = getDexFeeAndRevenueCut();
uint256 feePercentageChange_ = _configPercentDiff(currentFee_, newFee_);
// should be more than 0.001% to update
if (feePercentageChange_ > UPDATE_FEE_TRIGGER_BUFFER) {
IFluidDexT1Admin(DEX).updateFeeAndRevenueCut(newFee_, currentRevenueCut_ * FOUR_DECIMALS);
emit LogRebalanceFeeAndRevenueCut(DEX, newFee_, currentRevenueCut_ * FOUR_DECIMALS);
} else {
revert FluidConfigError(ErrorTypes.DexFeeHandler__FeeUpdateNotRequired);
}
}
/// @notice returns how much new config would be different from current config in percent (100 = 1%, 1 = 0.01%).
function relativeConfigPercentDiff() public returns (uint256) {
return _configPercentDiff(_getDexFee(), getDexDynamicFee());
}
/// @notice returns how much new config would be different from current config.
function absoluteConfigDiff() public returns (uint256) {
uint256 newFee_ = getDexDynamicFee();
uint256 oldFee_ = _getDexFee();
return newFee_ > oldFee_ ? newFee_ - oldFee_ : oldFee_ - newFee_;
}
/// @notice returns the new calculated fee
function newConfig() public returns (uint256) {
return getDexDynamicFee();
}
/// @notice returns the currently configured fee
function currentConfig() public view returns (uint256) {
return _getDexFee();
}
}
error.sol 6 lines
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
contract Error {
error FluidConfigError(uint256 errorId_);
}
errorTypes.sol 151 lines
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
library ErrorTypes {
/***********************************|
| ExpandPercentConfigHandler |
|__________________________________*/
/// @notice thrown when an input address is zero
uint256 internal constant ExpandPercentConfigHandler__AddressZero = 100001;
/// @notice thrown when an unauthorized `msg.sender` calls a protected method
uint256 internal constant ExpandPercentConfigHandler__Unauthorized = 100002;
/// @notice thrown when invalid params are passed into a method
uint256 internal constant ExpandPercentConfigHandler__InvalidParams = 100003;
/// @notice thrown when no update is currently needed
uint256 internal constant ExpandPercentConfigHandler__NoUpdate = 100004;
/// @notice thrown when slot is not used, e.g. when borrow token is 0 there is no borrow data
uint256 internal constant ExpandPercentConfigHandler__SlotDoesNotExist = 100005;
/***********************************|
| EthenaRateConfigHandler |
|__________________________________*/
/// @notice thrown when an input address is zero
uint256 internal constant EthenaRateConfigHandler__AddressZero = 100011;
/// @notice thrown when an unauthorized `msg.sender` calls a protected method
uint256 internal constant EthenaRateConfigHandler__Unauthorized = 100012;
/// @notice thrown when invalid params are passed into a method
uint256 internal constant EthenaRateConfigHandler__InvalidParams = 100013;
/// @notice thrown when no update is currently needed
uint256 internal constant EthenaRateConfigHandler__NoUpdate = 100014;
/***********************************|
| MaxBorrowConfigHandler |
|__________________________________*/
/// @notice thrown when an input address is zero
uint256 internal constant MaxBorrowConfigHandler__AddressZero = 100021;
/// @notice thrown when an unauthorized `msg.sender` calls a protected method
uint256 internal constant MaxBorrowConfigHandler__Unauthorized = 100022;
/// @notice thrown when invalid params are passed into a method
uint256 internal constant MaxBorrowConfigHandler__InvalidParams = 100023;
/// @notice thrown when no update is currently needed
uint256 internal constant MaxBorrowConfigHandler__NoUpdate = 100024;
/***********************************|
| BufferRateConfigHandler |
|__________________________________*/
/// @notice thrown when an input address is zero
uint256 internal constant BufferRateConfigHandler__AddressZero = 100031;
/// @notice thrown when an unauthorized `msg.sender` calls a protected method
uint256 internal constant BufferRateConfigHandler__Unauthorized = 100032;
/// @notice thrown when invalid params are passed into a method
uint256 internal constant BufferRateConfigHandler__InvalidParams = 100033;
/// @notice thrown when no update is currently needed
uint256 internal constant BufferRateConfigHandler__NoUpdate = 100034;
/// @notice thrown when rate data version is not supported
uint256 internal constant BufferRateConfigHandler__RateVersionUnsupported = 100035;
/***********************************|
| FluidRatesAuth |
|__________________________________*/
/// @notice thrown when no update is currently needed
uint256 internal constant RatesAuth__NoUpdate = 100041;
/// @notice thrown when an unauthorized `msg.sender` calls a protected method
uint256 internal constant RatesAuth__Unauthorized = 100042;
/// @notice thrown when invalid params are passed into a method
uint256 internal constant RatesAuth__InvalidParams = 100043;
/// @notice thrown when cooldown is not yet expired
uint256 internal constant RatesAuth__CooldownLeft = 100044;
/// @notice thrown when version is invalid
uint256 internal constant RatesAuth__InvalidVersion = 100045;
/***********************************|
| ListTokenAuth |
|__________________________________*/
/// @notice thrown when an unauthorized `msg.sender` calls a protected method
uint256 internal constant ListTokenAuth__Unauthorized = 100051;
/// @notice thrown when invalid params are passed into a method
uint256 internal constant ListTokenAuth_AlreadyInitialized = 100052;
/// @notice thrown when invalid params are passed into a method
uint256 internal constant ListTokenAuth__InvalidParams = 100053;
/***********************************|
| CollectRevenueAuth |
|__________________________________*/
/// @notice thrown when an unauthorized `msg.sender` calls a protected method
uint256 internal constant CollectRevenueAuth__Unauthorized = 100061;
/// @notice thrown when invalid params are passed into a method
uint256 internal constant CollectRevenueAuth__InvalidParams = 100062;
/***********************************|
| FluidWithdrawLimitAuth |
|__________________________________*/
/// @notice thrown when an unauthorized `msg.sender` calls a protected method
uint256 internal constant WithdrawLimitAuth__NoUserSupply = 100071;
/// @notice thrown when an unauthorized `msg.sender` calls a protected method
uint256 internal constant WithdrawLimitAuth__Unauthorized = 100072;
/// @notice thrown when invalid params are passed into a method
uint256 internal constant WithdrawLimitAuth__InvalidParams = 100073;
/// @notice thrown when no more withdrawal limit can be set for the day
uint256 internal constant WithdrawLimitAuth__DailyLimitReached = 100074;
/// @notice thrown when no more withdrawal limit can be set for the hour
uint256 internal constant WithdrawLimitAuth__HourlyLimitReached = 100075;
/// @notice thrown when the withdrawal limit and userSupply difference exceeds 5%
uint256 internal constant WithdrawLimitAuth__ExcessPercentageDifference = 100076;
/***********************************|
| DexFeeHandler |
|__________________________________*/
/// @notice thrown when fee update is not required
uint256 internal constant DexFeeHandler__FeeUpdateNotRequired = 100081;
/// @notice thrown when invalid params are passed into a method
uint256 internal constant DexFeeHandler__InvalidParams = 100082;
/// @notice thrown when an unauthorized `msg.sender` calls
uint256 internal constant DexFeeHandler__Unauthorized = 100083;
}
iProxy.sol 22 lines
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;
interface IProxy {
function setAdmin(address newAdmin_) external;
function setDummyImplementation(address newDummyImplementation_) external;
function addImplementation(address implementation_, bytes4[] calldata sigs_) external;
function removeImplementation(address implementation_) external;
function getAdmin() external view returns (address);
function getDummyImplementation() external view returns (address);
function getImplementationSigs(address impl_) external view returns (bytes4[] memory);
function getSigsImplementation(bytes4 sig_) external view returns (address);
function readFromStorage(bytes32 slot_) external view returns (uint256 result_);
}
addressCalcs.sol 35 lines
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
/// @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))));
}
}
dexSlotsLink.sol 65 lines
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
/// @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_));
}
}
structs.sol 145 lines
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
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;
}
}
iLiquidity.sol 129 lines
//SPDX-License-Identifier: MIT
pragma solidity 0.8.21;
import { IProxy } from "../../infiniteProxy/interfaces/iProxy.sol";
import { Structs as AdminModuleStructs } from "../adminModule/structs.sol";
interface IFluidLiquidityAdmin {
/// @notice adds/removes auths. Auths generally could be contracts which can have restricted actions defined on contract.
/// auths can be helpful in reducing governance overhead where it's not needed.
/// @param authsStatus_ array of structs setting allowed status for an address.
/// status true => add auth, false => remove auth
function updateAuths(AdminModuleStructs.AddressBool[] calldata authsStatus_) external;
/// @notice adds/removes guardians. Only callable by Governance.
/// @param guardiansStatus_ array of structs setting allowed status for an address.
/// status true => add guardian, false => remove guardian
function updateGuardians(AdminModuleStructs.AddressBool[] calldata guardiansStatus_) external;
/// @notice changes the revenue collector address (contract that is sent revenue). Only callable by Governance.
/// @param revenueCollector_ new revenue collector address
function updateRevenueCollector(address revenueCollector_) external;
/// @notice changes current status, e.g. for pausing or unpausing all user operations. Only callable by Auths.
/// @param newStatus_ new status
/// status = 2 -> pause, status = 1 -> resume.
function changeStatus(uint256 newStatus_) external;
/// @notice update tokens rate data version 1. Only callable by Auths.
/// @param tokensRateData_ array of RateDataV1Params with rate data to set for each token
function updateRateDataV1s(AdminModuleStructs.RateDataV1Params[] calldata tokensRateData_) external;
/// @notice update tokens rate data version 2. Only callable by Auths.
/// @param tokensRateData_ array of RateDataV2Params with rate data to set for each token
function updateRateDataV2s(AdminModuleStructs.RateDataV2Params[] calldata tokensRateData_) external;
/// @notice updates token configs: fee charge on borrowers interest & storage update utilization threshold.
/// Only callable by Auths.
/// @param tokenConfigs_ contains token address, fee & utilization threshold
function updateTokenConfigs(AdminModuleStructs.TokenConfig[] calldata tokenConfigs_) external;
/// @notice updates user classes: 0 is for new protocols, 1 is for established protocols.
/// Only callable by Auths.
/// @param userClasses_ struct array of uint256 value to assign for each user address
function updateUserClasses(AdminModuleStructs.AddressUint256[] calldata userClasses_) external;
/// @notice sets user supply configs per token basis. Eg: with interest or interest-free and automated limits.
/// Only callable by Auths.
/// @param userSupplyConfigs_ struct array containing user supply config, see `UserSupplyConfig` struct for more info
function updateUserSupplyConfigs(AdminModuleStructs.UserSupplyConfig[] memory userSupplyConfigs_) external;
/// @notice sets a new withdrawal limit as the current limit for a certain user
/// @param user_ user address for which to update the withdrawal limit
/// @param token_ token address for which to update the withdrawal limit
/// @param newLimit_ new limit until which user supply can decrease to.
/// Important: input in raw. Must account for exchange price in input param calculation.
/// Note any limit that is < max expansion or > current user supply will set max expansion limit or
/// current user supply as limit respectively.
/// - set 0 to make maximum possible withdrawable: instant full expansion, and if that goes
/// below base limit then fully down to 0.
/// - set type(uint256).max to make current withdrawable 0 (sets current user supply as limit).
function updateUserWithdrawalLimit(address user_, address token_, uint256 newLimit_) external;
/// @notice setting user borrow configs per token basis. Eg: with interest or interest-free and automated limits.
/// Only callable by Auths.
/// @param userBorrowConfigs_ struct array containing user borrow config, see `UserBorrowConfig` struct for more info
function updateUserBorrowConfigs(AdminModuleStructs.UserBorrowConfig[] memory userBorrowConfigs_) external;
/// @notice pause operations for a particular user in class 0 (class 1 users can't be paused by guardians).
/// Only callable by Guardians.
/// @param user_ address of user to pause operations for
/// @param supplyTokens_ token addresses to pause withdrawals for
/// @param borrowTokens_ token addresses to pause borrowings for
function pauseUser(address user_, address[] calldata supplyTokens_, address[] calldata borrowTokens_) external;
/// @notice unpause operations for a particular user in class 0 (class 1 users can't be paused by guardians).
/// Only callable by Guardians.
/// @param user_ address of user to unpause operations for
/// @param supplyTokens_ token addresses to unpause withdrawals for
/// @param borrowTokens_ token addresses to unpause borrowings for
function unpauseUser(address user_, address[] calldata supplyTokens_, address[] calldata borrowTokens_) external;
/// @notice collects revenue for tokens to configured revenueCollector address.
/// @param tokens_ array of tokens to collect revenue for
/// @dev Note that this can revert if token balance is < revenueAmount (utilization > 100%)
function collectRevenue(address[] calldata tokens_) external;
/// @notice gets the current updated exchange prices for n tokens and updates all prices, rates related data in storage.
/// @param tokens_ tokens to update exchange prices for
/// @return supplyExchangePrices_ new supply rates of overall system for each token
/// @return borrowExchangePrices_ new borrow rates of overall system for each token
function updateExchangePrices(
address[] calldata tokens_
) external returns (uint256[] memory supplyExchangePrices_, uint256[] memory borrowExchangePrices_);
}
interface IFluidLiquidityLogic is IFluidLiquidityAdmin {
/// @notice Single function which handles supply, withdraw, borrow & payback
/// @param token_ address of token (0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE for native)
/// @param supplyAmount_ if +ve then supply, if -ve then withdraw, if 0 then nothing
/// @param borrowAmount_ if +ve then borrow, if -ve then payback, if 0 then nothing
/// @param withdrawTo_ if withdrawal then to which address
/// @param borrowTo_ if borrow then to which address
/// @param callbackData_ callback data passed to `liquidityCallback` method of protocol
/// @return memVar3_ updated supplyExchangePrice
/// @return memVar4_ updated borrowExchangePrice
/// @dev to trigger skipping in / out transfers (gas optimization):
/// - ` callbackData_` MUST be encoded so that "from" address is the last 20 bytes in the last 32 bytes slot,
/// also for native token operations where liquidityCallback is not triggered!
/// from address must come at last position if there is more data. I.e. encode like:
/// abi.encode(otherVar1, otherVar2, FROM_ADDRESS). Note dynamic types used with abi.encode come at the end
/// so if dynamic types are needed, you must use abi.encodePacked to ensure the from address is at the end.
/// - this "from" address must match withdrawTo_ or borrowTo_ and must be == `msg.sender`
/// - `callbackData_` must in addition to the from address as described above include bytes32 SKIP_TRANSFERS
/// in the slot before (bytes 32 to 63)
/// - `msg.value` must be 0.
/// - Amounts must be either:
/// - supply(+) == borrow(+), withdraw(-) == payback(-).
/// - Liquidity must be on the winning side (deposit < borrow OR payback < withdraw).
function operate(
address token_,
int256 supplyAmount_,
int256 borrowAmount_,
address withdrawTo_,
address borrowTo_,
bytes calldata callbackData_
) external payable returns (uint256 memVar3_, uint256 memVar4_);
}
interface IFluidLiquidity is IProxy, IFluidLiquidityLogic {}
iDexT1.sol 294 lines
// 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 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 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_
);
}
iReserveContract.sol 31 lines
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
import { IFluidLiquidity } from "../../liquidity/interfaces/iLiquidity.sol";
interface IFluidReserveContract {
function isRebalancer(address user) external returns (bool);
function initialize(
address[] memory _auths,
address[] memory _rebalancers,
IFluidLiquidity liquidity_,
address owner_
) external;
function rebalanceFToken(address protocol_) external;
function rebalanceVault(address protocol_) external;
function transferFunds(address token_) external;
function getProtocolTokens(address protocol_) external;
function updateAuth(address auth_, bool isAuth_) external;
function updateRebalancer(address rebalancer_, bool isRebalancer_) external;
function approve(address[] memory protocols_, address[] memory tokens_, uint256[] memory amounts_) external;
function revoke(address[] memory protocols_, address[] memory tokens_) external;
}
Read Contract
CENTER_PRICE_ACTIVE 0x1c37ae38 → bool
DEX 0x80935aa9 → address
MAX_DEVIATION 0xb05f233a → uint256
MAX_FEE 0xbc063e1a → uint256
MIN_DEVIATION 0x274d713c → uint256
MIN_FEE 0x76c7a3c7 → uint256
RESERVE_CONTRACT 0x6449b8d7 → address
UPDATE_FEE_TRIGGER_BUFFER 0xf28a169d → uint256
currentConfig 0xcae58807 → uint256
dynamicFeeFromDeviation 0x76aaa6ce → uint256
getDexFeeAndRevenueCut 0x2e2e5b47 → uint256, uint256
getDexRevenueCut 0xbc669afb → uint256
getDexVariables 0xd50741f7 → uint256, uint256, uint256
Write Contract 8 functions
These functions modify contract state and require a wallet transaction to execute.
absoluteConfigDiff 0x724d7625
No parameters
returns: uint256
dynamicFeeFromPrice 0x02918d13
uint256 price
returns: uint256
getDeviationFromPrice 0x9b5075a4
uint256 price_
returns: uint256
getDexCenterPrice 0x813e6745
No parameters
returns: uint256
getDexDynamicFee 0xbe47bd83
No parameters
returns: uint256
newConfig 0x941da44e
No parameters
returns: uint256
rebalance 0x7d7c2a1c
No parameters
relativeConfigPercentDiff 0x1d156e8b
No parameters
returns: uint256
Recent Transactions
No transactions found for this address