File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // SPDX-License-Identifier: MIT
2+
3+ pragma solidity ^ 0.8.0 ;
4+
5+ import { TransientLib, tuint256 } from "./Transient.sol " ;
6+
7+ struct ReentrancyGuard {
8+ tuint256 _raw;
9+ }
10+
11+ library ReentrancyGuardLib {
12+ using TransientLib for tuint256;
13+
14+ error ReentrantCallDetected ();
15+ error EnterLeaveDisbalance ();
16+
17+ function enter (ReentrancyGuard storage self ) internal {
18+ if (self._raw.inc () != 1 ) revert ReentrantCallDetected ();
19+ }
20+
21+ function enterNoIncrement (ReentrancyGuard storage self ) internal view {
22+ if (self._raw.tload () != 0 ) revert ReentrantCallDetected ();
23+ }
24+
25+ function leave (ReentrancyGuard storage self ) internal {
26+ self._raw.dec (EnterLeaveDisbalance.selector );
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ // SPDX-License-Identifier: MIT
2+
3+ pragma solidity ^ 0.8.0 ;
4+
5+ import { ReentrancyGuard, ReentrancyGuardLib } from "./ReentrancyGuard.sol " ;
6+
7+ contract ReentrancyGuardBase {
8+ using ReentrancyGuardLib for ReentrancyGuard;
9+
10+ ReentrancyGuard private _lock;
11+
12+ modifier nonReentrant {
13+ _lock.enter ();
14+ _;
15+ _lock.leave ();
16+ }
17+
18+ modifier nonReentrantView {
19+ _lock.enterNoIncrement ();
20+ _;
21+ }
22+
23+ modifier nonReentrantGuard (ReentrancyGuard storage lock ) {
24+ lock.enter ();
25+ _;
26+ lock.leave ();
27+ }
28+
29+ modifier nonReentrantViewGuard (ReentrancyGuard storage lock ) {
30+ lock.enterNoIncrement ();
31+ _;
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments