This contract provides a gas efficient reentrancy guard by leveraging EIP-1153 transient storage opcodes (tstore and tload). Unlike traditional guards that use permanent storage (sstore), this lock is automatically cleared at the end of the transaction, significantly reducing gas costs for users.
-
Ultra gas efficient: Eliminates the "cold" vs "warm" storage costs associated with traditional reentrancy guards.
-
Automatic reset: Since it uses transient storage, the state is cleared at the end of every transaction, preventing "stuck" locks.
-
Minimal footprint: Uses assembly to interact directly with storage slots for maximum optimization.
-
Inherit from the
ReentrancyLockcontract. -
Apply the
cantReentermodifier to sensitive external or public functions.
import "./ReentrancyLock.sol";
contract MySecureContract is ReentrancyLock {
function sensitiveWithdraw() external cantReenter {
// function logic
}
}Do not deploy this on chains that do not support the Cancun hardfork. On incompatible chains, the tstore and tload opcodes will cause the transaction to fail or behave unpredictably.