-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
499 additions
and
430 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,63 @@ | ||
// SPDX-License-Identifier: CC0-1.0 | ||
pragma solidity ^0.8.22; | ||
|
||
/// @dev OZ Library version has been tested with version 5.0.0. | ||
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; | ||
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol"; | ||
|
||
/** | ||
@title Dictionary Contract | ||
@dev This is the reference implementation for ERC-7546 Dictionary Contract. | ||
*/ | ||
contract Dictionary is Ownable, IERC165 { | ||
/********************** | ||
Storage & Event | ||
**********************/ | ||
mapping(bytes4 functionSelector => address implementation) implementations; // MUST | ||
event ImplementationUpgraded(bytes4 functionSelector, address implementation); // SHOULD | ||
error InvalidImplementation(address implementation); // OPTIONAL | ||
bytes4[] functionSelectorList; // RECOMMENDED | ||
|
||
constructor(address owner) Ownable(owner) {} | ||
|
||
|
||
/*************** | ||
Functions | ||
***************/ | ||
function getImplementation(bytes4 functionSelector) external view returns(address implementation) { | ||
implementation = implementations[functionSelector]; | ||
} | ||
|
||
function setImplementation(bytes4 functionSelector, address implementation) external onlyOwner { | ||
/// @notice Mismatch Function Selector in Security Considerations section | ||
if (implementation.code.length == 0) { | ||
revert InvalidImplementation(implementation); | ||
} | ||
|
||
// In the case of a new functionSelector, add to the functionSelectorList. | ||
bool _hasSetFunctionSelector; | ||
bytes4[] memory _functionSelectorList = functionSelectorList; | ||
for (uint i; i < _functionSelectorList.length; ++i) { | ||
if (functionSelector == _functionSelectorList[i]) { | ||
_hasSetFunctionSelector = true; | ||
} | ||
} | ||
if (!_hasSetFunctionSelector) functionSelectorList.push(functionSelector); | ||
|
||
// Add the pair of functionSelector and implementation address to the mapping. | ||
implementations[functionSelector] = implementation; | ||
|
||
// Notify the change of the mapping. | ||
emit ImplementationUpgraded(functionSelector, implementation); | ||
} | ||
|
||
/// @dev The interfaceId equals to the function selector | ||
function supportsInterface(bytes4 interfaceId) public view returns (bool) { | ||
return implementations[interfaceId] != address(0); | ||
} | ||
|
||
function supportsInterfaces() external view returns (bytes4[] memory) { | ||
return functionSelectorList; | ||
} | ||
|
||
} |
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,66 @@ | ||
// SPDX-License-Identifier: CC0-1.0 | ||
pragma solidity ^0.8.22; | ||
|
||
/// @dev OZ Library version has been tested with version 5.0.0. | ||
import {Proxy as OZProxy} from "@openzeppelin/contracts/proxy/Proxy.sol"; | ||
import {StorageSlot} from "@openzeppelin/contracts/utils/StorageSlot.sol"; | ||
import {Address} from "@openzeppelin/contracts/utils/Address.sol"; | ||
|
||
import {Dictionary} from "./Dictionary.sol"; | ||
|
||
/** | ||
@title Proxy Contract | ||
@dev This is the reference implementation for ERC-7546 Proxy Contract. | ||
@dev In this reference implementation, the transfer functionality inherits from the OpenZeppelin contracts. | ||
*/ | ||
contract Proxy is OZProxy { | ||
/************* | ||
Storage | ||
*************/ | ||
/** | ||
* @dev The only storage slot explicitly used within the Proxy Contract is for holding the Dictionary Contract address. | ||
* @dev This slot is the keccak-256 hash of "erc7546.proxy.dictionary" subtracted by 1. | ||
*/ | ||
bytes32 internal constant DICTIONARY_SLOT = 0x267691be3525af8a813d30db0c9e2bad08f63baecf6dceb85e2cf3676cff56f4; | ||
|
||
constructor(address dictionary, bytes memory _data) payable { | ||
_upgradeDictionaryToAndCall(dictionary, _data); | ||
} | ||
|
||
|
||
/*********** | ||
Event | ||
***********/ | ||
event DictionaryUpgraded(address dictionary); | ||
|
||
|
||
/************************ | ||
Internal Functions | ||
************************/ | ||
/** | ||
* @dev In this reference implementation, we use the OZ library to extract storage slot values as addresses. | ||
*/ | ||
function _getDictionary() internal view returns (address) { | ||
return StorageSlot.getAddressSlot(DICTIONARY_SLOT).value; | ||
} | ||
|
||
/** | ||
* @dev Override an internal function that returns the destination address to utilize the forwarding functionality of the OZ contract's Proxy. | ||
*/ | ||
function _implementation() internal view override returns (address) { | ||
return Dictionary(_getDictionary()).getImplementation(msg.sig); | ||
} | ||
|
||
function _upgradeDictionaryToAndCall(address newDictionary, bytes memory data) internal { | ||
/// @dev In this reference implementation, we've omitted it for simplicity, | ||
/// but it's recommended to implement a check to ensure the Dictionary Contract is indeed a contract. | ||
StorageSlot.getAddressSlot(DICTIONARY_SLOT).value = newDictionary; | ||
emit DictionaryUpgraded(newDictionary); | ||
|
||
/// @dev Similarly, it is recommended to verify that it is non-payable before proceeding with the initialization process, | ||
/// though this has been omitted in this case. | ||
if (data.length > 0) { | ||
Address.functionDelegateCall(Dictionary(newDictionary).getImplementation(bytes4(data)), data); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.