-
Notifications
You must be signed in to change notification settings - Fork 61
docs: add NatSpec documentation for ERC20 and ERC721 libraries #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mudgen
merged 2 commits into
Perfect-Abstractions:main
from
Haroldwonder:docs/natspec-libs
Oct 18, 2025
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,38 +1,69 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity >=0.8.30; | ||
|
|
||
| /// @title LibERC20 — Minimal ERC-20 Library for Diamond Storage Implementation | ||
| /// @notice Provides internal functions and storage layout for ERC-20 token logic. | ||
| /// @dev Uses ERC-8042 for storage location standardization and ERC-6093 for error conventions. | ||
| library LibERC20 { | ||
|
|
||
| // ERC-6093: Custom errors for ERC-20 | ||
|
|
||
| /// @notice Thrown when a sender attempts to transfer or burn more tokens than their balance. | ||
| /// @param _sender The address attempting the transfer or burn. | ||
| /// @param _balance The sender's current balance. | ||
| /// @param _needed The amount required to complete the operation. | ||
| error ERC20InsufficientBalance(address _sender, uint256 _balance, uint256 _needed); | ||
|
|
||
| /// @notice Thrown when the sender address is invalid (e.g., zero address). | ||
| /// @param _sender The invalid sender address. | ||
| error ERC20InvalidSender(address _sender); | ||
|
|
||
| /// @notice Thrown when the receiver address is invalid (e.g., zero address). | ||
| /// @param _receiver The invalid receiver address. | ||
| error ERC20InvalidReceiver(address _receiver); | ||
|
|
||
| /// @notice Thrown when a spender tries to spend more than their allowance. | ||
| /// @param _spender The address attempting to spend. | ||
| /// @param _allowance The current allowance. | ||
| /// @param _needed The required amount to complete the transfer. | ||
| error ERC20InsufficientAllowance(address _spender, uint256 _allowance, uint256 _needed); | ||
|
|
||
|
|
||
| /// @notice Emitted when tokens are transferred between addresses. | ||
| /// @param _from The address tokens are transferred from. | ||
| /// @param _to The address tokens are transferred to. | ||
| /// @param _value The amount of tokens transferred. | ||
| event Transfer(address indexed _from, address indexed _to, uint256 _value); | ||
| // Struct storage position defined by keccak256 hash | ||
| // of diamond storage identifier | ||
|
|
||
|
|
||
| /// @notice Storage slot identifier, defined using keccak256 hash of the library namespace. | ||
|
||
| bytes32 constant STORAGE_POSITION = keccak256("compose.erc20"); | ||
|
|
||
| // Storage defined using the ERC-8042 standard | ||
| // @custom:storage-location erc8042:compose.erc20 | ||
| /// @notice ERC-20 storage layout using the ERC-8042 standard. | ||
| /// @custom:storage-location erc8042:compose.erc20 | ||
| struct ERC20Storage { | ||
| string name; | ||
| string symbol; | ||
| uint8 decimals; | ||
| uint256 totalSupply; | ||
| mapping(address owner => uint256 balance) balanceOf; | ||
| mapping(address owner => mapping(address spender => uint256 allowance)) allowances; | ||
| mapping(address owner => mapping(address spender => uint256 allowance)) allowances; | ||
| } | ||
|
|
||
|
|
||
| /// @notice Returns a pointer to the ERC-20 storage struct. | ||
| /// @dev Uses inline assembly to bind the storage struct to the fixed storage position. | ||
| /// @return s The ERC-20 storage struct. | ||
| function getStorage() internal pure returns (ERC20Storage storage s) { | ||
| bytes32 position = STORAGE_POSITION; | ||
| assembly { | ||
| s.slot := position | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// @notice Mints new tokens to a specified address. | ||
| /// @dev Increases both total supply and the recipient’s balance. | ||
| /// @param _account The address receiving the newly minted tokens. | ||
| /// @param _value The number of tokens to mint. | ||
| function mint(address _account, uint256 _value) internal { | ||
| ERC20Storage storage s = getStorage(); | ||
| if (_account == address(0)) { | ||
|
|
@@ -45,6 +76,10 @@ library LibERC20 { | |
| emit Transfer(address(0), _account, _value); | ||
| } | ||
|
|
||
| /// @notice Burns tokens from a specified address. | ||
| /// @dev Decreases both total supply and the sender’s balance. | ||
| /// @param _account The address whose tokens will be burned. | ||
| /// @param _value The number of tokens to burn. | ||
| function burn(address _account, uint256 _value) internal { | ||
| ERC20Storage storage s = getStorage(); | ||
| if (_account == address(0)) { | ||
|
|
@@ -61,6 +96,11 @@ library LibERC20 { | |
| emit Transfer(_account, address(0), _value); | ||
| } | ||
|
|
||
| /// @notice Transfers tokens from one address to another using an allowance. | ||
| /// @dev Deducts the spender’s allowance and updates balances. | ||
| /// @param _from The address to send tokens from. | ||
| /// @param _to The address to send tokens to. | ||
| /// @param _value The number of tokens to transfer. | ||
| function transferFrom(address _from, address _to, uint256 _value) internal { | ||
| ERC20Storage storage s = getStorage(); | ||
| if (_from == address(0)) { | ||
|
|
@@ -84,6 +124,4 @@ library LibERC20 { | |
| } | ||
| emit Transfer(_from, _to, _value); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.