From 42cef0dea20f377c622366ae555fdf5809554a70 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Mon, 25 Aug 2025 13:51:07 +0000 Subject: [PATCH 1/2] Add accural functionality --- src/Gasback.sol | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/Gasback.sol b/src/Gasback.sol index 45c4859..1d1b162 100644 --- a/src/Gasback.sol +++ b/src/Gasback.sol @@ -30,6 +30,10 @@ contract Gasback { // recipient of the base fee vault, it can be configured to auto-pull // funds from the base fee vault when it runs out of ETH. address baseFeeVault; + // The amount of ETH accrued that can be safely withdrawn by the owner + uint256 accrued; + // A mapping of addresses authorized to withdraw the accrued ETH. + mapping(address => bool) accuralWithdrawers; } /// @dev Returns a pointer to the storage struct. @@ -72,6 +76,41 @@ contract Gasback { return _getGasbackStorage().baseFeeVault; } + /*«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-*/ + /* ACCURAL FUNCTIONS */ + /*-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»*/ + + /// @dev Returns the amount of ETH accrued. + function accrued() public view virtual returns (uint256) { + return _getGasbackStorage().accrued; + } + + /// @dev Withdraws from the accrued amount. + function withdrawAccrued(address to, uint256 amount) public virtual returns (bool) { + require(_getGasbackStorage().accuralWithdrawers[msg.sender]); + // Checked math prevents underflow. + _getGasbackStorage().accrued -= amount; + /// @solidity memory-safe-assembly + assembly { + if iszero(call(gas(), to, amount, 0x00, 0x00, 0x00, 0x00)) { revert(0x00, 0x00) } + } + return true; + } + + /// @dev Returns whether `addr` is authorized to call `withdrawAccrued`. + function isAuthorizedAccuralWithdrawer(address addr) public view virtual returns (bool) { + return _getGasbackStorage().accuralWithdrawers[addr]; + } + + /// @dev Set whether `addr` is authorized to call `withdrawAccrued`. + function setAccuralWithdrawer(address addr, bool authorized) + public + onlySystemOrThis + returns (bool) + { + _getGasbackStorage().accuralWithdrawers[addr] = authorized; + } + /*«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-*/ /* ADMIN FUNCTIONS */ /*-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»*/ @@ -137,8 +176,11 @@ contract Gasback { GasbackStorage storage $ = _getGasbackStorage(); - uint256 ethToGive = - (gasToBurn * block.basefee * $.gasbackRatioNumerator) / GASBACK_RATIO_DENOMINATOR; + uint256 ethFromGas = gasToBurn * block.basefee; + uint256 ethToGive = (ethFromGas * $.gasbackRatioNumerator) / GASBACK_RATIO_DENOMINATOR; + unchecked { + $.accrued += ethFromGas - ethToGive; + } // If the contract has insufficient ETH, try to pull from the base fee vault. if (ethToGive > address(this).balance) { From 1881cb29c33064358bf9298c20bfca51128307a6 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Mon, 25 Aug 2025 14:05:06 +0000 Subject: [PATCH 2/2] Fix comment --- src/Gasback.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Gasback.sol b/src/Gasback.sol index 023f3b1..9ffc196 100644 --- a/src/Gasback.sol +++ b/src/Gasback.sol @@ -32,7 +32,7 @@ contract Gasback { address baseFeeVault; // The minimum balance of the base fee vault. uint256 minVaultBalance; - // The amount of ETH accrued that can be safely withdrawn by the owner + // The amount of ETH accrued by taking a cut from the gas burned. uint256 accrued; // A mapping of addresses authorized to withdraw the accrued ETH. mapping(address => bool) accuralWithdrawers;