-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from axieinfinity/implement-feature/rework/us…
…er-defined-config feat(rework): implement `user-defined-config`
- Loading branch information
Showing
3 changed files
with
63 additions
and
1 deletion.
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,40 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.19; | ||
|
||
import { IUserDefinedConfig } from "../interfaces/configs/IUserDefinedConfig.sol"; | ||
|
||
abstract contract UserDefinedConfig is IUserDefinedConfig { | ||
bytes32 private constant $$_UserDefinedDataStorageLocation = keccak256("@fdk.UserDefinedConfig.UserDefinedData"); | ||
|
||
string[] private _userDefinedKeys; | ||
mapping(bytes32 hashKey => bool registered) private _registry; | ||
|
||
function setUserDefinedConfig(string calldata key, bytes calldata value) external { | ||
UserDefinedData storage $ = _getUserDefinedData(key); | ||
$._value = value; | ||
|
||
bytes32 hashKey = keccak256(bytes(key)); | ||
|
||
if (!_registry[hashKey]) { | ||
_userDefinedKeys.push(key); | ||
_registry[hashKey] = true; | ||
} | ||
} | ||
|
||
function getUserDefinedConfig(string calldata key) external view returns (bytes memory value) { | ||
UserDefinedData storage $ = _getUserDefinedData(key); | ||
return $._value; | ||
} | ||
|
||
function getAllKeys() external view returns (string[] memory) { | ||
return _userDefinedKeys; | ||
} | ||
|
||
function _getUserDefinedData(string calldata key) private pure returns (UserDefinedData storage $) { | ||
bytes32 location = keccak256(abi.encode($$_UserDefinedDataStorageLocation, keccak256(bytes(key)))); | ||
|
||
assembly ("memory-safe") { | ||
$.slot := location | ||
} | ||
} | ||
} |
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,14 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.19; | ||
|
||
interface IUserDefinedConfig { | ||
struct UserDefinedData { | ||
bytes _value; | ||
} | ||
|
||
function setUserDefinedConfig(string calldata key, bytes calldata value) external; | ||
|
||
function getUserDefinedConfig(string calldata key) external view returns (bytes memory value); | ||
|
||
function getAllKeys() external view returns (string[] memory); | ||
} |