Skip to content

Commit

Permalink
Merge pull request #140 from axieinfinity/implement-feature/rework/us…
Browse files Browse the repository at this point in the history
…er-defined-config

feat(rework): implement `user-defined-config`
  • Loading branch information
TuDo1403 authored May 17, 2024
2 parents ce22e6e + 76d4ffd commit 787ff64
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
10 changes: 9 additions & 1 deletion script/BaseGeneralConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ import { console } from "../lib/forge-std/src/console.sol";
import { WalletConfig } from "./configs/WalletConfig.sol";
import { RuntimeConfig } from "./configs/RuntimeConfig.sol";
import { MigrationConfig } from "./configs/MigrationConfig.sol";
import { UserDefinedConfig } from "./configs/UserDefinedConfig.sol";
import { TNetwork, NetworkConfig } from "./configs/NetworkConfig.sol";
import { EnumerableSet, TContract, ContractConfig } from "./configs/ContractConfig.sol";
import { ISharedParameter } from "./interfaces/configs/ISharedParameter.sol";
import { DefaultNetwork } from "./utils/DefaultNetwork.sol";
import { DefaultContract } from "./utils/DefaultContract.sol";
import { LibSharedAddress } from "./libraries/LibSharedAddress.sol";

contract BaseGeneralConfig is RuntimeConfig, WalletConfig, ContractConfig, NetworkConfig, MigrationConfig {
contract BaseGeneralConfig is
RuntimeConfig,
WalletConfig,
ContractConfig,
NetworkConfig,
MigrationConfig,
UserDefinedConfig
{
using StdStyle for *;
using EnumerableSet for EnumerableSet.AddressSet;

Expand Down
40 changes: 40 additions & 0 deletions script/configs/UserDefinedConfig.sol
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
}
}
}
14 changes: 14 additions & 0 deletions script/interfaces/configs/IUserDefinedConfig.sol
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);
}

0 comments on commit 787ff64

Please sign in to comment.