Skip to content

Instantly share code, notes, and snippets.

@darkerego
Created January 24, 2025 22:56
Show Gist options
  • Save darkerego/157c998fd5e38d8ecdee4d2b86795eae to your computer and use it in GitHub Desktop.
Save darkerego/157c998fd5e38d8ecdee4d2b86795eae to your computer and use it in GitHub Desktop.
Assembly ReenterancyGaurd
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
abstract contract NoReEnter {
uint8 internal mutex;
bytes32 constant internal ERROR_STATE_LOCKED = 0x07b2e09e00000000000000000000000000000000000000000000000000000000;
error StateLocked();
function _auth() private view returns(bool unlocked) {
assembly {
let _mutex := sload(mutex.slot)
if iszero(eq(_mutex, 0x00)) {
mstore(0x00, ERROR_STATE_LOCKED)
revert(0x00, 0x04)
}
unlocked := 0x01
}
}
function toggle(uint8 locked) internal {
assembly {
if eq(locked, 0x0) {
sstore(mutex.slot, 0x01)
}
if eq(locked, 0x01) {
sstore(mutex.slot, 0x00)
}
}
}
modifier reenterancyGaurd {
if (_auth()) {
toggle(1);
_;
toggle(0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment