Skip to content

Commit

Permalink
feat: just use vault array
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlagonia committed Oct 17, 2023
1 parent e7569f7 commit 80edb9e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 198 deletions.
6 changes: 3 additions & 3 deletions contracts/Mocks/MockFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
pragma solidity 0.8.18;

contract MockFactory {
string public api_version;
string public apiVersion;

address public vault_bluePrint;

constructor(string memory apiVersion) {
api_version = apiVersion;
constructor(string memory _apiVersion) {
apiVersion = _apiVersion;
}
}
226 changes: 35 additions & 191 deletions contracts/registry/Registry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ import {ReleaseRegistry} from "./ReleaseRegistry.sol";
interface IVault {
function asset() external view returns (address);

function api_version() external view returns (string memory);
}

interface IStrategy {
function asset() external view returns (address);

function apiVersion() external view returns (string memory);
}

Expand Down Expand Up @@ -62,10 +56,10 @@ contract Registry is Governance {
}

// Default type used for Multi strategy "allocator" vaults.
uint256 public constant ALLOCATOR_VAULT_TYPE = 1;
uint256 public constant MULTI_STRATEGY_TYPE = 1;

// Default type used for single strategy vaults.
uint256 public constant TOKENIZED_STRATEGY_TYPE = 2;
uint256 public constant SINGLE_STRATEGY_TYPE = 2;

// Custom name for this Registry.
string public name;
Expand All @@ -82,9 +76,6 @@ contract Registry is Governance {
// asset => array of all endorsed vaults.
mapping(address => address[]) internal _endorsedVaults;

// asset => array of all endorsed strategies.
mapping(address => address[]) internal _endorsedStrategies;

// vault/strategy address => Info struct.
mapping(address => Info) public info;

Expand Down Expand Up @@ -128,16 +119,6 @@ contract Registry is Governance {
return _endorsedVaults[_asset].length;
}

/**
* @notice The amount of endorsed strategies for a specific token.
* @return The amount of endorsed strategies.
*/
function numEndorsedStrategies(
address _asset
) public view returns (uint256) {
return _endorsedStrategies[_asset].length;
}

/**
* @notice Get the array of vaults endorsed for an `_asset`.
* @param _asset The underlying token used by the vaults.
Expand All @@ -149,17 +130,6 @@ contract Registry is Governance {
return _endorsedVaults[_asset];
}

/**
* @notice Get the array of strategies endorsed for an `_asset`.
* @param _asset The underlying token used by the strategies.
* @return The endorsed strategies.
*/
function getEndorsedStrategies(
address _asset
) external view returns (address[] memory) {
return _endorsedStrategies[_asset];
}

/**
* @notice Get all endorsed vaults deployed using the Registry.
* @dev This will return a nested array of all vaults deployed
Expand All @@ -184,30 +154,6 @@ contract Registry is Governance {
}
}

/**
* @notice Get all strategies endorsed through this registry.
* @dev This will return a nested array of all endorsed strategies
* separated by their underlying asset.
*
* This is only meant for off chain viewing and should not be used during any
* on chain tx's.
*
* @return allEndorsedStrategies A nested array containing all strategies.
*/
function getAllEndorsedStrategies()
external
view
returns (address[][] memory allEndorsedStrategies)
{
address[] memory allAssets = assets;
uint256 length = assets.length;

allEndorsedStrategies = new address[][](length);
for (uint256 i; i < length; ++i) {
allEndorsedStrategies[i] = _endorsedStrategies[allAssets[i]];
}
}

/**
* @notice
* Create a new vault for the given asset using a given release in the
Expand Down Expand Up @@ -288,28 +234,36 @@ contract Registry is Governance {
_profitMaxUnlockTime
);

// Add to the endorsed vaults array.
_endorsedVaults[_asset].push(_vault);

// Register the vault with this Registry
_registerVault(
_vault,
_asset,
_releaseTarget,
ALLOCATOR_VAULT_TYPE,
MULTI_STRATEGY_TYPE,
block.timestamp
);
}

/**
* @notice Endorse an already deployed vault.
* @dev To be used with default values for `_releaseDelta` and
* `_deploymentTimestamp`.
* @notice Endorse an already deployed multi strategy vault.
* @dev To be used with default values for `_releaseDelta`, `_vaultType`
* and `_deploymentTimestamp`.
* @param _vault Address of the vault to endorse.
*/
function endorseMultiStrategyVault(address _vault) external {
endorseVault(_vault, 0, MULTI_STRATEGY_TYPE, 0);
}

/**
* @notice Endorse an already deployed Single Strategy vault.
* @dev To be used with default values for `_releaseDelta`, `_vaultType`
* and `_deploymentTimestamp`.
*
* @param _vault Address of the vault to endorse.
*/
function endorseVault(address _vault) external {
endorseVault(_vault, 0, ALLOCATOR_VAULT_TYPE, 0);
function endorseSingleStrategyVault(address _vault) external {
endorseVault(_vault, 0, SINGLE_STRATEGY_TYPE, 0);
}

/**
Expand All @@ -322,7 +276,7 @@ contract Registry is Governance {
* Emits a `NewEndorsedVault` event.
* @param _vault The vault that will be endorsed by the Registry.
* @param _releaseDelta Specify the number of releases prior to the latest to use as a target.
* @param _vaultType Type of vault to endorsed.
* @param _vaultType Type of vault to endorse.
* @param _deploymentTimestamp The timestamp of when the vault was deployed for FE use.
*/
function endorseVault(
Expand All @@ -331,7 +285,8 @@ contract Registry is Governance {
uint256 _vaultType,
uint256 _deploymentTimestamp
) public onlyGovernance {
require(_vaultType != 0, "no 0 type");
// Cannot endorse twice.
require(info[_vault].asset == address(0), "endorsed");

// Will underflow if no releases created yet, or targeting prior to release history
uint256 _releaseTarget = ReleaseRegistry(releaseRegistry)
Expand All @@ -342,23 +297,18 @@ contract Registry is Governance {
// Get the API version for the target specified
string memory apiVersion = IVaultFactory(
ReleaseRegistry(releaseRegistry).factories(_releaseTarget)
).api_version();
).apiVersion();

require(
keccak256(bytes(IVault(_vault).api_version())) ==
keccak256(bytes(IVault(_vault).apiVersion())) ==
keccak256(bytes((apiVersion))),
"Wrong API Version"
);

address _asset = IVault(_vault).asset();

// Add to the endorsed vaults array.
_endorsedVaults[_asset].push(_vault);

// Add to the end of the list of vaults for asset
_registerVault(
_vault,
_asset,
IVault(_vault).asset(),
_releaseTarget,
_vaultType,
_deploymentTimestamp
Expand All @@ -372,6 +322,11 @@ contract Registry is Governance {
uint256 _vaultType,
uint256 _deploymentTimestamp
) internal {
require(_vaultType != 0, "no 0 type");

// Add to the endorsed vaults array.
_endorsedVaults[_asset].push(_vault);

// Set the Info struct for this vault
info[_vault] = Info({
asset: _asset,
Expand All @@ -390,67 +345,6 @@ contract Registry is Governance {
emit NewEndorsedVault(_vault, _asset, _releaseTarget, _vaultType);
}

/**
* @notice Endorse an already deployed strategy.
* @dev To be used with default values for `_releaseDelta` `_strategyType`
* and `_deploymentTimestamp`.
*
* @param _strategy Address of the strategy to endorse.
*/
function endorseStrategy(address _strategy) external {
endorseStrategy(_strategy, 0, TOKENIZED_STRATEGY_TYPE, 0);
}

/**
* @notice
* Adds an existing strategy to the list of "endorsed" strategies for that asset.
* @dev
* Throws if caller isn't `owner`.
* Throws if no releases are registered yet.
* Throws if `strategies`'s api version does not match the release specified.
* Emits a `NewEndorsedStrategy` event.
* @param _strategy The strategy that will be endorsed by the Registry.
* @param _releaseDelta Specify the number of releases prior to the latest to use as a target.
* @param _strategyType The type of strategy to endorse.
* @param _deploymentTimestamp The timestamp of when the strategy was deployed for FE use.
*/
function endorseStrategy(
address _strategy,
uint256 _releaseDelta,
uint256 _strategyType,
uint256 _deploymentTimestamp
) public onlyGovernance {
// Will underflow if no releases created yet, or targeting prior to release history
uint256 _releaseTarget = ReleaseRegistry(releaseRegistry)
.numReleases() -
1 -
_releaseDelta; // dev: no releases

// Get the API version for this release
string memory apiVersion = IVaultFactory(
ReleaseRegistry(releaseRegistry).factories(_releaseTarget)
).api_version();

// Make sure the API versions match
require(
keccak256(bytes((IStrategy(_strategy).apiVersion()))) ==
keccak256(bytes((apiVersion))),
"Wrong API Version"
);

address _asset = IStrategy(_strategy).asset();

_endorsedStrategies[_asset].push(_strategy);

_registerVault(
_strategy,
_asset,
_releaseTarget,
_strategyType,
_deploymentTimestamp
);
}

/**
* @notice Tag a vault with a specific string.
* @dev This is available to governance to tag any vault or strategy
Expand Down Expand Up @@ -489,7 +383,7 @@ contract Registry is Governance {
address asset = IVault(_vault).asset();
// Get the release version for this specific vault.
uint256 releaseTarget = ReleaseRegistry(releaseRegistry).releaseTargets(
IVault(_vault).api_version()
IVault(_vault).apiVersion()
);

require(_endorsedVaults[asset][_index] == _vault, "wrong index");
Expand All @@ -502,57 +396,11 @@ contract Registry is Governance {
// Pop the last item off the array.
_endorsedVaults[asset].pop();

// Reset the info config.
delete info[_vault];

// Emit the event.
emit RemovedVault(_vault, asset, releaseTarget, ALLOCATOR_VAULT_TYPE);
}

/**
* @notice Remove a `_strategy` at a specific `_index`.
* @dev Can be used as a efficient way to remove a strategy
* to not have to iterate over the full array.
*
* NOTE: This will not remove the asset from the `assets` array
* if it is no longer in use and will have to be done manually.
*
* @param _strategy Address of the strategy to remove.
* @param _index Index in the `endorsedStrategies` array `_strategy` sits at.
*/
function removeStrategy(
address _strategy,
uint256 _index
) external onlyGovernance {
require(info[_strategy].asset != address(0), "!endorsed");
emit RemovedVault(_vault, asset, releaseTarget, info[_vault].vaultType);

// Get the asset the vault is using.
address asset = IStrategy(_strategy).asset();
// Get the release version for this specific vault.
uint256 releaseTarget = ReleaseRegistry(releaseRegistry).releaseTargets(
IStrategy(_strategy).apiVersion()
);

require(_endorsedStrategies[asset][_index] == _strategy, "wrong index");

// Set the last index to the spot we are removing.
_endorsedStrategies[asset][_index] = _endorsedStrategies[asset][
_endorsedStrategies[asset].length - 1
];

// Pop the last item off the array.
_endorsedStrategies[asset].pop();

// Reset the info config.
delete info[_strategy];

// Emit the event.
emit RemovedVault(
_strategy,
asset,
releaseTarget,
TOKENIZED_STRATEGY_TYPE
);
// Delete the struct.
delete info[_vault];
}

/**
Expand All @@ -568,11 +416,7 @@ contract Registry is Governance {
uint256 _index
) external onlyGovernance {
require(assetIsUsed[_asset], "!in use");
require(
_endorsedVaults[_asset].length == 0 &&
_endorsedStrategies[_asset].length == 0,
"still in use"
);
require(_endorsedVaults[_asset].length == 0, "still in use");

require(assets[_index] == _asset, "wrong asset");

Expand Down
Loading

0 comments on commit 80edb9e

Please sign in to comment.