概要
ERC20CappedUpgradeable は、ERC20トークンの総供給量(totalSupply)に上限(Cap)を設けるための拡張コントラクトです。 トークンのMint時に供給量が上限を超えないように検証し、制限を超える操作を無効化します。
このコントラクトはOpenZeppelinのERC20Upgradeableを継承し、Mint処理(from == address(0))時に totalSupply() が cap() を超えないように制御します。
また、初期化時や cap の更新時には、その値が 0 でないかつ totalSupply() 以上であることが求められます。
ストレージは keccak256 で固定スロット化されており、アップグレード対応の安全なストレージ配置となっています。
コード
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;
import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
/**
* @title ERC20 Capped Extension
* @author Kinexys by J.P.Morgan
* @dev Extension of ERC20 that adds a cap to the supply of tokens.
*/
abstract contract ERC20CappedUpgradeable is ERC20Upgradeable {
function __ERC20Capped_init(uint256 cap_) internal onlyInitializing {
__ERC20Capped_init_unchained(cap_);
}
function __ERC20Capped_init_unchained(uint256 cap_) internal onlyInitializing {
CappedStorage storage $ = _getCappedStorage();
$._cap = cap_;
emit CapUpdated(cap_);
}
/**
* @dev Cap amount for the token.
*/
struct CappedStorage {
uint256 _cap;
}
// keccak256(abi.encode(uint256(keccak256("Token.storage.Capped")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant CappedStorageLocation = 0x750f899a1959b60740f02dd5bc2f6b2d916542dcf68bc8d7e4c7f44504005f00;
/**
* @dev Private function that returns the CappedStorage struct from a fixed storage slot.
* @return $ CappedStorage struct that contains the cap amount.
*/
function _getCappedStorage() private pure returns (CappedStorage storage $) {
assembly {
$.slot := CappedStorageLocation
}
}
/**
* @dev Emitted when cap is updated.
* @param newCap Value of the new cap set for the token.
*/
event CapUpdated(uint256 newCap);
/**
* @dev Total supply cap has been exceeded.
* @param increasedSupply The total supply value that exceeded the set cap.
* @param cap The current cap amount.
*/
error ERC20ExceededCap(uint256 increasedSupply, uint256 cap);
/**
* @dev The supplied cap is not a valid cap.
* @param cap The invalid cap amount being set.
*/
error ERC20InvalidCap(uint256 cap);
/**
* @dev Returns the cap on the token's total supply.
* @return The current cap amount.
*/
function cap() public view virtual returns (uint256) {
CappedStorage storage $ = _getCappedStorage();
return $._cap;
}
/**
* @dev Sets the value of the `cap`.
* @param newCap The new cap amount to be set.
*/
function _setCap(uint256 newCap) internal virtual {
uint256 supply = totalSupply();
if (newCap == 0 || newCap < supply) {
revert ERC20InvalidCap(newCap);
}
CappedStorage storage $ = _getCappedStorage();
$._cap = newCap;
emit CapUpdated(newCap);
}
/**
* @dev See {ERC20Upgradeable-_update}.
*/
function _update(address from, address to, uint256 value) internal virtual override {
super._update(from, to, value);
if (from == address(0)) {
uint256 maxSupply = cap();
uint256 supply = totalSupply();
if (supply > maxSupply) {
revert ERC20ExceededCap(supply, maxSupply);
}
}
}
}変数
bytes32 private constant CappedStorageLocation = 0x750f899a1959b60740f02dd5bc2f6b2d916542dcf68bc8d7e4c7f44504005f00;- 固定ストレージスロットの位置(
bytes32)。 - 値
0x750f899a1959b60740f02dd5bc2f6b2d916542dcf68bc8d7e4c7f44504005f00
struct CappedStorage {
uint256 _cap;
}_cap(uint256)- トークン供給上限。
関数
cap
function cap() public view virtual returns (uint256) {
CappedStorage storage $ = _getCappedStorage();
return $._cap;
}現在設定されている供給上限(cap)を取得する関数。
戻り値
uint256- 現在の供給上限。
_setCap
function _setCap(uint256 newCap) internal virtual {
uint256 supply = totalSupply();
if (newCap == 0 || newCap < supply) {
revert ERC20InvalidCap(newCap);
}
CappedStorage storage $ = _getCappedStorage();
$._cap = newCap;
emit CapUpdated(newCap);
}供給上限(cap)を設定・更新する関数。
新しい cap 値が 0 でないこと、かつ現在の総供給量以上であることを検証して問題がなければ _cap を更新します。
更新時には CapUpdated イベントを発行します。
引数
newCap- 新しく設定する供給上限。
_getCappedStorage
function _getCappedStorage() private pure returns (CappedStorage storage $) {
assembly {
$.slot := CappedStorageLocation
}
}CappedStorage 構造体にアクセスする関数。
事前に定義された bytes32 型のスロット位置に基づき、_cap にアクセスできるようにします。
アップグレード対応のストレージ管理の一環です。
戻り値
$CappedStorageへの参照。
_update
function _update(address from, address to, uint256 value) internal virtual override {
super._update(from, to, value);
if (from == address(0)) {
uint256 maxSupply = cap();
uint256 supply = totalSupply();
if (supply > maxSupply) {
revert ERC20ExceededCap(supply, maxSupply);
}
}
}トークンの更新処理に cap 制御を加えた関数。
ERC20トークンの _update 処理を拡張し、Mint(from == address(0))時に totalSupply() が cap() を超えていないことを検証します。
制限を超えると ERC20ExceededCap エラーで返します。
引数
from- 送信元アドレス(ミント時は
address(0))。
- 送信元アドレス(ミント時は
to- 送信先アドレス。
value- 送付またはMintされるトークン数。
Event・Error
event CapUpdated(uint256 newCap)新しい cap が設定された時に発行されるイベント。
error ERC20ExceededCap(uint256 increasedSupply, uint256 cap)totalSupply が cap を超過した場合に返されるエラー。
error ERC20InvalidCap(uint256 cap)設定しようとした cap が無効(0 または現在の供給量未満)な場合に返されるエラー。