22
33Constants in Huff contracts are not included in the contract's storage; Instead,
44they are able to be called within the contract at compile time. Constants
5- can be bytes (32 max), ` FREE_STORAGE_POINTER ` , a built-in function, or an arithmetic expression.
5+ can be bytes (32 max), string literals, ` FREE_STORAGE_POINTER ` , a built-in function, or an arithmetic expression.
66A ` FREE_STORAGE_POINTER ` constant will always represent an unused storage slot in the contract.
77
88In order to push a constant to the stack, use bracket notation: ` [CONSTANT] `
@@ -13,6 +13,7 @@ In order to push a constant to the stack, use bracket notation: `[CONSTANT]`
1313``` javascript
1414#define constant NUM = 0x420
1515#define constant HELLO_WORLD = 0x48656c6c6f2c20576f726c6421
16+ #define constant GREETING = " hello"
1617#define constant FREE_STORAGE = FREE_STORAGE_POINTER ()
1718#define constant TEST = __FUNC_SIG (" test(uint256)" )
1819```
@@ -131,4 +132,69 @@ Expressions are evaluated during compilation. The compiler replaces the expressi
131132#define macro EXAMPLE () = takes (0 ) returns (0 ) {
132133 [C ] // Compiles to: PUSH1 0x15
133134}
135+ ```
136+
137+ ## String Literals
138+
139+ String literal constants can be defined using double quotes. String constants cannot be pushed to the stack directly - they must be converted to bytes using the ` __BYTES ` builtin function.
140+
141+ ### String Constant Declaration
142+
143+ ``` javascript
144+ #define constant GREETING = " hello"
145+ #define constant MESSAGE = " transfer(address,uint256)"
146+ ```
147+
148+ ### String Constant Usage
149+
150+ String constants must be used with the ` __BYTES ` builtin function, which converts the string to UTF-8 encoded bytes:
151+
152+ ``` javascript
153+ #define constant GREETING = " hello"
154+
155+ #define macro MAIN () = takes (0 ) returns (0 ) {
156+ __BYTES ([GREETING ]) // Compiles to: PUSH5 0x68656c6c6f
157+ }
158+ ```
159+
160+ ** Direct usage is not allowed:**
161+ ``` javascript
162+ // ❌ ERROR: String constants cannot be pushed directly
163+ #define constant MSG = " hello"
164+
165+ #define macro EXAMPLE () = takes (0 ) returns (0 ) {
166+ [MSG ] // Compilation error: use __BYTES([MSG]) instead
167+ }
168+ ```
169+
170+ ### Use Cases
171+
172+ ** Function Signatures:**
173+ ``` javascript
174+ #define constant TRANSFER_SIG = " transfer(address,uint256)"
175+
176+ #define macro GET_SELECTOR () = takes (0 ) returns (1 ) {
177+ __BYTES ([TRANSFER_SIG ]) // Push signature as bytes
178+ }
179+ ```
180+
181+ ** Error Messages (limited to 32 bytes):**
182+ ``` javascript
183+ #define constant ERROR_MSG = " Insufficient balance"
184+
185+ #define macro REVERT_WITH_MSG () = takes (0 ) returns (0 ) {
186+ __BYTES ([ERROR_MSG ])
187+ 0x00 mstore
188+ 0x20 0x00 revert
189+ }
190+ ```
191+
192+ ** Combining with Other Builtins:**
193+ ``` javascript
194+ #define constant GREETING = " hi"
195+
196+ #define macro EXAMPLE () = takes (0 ) returns (0 ) {
197+ // String bytes can be padded
198+ __RIGHTPAD (__BYTES ([GREETING ])) // Right-pad the UTF-8 bytes
199+ }
134200```
0 commit comments