Skip to content

Commit

Permalink
Merge pull request #78 from neptune-mutual-blue/fix/withdraw-from-dis…
Browse files Browse the repository at this point in the history
…abled

Withdraw from Disabled
  • Loading branch information
heyaibi authored May 30, 2022
2 parents 5c92bb0 + 3075ec6 commit 94a76d9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
10 changes: 10 additions & 0 deletions contracts/core/liquidity/LiquidityEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ contract LiquidityEngine is ILiquidityEngine, Recoverable {
emit StrategyDisabled(strategy);
}

function deleteStrategy(address strategy) external override nonReentrant {
// @suppress-address-trust-issue The address strategy can be trusted
// because this function can only be invoked by a liquidity manager.
s.mustNotBePaused();
AccessControlLibV1.mustBeLiquidityManager(s);

s.deleteStrategyInternal(strategy);
emit StrategyDeleted(strategy);
}

function setLendingPeriods(
bytes32 coverKey,
uint256 lendingPeriod,
Expand Down
3 changes: 3 additions & 0 deletions contracts/interfaces/ILiquidityEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pragma solidity 0.8.0;
interface ILiquidityEngine is IMember {
event StrategyAdded(address indexed strategy);
event StrategyDisabled(address indexed strategy);
event StrategyDeleted(address indexed strategy);
event LendingPeriodSet(bytes32 indexed coverKey, uint256 lendingPeriod, uint256 withdrawalWindow);
event LiquidityStateUpdateIntervalSet(uint256 duration);
event MaxLendingRatioSet(uint256 ratio);
Expand All @@ -16,6 +17,8 @@ interface ILiquidityEngine is IMember {

function disableStrategy(address strategy) external;

function deleteStrategy(address strategy) external;

function setLendingPeriods(
bytes32 coverKey,
uint256 lendingPeriod,
Expand Down
24 changes: 22 additions & 2 deletions contracts/libraries/StrategyLibV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ library StrategyLibV1 {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_LENDING_STRATEGY_ACTIVE, strategyAddress));
}

function _getIsDisabledStrategyKey(address strategyAddress) private pure returns (bytes32) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_LENDING_STRATEGY_DISABLED, strategyAddress));
}

function disableStrategyInternal(IStore s, address toFind) external {
// @suppress-address-trust-issue Check caller.
_deleteStrategy(s, toFind);
_disableStrategy(s, toFind);

s.setAddressArrayByKey(ProtoUtilV1.NS_LENDING_STRATEGY_DISABLED, toFind);
}

function deleteStrategyInternal(IStore s, address toFind) external {
// @suppress-address-trust-issue Check caller.
_deleteStrategy(s, toFind);
}

function addStrategiesInternal(IStore s, address[] memory strategies) external {
for (uint256 i = 0; i < strategies.length; i++) {
address strategy = strategies[i];
Expand Down Expand Up @@ -99,14 +108,25 @@ library StrategyLibV1 {
emit StrategyAdded(deployedOn);
}

function _deleteStrategy(IStore s, address toFind) private {
function _disableStrategy(IStore s, address toFind) private {
bytes32 key = ProtoUtilV1.NS_LENDING_STRATEGY_ACTIVE;

uint256 pos = s.getAddressArrayItemPosition(key, toFind);
require(pos > 0, "Invalid strategy");

s.deleteAddressArrayItem(key, toFind);
s.setBoolByKey(_getIsActiveStrategyKey(toFind), false);
s.setBoolByKey(_getIsDisabledStrategyKey(toFind), true);
}

function _deleteStrategy(IStore s, address toFind) private {
bytes32 key = ProtoUtilV1.NS_LENDING_STRATEGY_DISABLED;

uint256 pos = s.getAddressArrayItemPosition(key, toFind);
require(pos > 0, "Invalid strategy");

s.deleteAddressArrayItem(key, toFind);
s.setBoolByKey(_getIsDisabledStrategyKey(toFind), false);
}

function getDisabledStrategiesInternal(IStore s) external view returns (address[] memory strategies) {
Expand Down
18 changes: 12 additions & 6 deletions contracts/libraries/ValidationLibV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,30 @@ library ValidationLibV1 {
require(senderIsStrategyContract == true, "Not a strategy contract");
}

function callerMustBeStrategyContract(IStore s, address caller) external view {
bool callerIsStrategyContract = s.getBoolByKey(_getIsActiveStrategyKey(caller));
require(callerIsStrategyContract == true, "Not a strategy contract");
function callerMustBeStrategyContract(IStore s, address caller) public view {
bool isActive = s.getBoolByKey(_getIsActiveStrategyKey(caller));
bool wasDisabled = s.getBoolByKey(_getIsDisabledStrategyKey(caller));

require(isActive == true || wasDisabled == true, "Not a strategy contract");
}

function callerMustBeSpecificStrategyContract(
IStore s,
address caller,
bytes32 /*strategyName*/
bytes32 strategyName
) external view {
bool callerIsStrategyContract = s.getBoolByKey(_getIsActiveStrategyKey(caller));
require(callerIsStrategyContract == true, "Not a strategy contract");
callerMustBeStrategyContract(s, caller);
require(IMember(caller).getName() == strategyName, "Access denied");
}

function _getIsActiveStrategyKey(address strategyAddress) private pure returns (bytes32) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_LENDING_STRATEGY_ACTIVE, strategyAddress));
}

function _getIsDisabledStrategyKey(address strategyAddress) private pure returns (bytes32) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_LENDING_STRATEGY_DISABLED, strategyAddress));
}

function senderMustBeProtocolMember(IStore s) external view {
require(s.isProtocolMember(msg.sender), "Forbidden");
}
Expand Down

0 comments on commit 94a76d9

Please sign in to comment.