diff --git a/solidity/contracts/examples/modules/access/WhitelistAccessModule.sol b/solidity/contracts/examples/modules/access/WhitelistAccessModule.sol new file mode 100644 index 0000000..edb8564 --- /dev/null +++ b/solidity/contracts/examples/modules/access/WhitelistAccessModule.sol @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.19; + +import {IModule, Module} from '@defi-wonderland/prophet-core/solidity/contracts/Module.sol'; +import {IOracle} from '@defi-wonderland/prophet-core/solidity/interfaces/IOracle.sol'; + +import { + IAccessModule, + IWhitelistAccessModule +} from '../../../../interfaces/examples/modules/access/IWhitelistAccessModule.sol'; + +contract WhitelistAccessModule is Module, IWhitelistAccessModule { + /// @inheritdoc IWhitelistAccessModule + mapping(address _user => mapping(address _allowedSender => bool _status)) public whitelist; + + constructor(IOracle _oracle) Module(_oracle) {} + + /// @inheritdoc IModule + function moduleName() external pure returns (string memory _moduleName) { + return 'WhitelistAccessModule'; + } + + /// @inheritdoc IAccessModule + function decodeAccessControlParameters(bytes calldata _data) + public + pure + returns (IAccessModule.AccessControlParameters memory _params) + { + _params = abi.decode(_data, (IAccessModule.AccessControlParameters)); + } + + /// @inheritdoc IAccessModule + function hasAccess(bytes calldata _data) external view returns (bool _hasAccess) { + IAccessModule.AccessControlParameters memory _params = decodeAccessControlParameters(_data); + + _hasAccess = whitelist[_params.accessControl.user][_params.sender]; + } + + /// @inheritdoc IWhitelistAccessModule + function setSenderApproval(address _allowedSender, bool _status) external { + whitelist[msg.sender][_allowedSender] = _status; + emit SetApproval(msg.sender, _allowedSender, _status); + } +} diff --git a/solidity/interfaces/examples/modules/access/IWhitelistAccessModule.sol b/solidity/interfaces/examples/modules/access/IWhitelistAccessModule.sol new file mode 100644 index 0000000..87d2553 --- /dev/null +++ b/solidity/interfaces/examples/modules/access/IWhitelistAccessModule.sol @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.19; + +import {IAccessModule} from '@defi-wonderland/prophet-core/solidity/interfaces/modules/access/IAccessModule.sol'; + +/* + * @title IWhitelistAccessModule + */ +interface IWhitelistAccessModule is IAccessModule { + /*/////////////////////////////////////////////////////////////// + STRUCTS + //////////////////////////////////////////////////////////////*/ + + /*/////////////////////////////////////////////////////////////// + EVENTS + //////////////////////////////////////////////////////////////*/ + + /** + * @dev Emitted when the approval status of a user is updated. + * @param _user The address of the user authorizing a sender. + * @param _sender The address of the sender whose status is being updated + * @param _status True if approved, false otherwise. + */ + event SetApproval(address _user, address _sender, bool _status); + + /*/////////////////////////////////////////////////////////////// + VARIABLES + //////////////////////////////////////////////////////////////*/ + + /** + * @notice Checks if a user has whitelisted the sender address. + * @param _user The address of the user whose whitelist is being checked. + * @param _sender The address of the sender to check against the user's whitelist. + * @return _status True if approved, false otherwise. + */ + function whitelist(address _user, address _sender) external view returns (bool _status); + + /*/////////////////////////////////////////////////////////////// + LOGIC + //////////////////////////////////////////////////////////////*/ + + /** + * @notice Sets the approval status for a given sender address. + * @param _allowedSender The address of the sender to be approved or disapproved. + * @param _status True if approved, false otherwise. + */ + function setSenderApproval(address _allowedSender, bool _status) external; +}