合约额度更新
两个approval看看怎么回事

code
https://vscode.blockscan.com/optimism/0x94b008aa00579c1307b0ef2c499ad98a8ce58e58
transferFrom 结束后,会更新_approve额度?
function transferFrom(address sender, address recipient, uint256 amount) public
onlyPayloadSize(2 * 32)
override returns (bool)
{
_transfer(sender, recipient, amount);
uint256 currentAllowance = allowance(sender, _msgSender());
if (currentAllowance < type(uint).max)
{
_approve(sender, _msgSender(), currentAllowance.sub(amount, "ERC20: transfer amount exceeds allowance"));
}
return true;
}
transferFrom 结束后,会更新_approve额度?
在你提供的 transferFrom 函数中,更新 _approve 额度的逻辑是有条件的。让我们逐步分析这个函数的工作原理。
函数解析
-
参数:
sender: 发起转账的地址。recipient: 接收转账的地址。amount: 转账的金额。
-
功能:
- 调用
_transfer(sender, recipient, amount)执行实际的转账操作。 - 获取当前的允许额度
currentAllowance,即sender允许msg.sender使用的代币数量。
- 调用
-
条件检查:
- 如果
currentAllowance小于type(uint).max(最大值),则执行更新_approve的逻辑。 - 这里的
currentAllowance.sub(amount, "ERC20: transfer amount exceeds allowance")是在检查转账金额是否超过了允许额度。
- 如果
更新 _approve 额度
-
更新条件:
- 只有在
currentAllowance小于type(uint).max的情况下,_approve的额度才会被更新。
- 只有在
-
更新的逻辑:
_approve(sender, _msgSender(), currentAllowance.sub(amount, ...))将sender对msg.sender的允许额度更新为currentAllowance减去amount。
结论
在 transferFrom 函数结束后,只有在 currentAllowance 小于最大值的情况下,才会更新 approve 的额度。如果 currentAllowance 已经是最大值,则不会更新。
因此,是否更新 _approve 额度取决于 currentAllowance 的值。希望这个解释对你有帮助!如果还有其他问题,请随时问我。
合约不会自动更新额度,需要执行代码来更新?
是的,合约不会自动更新额度,必须通过执行代码来进行更新。具体来说,ERC20 代币合约的 approve 和 transferFrom 函数都是需要显式调用的。
详细说明
-
approve函数:- 用户(代币持有者)需要调用
approve函数,明确授权某个地址(通常是合约)可以花费一定数量的代币。这个操作是显式的,合约不会自动改变任何额度。
- 用户(代币持有者)需要调用
-
transferFrom函数:- 当用户调用
transferFrom函数时,合约会检查当前的允许额度。如果msg.sender的转账请求超过了允许额度,合约会抛出错误。 - 在
transferFrom函数中,只有在currentAllowance小于type(uint).max的情况下,才会更新额度。这是通过调用_approve函数来实现的。
- 当用户调用
总结
- 额度的更新需要通过调用合约中的函数(如
approve和transferFrom)来显式执行。 - 合约本身不会自动调整额度;所有的额度管理都依赖于用户的操作。
如果你还有其他问题,或者需要更深入的解释,请告诉我!