modifiers.sol
Overview
This file defines an abstract Solidity contract named Modifiers, which encapsulates several reusable function modifiers for access control and transaction validation in smart contracts. These modifiers enforce constraints such as ownership verification, minimum value transfers, and minimum contract balance requirements. By inheriting this contract, other contracts can easily apply these common checks to their functions, promoting code reuse and standardized error handling.
The contract extends from an imported Errors contract, thereby utilizing predefined error messages for consistent revert reasons. It also imports some data structures from structs.sol, specifically making use of an optional(uint256) type in one of the modifiers.
Contract: Modifiers
Declaration
abstract contract Modifiers is Errors
abstract: Cannot be instantiated directly; meant to be inherited.
inherits:
Errorscontract, providing error message constants used in the modifiers.
Constants
Name | Type | Value | Description |
|---|---|---|---|
versionModifiers | string |
| Version identifier of this contract |
MIN_BALANCE | uint64 |
| Minimum balance constant, using |
vmshellis a denomination unit, likely specific to the environment or blockchain this Solidity variant targets.
Modifiers
Modifiers are used to add pre-condition checks to smart contract functions.
1. onlyOwnerPubkeyOptional
modifier onlyOwnerPubkeyOptional(optional(uint256) rootpubkey)
Parameters:
rootpubkey- anoptional(uint256)representing a public key that may or may not be set.
Behavior:
Checks if
rootpubkeyhas a value (hasValue() == true).Requires that the message's public key (
msg.pubkey()) equals the providedrootpubkey.If either check fails, the transaction reverts with
ERR_NOT_OWNER.
Usage:
Used when ownership may be optionally defined; enforces that if a public key is provided, only that owner can call the function.
2. onlyOwnerPubkey
modifier onlyOwnerPubkey(uint256 rootpubkey)
Parameters:
rootpubkey- the expected public key of the owner.
Behavior:
Requires that the caller's public key (
msg.pubkey()) matchesrootpubkey.Reverts with
ERR_NOT_OWNERif the condition is not met.
Usage:
Strict ownership check via public key; ensures only the owner with this public key can invoke the function.
3. onlyOwnerAddress
modifier onlyOwnerAddress(address addr)
Parameters:
addr- the expected Ethereum-style sender address.
Behavior:
Requires that the sender's address (
msg.sender) matches the givenaddr.Reverts with
ERR_NOT_OWNERif not.
Usage:
Ownership or access control via sender address rather than public key.
4. minValue
modifier minValue(uint128 val)
Parameters:
val- minimum required amount of value (in contract's currency units) sent with the transaction.
Behavior:
Ensures that the transaction's attached value (
msg.value) is at leastval.Reverts with
ERR_LOW_VALUEif insufficient.
Usage:
Ensures functions receive a minimum payment or deposit.
5. senderIs
modifier senderIs(address sender)
Parameters:
sender- expected sender address.
Behavior:
Requires that
msg.sendermatches the providedsenderaddress.Reverts with
ERR_INVALID_SENDERif not.
Usage:
Validates that the function caller is a specific sender, useful for restricted access.
6. minBalance
modifier minBalance(uint128 val)
Parameters:
val- minimum required balance the contract must have.
Behavior:
Checks that the contract's balance (
address(this).balance) is greater thanval + 1 vmshell.Reverts with
ERR_LOW_BALANCEif the balance is insufficient.
Usage:
Ensures contract has a minimum balance before executing certain operations.
Implementation Details
The modifiers use require statements with error constants imported from the
Errorscontract to enforce conditions.The use of
optional(uint256)foronlyOwnerPubkeyOptionalsuggests that the contract uses an advanced Solidity extension supporting optional types, improving safety and expressiveness.The
vmshellunit indicates a custom denomination, likely part of the underlying blockchain's currency unit system.The contract does not define any state variables aside from constants, focusing purely on reusable access control logic.
All modifiers end with
_to indicate where the modified function's code is injected when the modifier is applied.
Interaction with Other Files
errors.sol: Provides error message constants like
ERR_NOT_OWNER,ERR_LOW_VALUE,ERR_INVALID_SENDER, andERR_LOW_BALANCE. The Modifiers contract relies on these for consistent revert messages.structs/structs.sol: Supplies the
optional(uint256)type used in theonlyOwnerPubkeyOptionalmodifier, enabling optional parameter handling.
Contracts that inherit Modifiers will gain access to these modifiers and the error messages, facilitating consistent and reusable access control checks across the system.
Usage Example
contract MyContract is Modifiers {
uint256 public ownerPubkey;
constructor(uint256 _ownerPubkey) public {
ownerPubkey = _ownerPubkey;
}
function sensitiveAction() external onlyOwnerPubkey(ownerPubkey) minValue(1_000_000) {
// Function body only executes if caller is owner and sent at least 1_000_000 units of value
}
}
Structure Diagram
classDiagram
class Modifiers {
<<abstract>>
+versionModifiers: string
+MIN_BALANCE: uint64
+onlyOwnerPubkeyOptional()
+onlyOwnerPubkey()
+onlyOwnerAddress()
+minValue()
+senderIs()
+minBalance()
}
Modifiers --|> Errors
The diagram shows that
Modifiersis an abstract contract inheriting fromErrors.Lists the constants and modifiers defined within
Modifiers, representing its core functionality.