-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement gsm fee strategy factory
- Loading branch information
1 parent
9359796
commit 25edbf7
Showing
6 changed files
with
171 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.10; | ||
|
||
import {EnumerableSet} from '@openzeppelin/contracts/utils/structs/EnumerableSet.sol'; | ||
import {VersionedInitializable} from '@aave/core-v3/contracts/protocol/libraries/aave-upgradeability/VersionedInitializable.sol'; | ||
import {IGsmFeeStrategyFactory} from './interfaces/IGsmFeeStrategyFactory.sol'; | ||
import {IGsmFeeStrategy} from 'src/contracts/facilitators/gsm/feeStrategy/interfaces/IGsmFeeStrategy.sol'; | ||
import {FixedFeeStrategy} from '../facilitators/gsm/feeStrategy/FixedFeeStrategy.sol'; | ||
|
||
/** | ||
* @title GsmFeeStrategyFactory | ||
* @author Aave Labs | ||
* @notice Factory contract to create and keep record of Gsm Fee contracts | ||
*/ | ||
contract GsmFeeStrategyFactory is VersionedInitializable, IGsmFeeStrategyFactory { | ||
using EnumerableSet for EnumerableSet.AddressSet; | ||
|
||
mapping(uint256 => mapping(uint256 => address)) internal _gsmFeeStrategiesByRates; | ||
EnumerableSet.AddressSet internal _gsmFeeStrategies; | ||
|
||
/** | ||
* @notice GsmFeeStrategyFactory initializer | ||
* @dev assumes that the addresses provided are deployed fee strategies. | ||
* @param feeStrategiesList List of fee strategies | ||
*/ | ||
function initialize(address[] memory feeStrategiesList) external initializer { | ||
for (uint256 i = 0; i < feeStrategiesList.length; i++) { | ||
address feeStrategy = feeStrategiesList[i]; | ||
uint256 buyFee = IGsmFeeStrategy(feeStrategy).getBuyFee(1e4); | ||
uint256 sellFee = IGsmFeeStrategy(feeStrategy).getSellFee(1e4); | ||
|
||
_gsmFeeStrategiesByRates[buyFee][sellFee] = feeStrategy; | ||
_gsmFeeStrategies.add(feeStrategy); | ||
|
||
emit FeeStrategyCreated(feeStrategy, buyFee, sellFee); | ||
} | ||
} | ||
|
||
///@inheritdoc IGsmFeeStrategyFactory | ||
function createStrategies( | ||
uint256[] memory buyFeeList, | ||
uint256[] memory sellFeeList | ||
) public returns (address[] memory) { | ||
require(buyFeeList.length == sellFeeList.length, 'INVALID_FEE_LIST'); | ||
address[] memory strategies = new address[](buyFeeList.length); | ||
for (uint256 i = 0; i < buyFeeList.length; i++) { | ||
uint256 buyFee = buyFeeList[i]; | ||
uint256 sellFee = sellFeeList[i]; | ||
address cachedStrategy = _gsmFeeStrategiesByRates[buyFee][sellFee]; | ||
|
||
if (cachedStrategy == address(0)) { | ||
cachedStrategy = address(new FixedFeeStrategy(buyFee, sellFee)); | ||
_gsmFeeStrategiesByRates[buyFee][sellFee] = cachedStrategy; | ||
_gsmFeeStrategies.add(cachedStrategy); | ||
|
||
emit FeeStrategyCreated(cachedStrategy, buyFee, sellFee); | ||
} | ||
|
||
strategies[i] = cachedStrategy; | ||
} | ||
|
||
return strategies; | ||
} | ||
|
||
///@inheritdoc IGsmFeeStrategyFactory | ||
function getGsmFeeStrategies() external view returns (address[] memory) { | ||
return _gsmFeeStrategies.values(); | ||
} | ||
|
||
///@inheritdoc IGsmFeeStrategyFactory | ||
function getStrategyByRates(uint256 buyFee, uint256 sellFee) external view returns (address) { | ||
return _gsmFeeStrategiesByRates[buyFee][sellFee]; | ||
} | ||
|
||
///@inheritdoc IGsmFeeStrategyFactory | ||
function REVISION() public pure virtual override returns (uint256) { | ||
return 1; | ||
} | ||
|
||
/// @inheritdoc VersionedInitializable | ||
function getRevision() internal pure virtual override returns (uint256) { | ||
return REVISION(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
interface IGsmFeeStrategyFactory { | ||
/** | ||
* @dev Emitted when a new strategy is created | ||
* @param strategy The address of the new Gsm fee strategy | ||
* @param buyFee The buy fee of the new strategy | ||
* @param sellFee The sell fee of the new strategy | ||
*/ | ||
event FeeStrategyCreated( | ||
address indexed strategy, | ||
uint256 indexed buyFee, | ||
uint256 indexed sellFee | ||
); | ||
|
||
/** | ||
* @notice Creates new Gsm Fee strategy contracts from lists of buy and sell fees | ||
* @dev Returns the address of a cached contract if a strategy with same fees already exists | ||
* @param buyFeeList The list of buy fees for Gsm fee strategies | ||
* @param sellFeeList The list of sell fees for Gsm fee strategies | ||
* @return The list of Gsm fee strategy contracts | ||
*/ | ||
function createStrategies( | ||
uint256[] memory buyFeeList, | ||
uint256[] memory sellFeeList | ||
) external returns (address[] memory); | ||
|
||
/** | ||
* @notice Returns all the fee strategy contracts of the factory | ||
* @return The list of fee strategy contracts | ||
*/ | ||
function getGsmFeeStrategies() external view returns (address[] memory); | ||
|
||
/** | ||
* @notice Returns the fee strategy contract which corresponds to the given fees. | ||
* @dev Returns `address(0)` if there is no fee strategy for the given fees | ||
* @param buyFee The buy fee of the fee strategy contract | ||
* @param sellFee The sell fee of the fee strategy contract | ||
* @return The address of the fee strategy contract | ||
*/ | ||
function getStrategyByRates(uint256 buyFee, uint256 sellFee) external view returns (address); | ||
|
||
/** | ||
* @notice Returns the GsmFeeStrategyFactory revision number | ||
* @return The revision number | ||
*/ | ||
function REVISION() external pure returns (uint256); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters