Skip to content

Commit

Permalink
build: utils (#32)
Browse files Browse the repository at this point in the history
* build: util contracts

* fix: name

* feat: virtualize

* fix: two step gov
  • Loading branch information
Schlagonia authored Nov 14, 2023
1 parent ed73d39 commit b1ed340
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 33 deletions.
14 changes: 9 additions & 5 deletions src/AprOracle/AprOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ contract AprOracle {
function getStrategyApr(
address _strategy,
int256 _debtChange
) public view returns (uint256) {
) public view virtual returns (uint256) {
// Get the oracle set for this specific strategy.
address oracle = oracles[_strategy];

Expand All @@ -70,7 +70,9 @@ contract AprOracle {
* @param _strategy Address of the strategy.
* @return . The current weighted APR of the strategy.
*/
function weightedApr(address _strategy) external view returns (uint256) {
function weightedApr(
address _strategy
) external view virtual returns (uint256) {
return
IStrategy(_strategy).totalAssets() * getStrategyApr(_strategy, 0);
}
Expand All @@ -84,7 +86,7 @@ contract AprOracle {
* @param _strategy Address of the strategy.
* @param _oracle Address of the APR Oracle.
*/
function setOracle(address _strategy, address _oracle) external {
function setOracle(address _strategy, address _oracle) external virtual {
require(msg.sender == IStrategy(_strategy).management(), "!authorized");

oracles[_strategy] = _oracle;
Expand All @@ -100,7 +102,9 @@ contract AprOracle {
* @param _vault The address of the vault or strategy.
* @return apr The current apr expressed as 1e18.
*/
function getCurrentApr(address _vault) external view returns (uint256 apr) {
function getCurrentApr(
address _vault
) external view virtual returns (uint256 apr) {
return getExpectedApr(_vault, 0);
}

Expand All @@ -122,7 +126,7 @@ contract AprOracle {
function getExpectedApr(
address _vault,
int256 _delta
) public view returns (uint256 apr) {
) public view virtual returns (uint256 apr) {
IVault vault = IVault(_vault);

// Check if the full profit has already been unlocked.
Expand Down
26 changes: 13 additions & 13 deletions src/ReportTrigger/CommonReportTrigger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ contract CommonReportTrigger is Governance {
function setCustomStrategyTrigger(
address _strategy,
address _trigger
) external {
) external virtual {
require(msg.sender == IStrategy(_strategy).management(), "!authorized");
customStrategyTrigger[_strategy] = _trigger;

Expand All @@ -144,7 +144,7 @@ contract CommonReportTrigger is Governance {
function setCustomStrategyBaseFee(
address _strategy,
uint256 _baseFee
) external {
) external virtual {
require(msg.sender == IStrategy(_strategy).management(), "!authorized");
customStrategyBaseFee[_strategy] = _baseFee;

Expand All @@ -171,7 +171,7 @@ contract CommonReportTrigger is Governance {
address _vault,
address _strategy,
address _trigger
) external {
) external virtual {
// Check that the address has the ADD_STRATEGY_MANAGER role on
// the vault. Just check their role has a 1 at the first position.
uint256 mask = 1;
Expand Down Expand Up @@ -204,7 +204,7 @@ contract CommonReportTrigger is Governance {
address _vault,
address _strategy,
uint256 _baseFee
) external {
) external virtual {
// Check that the address has the ADD_STRATEGY_MANAGER role on
// the vault. Just check their role has a 1 at the first position.
uint256 mask = 1;
Expand Down Expand Up @@ -232,7 +232,7 @@ contract CommonReportTrigger is Governance {
*/
function strategyReportTrigger(
address _strategy
) external view returns (bool, bytes memory) {
) external view virtual returns (bool, bytes memory) {
address _trigger = customStrategyTrigger[_strategy];

// If a custom trigger contract is set use that one.
Expand Down Expand Up @@ -266,7 +266,7 @@ contract CommonReportTrigger is Governance {
*/
function defaultStrategyReportTrigger(
address _strategy
) public view returns (bool, bytes memory) {
) public view virtual returns (bool, bytes memory) {
// Cache the strategy instance.
IStrategy strategy = IStrategy(_strategy);

Expand Down Expand Up @@ -314,7 +314,7 @@ contract CommonReportTrigger is Governance {
function vaultReportTrigger(
address _vault,
address _strategy
) external view returns (bool, bytes memory) {
) external view virtual returns (bool, bytes memory) {
address _trigger = customVaultTrigger[_vault][_strategy];

// If a custom trigger contract is set use that.
Expand Down Expand Up @@ -351,7 +351,7 @@ contract CommonReportTrigger is Governance {
function defaultVaultReportTrigger(
address _vault,
address _strategy
) public view returns (bool, bytes memory) {
) public view virtual returns (bool, bytes memory) {
// Cache the vault instance.
IVault vault = IVault(_vault);

Expand Down Expand Up @@ -404,7 +404,7 @@ contract CommonReportTrigger is Governance {
*/
function strategyTendTrigger(
address _strategy
) external view returns (bool, bytes memory) {
) external view virtual returns (bool, bytes memory) {
// Return the status of the tend trigger.
return IStrategy(_strategy).tendTrigger();
}
Expand All @@ -414,7 +414,7 @@ contract CommonReportTrigger is Governance {
* @dev Will return 0 if a base fee provider is not set.
* @return . The current base fee for the chain.
*/
function getCurrentBaseFee() public view returns (uint256) {
function getCurrentBaseFee() public view virtual returns (uint256) {
address _baseFeeProvider = baseFeeProvider;
if (_baseFeeProvider == address(0)) return 0;

Expand All @@ -431,7 +431,7 @@ contract CommonReportTrigger is Governance {
*
* @return . IF the current base fee is acceptable.
*/
function isCurrentBaseFeeAcceptable() external view returns (bool) {
function isCurrentBaseFeeAcceptable() external view virtual returns (bool) {
address _baseFeeProvider = baseFeeProvider;
// If no provider is set return true.
if (_baseFeeProvider == address(0)) return true;
Expand All @@ -450,7 +450,7 @@ contract CommonReportTrigger is Governance {
*/
function setBaseFeeProvider(
address _baseFeeProvider
) external onlyGovernance {
) external virtual onlyGovernance {
baseFeeProvider = _baseFeeProvider;

emit NewBaseFeeProvider(_baseFeeProvider);
Expand All @@ -463,7 +463,7 @@ contract CommonReportTrigger is Governance {
*/
function setAcceptableBaseFee(
uint256 _newAcceptableBaseFee
) external onlyGovernance {
) external virtual onlyGovernance {
acceptableBaseFee = _newAcceptableBaseFee;

emit UpdatedAcceptableBaseFee(_newAcceptableBaseFee);
Expand Down
10 changes: 5 additions & 5 deletions src/swappers/SolidlySwapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ contract SolidlySwapper {
address _token0,
address _token1,
bool _stable
) internal {
) internal virtual {
stable[_token0][_token1] = _stable;
stable[_token1][_token0] = _stable;
}
Expand All @@ -66,7 +66,7 @@ contract SolidlySwapper {
address _to,
uint256 _amountIn,
uint256 _minAmountOut
) internal {
) internal virtual {
if (_amountIn > minAmountToSell) {
_checkAllowance(router, _from, _amountIn);

Expand Down Expand Up @@ -95,7 +95,7 @@ contract SolidlySwapper {
address _from,
address _to,
uint256 _amountIn
) internal view returns (uint256) {
) internal view virtual returns (uint256) {
uint256[] memory amounts = ISolidly(router).getAmountsOut(
_amountIn,
_getTokenOutPath(_from, _to)
Expand All @@ -115,7 +115,7 @@ contract SolidlySwapper {
function _getTokenOutPath(
address _tokenIn,
address _tokenOut
) internal view returns (ISolidly.route[] memory _path) {
) internal view virtual returns (ISolidly.route[] memory _path) {
bool isBase = _tokenIn == base || _tokenOut == base;
_path = new ISolidly.route[](isBase ? 1 : 2);

Expand Down Expand Up @@ -143,7 +143,7 @@ contract SolidlySwapper {
address _contract,
address _token,
uint256 _amount
) internal {
) internal virtual {
if (ERC20(_token).allowance(address(this), _contract) < _amount) {
ERC20(_token).approve(_contract, 0);
ERC20(_token).approve(_contract, _amount);
Expand Down
8 changes: 4 additions & 4 deletions src/swappers/UniswapV2Swapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ contract UniswapV2Swapper {
address _to,
uint256 _amountIn,
uint256 _minAmountOut
) internal {
) internal virtual {
if (_amountIn > minAmountToSell) {
_checkAllowance(router, _from, _amountIn);

Expand Down Expand Up @@ -72,7 +72,7 @@ contract UniswapV2Swapper {
address _from,
address _to,
uint256 _amountIn
) internal view returns (uint256) {
) internal view virtual returns (uint256) {
uint256[] memory amounts = IUniswapV2Router02(router).getAmountsOut(
_amountIn,
_getTokenOutPath(_from, _to)
Expand All @@ -92,7 +92,7 @@ contract UniswapV2Swapper {
function _getTokenOutPath(
address _tokenIn,
address _tokenOut
) internal view returns (address[] memory _path) {
) internal view virtual returns (address[] memory _path) {
bool isBase = _tokenIn == base || _tokenOut == base;
_path = new address[](isBase ? 2 : 3);
_path[0] = _tokenIn;
Expand All @@ -117,7 +117,7 @@ contract UniswapV2Swapper {
address _contract,
address _token,
uint256 _amount
) internal {
) internal virtual {
if (ERC20(_token).allowance(address(this), _contract) < _amount) {
ERC20(_token).approve(_contract, 0);
ERC20(_token).approve(_contract, _amount);
Expand Down
8 changes: 4 additions & 4 deletions src/swappers/UniswapV3Swapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ contract UniswapV3Swapper {
address _token0,
address _token1,
uint24 _fee
) internal {
) internal virtual {
uniFees[_token0][_token1] = _fee;
uniFees[_token1][_token0] = _fee;
}
Expand All @@ -71,7 +71,7 @@ contract UniswapV3Swapper {
address _to,
uint256 _amountIn,
uint256 _minAmountOut
) internal returns (uint256 _amountOut) {
) internal virtual returns (uint256 _amountOut) {
if (_amountIn > minAmountToSell) {
_checkAllowance(router, _from, _amountIn);
if (_from == base || _to == base) {
Expand Down Expand Up @@ -134,7 +134,7 @@ contract UniswapV3Swapper {
address _to,
uint256 _amountTo,
uint256 _maxAmountFrom
) internal returns (uint256 _amountIn) {
) internal virtual returns (uint256 _amountIn) {
if (_maxAmountFrom > minAmountToSell) {
_checkAllowance(router, _from, _maxAmountFrom);
if (_from == base || _to == base) {
Expand Down Expand Up @@ -185,7 +185,7 @@ contract UniswapV3Swapper {
address _contract,
address _token,
uint256 _amount
) internal {
) internal virtual {
if (ERC20(_token).allowance(address(this), _contract) < _amount) {
ERC20(_token).approve(_contract, 0);
ERC20(_token).approve(_contract, _amount);
Expand Down
40 changes: 40 additions & 0 deletions src/utils/Clonable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: AGPL-3.0
pragma solidity 0.8.18;

contract Clonable {
/// @notice Set to the address to auto clone from.
address public original;

/**
* @notice Clone the contracts default `original` contract.
* @return Address of the new Minimal Proxy clone.
*/
function _clone() internal virtual returns (address) {
return _clone(original);
}

/**
* @notice Clone any `_original` contract.
* @return _newContract Address of the new Minimal Proxy clone.
*/
function _clone(
address _original
) internal virtual returns (address _newContract) {
// Copied from https://github.com/optionality/clone-factory/blob/master/contracts/CloneFactory.sol
bytes20 addressBytes = bytes20(_original);
assembly {
// EIP-1167 bytecode
let clone_code := mload(0x40)
mstore(
clone_code,
0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000
)
mstore(add(clone_code, 0x14), addressBytes)
mstore(
add(clone_code, 0x28),
0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000
)
_newContract := create(0, clone_code, 0x37)
}
}
}
6 changes: 4 additions & 2 deletions src/utils/Governance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity 0.8.18;

contract Governance {
/// @notice Emitted when the governance address is updated.
event GovernanceTransferred(
address indexed previousGovernance,
address indexed newGovernance
Expand All @@ -12,11 +13,12 @@ contract Governance {
_;
}

/// @notice Checks if the msg sender is the governance.
function _checkGovernance() internal view virtual {
require(governance == msg.sender, "!governance");
}

// Address that can set the default base fee and provider
/// @notice Address that can set the default base fee and provider
address public governance;

constructor(address _governance) {
Expand All @@ -32,7 +34,7 @@ contract Governance {
*/
function transferGovernance(
address _newGovernance
) external onlyGovernance {
) external virtual onlyGovernance {
require(_newGovernance != address(0), "ZERO ADDRESS");
address oldGovernance = governance;
governance = _newGovernance;
Expand Down
40 changes: 40 additions & 0 deletions src/utils/Governance2Step.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: AGPL-3.0
pragma solidity 0.8.18;

import {Governance} from "./Governance.sol";

contract Governance2Step is Governance {
/// @notice Emitted when the pending governance address is set.
event UpdatePendingGovernance(address indexed newPendingGovernance);

/// @notice Address that is set to take over governance.
address public pendingGovernance;

constructor(address _governance) Governance(_governance) {}

/**
* @notice Sets a new address as the `pendingGovernance` of the contract.
* @dev Throws if the caller is not current governance.
* @param _newGovernance The new governance address.
*/
function transferGovernance(
address _newGovernance
) external virtual override onlyGovernance {
require(_newGovernance != address(0), "ZERO ADDRESS");
pendingGovernance = _newGovernance;

emit UpdatePendingGovernance(_newGovernance);
}

/**
* @notice Allows the `pendingGovernance` to accept the role.
*/
function acceptGovernance() external virtual {
require(msg.sender == pendingGovernance, "!pending governance");

emit GovernanceTransferred(governance, msg.sender);

governance = msg.sender;
pendingGovernance = address(0);
}
}

0 comments on commit b1ed340

Please sign in to comment.