-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestPractices.sol
More file actions
613 lines (496 loc) · 17.5 KB
/
Copy pathBestPractices.sol
File metadata and controls
613 lines (496 loc) · 17.5 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
// ============================================================================
// SOLIDITY BEST PRACTICES vs BAD PRACTICES - Complete Guide
// Senior Level Professional Development
// ============================================================================
// ============================================================================
// 1. NAMING CONVENTIONS & CODE STYLE
// ============================================================================
// BAD: Inconsistent naming, unclear purpose
contract bad_contract {
uint x;
uint Y;
address Owner;
mapping(address => uint) BALANCES;
function DoSomething(uint _a) public returns (uint) {
return _a * 2;
}
}
// GOOD: Consistent naming, clear purpose
contract TokenVault {
uint256 public totalSupply;
uint256 public lastUpdateBlock;
address public owner;
mapping(address => uint256) public balances;
function calculateReward(uint256 amount) public pure returns (uint256) {
return amount * 2;
}
}
// ============================================================================
// 2. STATE VARIABLE ORGANIZATION
// ============================================================================
// BAD: Poor organization, no grouping
contract BadOrganization {
uint256 public balance;
address private owner;
uint256 public constant MAX = 100;
mapping(address => bool) public whitelist;
uint256 private secret;
address public immutable token;
bool public paused;
constructor(address _token) {
token = _token;
owner = msg.sender;
}
}
// GOOD: Logical grouping, clear sections
contract GoodOrganization {
// Constants
uint256 public constant MAX_SUPPLY = 100;
uint256 public constant MIN_DEPOSIT = 1 ether;
// Immutables
address public immutable token;
address public immutable factory;
// State variables
address public owner;
bool public paused;
// Storage
uint256 public totalBalance;
mapping(address => bool) public whitelist;
mapping(address => uint256) public deposits;
constructor(address _token, address _factory) {
token = _token;
factory = _factory;
owner = msg.sender;
}
}
// ============================================================================
// 3. FUNCTION VISIBILITY & ORDERING
// ============================================================================
// BAD: Random order, unclear visibility
contract BadFunctionOrder {
function helper() internal pure returns (uint256) {
return 42;
}
constructor() {}
function publicFunc() public {}
receive() external payable {}
function _internalFunc() private {}
function externalFunc() external {}
}
// GOOD: Proper ordering (constructor → receive/fallback → external → public → internal → private)
contract GoodFunctionOrder {
constructor() {}
receive() external payable {}
fallback() external payable {}
// External functions
function deposit() external payable {}
function withdraw(uint256 amount) external {}
// Public functions
function getBalance() public view returns (uint256) {
return address(this).balance;
}
// Internal functions
function _updateState() internal {}
// Private functions
function _validate() private pure returns (bool) {
return true;
}
}
// ============================================================================
// 4. ERROR HANDLING - MODERN APPROACH
// ============================================================================
// BAD: String errors (expensive gas)
contract BadErrors {
function withdraw(uint256 amount) external {
require(amount > 0, "Amount must be greater than zero");
require(
amount <= address(this).balance,
"Insufficient contract balance"
);
require(msg.sender != address(0), "Invalid sender address");
}
}
// GOOD: Custom errors (cheaper gas)
contract GoodErrors {
error InvalidAmount(uint256 requested, uint256 minimum);
error InsufficientBalance(uint256 requested, uint256 available);
error ZeroAddress();
function withdraw(uint256 amount) external {
if (amount == 0) revert InvalidAmount(amount, 1);
uint256 balance = address(this).balance;
if (amount > balance) revert InsufficientBalance(amount, balance);
if (msg.sender == address(0)) revert ZeroAddress();
(bool success, ) = msg.sender.call{value: amount}("");
require(success);
}
}
// ============================================================================
// 5. EVENT DESIGN
// ============================================================================
// BAD: Missing events, poor indexing
contract BadEvents {
mapping(address => uint256) public balances;
function transfer(address to, uint256 amount) external {
balances[msg.sender] -= amount;
balances[to] += amount;
// No event emitted!
}
}
// GOOD: Comprehensive events, proper indexing
contract GoodEvents {
mapping(address => uint256) public balances;
// Index important parameters (max 3)
event Transfer(
address indexed from,
address indexed to,
uint256 amount,
uint256 timestamp
);
event BalanceUpdated(address indexed account, uint256 newBalance);
function transfer(address to, uint256 amount) external {
balances[msg.sender] -= amount;
balances[to] += amount;
emit Transfer(msg.sender, to, amount, block.timestamp);
emit BalanceUpdated(msg.sender, balances[msg.sender]);
emit BalanceUpdated(to, balances[to]);
}
}
// ============================================================================
// 6. CHECKS-EFFECTS-INTERACTIONS PATTERN
// ============================================================================
// BAD: External call before state update (reentrancy risk)
contract BadCEI {
mapping(address => uint256) public balances;
function withdraw() external {
uint256 amount = balances[msg.sender];
// External interaction BEFORE state change - DANGER!
(bool success, ) = msg.sender.call{value: amount}("");
require(success);
balances[msg.sender] = 0; // Too late!
}
}
// GOOD: Follow Checks-Effects-Interactions
contract GoodCEI {
mapping(address => uint256) public balances;
event Withdrawal(address indexed user, uint256 amount);
function withdraw() external {
// 1. CHECKS
uint256 amount = balances[msg.sender];
require(amount > 0, "No balance");
// 2. EFFECTS
balances[msg.sender] = 0;
emit Withdrawal(msg.sender, amount);
// 3. INTERACTIONS
(bool success, ) = msg.sender.call{value: amount}("");
require(success);
}
}
// ============================================================================
// 7. INPUT VALIDATION
// ============================================================================
// BAD: No validation
contract BadValidation {
address public owner;
function setOwner(address newOwner) external {
owner = newOwner; // Could be address(0)!
}
function transfer(address to, uint256 amount) external {
// No checks!
}
}
// GOOD: Comprehensive validation
contract GoodValidation {
address public owner;
error ZeroAddress();
error InvalidAmount(uint256 amount);
error Unauthorized(address caller);
modifier onlyOwner() {
if (msg.sender != owner) revert Unauthorized(msg.sender);
_;
}
function setOwner(address newOwner) external onlyOwner {
if (newOwner == address(0)) revert ZeroAddress();
owner = newOwner;
}
function transfer(address to, uint256 amount) external {
if (to == address(0)) revert ZeroAddress();
if (amount == 0) revert InvalidAmount(amount);
// Process transfer...
}
}
// ============================================================================
// 8. MODIFIERS - PROPER USE
// ============================================================================
// BAD: Complex logic in modifiers, multiple side effects
contract BadModifiers {
uint256 public counter;
modifier badModifier() {
counter++; // Side effect in modifier!
require(counter < 10, "Limit reached");
_;
counter++; // More side effects!
// Complex calculations...
}
function doSomething() external badModifier {
// Function logic
}
}
// GOOD: Simple checks only, clear purpose
contract GoodModifiers {
address public owner;
bool public paused;
error Unauthorized();
error ContractPaused();
modifier onlyOwner() {
if (msg.sender != owner) revert Unauthorized();
_;
}
modifier whenNotPaused() {
if (paused) revert ContractPaused();
_;
}
function adminFunction() external onlyOwner whenNotPaused {
// Function logic
}
}
// ============================================================================
// 9. RETURN VALUES
// ============================================================================
// BAD: Unused return values, no handling
contract BadReturns {
function transfer(address token, address to, uint256 amount) external {
// Ignoring return value is dangerous!
IERC20(token).transfer(to, amount);
}
}
// GOOD: Check return values, handle errors
contract GoodReturns {
error TransferFailed();
function transfer(address token, address to, uint256 amount) external {
bool success = IERC20(token).transfer(to, amount);
if (!success) revert TransferFailed();
}
// Or use SafeERC20
function safeTransfer(address token, address to, uint256 amount) external {
SafeERC20.safeTransfer(IERC20(token), to, amount);
}
}
// ============================================================================
// 10. TIMESTAMP USAGE
// ============================================================================
// BAD: Precise timestamp comparisons (miner manipulation)
contract BadTimestamp {
uint256 public deadline;
function checkDeadline() external view returns (bool) {
return block.timestamp == deadline; // Exact match - bad!
}
function timelock() external view {
require(block.timestamp == deadline, "Not exact time"); // Vulnerable!
}
}
// GOOD: Use ranges, not exact comparisons
contract GoodTimestamp {
uint256 public constant TIMELOCK_DURATION = 2 days;
uint256 public unlockTime;
error TooEarly(uint256 currentTime, uint256 unlockTime);
function setUnlockTime() external {
unlockTime = block.timestamp + TIMELOCK_DURATION;
}
function withdraw() external {
if (block.timestamp < unlockTime) {
revert TooEarly(block.timestamp, unlockTime);
}
// Process withdrawal
}
}
// ============================================================================
// 11. LOOPS & GAS LIMITS
// ============================================================================
// BAD: Unbounded loops (DoS risk)
contract BadLoops {
address[] public users;
function addUser(address user) external {
users.push(user);
}
function payAllUsers() external {
// Dangerous! Could run out of gas
for (uint256 i = 0; i < users.length; i++) {
payable(users[i]).transfer(1 ether);
}
}
}
// GOOD: Bounded loops or pull pattern
contract GoodLoops {
mapping(address => uint256) public pendingPayments;
address[] public users;
uint256 public constant MAX_BATCH = 50;
// Pull pattern - users withdraw themselves
function withdraw() external {
uint256 amount = pendingPayments[msg.sender];
require(amount > 0, "No payment");
pendingPayments[msg.sender] = 0;
payable(msg.sender).transfer(amount);
}
// Batch processing with limits
function processBatch(uint256 startIndex, uint256 count) external {
require(count <= MAX_BATCH, "Batch too large");
uint256 end = startIndex + count;
require(end <= users.length, "Out of bounds");
for (uint256 i = startIndex; i < end; i++) {
// Process user
}
}
}
// ============================================================================
// 12. DOCUMENTATION & NATSPEC
// ============================================================================
// BAD: No documentation
contract BadDocs {
function calc(uint256 a, uint256 b) external pure returns (uint256) {
return a * b + 100;
}
}
// GOOD: Complete NatSpec documentation
contract GoodDocs {
/// @title Reward Calculator
/// @author Your Name
/// @notice Calculates rewards with bonus
/// @dev Uses fixed point math for precision
/// @notice Calculate total reward with bonus
/// @param baseAmount The base amount to calculate from
/// @param multiplier The multiplier to apply
/// @return totalReward The calculated reward including bonus
/// @dev Reverts if multiplication overflows
function calculateReward(
uint256 baseAmount,
uint256 multiplier
) external pure returns (uint256 totalReward) {
totalReward = baseAmount * multiplier + 100;
}
}
// ============================================================================
// 13. TESTING CONSIDERATIONS
// ============================================================================
// BAD: Untestable code, tightly coupled
contract BadTestability {
function process() external {
uint256 random = uint256(
keccak256(abi.encodePacked(block.timestamp, msg.sender))
);
// Hard to test!
}
}
// GOOD: Testable, injectable dependencies
contract GoodTestability {
IOracle public oracle;
constructor(address _oracle) {
oracle = IOracle(_oracle);
}
function process() external view returns (uint256) {
return oracle.getRandomNumber(); // Easy to mock!
}
// Allow admin to update oracle for testing
function setOracle(address _oracle) external {
oracle = IOracle(_oracle);
}
}
// ============================================================================
// 14. UPGRADE SAFETY
// ============================================================================
// BAD: No upgrade path, locked forever
contract BadUpgradeability {
uint256 public value;
function setValue(uint256 _value) external {
value = _value;
}
// No way to fix bugs or add features!
}
// GOOD: Proxy pattern or planned upgrades
contract GoodUpgradeability {
address public implementation;
address public admin;
modifier onlyAdmin() {
require(msg.sender == admin);
_;
}
function upgrade(address newImplementation) external onlyAdmin {
// Add timelock in production!
implementation = newImplementation;
}
fallback() external payable {
address impl = implementation;
assembly {
calldatacopy(0, 0, calldatasize())
let result := delegatecall(gas(), impl, 0, calldatasize(), 0, 0)
returndatacopy(0, 0, returndatasize())
switch result
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
}
}
}
// ============================================================================
// 15. PROFESSIONAL CHECKLIST
// ============================================================================
/*
✅ Code Quality:
- [ ] Consistent naming (camelCase for functions, PascalCase for contracts)
- [ ] Proper variable ordering (constants → immutables → state)
- [ ] Function ordering (constructor → receive → external → public → internal → private)
- [ ] NatSpec documentation for all public functions
- [ ] Custom errors instead of string errors (0.8.4+)
✅ Security:
- [ ] Checks-Effects-Interactions pattern
- [ ] ReentrancyGuard on external calls
- [ ] Input validation (address(0), zero amounts, bounds)
- [ ] Access control (onlyOwner, roles)
- [ ] No timestamp manipulation risks
- [ ] Pull over push pattern for payments
- [ ] SafeERC20 for token transfers
✅ Gas Optimization:
- [ ] Pack storage variables (<32 bytes together)
- [ ] Use calldata instead of memory for external functions
- [ ] Cache storage variables in memory
- [ ] Short-circuit boolean expressions
- [ ] Batch operations when possible
- [ ] Use immutable for contract addresses
- [ ] Avoid loops over dynamic arrays
✅ Testing:
- [ ] Unit tests for all functions
- [ ] Edge case testing (zero, max, boundaries)
- [ ] Fuzz testing for complex logic
- [ ] Integration tests with other contracts
- [ ] Gas profiling and optimization
- [ ] Test coverage >90%
✅ Deployment:
- [ ] Upgrade strategy planned
- [ ] Pause mechanism for emergencies
- [ ] Event emission for all state changes
- [ ] Verified contract source on Etherscan
- [ ] Multi-sig for admin functions
- [ ] Timelock for critical changes
*/
// Helper interfaces
interface IERC20 {
function transfer(address to, uint256 amount) external returns (bool);
}
interface IOracle {
function getRandomNumber() external view returns (uint256);
}
library SafeERC20 {
function safeTransfer(IERC20 token, address to, uint256 amount) internal {
(bool success, bytes memory data) = address(token).call(
abi.encodeWithSelector(token.transfer.selector, to, amount)
);
require(
success && (data.length == 0 || abi.decode(data, (bool))),
"Transfer failed"
);
}
}