/
NoDelegateCall.sol
38 lines (29 loc) · 1.27 KB
/
NoDelegateCall.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//SPDX-License-Identifier: Unlicense
pragma solidity =0.8.8;
/// @title Prevents delegatecall to a contract
/// @notice Base contract that provides a modifier for preventing delegatecall to methods in a child contract.
contract NoDelegateCall {
/* ===== ERROR ===== */
/// @dev Reverts when called using delegatecall.
error CannotBeDelegateCalled();
/* ===== MODEL ===== */
/// @dev The original address of this contract.
address private immutable original;
/* ===== INIT ===== */
constructor() {
// Immutables are computed in the init code of the contract, and then inlined into the deployed bytecode.
// In other words, this variable won't change when it's checked at runtime.
original = address(this);
}
/* ===== MODIFIER ===== */
/// @dev Private method is used instead of inlining into modifier because modifiers are copied into each method,
/// and the use of immutable means the address bytes are copied in every place the modifier is used.
function checkNotDelegateCall() private view {
if (address(this) != original) revert CannotBeDelegateCalled();
}
/// @notice Prevents delegatecall into the modified method
modifier noDelegateCall() {
checkNotDelegateCall();
_;
}
}