Skip to content

Commit

Permalink
refactor: token interface directory, remove hook style to override _u…
Browse files Browse the repository at this point in the history
…pdate

Signed-off-by: MASDXI <[email protected]>
  • Loading branch information
MASDXI committed Oct 25, 2024
1 parent 5a54b00 commit b5b77e0
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 57 deletions.
2 changes: 1 addition & 1 deletion contracts/abstracts/LightWeightERC20EXPBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pragma solidity >=0.8.0 <0.9.0;

import {SlidingWindow} from "./LightWeightSlidingWindow.sol";
import {SortedCircularDoublyLinkedList as SCDLL} from "../utils/LightWeightSortedCircularDoublyLinkedList.sol";
import {IERC20EXPBase} from "../interfaces/IERC20EXPBase.sol";
import {IERC20EXPBase} from "../tokens/ERC20/IERC20EXPBase.sol";
import {Context} from "@openzeppelin/contracts/utils/Context.sol";
import {IERC20Errors} from "@openzeppelin/contracts/interfaces/draft-IERC6093.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/devs/ERC20EXPBaseDev.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pragma solidity >=0.8.0 <0.9.0;

import {SlidingWindow} from "./SlidingWindowDev.sol";
import {SortedCircularDoublyLinkedList as SCDLL} from "../utils/SortedCircularDoublyLinkedList.sol";
import {IERC20EXPBase} from "../interfaces/IERC20EXPBase.sol";
import {IERC20EXPBase} from "../tokens/ERC20/IERC20EXPBase.sol";
import {Context} from "@openzeppelin/contracts/utils/Context.sol";
import {IERC20Errors} from "@openzeppelin/contracts/interfaces/draft-IERC6093.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
Expand Down
6 changes: 5 additions & 1 deletion contracts/tokens/ERC1155/ERC1155EXPBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pragma solidity >=0.8.0 <0.9.0;

import {SlidingWindow as Slide} from "../../utils/SlidingWindow.sol";
import {SortedCircularDoublyLinkedList as SCDLL} from "../../utils/LightWeightSortedCircularDoublyLinkedList.sol";
import {IERC1155EXPBase} from "../../interfaces/IERC1155EXPBase.sol";
import {IERC1155EXPBase} from "./IERC1155EXPBase.sol";
import {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import {IERC1155MetadataURI} from "@openzeppelin/contracts/token/ERC1155/extensions/IERC1155MetadataURI.sol";

Expand Down Expand Up @@ -45,4 +45,8 @@ abstract contract ERC1155EXPBase is IERC1155, IERC1155EXPBase, IERC1155MetadataU
}

// @TODO finish other method
/// @custom:gas-inefficiency balanceOf(accounts, ids)
/// @custom:gas-inefficiency _safeBatchTransferFrom(from, to, ids, values, data)
/// @custom:gas-inefficiency _mintBatch(to, ids, values, data)
/// @custom:gas-inefficiency _burnBatch(from, ids, values)
}
File renamed without changes.
30 changes: 5 additions & 25 deletions contracts/tokens/ERC20/ERC20EXPBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pragma solidity >=0.8.0 <0.9.0;

import {SlidingWindow} from "../../abstracts/SlidingWindow.sol";
import {SortedCircularDoublyLinkedList as SCDLL} from "../../utils/SortedCircularDoublyLinkedList.sol";
import {IERC20EXPBase} from "../../interfaces/IERC20EXPBase.sol";
import {IERC20EXPBase} from "./IERC20EXPBase.sol";
import {Context} from "@openzeppelin/contracts/utils/Context.sol";
import {IERC20Errors} from "@openzeppelin/contracts/interfaces/draft-IERC6093.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
Expand Down Expand Up @@ -149,10 +149,7 @@ abstract contract ERC20EXPBase is Context, IERC20, IERC20Metadata, IERC20Errors,
/// @param from The address from which tokens are being transferred (or minted/burned).
/// @param to The address to which tokens are being transferred (or burned to if `to` is `zero address`).
/// @param value The amount of tokens being transferred, minted, or burned.
function _update(address from, address to, uint256 value) private {
// Hook before transfer
_beforeTokenTransfer(from, to, value);

function _update(address from, address to, uint256 value) internal virtual {
uint256 blockNumberCache = _blockNumberProvider();
uint256 blockLengthCache = _getFrameSizeInBlockLength();
uint8 slotSizeCache = _getSlotPerEra();
Expand Down Expand Up @@ -266,9 +263,6 @@ abstract contract ERC20EXPBase is Context, IERC20, IERC20Metadata, IERC20Errors,
}

emit Transfer(from, to, value);

// Hook after transfer
_afterTokenTransfer(from, to, value);
}

/// @notice Retrieves the Slot storage for a given account, era, and slot.
Expand Down Expand Up @@ -311,7 +305,7 @@ abstract contract ERC20EXPBase is Context, IERC20, IERC20Metadata, IERC20Errors,
/// Reverts if the `account` address is zero.
/// @param account The address of the account to receive the minted tokens.
/// @param value The amount of tokens to be minted.
function _mint(address account, uint256 value) internal virtual {
function _mint(address account, uint256 value) internal {
if (account == address(0)) {
revert ERC20InvalidReceiver(address(0));
}
Expand All @@ -323,27 +317,13 @@ abstract contract ERC20EXPBase is Context, IERC20, IERC20Metadata, IERC20Errors,
/// Reverts if the `account` address is zero.
/// @param account The address of the account from which tokens will be burned.
/// @param value The amount of tokens to be burned.
function _burn(address account, uint256 value) internal virtual {
function _burn(address account, uint256 value) internal {
if (account == address(0)) {
revert ERC20InvalidSender(address(0));
}
_update(account, address(0), value);
}

/// @notice Abstract hook called before every token transfer operation.
/// @dev This function is called before every token transfer operation for additional checks or actions.
/// @param from The address sending tokens.
/// @param to The address receiving tokens.
/// @param amount The amount of tokens being transferred.
function _beforeTokenTransfer(address from, address to, uint amount) internal virtual {}

/// @notice Abstract hook called after every token transfer operation.
/// @dev This function is called after every token transfer operation for additional processing or logging.
/// @param from The address sending tokens.
/// @param to The address receiving tokens.
/// @param amount The amount of tokens being transferred.
function _afterTokenTransfer(address from, address to, uint256 amount) internal virtual {}

/// @notice Spends the specified allowance by reducing the allowance of the spender.
/// @dev This function deducts the `value` amount from the current allowance of the `spender` by the `owner`.
/// If the current allowance is less than `value`, the function reverts with an error.
Expand Down Expand Up @@ -399,7 +379,7 @@ abstract contract ERC20EXPBase is Context, IERC20, IERC20Metadata, IERC20Errors,
/// @param from The address from which the tokens are transferred.
/// @param to The address to which the tokens are transferred.
/// @param value The amount of tokens to transfer.
function _transfer(address from, address to, uint256 value) internal virtual {
function _transfer(address from, address to, uint256 value) internal {
if (from == address(0)) {
revert ERC20InvalidSender(address(0));
}
Expand Down
File renamed without changes.
17 changes: 2 additions & 15 deletions contracts/tokens/ERC20/extensions/ERC20EXPBlacklist.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,11 @@ abstract contract ERC20EXPBlacklist is ERC20EXPBase {
emit Unblacklisted(_msgSender(), account);
}

/// @notice Mints tokens to the specified account
/// @dev This function overrides the `_mint` function to include blacklist checks
/// @param account The address to mint tokens to
/// @param value The amount of tokens to mint
function _mint(address account, uint256 value) internal virtual override notBlacklisted(account) {
super._mint(account, value);
}

/// @notice Transfers tokens from one account to another
/// @dev This function overrides the `_transfer` function to include blacklist checks
/// @param from The address sending the tokens
/// @param to The address receiving the tokens
/// @param value The amount of tokens to transfer
function _transfer(
function _update(
address from,
address to,
uint256 value
) internal virtual override notBlacklisted(from) notBlacklisted(to) {
super._transfer(from, to, value);
super._update(from, to, value);
}
}
5 changes: 0 additions & 5 deletions contracts/tokens/ERC20/extensions/ERC20EXPWhitelist.sol
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,6 @@ abstract contract ERC20EXPWhitelist is ERC20EXPBase {
/// @param to The address to which tokens are being transferred.
/// @param value The amount of tokens being transferred.
function _transferHandler(address from, address to, uint256 value) internal {
// hook before transfer
_beforeTokenTransfer(from, to, value);
uint256 selector = (_whitelist[from] ? 2 : 0) | (_whitelist[to] ? 1 : 0);
if (selector == 0) {
_transfer(from, to, value);
Expand All @@ -198,9 +196,6 @@ abstract contract ERC20EXPWhitelist is ERC20EXPBase {
// wholesale to wholesale transfer only use spendable balance.
_updateSpendableBalance(from, to, value);
}

// hook after transfer
_afterTokenTransfer(from, to, value);
}

/// @inheritdoc IERC20
Expand Down
2 changes: 1 addition & 1 deletion contracts/tokens/ERC721/ERC721EXPBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pragma solidity >=0.8.0 <0.9.0;

import {SlidingWindow} from "../../abstracts/SlidingWindow.sol";
import {SortedCircularDoublyLinkedList as SCDLL} from "../../utils/LightWeightSortedCircularDoublyLinkedList.sol";
import {IERC721EXPBase} from "../../interfaces/IERC721EXPBase.sol";
import {IERC721EXPBase} from "./IERC721EXPBase.sol";
import {IERC721Errors} from "@openzeppelin/contracts/interfaces/draft-IERC6093.sol";
import {Context} from "@openzeppelin/contracts/utils/Context.sol";
import {EnumerableSet as EnumSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ interface IERC721EXPBase {
/// @param tokenId The ID of the token to check for expiration.
/// @return bool Returns `true` if the token has expired, otherwise `false`.
function hasExpired(uint256 tokenId) external view returns (bool);

function tokenList(address account) external view returns (bool);

// function balanceOf(address account, bool safe) external view returns (uint256);
}
12 changes: 4 additions & 8 deletions mocks/contracts/extensions/MockERC20EXPBacklist.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ contract MockERC20EXPBacklist is ERC20EXPBase, ERC20EXPBlacklist {
uint8 slotSize_
) ERC20EXPBase(_name, _symbol, block.number, blockTime_, frameSize_, slotSize_) {}

function mint(address to, uint256 value) public {
_mint(to, value);
}

function addToBlacklist(address account) public {
_addToBlacklist(account);
}
Expand All @@ -25,15 +21,15 @@ contract MockERC20EXPBacklist is ERC20EXPBase, ERC20EXPBlacklist {
_removeFromBlacklist(account);
}

function _mint(address account, uint256 value) internal virtual override(ERC20EXPBase, ERC20EXPBlacklist) {
return super._mint(account, value);
function mint(address to, uint256 value) public {
_mint(to, value);
}

function _transfer(
function _update(
address from,
address to,
uint256 value
) internal virtual override(ERC20EXPBase, ERC20EXPBlacklist) {
return super._transfer(from, to, value);
super._update(from, to, value);
}
}

0 comments on commit b5b77e0

Please sign in to comment.