-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStandards.sol
More file actions
746 lines (646 loc) · 21.5 KB
/
Standards.sol
File metadata and controls
746 lines (646 loc) · 21.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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
// ============================================================================
// SOLIDITY TOKEN STANDARDS - Complete Reference
// From Zero to Professional
// ============================================================================
// ============================================================================
// 1. ERC-20 TOKEN STANDARD (FUNGIBLE TOKENS)
// ============================================================================
interface IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 amount) external returns (bool);
function allowance(
address owner,
address spender
) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(
address from,
address to,
uint256 amount
) external returns (bool);
}
contract ERC20Token is IERC20 {
string public name;
string public symbol;
uint8 public decimals;
uint256 private _totalSupply;
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
constructor(
string memory _name,
string memory _symbol,
uint8 _decimals,
uint256 initialSupply
) {
name = _name;
symbol = _symbol;
decimals = _decimals;
_totalSupply = initialSupply;
_balances[msg.sender] = initialSupply;
emit Transfer(address(0), msg.sender, initialSupply);
}
function totalSupply() external view override returns (uint256) {
return _totalSupply;
}
function balanceOf(
address account
) external view override returns (uint256) {
return _balances[account];
}
function transfer(
address to,
uint256 amount
) external override returns (bool) {
_transfer(msg.sender, to, amount);
return true;
}
function allowance(
address owner,
address spender
) external view override returns (uint256) {
return _allowances[owner][spender];
}
function approve(
address spender,
uint256 amount
) external override returns (bool) {
_approve(msg.sender, spender, amount);
return true;
}
function transferFrom(
address from,
address to,
uint256 amount
) external override returns (bool) {
uint256 currentAllowance = _allowances[from][msg.sender];
require(currentAllowance >= amount, "ERC20: insufficient allowance");
unchecked {
_approve(from, msg.sender, currentAllowance - amount);
}
_transfer(from, to, amount);
return true;
}
function _transfer(address from, address to, uint256 amount) internal {
require(from != address(0), "ERC20: transfer from zero address");
require(to != address(0), "ERC20: transfer to zero address");
require(_balances[from] >= amount, "ERC20: insufficient balance");
unchecked {
_balances[from] -= amount;
_balances[to] += amount;
}
emit Transfer(from, to, amount);
}
function _approve(address owner, address spender, uint256 amount) internal {
require(owner != address(0), "ERC20: approve from zero address");
require(spender != address(0), "ERC20: approve to zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
function mint(address to, uint256 amount) external {
require(to != address(0), "ERC20: mint to zero address");
_totalSupply += amount;
_balances[to] += amount;
emit Transfer(address(0), to, amount);
}
function burn(uint256 amount) external {
require(_balances[msg.sender] >= amount, "ERC20: insufficient balance");
unchecked {
_balances[msg.sender] -= amount;
_totalSupply -= amount;
}
emit Transfer(msg.sender, address(0), amount);
}
}
// ============================================================================
// 2. ERC-721 TOKEN STANDARD (NON-FUNGIBLE TOKENS - NFTs)
// ============================================================================
interface IERC721 {
event Transfer(
address indexed from,
address indexed to,
uint256 indexed tokenId
);
event Approval(
address indexed owner,
address indexed approved,
uint256 indexed tokenId
);
event ApprovalForAll(
address indexed owner,
address indexed operator,
bool approved
);
function balanceOf(address owner) external view returns (uint256);
function ownerOf(uint256 tokenId) external view returns (address);
function safeTransferFrom(
address from,
address to,
uint256 tokenId
) external;
function transferFrom(address from, address to, uint256 tokenId) external;
function approve(address to, uint256 tokenId) external;
function getApproved(uint256 tokenId) external view returns (address);
function setApprovalForAll(address operator, bool approved) external;
function isApprovedForAll(
address owner,
address operator
) external view returns (bool);
function safeTransferFrom(
address from,
address to,
uint256 tokenId,
bytes calldata data
) external;
}
interface IERC721Receiver {
function onERC721Received(
address operator,
address from,
uint256 tokenId,
bytes calldata data
) external returns (bytes4);
}
contract ERC721Token is IERC721 {
string public name;
string public symbol;
mapping(uint256 => address) private _owners;
mapping(address => uint256) private _balances;
mapping(uint256 => address) private _tokenApprovals;
mapping(address => mapping(address => bool)) private _operatorApprovals;
uint256 private _nextTokenId;
constructor(string memory _name, string memory _symbol) {
name = _name;
symbol = _symbol;
}
function balanceOf(address owner) external view override returns (uint256) {
require(owner != address(0), "ERC721: zero address");
return _balances[owner];
}
function ownerOf(uint256 tokenId) public view override returns (address) {
address owner = _owners[tokenId];
require(owner != address(0), "ERC721: nonexistent token");
return owner;
}
function approve(address to, uint256 tokenId) external override {
address owner = ownerOf(tokenId);
require(to != owner, "ERC721: approval to current owner");
require(
msg.sender == owner || isApprovedForAll(owner, msg.sender),
"ERC721: not authorized"
);
_tokenApprovals[tokenId] = to;
emit Approval(owner, to, tokenId);
}
function getApproved(
uint256 tokenId
) public view override returns (address) {
require(_owners[tokenId] != address(0), "ERC721: nonexistent token");
return _tokenApprovals[tokenId];
}
function setApprovalForAll(
address operator,
bool approved
) external override {
require(operator != msg.sender, "ERC721: approve to caller");
_operatorApprovals[msg.sender][operator] = approved;
emit ApprovalForAll(msg.sender, operator, approved);
}
function isApprovedForAll(
address owner,
address operator
) public view override returns (bool) {
return _operatorApprovals[owner][operator];
}
function transferFrom(
address from,
address to,
uint256 tokenId
) public override {
require(
_isApprovedOrOwner(msg.sender, tokenId),
"ERC721: not authorized"
);
_transfer(from, to, tokenId);
}
function safeTransferFrom(
address from,
address to,
uint256 tokenId
) external override {
safeTransferFrom(from, to, tokenId, "");
}
function safeTransferFrom(
address from,
address to,
uint256 tokenId,
bytes memory data
) public override {
require(
_isApprovedOrOwner(msg.sender, tokenId),
"ERC721: not authorized"
);
_safeTransfer(from, to, tokenId, data);
}
function _safeTransfer(
address from,
address to,
uint256 tokenId,
bytes memory data
) internal {
_transfer(from, to, tokenId);
require(
_checkOnERC721Received(from, to, tokenId, data),
"ERC721: transfer to non receiver"
);
}
function _transfer(address from, address to, uint256 tokenId) internal {
require(
ownerOf(tokenId) == from,
"ERC721: transfer from incorrect owner"
);
require(to != address(0), "ERC721: transfer to zero address");
delete _tokenApprovals[tokenId];
unchecked {
_balances[from] -= 1;
_balances[to] += 1;
}
_owners[tokenId] = to;
emit Transfer(from, to, tokenId);
}
function _isApprovedOrOwner(
address spender,
uint256 tokenId
) internal view returns (bool) {
address owner = ownerOf(tokenId);
return (spender == owner ||
getApproved(tokenId) == spender ||
isApprovedForAll(owner, spender));
}
function _checkOnERC721Received(
address from,
address to,
uint256 tokenId,
bytes memory data
) private returns (bool) {
if (to.code.length > 0) {
try
IERC721Receiver(to).onERC721Received(
msg.sender,
from,
tokenId,
data
)
returns (bytes4 retval) {
return retval == IERC721Receiver.onERC721Received.selector;
} catch {
return false;
}
} else {
return true;
}
}
function mint(address to) external returns (uint256) {
require(to != address(0), "ERC721: mint to zero address");
uint256 tokenId = _nextTokenId++;
_balances[to] += 1;
_owners[tokenId] = to;
emit Transfer(address(0), to, tokenId);
return tokenId;
}
function burn(uint256 tokenId) external {
address owner = ownerOf(tokenId);
require(msg.sender == owner, "ERC721: not owner");
delete _tokenApprovals[tokenId];
unchecked {
_balances[owner] -= 1;
}
delete _owners[tokenId];
emit Transfer(owner, address(0), tokenId);
}
}
// ============================================================================
// 3. ERC-1155 TOKEN STANDARD (MULTI-TOKEN)
// ============================================================================
interface IERC1155 {
event TransferSingle(
address indexed operator,
address indexed from,
address indexed to,
uint256 id,
uint256 value
);
event TransferBatch(
address indexed operator,
address indexed from,
address indexed to,
uint256[] ids,
uint256[] values
);
event ApprovalForAll(
address indexed account,
address indexed operator,
bool approved
);
event URI(string value, uint256 indexed id);
function balanceOf(
address account,
uint256 id
) external view returns (uint256);
function balanceOfBatch(
address[] calldata accounts,
uint256[] calldata ids
) external view returns (uint256[] memory);
function setApprovalForAll(address operator, bool approved) external;
function isApprovedForAll(
address account,
address operator
) external view returns (bool);
function safeTransferFrom(
address from,
address to,
uint256 id,
uint256 amount,
bytes calldata data
) external;
function safeBatchTransferFrom(
address from,
address to,
uint256[] calldata ids,
uint256[] calldata amounts,
bytes calldata data
) external;
}
interface IERC1155Receiver {
function onERC1155Received(
address operator,
address from,
uint256 id,
uint256 value,
bytes calldata data
) external returns (bytes4);
function onERC1155BatchReceived(
address operator,
address from,
uint256[] calldata ids,
uint256[] calldata values,
bytes calldata data
) external returns (bytes4);
}
contract ERC1155Token is IERC1155 {
mapping(uint256 => mapping(address => uint256)) private _balances;
mapping(address => mapping(address => bool)) private _operatorApprovals;
string public uri;
constructor(string memory _uri) {
uri = _uri;
}
function balanceOf(
address account,
uint256 id
) external view override returns (uint256) {
require(account != address(0), "ERC1155: zero address");
return _balances[id][account];
}
function balanceOfBatch(
address[] calldata accounts,
uint256[] calldata ids
) external view override returns (uint256[] memory) {
require(accounts.length == ids.length, "ERC1155: length mismatch");
uint256[] memory batchBalances = new uint256[](accounts.length);
for (uint256 i = 0; i < accounts.length; ++i) {
batchBalances[i] = _balances[ids[i]][accounts[i]];
}
return batchBalances;
}
function setApprovalForAll(
address operator,
bool approved
) external override {
require(msg.sender != operator, "ERC1155: setting approval for self");
_operatorApprovals[msg.sender][operator] = approved;
emit ApprovalForAll(msg.sender, operator, approved);
}
function isApprovedForAll(
address account,
address operator
) public view override returns (bool) {
return _operatorApprovals[account][operator];
}
function safeTransferFrom(
address from,
address to,
uint256 id,
uint256 amount,
bytes memory data
) public override {
require(
from == msg.sender || isApprovedForAll(from, msg.sender),
"ERC1155: not authorized"
);
require(to != address(0), "ERC1155: transfer to zero address");
require(_balances[id][from] >= amount, "ERC1155: insufficient balance");
unchecked {
_balances[id][from] -= amount;
}
_balances[id][to] += amount;
emit TransferSingle(msg.sender, from, to, id, amount);
_doSafeTransferAcceptanceCheck(msg.sender, from, to, id, amount, data);
}
function safeBatchTransferFrom(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) public override {
require(
from == msg.sender || isApprovedForAll(from, msg.sender),
"ERC1155: not authorized"
);
require(ids.length == amounts.length, "ERC1155: length mismatch");
require(to != address(0), "ERC1155: transfer to zero address");
for (uint256 i = 0; i < ids.length; ++i) {
uint256 id = ids[i];
uint256 amount = amounts[i];
require(
_balances[id][from] >= amount,
"ERC1155: insufficient balance"
);
unchecked {
_balances[id][from] -= amount;
}
_balances[id][to] += amount;
}
emit TransferBatch(msg.sender, from, to, ids, amounts);
_doSafeBatchTransferAcceptanceCheck(
msg.sender,
from,
to,
ids,
amounts,
data
);
}
function _doSafeTransferAcceptanceCheck(
address operator,
address from,
address to,
uint256 id,
uint256 amount,
bytes memory data
) private {
if (to.code.length > 0) {
try
IERC1155Receiver(to).onERC1155Received(
operator,
from,
id,
amount,
data
)
returns (bytes4 response) {
if (response != IERC1155Receiver.onERC1155Received.selector) {
revert("ERC1155: rejected");
}
} catch {
revert("ERC1155: transfer to non receiver");
}
}
}
function _doSafeBatchTransferAcceptanceCheck(
address operator,
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) private {
if (to.code.length > 0) {
try
IERC1155Receiver(to).onERC1155BatchReceived(
operator,
from,
ids,
amounts,
data
)
returns (bytes4 response) {
if (
response != IERC1155Receiver.onERC1155BatchReceived.selector
) {
revert("ERC1155: rejected");
}
} catch {
revert("ERC1155: transfer to non receiver");
}
}
}
function mint(
address to,
uint256 id,
uint256 amount,
bytes memory data
) external {
require(to != address(0), "ERC1155: mint to zero address");
_balances[id][to] += amount;
emit TransferSingle(msg.sender, address(0), to, id, amount);
_doSafeTransferAcceptanceCheck(
msg.sender,
address(0),
to,
id,
amount,
data
);
}
function mintBatch(
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) external {
require(to != address(0), "ERC1155: mint to zero address");
require(ids.length == amounts.length, "ERC1155: length mismatch");
for (uint256 i = 0; i < ids.length; i++) {
_balances[ids[i]][to] += amounts[i];
}
emit TransferBatch(msg.sender, address(0), to, ids, amounts);
_doSafeBatchTransferAcceptanceCheck(
msg.sender,
address(0),
to,
ids,
amounts,
data
);
}
}
// ============================================================================
// 4. ERC-2981 ROYALTY STANDARD (NFT ROYALTIES)
// ============================================================================
interface IERC2981 {
function royaltyInfo(
uint256 tokenId,
uint256 salePrice
) external view returns (address receiver, uint256 royaltyAmount);
}
contract ERC2981Royalty is IERC2981 {
struct RoyaltyInfo {
address receiver;
uint96 royaltyFraction; // Basis points (1% = 100)
}
RoyaltyInfo private _defaultRoyalty;
mapping(uint256 => RoyaltyInfo) private _tokenRoyalty;
uint96 private constant _DENOMINATOR = 10000; // 100% = 10000
function royaltyInfo(
uint256 tokenId,
uint256 salePrice
) external view override returns (address, uint256) {
RoyaltyInfo memory royalty = _tokenRoyalty[tokenId];
if (royalty.receiver == address(0)) {
royalty = _defaultRoyalty;
}
uint256 royaltyAmount = (salePrice * royalty.royaltyFraction) /
_DENOMINATOR;
return (royalty.receiver, royaltyAmount);
}
function setDefaultRoyalty(address receiver, uint96 feeNumerator) external {
require(feeNumerator <= _DENOMINATOR, "ERC2981: royalty too high");
_defaultRoyalty = RoyaltyInfo(receiver, feeNumerator);
}
function setTokenRoyalty(
uint256 tokenId,
address receiver,
uint96 feeNumerator
) external {
require(feeNumerator <= _DENOMINATOR, "ERC2981: royalty too high");
_tokenRoyalty[tokenId] = RoyaltyInfo(receiver, feeNumerator);
}
}
// ============================================================================
// KEY STANDARD TAKEAWAYS FOR PROFESSIONAL DEVELOPERS:
// ============================================================================
// 1. ERC-20: Fungible tokens, use for currencies/utility tokens
// 2. ERC-721: Non-fungible tokens (NFTs), unique items
// 3. ERC-1155: Multi-token standard, efficient for gaming
// 4. ERC-2981: Royalty standard for NFT marketplaces
// 5. Always implement safe transfer callbacks for security
// 6. Use unchecked for gas optimization where safe
// 7. Consider ERC-20 approve/transferFrom attack vectors
// 8. ERC-721 requires receiver check for contract transfers
// 9. ERC-1155 saves gas for batch operations
// 10. Standards ensure interoperability with wallets/exchanges
// 11. Test thoroughly with OpenZeppelin implementations
// 12. Follow EIP specifications exactly for compatibility
// 13. Events are crucial for indexing and UI updates
// 14. Consider extensions: burnable, pausable, snapshots
// 15. Audit carefully - token bugs are very costly
// ============================================================================