ERC-7674 · Review · ERC · 2024-04-02
ERC-7674: Temporary Approval Extension for ERC-20
Adds temporaryApprove to ERC-20: an allowance kept in transient storage that lives for one transaction only, so one-off approvals never linger.
What problem does it solve?
Using a DEX, bridge or vault with an ERC-20 token usually means two steps: approve the protocol, then call it so it can transferFrom your tokens. The approval is stored permanently. Users often approve more than they need, or forget to revoke, and those leftover allowances are what attackers drain when a protocol is later compromised.
ERC-7674 adds an approval that cannot linger. It is stored in transient storage (EIP-1153), which the EVM wipes automatically at the end of every transaction. It is also cheaper than a normal approval because transient writes cost far less than storage writes.
How it works
The token adds one function:
function temporaryApprove(address spender, uint256 value) public returns (bool success);
- It lets
spenderpull up tovaluetokens frommsg.sender, possibly over severaltransferFromcalls, but only within the current transaction. - The temporary allowance is in addition to the normal persistent allowance.
transferFromshould use the temporary one first and only touch the persistent one (in storage) if the temporary one is not enough. allowance(owner, spender)must return temporary + persistent. If that sum overflows, it returnstype(uint256).max.- At the end of the transaction the temporary allowance is gone, whether or not it was used.
The temporary allowance must live in a different slot from the normal one. The reference approach derives a transient slot from a namespace hash, the owner and the spender. Emitting an event is optional; the spec suggests TransientApproval(owner, spender, value) if you want one.
Sample code
A sketch of the extension on top of OpenZeppelin v5 ERC20 (requires Solidity ≥ 0.8.24 for tstore/tload). Illustrative, not audited:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
abstract contract ERC20TemporaryApproval is ERC20 {
bytes32 private constant NAMESPACE = keccak256("example.erc7674.temporaryAllowance");
function _slot(address owner, address spender) private pure returns (bytes32) {
return keccak256(abi.encode(spender, keccak256(abi.encode(owner, NAMESPACE))));
}
function _temp(address owner, address spender) private view returns (uint256 v) {
bytes32 s = _slot(owner, spender);
assembly { v := tload(s) }
}
function temporaryApprove(address spender, uint256 value) public returns (bool) {
bytes32 s = _slot(msg.sender, spender);
assembly { tstore(s, value) }
return true;
}
function allowance(address owner, address spender) public view override returns (uint256) {
uint256 t = _temp(owner, spender);
uint256 p = super.allowance(owner, spender);
unchecked {
uint256 sum = t + p;
return sum < t ? type(uint256).max : sum;
}
}
function _spendAllowance(address owner, address spender, uint256 value) internal override {
uint256 t = _temp(owner, spender);
uint256 fromTemp = t < value ? t : value;
if (fromTemp > 0) {
bytes32 s = _slot(owner, spender);
uint256 left = t - fromTemp;
assembly { tstore(s, left) }
}
if (value > fromTemp) super._spendAllowance(owner, spender, value - fromTemp);
}
}
Things to know
- Status is Review: the interface may still change before it is final.
temporaryApproveis called by the token owner and expires with the transaction, so the owner must approve and trigger the spend in the same transaction. That fits smart accounts and batched calls (for example an EOA using EIP-7702) or contracts that hold tokens; a plain EOA sending one transaction cannot use it on its own.allowancenow includes the temporary part, which slightly changes its meaning compared with plain ERC-20; integrations that cache allowances should be aware.- Signature-based approvals such as ERC-2612
permitsolve a related problem without needing a batch, but still leave a stored allowance if not fully used.