Use the following format for section comments:
///////////////////
// Section Name
///////////////////
- Within a contract, surround function declarations with a single blank line.
- Maximum suggested line length is 120 characters.
- Use CapWords style (also known as PascalCase).
Example:
MyContract
,LibraryCoolFeature
- Use CapWords style.
Example:
UserProfile
,PaymentDetails
- Use CapWords style.
Example:
Transfer
,Approval
- Use mixedCase.
Example:
getBalance
,sendTokens
- Use mixedCase.
Example:
initialSupply
,account
- Use mixedCase.
Example:
totalSupply
,balanceOf
- Use capital letters with underscores separating words.
Example:
MAX_SUPPLY
,DECIMALS
- Use mixedCase.
Example:
onlyOwner
,whenNotPaused
- Use CapWords style for the enum name, and all uppercase for the values.
Example:
enum Status { PENDING, SHIPPED, ACCEPTED, REJECTED, CANCELED }
- Use
singleTrailingUnderscore_
when the desired name collides with an existing state variable, function, built-in, or otherwise reserved name. Example:transfer_
- Use
_singleLeadingUnderscore
for private functions and variables. Example:_transfer
,_totalSupply
- Version
- Imports
- Errors
- Interfaces, libraries, contracts
- Type declarations
- State variables
- Events
- Modifiers
- Functions
- Constructor
- Receive function (if exists)
- Fallback function (if exists)
- External functions
- Public functions
- Internal functions
- Private functions
- Internal & private view & pure functions
- External & public view & pure functions
When defining a function, use the following order for modifiers:
- Visibility
- Mutability
- Virtual
- Override
- Custom modifiers
Example:
function foo() public payable virtual override onlyOwner returns (uint256) {
// ...
}
contractname_errorname
Solidity contracts can contain NatSpec comments. They are written with a triple slash (///) or a double asterisk block (/** ... */) and should be used directly above function declarations or statements.
Example:
/// @notice Transfers tokens from the caller to a recipient
/// @dev This function emits a Transfer event
/// @param recipient The address to receive the tokens
/// @param amount The number of tokens to transfer
/// @return A boolean indicating if the transfer was successful
function transfer(address recipient, uint256 amount) public returns (bool) {
// Function implementation
}
///////////////////
// State Variables
///////////////////
uint256 public totalSupply;
mapping(address => uint256) private _balances;
///////////////////
// Events
///////////////////
event Transfer(address indexed from, address indexed to, uint256 value);
///////////////////
// Modifiers
///////////////////
modifier onlyOwner() {
require(msg.sender == owner, "Not the owner");
_;
}
///////////////////
// Functions
///////////////////
function transfer(address to, uint256 amount) public virtual returns (bool) {
// Function implementation
}