ethereum - ERC20Capped:在合约创建期间无法读取不可变变量,这意味着它们无法在构造函数 OpenZeppelin 4 中读取
问题描述
当我尝试使用ERC20Capped
来自 OpenZeppelin 4的构造函数内部进行铸造时
contract SomeERC20 is ERC20Capped {
constructor (
string memory name,
string memory symbol,
uint256 cap,
uint256 initialBalance
)
ERC20(name, symbol)
ERC20Capped(cap)
{
_mint(_msgSender(), initialBalance);
}
}
错误
Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it
出现。
我应该怎么办?
解决方案
cap 是不可变的,ERC20Capped
因此在构造函数的 mint 过程中无法读取它。这样做是为了降低天然气成本。您可以在构造函数之外创建,也可以像这样使用_mint
common 中的函数ERC20
contract SomeERC20 is ERC20Capped {
constructor (
string memory name,
string memory symbol,
uint256 cap,
uint256 initialBalance
)
ERC20(name, symbol)
ERC20Capped(cap)
{
require(initialBalance <= cap, "CommonERC20: cap exceeded"); // this is needed to know for sure the cap is not exceded.
ERC20._mint(_msgSender(), initialBalance);
}
}
建议添加一个initialSupply
低于cap
的检查。检查最初是在_mint
函数中完成的,ERC20Capped
但不是在函数中完成ERC20
,因为您使用的是后者,所以省略了检查。
推荐阅读
- azure - azure functions 或 azure logic app 中是否有任何触发 Azure File Share 的触发器?
- sml - SML:错误:非构造函数应用于模式中的参数:-
- django - urlopen 错误 [Errno 54] 对等方重置连接
- java - Java 连接到 Amazon Redshift
- c - 查找字符串中最长单词的程序
- ios - iOS 为什么 NSParagraphStyle 会重置原始文本样式
- java - javax.mail.Part.getContent() ArrayIndexOutOfBoundsException:16448
- kubernetes - Kubernetes 集群自动扩缩器似乎不适用于 GKE?
- sql - 复制列中的所有数字并将其粘贴到 ID 值小 1 的行
- ruby - 使用 Ruby 正则表达式删除子字符串