Skip to content

Maintenance #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 64 additions & 24 deletions contracts/contracts-registry/AContractsRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ import {ADependant} from "./ADependant.sol";
* Users may also fetch all the contracts present in the system as they are now located in a single place.
*/
abstract contract AContractsRegistry is Initializable {
AdminableProxyUpgrader private _proxyUpgrader;
struct AContractsRegistryStorage {
AdminableProxyUpgrader proxyUpgrader;
mapping(string name => address contractAddress) contracts;
mapping(address contractAddress => bool isProxy) isProxy;
}

mapping(string => address) private _contracts;
mapping(address => bool) private _isProxy;
//bytes32(uint256(keccak256("solarity.contract.AContractsRegistry")) - 1)
bytes32 private constant A_CONTRACTS_REGISTRY_STORAGE =
0x769f3b456cd81d706504548e533f55ce8f4cb7a5f9b80697cfd5d8146de0ca61;

event ContractAdded(string name, address contractAddress);
event ProxyContractAdded(string name, address contractAddress, address implementation);
Expand All @@ -56,7 +61,9 @@ abstract contract AContractsRegistry is Initializable {
* @notice The initialization function
*/
function __AContractsRegistry_init() internal onlyInitializing {
_proxyUpgrader = new AdminableProxyUpgrader(address(this));
AContractsRegistryStorage storage $ = _getAContractsRegistryStorage();

$.proxyUpgrader = new AdminableProxyUpgrader(address(this));
}

/**
Expand All @@ -65,7 +72,9 @@ abstract contract AContractsRegistry is Initializable {
* @return the address of the contract
*/
function getContract(string memory name_) public view returns (address) {
address contractAddress_ = _contracts[name_];
AContractsRegistryStorage storage $ = _getAContractsRegistryStorage();

address contractAddress_ = $.contracts[name_];

_checkIfMappingExist(contractAddress_, name_);

Expand All @@ -78,15 +87,19 @@ abstract contract AContractsRegistry is Initializable {
* @return true if the contract is present in the registry
*/
function hasContract(string memory name_) public view returns (bool) {
return _contracts[name_] != address(0);
AContractsRegistryStorage storage $ = _getAContractsRegistryStorage();

return $.contracts[name_] != address(0);
}

/**
* @notice The function that returns the admin of the added proxy contracts
* @return the proxy admin address
*/
function getProxyUpgrader() public view returns (address) {
return address(_proxyUpgrader);
AContractsRegistryStorage storage $ = _getAContractsRegistryStorage();

return address($.proxyUpgrader);
}

/**
Expand All @@ -95,12 +108,14 @@ abstract contract AContractsRegistry is Initializable {
* @return the implementation address
*/
function getImplementation(string memory name_) public view returns (address) {
address contractProxy_ = _contracts[name_];
AContractsRegistryStorage storage $ = _getAContractsRegistryStorage();

address contractProxy_ = $.contracts[name_];

if (contractProxy_ == address(0)) revert NoMappingExists(name_);
if (!_isProxy[contractProxy_]) revert NotAProxy(name_, contractProxy_);
if (!$.isProxy[contractProxy_]) revert NotAProxy(name_, contractProxy_);

return _proxyUpgrader.getImplementation(contractProxy_);
return $.proxyUpgrader.getImplementation(contractProxy_);
}

/**
Expand All @@ -120,7 +135,9 @@ abstract contract AContractsRegistry is Initializable {
string memory name_,
bytes memory data_
) internal virtual {
address contractAddress_ = _contracts[name_];
AContractsRegistryStorage storage $ = _getAContractsRegistryStorage();

address contractAddress_ = $.contracts[name_];

_checkIfMappingExist(contractAddress_, name_);

Expand Down Expand Up @@ -152,12 +169,14 @@ abstract contract AContractsRegistry is Initializable {
address newImplementation_,
bytes memory data_
) internal virtual {
address contractToUpgrade_ = _contracts[name_];
AContractsRegistryStorage storage $ = _getAContractsRegistryStorage();

address contractToUpgrade_ = $.contracts[name_];

if (contractToUpgrade_ == address(0)) revert NoMappingExists(name_);
if (!_isProxy[contractToUpgrade_]) revert NotAProxy(name_, contractToUpgrade_);
if (!$.isProxy[contractToUpgrade_]) revert NotAProxy(name_, contractToUpgrade_);

_proxyUpgrader.upgrade(contractToUpgrade_, newImplementation_, data_);
$.proxyUpgrader.upgrade(contractToUpgrade_, newImplementation_, data_);

emit ProxyContractUpgraded(name_, newImplementation_);
}
Expand All @@ -171,7 +190,9 @@ abstract contract AContractsRegistry is Initializable {
function _addContract(string memory name_, address contractAddress_) internal virtual {
if (contractAddress_ == address(0)) revert ZeroAddressProvided(name_);

_contracts[name_] = contractAddress_;
AContractsRegistryStorage storage $ = _getAContractsRegistryStorage();

$.contracts[name_] = contractAddress_;

emit ContractAdded(name_, contractAddress_);
}
Expand Down Expand Up @@ -200,10 +221,12 @@ abstract contract AContractsRegistry is Initializable {
) internal virtual {
if (contractAddress_ == address(0)) revert ZeroAddressProvided(name_);

address proxyAddr_ = _deployProxy(contractAddress_, address(_proxyUpgrader), data_);
AContractsRegistryStorage storage $ = _getAContractsRegistryStorage();

address proxyAddr_ = _deployProxy(contractAddress_, address($.proxyUpgrader), data_);

_contracts[name_] = proxyAddr_;
_isProxy[proxyAddr_] = true;
$.contracts[name_] = proxyAddr_;
$.isProxy[proxyAddr_] = true;

emit ProxyContractAdded(name_, proxyAddr_, contractAddress_);
}
Expand All @@ -221,13 +244,15 @@ abstract contract AContractsRegistry is Initializable {
) internal virtual {
if (contractAddress_ == address(0)) revert ZeroAddressProvided(name_);

_contracts[name_] = contractAddress_;
_isProxy[contractAddress_] = true;
AContractsRegistryStorage storage $ = _getAContractsRegistryStorage();

$.contracts[name_] = contractAddress_;
$.isProxy[contractAddress_] = true;

emit ProxyContractAdded(
name_,
contractAddress_,
_proxyUpgrader.getImplementation(contractAddress_)
$.proxyUpgrader.getImplementation(contractAddress_)
);
}

Expand All @@ -236,12 +261,14 @@ abstract contract AContractsRegistry is Initializable {
* @param name_ the associated name with the contract
*/
function _removeContract(string memory name_) internal virtual {
address contractAddress_ = _contracts[name_];
AContractsRegistryStorage storage $ = _getAContractsRegistryStorage();

address contractAddress_ = $.contracts[name_];

_checkIfMappingExist(contractAddress_, name_);

delete _isProxy[contractAddress_];
delete _contracts[name_];
delete $.isProxy[contractAddress_];
delete $.contracts[name_];

emit ContractRemoved(name_);
}
Expand All @@ -264,4 +291,17 @@ abstract contract AContractsRegistry is Initializable {
function _checkIfMappingExist(address contractAddress_, string memory name_) internal pure {
if (contractAddress_ == address(0)) revert NoMappingExists(name_);
}

/**
* @dev Returns a pointer to the storage namespace
*/
function _getAContractsRegistryStorage()
private
pure
returns (AContractsRegistryStorage storage $)
{
assembly {
$.slot := A_CONTRACTS_REGISTRY_STORAGE
}
}
}
77 changes: 60 additions & 17 deletions contracts/contracts-registry/pools/APoolContractsRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ abstract contract APoolContractsRegistry is Initializable, ADependant {
using Paginator for EnumerableSet.AddressSet;
using Math for uint256;

address internal _contractsRegistry;
struct APoolContractsRegistryStorage {
address contractsRegistry;
mapping(string name => UpgradeableBeacon beacon) beacons;
mapping(string name => EnumerableSet.AddressSet pools) pools;
}

mapping(string => UpgradeableBeacon) private _beacons;
mapping(string => EnumerableSet.AddressSet) private _pools; // name => pool
// bytes32(uint256(keccak256("solarity.contract.APoolContractsRegistry")) - 1)
bytes32 private constant A_POOL_CONTRACTS_REGISTRY_STORAGE =
0x8d5dd0f70e3c83ece432cedb954444f19062d979f9fc6b474d5ea33604196f67;

error NoMappingExists(string poolName);
error NoPoolsToInject(string poolName);
Expand All @@ -50,7 +55,9 @@ abstract contract APoolContractsRegistry is Initializable, ADependant {
address contractsRegistry_,
bytes memory
) public virtual override dependant {
_contractsRegistry = contractsRegistry_;
APoolContractsRegistryStorage storage $ = _getAPoolContractsRegistryStorage();

$.contractsRegistry = contractsRegistry_;
}

/**
Expand All @@ -69,9 +76,11 @@ abstract contract APoolContractsRegistry is Initializable, ADependant {
* @return address_ the implementation these pools point to
*/
function getImplementation(string memory name_) public view returns (address) {
if (address(_beacons[name_]) == address(0)) revert NoMappingExists(name_);
APoolContractsRegistryStorage storage $ = _getAPoolContractsRegistryStorage();

if (address($.beacons[name_]) == address(0)) revert NoMappingExists(name_);

return _beacons[name_].implementation();
return $.beacons[name_].implementation();
}

/**
Expand All @@ -80,7 +89,9 @@ abstract contract APoolContractsRegistry is Initializable, ADependant {
* @return address the BeaconProxy address
*/
function getProxyBeacon(string memory name_) public view returns (address) {
address beacon_ = address(_beacons[name_]);
APoolContractsRegistryStorage storage $ = _getAPoolContractsRegistryStorage();

address beacon_ = address($.beacons[name_]);

if (beacon_ == address(0)) revert ProxyDoesNotExist(name_);

Expand All @@ -94,7 +105,9 @@ abstract contract APoolContractsRegistry is Initializable, ADependant {
* @return true if pool_ is whithing the registry
*/
function isPool(string memory name_, address pool_) public view returns (bool) {
return _pools[name_].contains(pool_);
APoolContractsRegistryStorage storage $ = _getAPoolContractsRegistryStorage();

return $.pools[name_].contains(pool_);
}

/**
Expand All @@ -103,7 +116,16 @@ abstract contract APoolContractsRegistry is Initializable, ADependant {
* @return the number of pools with this name
*/
function countPools(string memory name_) public view returns (uint256) {
return _pools[name_].length();
APoolContractsRegistryStorage storage $ = _getAPoolContractsRegistryStorage();

return $.pools[name_].length();
}

/**
* @dev Returns the address of the contracts registry
*/
function getContractsRegistry() public view returns (address) {
return _getAPoolContractsRegistryStorage().contractsRegistry;
}

/**
Expand All @@ -118,7 +140,9 @@ abstract contract APoolContractsRegistry is Initializable, ADependant {
uint256 offset_,
uint256 limit_
) public view returns (address[] memory pools_) {
return _pools[name_].part(offset_, limit_);
APoolContractsRegistryStorage storage $ = _getAPoolContractsRegistryStorage();

return $.pools[name_].part(offset_, limit_);
}

/**
Expand All @@ -131,15 +155,17 @@ abstract contract APoolContractsRegistry is Initializable, ADependant {
string[] memory names_,
address[] memory newImplementations_
) internal virtual {
APoolContractsRegistryStorage storage $ = _getAPoolContractsRegistryStorage();

for (uint256 i = 0; i < names_.length; i++) {
if (address(_beacons[names_[i]]) == address(0)) {
_beacons[names_[i]] = UpgradeableBeacon(
if (address($.beacons[names_[i]]) == address(0)) {
$.beacons[names_[i]] = UpgradeableBeacon(
_deployProxyBeacon(newImplementations_[i])
);
}

if (_beacons[names_[i]].implementation() != newImplementations_[i]) {
_beacons[names_[i]].upgradeTo(newImplementations_[i]);
if ($.beacons[names_[i]].implementation() != newImplementations_[i]) {
$.beacons[names_[i]].upgradeTo(newImplementations_[i]);
}
}
}
Expand Down Expand Up @@ -171,13 +197,15 @@ abstract contract APoolContractsRegistry is Initializable, ADependant {
uint256 offset_,
uint256 limit_
) internal virtual {
EnumerableSet.AddressSet storage _namedPools = _pools[name_];
APoolContractsRegistryStorage storage $ = _getAPoolContractsRegistryStorage();

EnumerableSet.AddressSet storage _namedPools = $.pools[name_];

uint256 to_ = (offset_ + limit_).min(_namedPools.length()).max(offset_);

if (to_ == offset_) revert NoPoolsToInject(name_);

address contractsRegistry_ = _contractsRegistry;
address contractsRegistry_ = $.contractsRegistry;

for (uint256 i = offset_; i < to_; i++) {
ADependant(_namedPools.at(i)).setDependencies(contractsRegistry_, data_);
Expand All @@ -190,7 +218,9 @@ abstract contract APoolContractsRegistry is Initializable, ADependant {
* @param poolAddress_ the proxy address of the pool
*/
function _addProxyPool(string memory name_, address poolAddress_) internal virtual {
_pools[name_].add(poolAddress_);
APoolContractsRegistryStorage storage $ = _getAPoolContractsRegistryStorage();

$.pools[name_].add(poolAddress_);
}

/**
Expand All @@ -200,4 +230,17 @@ abstract contract APoolContractsRegistry is Initializable, ADependant {
function _deployProxyBeacon(address implementation_) internal virtual returns (address) {
return address(new UpgradeableBeacon(implementation_, address(this)));
}

/**
* @dev Returns a pointer to the storage namespace
*/
function _getAPoolContractsRegistryStorage()
private
pure
returns (APoolContractsRegistryStorage storage $)
{
assembly {
$.slot := A_POOL_CONTRACTS_REGISTRY_STORAGE
}
}
}
Loading