ERC20的transferFrom函数不满足require语句会不会失败?
Does ERC20's transferFrom function fail if it doesn't meet the require statement?
我正在学习 OpenZeppelin 的 ERC20 合约,并对 approve 函数感到好奇:
function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(sender, recipient, amount);
uint256 currentAllowance = _allowances[sender][_msgSender()];
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
_approve(sender, _msgSender(), currentAllowance - amount);
return true;
}
从逻辑上讲,只有当 currentAllowance >= amount
.
但是,正如您在此函数体中看到的那样,require(currentAllowance >= amount)
发生在 之后 调用 _transfer(sender, recipient, amount)
。
现在我记得读过以太坊交易是原子的。在这种情况下,是否意味着如果 require 条件失败,那么 _transfer
也不执行?
does it mean that if the require condition fails, then the _transfer is also not executed
从技术上讲,它被执行了,但随后它恢复了,因为失败的 require
条件产生了无效的操作码。
所以没有状态变化(这可能是你所说的“未执行”的意思),但是gas已经被使用(用于执行)。
编辑澄清:在这种情况下没有状态变化意味着没有令牌被转移。
我正在学习 OpenZeppelin 的 ERC20 合约,并对 approve 函数感到好奇:
function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(sender, recipient, amount);
uint256 currentAllowance = _allowances[sender][_msgSender()];
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
_approve(sender, _msgSender(), currentAllowance - amount);
return true;
}
从逻辑上讲,只有当 currentAllowance >= amount
.
但是,正如您在此函数体中看到的那样,require(currentAllowance >= amount)
发生在 之后 调用 _transfer(sender, recipient, amount)
。
现在我记得读过以太坊交易是原子的。在这种情况下,是否意味着如果 require 条件失败,那么 _transfer
也不执行?
does it mean that if the require condition fails, then the _transfer is also not executed
从技术上讲,它被执行了,但随后它恢复了,因为失败的 require
条件产生了无效的操作码。
所以没有状态变化(这可能是你所说的“未执行”的意思),但是gas已经被使用(用于执行)。
编辑澄清:在这种情况下没有状态变化意味着没有令牌被转移。