This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
generated from huff-language/huff-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Owned.huff
95 lines (76 loc) · 2.78 KB
/
Owned.huff
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/// @title Owned
/// @notice SPDX-License-Identifier: MIT
/// @author asnared <https://github.com/abigger87>
/// @notice An single owner authorization module
/// @notice Adapted from <https://github.com/transmissions11/solmate/blob/main/src/auth/Owned.sol>
// Interface
#define function setOwner(address) nonpayable returns ()
#define function owner() view returns (address)
// Events
#define event OwnerUpdated(address indexed user, address indexed newOwner)
// Storage Slots
#define constant OWNER = FREE_STORAGE_POINTER()
// CONSTRUCTOR
#define macro OWNED_CONSTRUCTOR() = takes (0) returns (0) {
// Copy the owner into memory
// the constructor argument is at the end of code
0x20 // [size] - byte size to copy
0x20 codesize sub // [offset, size] - offset in the code to copy from
0x00 // [mem, offset, size] - offset in memory to copy to
codecopy // []
// Set the new owner
0x00 mload // [owner]
dup1 // [owner, owner]
[OWNER] // [OWNER, owner, owner]
sstore // [owner]
// Emit the owner updated event
caller // [from, owner]
__EVENT_HASH(OwnerUpdated) // [sig, from, owner]
0x00 0x00 // [0, 0, sig, from, owner]
log3 // []
}
/// @notice Only Owner Modifier
#define macro IS_OWNER() = takes (0) returns (0) {
caller // [msg.sender]
[OWNER] sload // [owner, msg.sender]
eq authed jumpi // [authed]
// Revert otherwise
0x00 0x00 revert
authed:
}
/// @notice Set the Owner
/// @param {owner} [address] - The new owner
#define macro SET_OWNER() = takes (0) returns (0) {
// Check that the caller is authorized
IS_OWNER()
// Set the new owner
0x04 calldataload // [newOwner]
dup1 // [newOwner, newOwner]
[OWNER] sstore // [newOwner]
// Emit the owner updated event
caller // [from, newOwner]
__EVENT_HASH(OwnerUpdated) // [sig, from, newOwner]
0x00 0x00 // [0, 32, sig, from, newOwner]
log3 // []
stop
}
/// @notice Get the owner of the contract
/// @return {owner} [address] - The owner of the contract
#define macro OWNER() = takes (0) returns (0) {
[OWNER] sload // [owner]
0x00 mstore // []
0x20 0x00 return
}
/// @notice Main Function Dispatcher
#define macro OWNED_MAIN() = takes (1) returns (1) {
// Input Stack: [function_selector]
dup1 __FUNC_SIG(setOwner) eq set_owner jumpi
dup1 __FUNC_SIG(owner) eq owner jumpi
// Bubble up to parent macro
no_match jump
set_owner:
SET_OWNER()
owner:
OWNER()
no_match:
}