Skip to content

Commit

Permalink
feat: flag to enable/disable flash loans
Browse files Browse the repository at this point in the history
  • Loading branch information
thaixuandang committed Jul 15, 2024
1 parent ff226c1 commit dd82739
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/core/KatanaV3Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ contract KatanaV3Factory is IKatanaV3Factory, KatanaV3PoolDeployer {
address public override owner;
/// @inheritdoc IKatanaV3Factory
address public override treasury;
/// @inheritdoc IKatanaV3Factory
bool public override flashLoanEnabled;

/// @inheritdoc IKatanaV3Factory
mapping(uint24 => int24) public override feeAmountTickSpacing;
Expand Down Expand Up @@ -83,6 +85,11 @@ contract KatanaV3Factory is IKatanaV3Factory, KatanaV3PoolDeployer {
treasury = _treasury;
}

function toggleFlashLoanPermission() external override {
require(msg.sender == owner);
flashLoanEnabled = !flashLoanEnabled;
}

/// @inheritdoc IKatanaV3Factory
function enableFeeAmount(uint24 fee, int24 tickSpacing, uint16 feeProtocol) public override {
require(msg.sender == owner);
Expand Down
15 changes: 11 additions & 4 deletions src/core/KatanaV3Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ contract KatanaV3Pool is IKatanaV3Pool {
/// @inheritdoc IKatanaV3PoolState
Oracle.Observation[65535] public override observations;

// These immutable constants are set when deploy the beacon proxy of the pool and cannot be changed unless upgrading.
/// @inheritdoc IKatanaV3PoolImmutables
address public override factory;
address public immutable override factory;

// These below immutable constants are set when deploying the beacon proxy of the pool and
// cannot be changed unless upgraded.
/// @inheritdoc IKatanaV3PoolImmutables
address public override token0;
/// @inheritdoc IKatanaV3PoolImmutables
Expand Down Expand Up @@ -118,6 +120,7 @@ contract KatanaV3Pool is IKatanaV3Pool {
}

constructor() {
factory = msg.sender;
// disable immutables initialization
_immutablesInitialized = true;
}
Expand All @@ -127,7 +130,9 @@ contract KatanaV3Pool is IKatanaV3Pool {
{
require(!_immutablesInitialized);

(factory, token0, token1, fee, tickSpacing) = (factory_, token0_, token1_, fee_, tickSpacing_);
require(factory_ == factory, "IF");

(token0, token1, fee, tickSpacing) = (token0_, token1_, fee_, tickSpacing_);

maxLiquidityPerTick = Tick.tickSpacingToMaxLiquidityPerTick(tickSpacing_);

Expand Down Expand Up @@ -349,7 +354,7 @@ contract KatanaV3Pool is IKatanaV3Pool {
uint256 _feeGrowthGlobal0X128 = feeGrowthGlobal0X128; // SLOAD for gas optimization
uint256 _feeGrowthGlobal1X128 = feeGrowthGlobal1X128; // SLOAD for gas optimization
uint128 _maxLiquidityPerTick = maxLiquidityPerTick; // SLOAD for gas optimization

// if we need to update the ticks, do it
bool flippedLower;
bool flippedUpper;
Expand Down Expand Up @@ -736,6 +741,8 @@ contract KatanaV3Pool is IKatanaV3Pool {

/// @inheritdoc IKatanaV3PoolActions
function flash(address recipient, uint256 amount0, uint256 amount1, bytes calldata data) external override lock {
require(IKatanaV3Factory(factory).flashLoanEnabled(), "FD");

uint128 _liquidity = liquidity;
require(_liquidity > 0, "L");

Expand Down
9 changes: 9 additions & 0 deletions src/core/interfaces/IKatanaV3Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ interface IKatanaV3Factory {
/// @return The address of the treasury
function treasury() external view returns (address);

/// @notice Returns whether flash loans are enabled
/// @dev Can be changed by the current owner via toggleFlashLoanPermission
/// @return Whether flash loans are enabled
function flashLoanEnabled() external view returns (bool);

/// @notice Returns the tick spacing for a given fee amount, if enabled, or 0 if not enabled
/// @dev A fee amount can never be removed, so this value should be hard coded or cached in the calling context
/// @param fee The enabled fee, denominated in hundredths of a bip. Returns 0 in case of unenabled fee
Expand Down Expand Up @@ -84,6 +89,10 @@ interface IKatanaV3Factory {
/// @param _treasury The new treasury address
function setTreasury(address _treasury) external;

/// @notice Toggles the ability to call the `flash` function on KatanaV3Pool
/// @dev Must be called by the current owner
function toggleFlashLoanPermission() external;

/// @notice Enables a fee amount with the given tickSpacing
/// @dev Fee amounts may never be removed once enabled
/// @param fee The fee amount to enable, denominated in hundredths of a bip (i.e. 1e-6)
Expand Down
7 changes: 6 additions & 1 deletion src/core/interfaces/pool/IKatanaV3PoolEvents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ interface IKatanaV3PoolEvents {
/// @param feeProtocolDenominatorOld The denominator of the previous value of protocol fee
/// @param feeProtocolNumeratorNew The numerator of the updated value of protocol fee
/// @param feeProtocolDenominatorNew The denominator of the oupdated value of protocol fee
event SetFeeProtocol(uint8 feeProtocolNumeratorOld, uint8 feeProtocolDenominatorOld, uint8 feeProtocolNumeratorNew, uint8 feeProtocolDenominatorNew);
event SetFeeProtocol(
uint8 feeProtocolNumeratorOld,
uint8 feeProtocolDenominatorOld,
uint8 feeProtocolNumeratorNew,
uint8 feeProtocolDenominatorNew
);

/// @notice Emitted when the collected protocol fees are withdrawn by the factory owner
/// @param sender The address that collects the protocol fees
Expand Down

0 comments on commit dd82739

Please sign in to comment.