-
Notifications
You must be signed in to change notification settings - Fork 102
feat: beacon proxify tokenization spoke #1315
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
Open
DhairyaSethi
wants to merge
2
commits into
main
Choose a base branch
from
fix/beacon-proxify-tokenization-spoke
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,17 +1,17 @@ | ||
| { | ||
| "deposit": "118614", | ||
| "depositWithSig": "129518", | ||
| "mint": "118251", | ||
| "mintWithSig": "129130", | ||
| "permit": "62766", | ||
| "redeem: on behalf, full": "95212", | ||
| "redeem: on behalf, partial": "119015", | ||
| "redeem: self, full": "94282", | ||
| "redeem: self, partial": "113482", | ||
| "redeemWithSig": "128864", | ||
| "withdraw: on behalf, full": "95646", | ||
| "withdraw: on behalf, partial": "119557", | ||
| "withdraw: self, full": "94824", | ||
| "withdraw: self, partial": "114024", | ||
| "withdrawWithSig": "129405" | ||
| "deposit": "128151", | ||
| "depositWithSig": "139020", | ||
| "mint": "127788", | ||
| "mintWithSig": "138620", | ||
| "permit": "65752", | ||
| "redeem: on behalf, full": "102440", | ||
| "redeem: on behalf, partial": "126440", | ||
| "redeem: self, full": "101707", | ||
| "redeem: self, partial": "120907", | ||
| "redeemWithSig": "136245", | ||
| "withdraw: on behalf, full": "102993", | ||
| "withdraw: on behalf, partial": "126993", | ||
| "withdraw: self, full": "102260", | ||
| "withdraw: self, partial": "121460", | ||
| "withdrawWithSig": "136821" | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
93 changes: 27 additions & 66 deletions
93
src/config-engine/libraries/TokenizationSpokeDeployer.sol
This file contains hidden or 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 |
|---|---|---|
| @@ -1,114 +1,75 @@ | ||
| // SPDX-License-Identifier: LicenseRef-BUSL | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| import {TransparentUpgradeableProxy} from 'src/dependencies/openzeppelin/TransparentUpgradeableProxy.sol'; | ||
| import {BeaconProxy} from 'src/dependencies/openzeppelin/BeaconProxy.sol'; | ||
| import {Create2Utils} from 'src/deployments/utils/libraries/Create2Utils.sol'; | ||
| import {TokenizationSpokeInstance} from 'src/spoke/instances/TokenizationSpokeInstance.sol'; | ||
| import {ITokenizationSpokeInstance} from 'src/deployments/utils/interfaces/ITokenizationSpokeInstance.sol'; | ||
|
|
||
| /// @title TokenizationSpokeDeployer | ||
| /// @author Aave Labs | ||
| /// @notice Library for deterministic CREATE2 deployment and address pre-computation of TokenizationSpoke proxies | ||
| /// using the Safe Singleton Factory. | ||
| /// @notice Library for deterministic CREATE2 deployment and address pre-computation of TokenizationSpoke | ||
| /// beacon proxies using the Safe Singleton Factory. | ||
| library TokenizationSpokeDeployer { | ||
| /// @notice Deploys a TokenizationSpokeInstance implementation and TransparentUpgradeableProxy via CREATE2 | ||
| /// through the Safe Singleton Factory. | ||
| /// @dev The proxy admin owner is set to `msg.sender`. | ||
| /// @notice Deploys a TokenizationSpoke BeaconProxy via CREATE2 through the Safe Singleton Factory. | ||
| /// @dev The proxy points to the shared `beacon`, whose owner controls the implementation upgrades. | ||
| /// @param beacon The address of the shared TokenizationSpoke beacon. | ||
| /// @param hub The address of the Hub. | ||
| /// @param underlying The address of the underlying asset. | ||
| /// @param name The ERC20 name for the TokenizationSpoke share token. | ||
| /// @param symbol The ERC20 symbol for the TokenizationSpoke share token. | ||
| /// @return proxy The address of the deployed proxy. | ||
| function deploy( | ||
| address beacon, | ||
| address hub, | ||
| address underlying, | ||
| string calldata name, | ||
| string calldata symbol | ||
| ) external returns (address proxy) { | ||
| bytes32 implSalt = _computeImplementationSalt(hub, underlying, name, symbol); | ||
| bytes memory implCreationCode = abi.encodePacked( | ||
| type(TokenizationSpokeInstance).creationCode, | ||
| abi.encode(hub, underlying) | ||
| bytes32 proxySalt = _computeProxySalt(beacon, hub, underlying, name, symbol); | ||
| bytes memory initData = abi.encodeCall( | ||
| ITokenizationSpokeInstance.initialize, | ||
| (hub, underlying, name, symbol) | ||
| ); | ||
| address impl = Create2Utils.create2Deploy(implSalt, implCreationCode); | ||
|
|
||
| bytes32 proxySalt = _computeProxySalt(hub, underlying, name, symbol); | ||
| bytes memory initData = abi.encodeCall(TokenizationSpokeInstance.initialize, (name, symbol)); | ||
| bytes memory proxyCreationCode = abi.encodePacked( | ||
| type(TransparentUpgradeableProxy).creationCode, | ||
| abi.encode(impl, msg.sender, initData) | ||
| type(BeaconProxy).creationCode, | ||
| abi.encode(beacon, initData) | ||
| ); | ||
| proxy = Create2Utils.create2Deploy(proxySalt, proxyCreationCode); | ||
| } | ||
|
|
||
| /// @notice Pre-computes the CREATE2 address of the TokenizationSpokeInstance implementation. | ||
| /// @notice Pre-computes the CREATE2 address of the TokenizationSpoke BeaconProxy. | ||
| /// @param beacon The address of the shared TokenizationSpoke beacon. | ||
| /// @param hub The address of the Hub. | ||
| /// @param underlying The address of the underlying asset. | ||
| /// @param name The ERC20 name for the TokenizationSpoke share token. | ||
| /// @param symbol The ERC20 symbol for the TokenizationSpoke share token. | ||
| /// @return The predicted implementation address. | ||
| function computeImplementationAddress( | ||
| address hub, | ||
| address underlying, | ||
| string memory name, | ||
| string memory symbol | ||
| ) external pure returns (address) { | ||
| return _computeImplementationAddress(hub, underlying, name, symbol); | ||
| } | ||
|
|
||
| /// @notice Pre-computes the CREATE2 address of the TransparentUpgradeableProxy. | ||
| /// @param hub The address of the Hub. | ||
| /// @param underlying The address of the underlying asset. | ||
| /// @param name The ERC20 name for the TokenizationSpoke share token. | ||
| /// @param symbol The ERC20 symbol for the TokenizationSpoke share token. | ||
| /// @param proxyAdminOwner The initial owner of the ProxyAdmin (msg.sender in `deploy`). | ||
| /// @return The predicted proxy address. | ||
| function computeProxyAddress( | ||
| address beacon, | ||
| address hub, | ||
| address underlying, | ||
| string memory name, | ||
| string memory symbol, | ||
| address proxyAdminOwner | ||
| string memory symbol | ||
| ) external pure returns (address) { | ||
| address impl = _computeImplementationAddress(hub, underlying, name, symbol); | ||
|
|
||
| bytes32 proxySalt = _computeProxySalt(hub, underlying, name, symbol); | ||
| bytes memory initData = abi.encodeCall(TokenizationSpokeInstance.initialize, (name, symbol)); | ||
| bytes memory creationCode = abi.encodePacked( | ||
| type(TransparentUpgradeableProxy).creationCode, | ||
| abi.encode(impl, proxyAdminOwner, initData) | ||
| bytes32 proxySalt = _computeProxySalt(beacon, hub, underlying, name, symbol); | ||
| bytes memory initData = abi.encodeCall( | ||
| ITokenizationSpokeInstance.initialize, | ||
| (hub, underlying, name, symbol) | ||
| ); | ||
| return Create2Utils.computeCreate2Address(proxySalt, creationCode); | ||
| } | ||
|
|
||
| function _computeImplementationAddress( | ||
| address hub, | ||
| address underlying, | ||
| string memory name, | ||
| string memory symbol | ||
| ) internal pure returns (address) { | ||
| bytes32 implSalt = _computeImplementationSalt(hub, underlying, name, symbol); | ||
| bytes memory creationCode = abi.encodePacked( | ||
| type(TokenizationSpokeInstance).creationCode, | ||
| abi.encode(hub, underlying) | ||
| type(BeaconProxy).creationCode, | ||
| abi.encode(beacon, initData) | ||
| ); | ||
| return Create2Utils.computeCreate2Address(implSalt, creationCode); | ||
| } | ||
|
|
||
| function _computeImplementationSalt( | ||
| address hub, | ||
| address underlying, | ||
| string memory name, | ||
| string memory symbol | ||
| ) internal pure returns (bytes32) { | ||
| return keccak256(abi.encode(hub, underlying, name, symbol, 'impl')); | ||
| return Create2Utils.computeCreate2Address(proxySalt, creationCode); | ||
| } | ||
|
|
||
| function _computeProxySalt( | ||
| address beacon, | ||
| address hub, | ||
| address underlying, | ||
| string memory name, | ||
| string memory symbol | ||
| ) internal pure returns (bytes32) { | ||
| return keccak256(abi.encode(hub, underlying, name, symbol, 'proxy')); | ||
| return keccak256(abi.encode(beacon, hub, underlying, name, symbol, 'proxy')); | ||
| } | ||
| } |
This file contains hidden or 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,57 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // OpenZeppelin Contracts (last updated v5.2.0) (proxy/beacon/BeaconProxy.sol) | ||
|
|
||
| pragma solidity ^0.8.22; | ||
|
|
||
| import {IBeacon} from "./IBeacon.sol"; | ||
| import {Proxy} from "./Proxy.sol"; | ||
| import {ERC1967Utils} from "./ERC1967Utils.sol"; | ||
|
|
||
| /** | ||
| * @dev This contract implements a proxy that gets the implementation address for each call from an {UpgradeableBeacon}. | ||
| * | ||
| * The beacon address can only be set once during construction, and cannot be changed afterwards. It is stored in an | ||
| * immutable variable to avoid unnecessary storage reads, and also in the beacon storage slot specified by | ||
| * https://eips.ethereum.org/EIPS/eip-1967[ERC-1967] so that it can be accessed externally. | ||
| * | ||
| * CAUTION: Since the beacon address can never be changed, you must ensure that you either control the beacon, or trust | ||
| * the beacon to not upgrade the implementation maliciously. | ||
| * | ||
| * IMPORTANT: Do not use the implementation logic to modify the beacon storage slot. Doing so would leave the proxy in | ||
| * an inconsistent state where the beacon storage slot does not match the beacon address. | ||
| */ | ||
| contract BeaconProxy is Proxy { | ||
| // An immutable address for the beacon to avoid unnecessary SLOADs before each delegate call. | ||
| address private immutable _beacon; | ||
|
|
||
| /** | ||
| * @dev Initializes the proxy with `beacon`. | ||
| * | ||
| * If `data` is nonempty, it's used as data in a delegate call to the implementation returned by the beacon. This | ||
| * will typically be an encoded function call, and allows initializing the storage of the proxy like a Solidity | ||
| * constructor. | ||
| * | ||
| * Requirements: | ||
| * | ||
| * - `beacon` must be a contract with the interface {IBeacon}. | ||
| * - If `data` is empty, `msg.value` must be zero. | ||
| */ | ||
| constructor(address beacon, bytes memory data) payable { | ||
| ERC1967Utils.upgradeBeaconToAndCall(beacon, data); | ||
| _beacon = beacon; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Returns the current implementation address of the associated beacon. | ||
| */ | ||
| function _implementation() internal view virtual override returns (address) { | ||
| return IBeacon(_getBeacon()).implementation(); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Returns the beacon. | ||
| */ | ||
| function _getBeacon() internal view virtual returns (address) { | ||
| return _beacon; | ||
| } | ||
| } |
This file contains hidden or 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,70 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // OpenZeppelin Contracts (last updated v5.0.0) (proxy/beacon/UpgradeableBeacon.sol) | ||
|
|
||
| pragma solidity ^0.8.20; | ||
|
|
||
| import {IBeacon} from "./IBeacon.sol"; | ||
| import {Ownable} from "./Ownable.sol"; | ||
|
|
||
| /** | ||
| * @dev This contract is used in conjunction with one or more instances of {BeaconProxy} to determine their | ||
| * implementation contract, which is where they will delegate all function calls. | ||
| * | ||
| * An owner is able to change the implementation the beacon points to, thus upgrading the proxies that use this beacon. | ||
| */ | ||
| contract UpgradeableBeacon is IBeacon, Ownable { | ||
| address private _implementation; | ||
|
|
||
| /** | ||
| * @dev The `implementation` of the beacon is invalid. | ||
| */ | ||
| error BeaconInvalidImplementation(address implementation); | ||
|
|
||
| /** | ||
| * @dev Emitted when the implementation returned by the beacon is changed. | ||
| */ | ||
| event Upgraded(address indexed implementation); | ||
|
|
||
| /** | ||
| * @dev Sets the address of the initial implementation, and the initial owner who can upgrade the beacon. | ||
| */ | ||
| constructor(address implementation_, address initialOwner) Ownable(initialOwner) { | ||
| _setImplementation(implementation_); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Returns the current implementation address. | ||
| */ | ||
| function implementation() public view virtual returns (address) { | ||
| return _implementation; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Upgrades the beacon to a new implementation. | ||
| * | ||
| * Emits an {Upgraded} event. | ||
| * | ||
| * Requirements: | ||
| * | ||
| * - msg.sender must be the owner of the contract. | ||
| * - `newImplementation` must be a contract. | ||
| */ | ||
| function upgradeTo(address newImplementation) public virtual onlyOwner { | ||
| _setImplementation(newImplementation); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Sets the implementation contract address for this beacon | ||
| * | ||
| * Requirements: | ||
| * | ||
| * - `newImplementation` must be a contract. | ||
| */ | ||
| function _setImplementation(address newImplementation) private { | ||
| if (newImplementation.code.length == 0) { | ||
| revert BeaconInvalidImplementation(newImplementation); | ||
| } | ||
| _implementation = newImplementation; | ||
| emit Upgraded(newImplementation); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be here or should as we add it as an immutable in hubEngine and it's non configurable? in the config engine or helpers we can abstract this by pulling from address-book (v3 takes the former approach)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the goal is to have 1 beacon per chain, should be an immutable in the Hub Engine, shouldn't allow to pass it to avoid mistakes.
If we want 1 beacon per market (for example between current one, and a whitelabel one or Horizon), then we should leave it configurable, or derived from the HubConfigurator managing a specific instance