-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHolographERC721.sol
executable file
·906 lines (828 loc) · 31.9 KB
/
HolographERC721.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
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
/*HOLOGRAPH_LICENSE_HEADER*/
/*SOLIDITY_COMPILER_VERSION*/
import "../abstract/Admin.sol";
import "../abstract/Initializable.sol";
import "../abstract/Owner.sol";
import "../enum/HolographERC721Event.sol";
import "../enum/InterfaceType.sol";
import "../interface/ERC165.sol";
import "../interface/ERC721.sol";
import "../interface/HolographERC721Interface.sol";
import "../interface/ERC721Metadata.sol";
import "../interface/ERC721TokenReceiver.sol";
import "../interface/Holographable.sol";
import "../interface/HolographedERC721.sol";
import "../interface/HolographInterface.sol";
import "../interface/HolographerInterface.sol";
import "../interface/HolographRegistryInterface.sol";
import "../interface/InitializableInterface.sol";
import "../interface/HolographInterfacesInterface.sol";
import "../interface/PA1DInterface.sol";
import "../interface/Ownable.sol";
/**
* @title Holograph Bridgeable ERC-721 Collection
* @author CXIP-Labs
* @notice A smart contract for minting and managing Holograph Bridgeable ERC721 NFTs.
* @dev The entire logic and functionality of the smart contract is self-contained.
*/
contract HolographERC721 is Admin, Owner, HolographERC721Interface, Initializable {
/**
* @dev bytes32(uint256(keccak256('eip1967.Holograph.holograph')) - 1)
*/
bytes32 constant _holographSlot = precomputeslot("eip1967.Holograph.holograph");
/**
* @dev bytes32(uint256(keccak256('eip1967.Holograph.sourceContract')) - 1)
*/
bytes32 constant _sourceContractSlot = precomputeslot("eip1967.Holograph.sourceContract");
/**
* @dev Configuration for events to trigger for source smart contract.
*/
uint256 private _eventConfig;
/**
* @dev Collection name.
*/
string private _name;
/**
* @dev Collection symbol.
*/
string private _symbol;
/**
* @dev Collection royalty base points.
*/
uint16 private _bps;
/**
* @dev Array of all token ids in collection.
*/
uint256[] private _allTokens;
/**
* @dev Map of token id to array index of _ownedTokens.
*/
mapping(uint256 => uint256) private _ownedTokensIndex;
/**
* @dev Token id to wallet (owner) address map.
*/
mapping(uint256 => address) private _tokenOwner;
/**
* @dev 1-to-1 map of token id that was assigned an approved operator address.
*/
mapping(uint256 => address) private _tokenApprovals;
/**
* @dev Map of total tokens owner by a specific address.
*/
mapping(address => uint256) private _ownedTokensCount;
/**
* @dev Map of array of token ids owned by a specific address.
*/
mapping(address => uint256[]) private _ownedTokens;
/**
* @notice Map of full operator approval for a particular address.
* @dev Usually utilised for supporting marketplace proxy wallets.
*/
mapping(address => mapping(address => bool)) private _operatorApprovals;
/**
* @dev Mapping from token id to position in the allTokens array.
*/
mapping(uint256 => uint256) private _allTokensIndex;
/**
* @dev Mapping of all token ids that have been burned. This is to prevent re-minting of same token ids.
*/
mapping(uint256 => bool) private _burnedTokens;
/**
* @notice Only allow calls from bridge smart contract.
*/
modifier onlyBridge() {
require(msg.sender == _holograph().getBridge(), "ERC721: bridge only call");
_;
}
/**
* @notice Only allow calls from source smart contract.
*/
modifier onlySource() {
address sourceContract;
assembly {
sourceContract := sload(_sourceContractSlot)
}
require(msg.sender == sourceContract, "ERC721: source only call");
_;
}
/**
* @dev Constructor is left empty and init is used instead
*/
constructor() {}
/**
* @notice Used internally to initialize the contract instead of through a constructor
* @dev This function is called by the deployer/factory when creating a contract
* @param initPayload abi encoded payload to use for contract initilaization
*/
function init(bytes memory initPayload) external override returns (bytes4) {
require(!_isInitialized(), "ERC721: already initialized");
InitializableInterface sourceContract;
assembly {
sstore(_ownerSlot, caller())
sourceContract := sload(_sourceContractSlot)
}
(
string memory contractName,
string memory contractSymbol,
uint16 contractBps,
uint256 eventConfig,
bool skipInit,
bytes memory initCode
) = abi.decode(initPayload, (string, string, uint16, uint256, bool, bytes));
_name = contractName;
_symbol = contractSymbol;
_bps = contractBps;
_eventConfig = eventConfig;
if (!skipInit) {
require(sourceContract.init(initCode) == InitializableInterface.init.selector, "ERC721: could not init source");
(bool success, bytes memory returnData) = _royalties().delegatecall(
abi.encodeWithSignature("initPA1D(bytes)", abi.encode(address(this), uint256(contractBps)))
);
bytes4 selector = abi.decode(returnData, (bytes4));
require(success && selector == InitializableInterface.init.selector, "ERC721: coud not init PA1D");
}
_setInitialized();
return InitializableInterface.init.selector;
}
/**
* @notice Gets a base64 encoded contract JSON file.
* @return string The URI.
*/
function contractURI() external view returns (string memory) {
return HolographInterfacesInterface(_interfaces()).contractURI(_name, "", "", _bps, address(this));
}
/**
* @notice Gets the name of the collection.
* @return string The collection name.
*/
function name() external view returns (string memory) {
return _name;
}
/**
* @notice Shows the interfaces the contracts support
* @dev Must add new 4 byte interface Ids here to acknowledge support
* @param interfaceId ERC165 style 4 byte interfaceId.
* @return bool True if supported.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool) {
HolographInterfacesInterface interfaces = HolographInterfacesInterface(_interfaces());
ERC165 erc165Contract;
assembly {
erc165Contract := sload(_sourceContractSlot)
}
if (
interfaces.supportsInterface(InterfaceType.ERC721, interfaceId) || // check global interfaces
interfaces.supportsInterface(InterfaceType.PA1D, interfaceId) || // check if royalties supports interface
erc165Contract.supportsInterface(interfaceId) // check if source supports interface
) {
return true;
} else {
return false;
}
}
/**
* @notice Gets the collection's symbol.
* @return string The symbol.
*/
function symbol() external view returns (string memory) {
return _symbol;
}
/**
* @notice Get's the URI of the token.
* @dev Defaults the the Arweave URI
* @return string The URI.
*/
function tokenURI(uint256 tokenId) external view returns (string memory) {
require(_exists(tokenId), "ERC721: token does not exist");
ERC721Metadata sourceContract;
assembly {
sourceContract := sload(_sourceContractSlot)
}
return sourceContract.tokenURI(tokenId);
}
/**
* @notice Get list of tokens owned by wallet.
* @param wallet The wallet address to get tokens for.
* @return uint256[] Returns an array of token ids owned by wallet.
*/
function tokensOfOwner(address wallet) external view returns (uint256[] memory) {
return _ownedTokens[wallet];
}
/**
* @notice Get set length list, starting from index, for tokens owned by wallet.
* @param wallet The wallet address to get tokens for.
* @param index The index to start enumeration from.
* @param length The length of returned results.
* @return tokenIds uint256[] Returns a set length array of token ids owned by wallet.
*/
function tokensOfOwner(
address wallet,
uint256 index,
uint256 length
) external view returns (uint256[] memory tokenIds) {
uint256 supply = _ownedTokensCount[wallet];
if (index + length > supply) {
length = supply - index;
}
tokenIds = new uint256[](length);
for (uint256 i = 0; i < length; i++) {
tokenIds[i] = _ownedTokens[wallet][index + i];
}
}
/**
* @notice Adds a new address to the token's approval list.
* @dev Requires the sender to be in the approved addresses.
* @param to The address to approve.
* @param tokenId The affected token.
*/
function approve(address to, uint256 tokenId) external payable {
address tokenOwner = _tokenOwner[tokenId];
require(to != tokenOwner, "ERC721: cannot approve self");
require(_isApproved(msg.sender, tokenId), "ERC721: not approved sender");
if (_isEventRegistered(HolographERC721Event.beforeApprove)) {
require(SourceERC721().beforeApprove(tokenOwner, to, tokenId));
}
_tokenApprovals[tokenId] = to;
emit Approval(tokenOwner, to, tokenId);
if (_isEventRegistered(HolographERC721Event.afterApprove)) {
require(SourceERC721().afterApprove(tokenOwner, to, tokenId));
}
}
/**
* @notice Burns the token.
* @dev The sender must be the owner or approved.
* @param tokenId The token to burn.
*/
function burn(uint256 tokenId) external {
require(_isApproved(msg.sender, tokenId), "ERC721: not approved sender");
address wallet = _tokenOwner[tokenId];
if (_isEventRegistered(HolographERC721Event.beforeBurn)) {
require(SourceERC721().beforeBurn(wallet, tokenId));
}
_burn(wallet, tokenId);
if (_isEventRegistered(HolographERC721Event.afterBurn)) {
require(SourceERC721().afterBurn(wallet, tokenId));
}
}
function bridgeIn(uint32 fromChain, bytes calldata payload) external onlyBridge returns (bytes4) {
(address from, address to, uint256 tokenId, bytes memory data) = abi.decode(
payload,
(address, address, uint256, bytes)
);
require(!_exists(tokenId), "ERC721: token already exists");
delete _burnedTokens[tokenId];
_mint(to, tokenId);
if (_isEventRegistered(HolographERC721Event.bridgeIn)) {
require(SourceERC721().bridgeIn(fromChain, from, to, tokenId, data), "HOLOGRAPH: bridge in failed");
}
return Holographable.bridgeIn.selector;
}
function bridgeOut(
uint32 toChain,
address sender,
bytes calldata payload
) external onlyBridge returns (bytes4 selector, bytes memory data) {
(address from, address to, uint256 tokenId) = abi.decode(payload, (address, address, uint256));
require(to != address(0), "ERC721: zero address");
require(_isApproved(sender, tokenId), "ERC721: sender not approved");
require(from == _tokenOwner[tokenId], "ERC721: from is not owner");
if (_isEventRegistered(HolographERC721Event.bridgeOut)) {
data = SourceERC721().bridgeOut(toChain, from, to, tokenId);
}
_burn(from, tokenId);
return (Holographable.bridgeOut.selector, abi.encode(from, to, tokenId, data));
}
/**
* @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
* are aware of the ERC721 protocol to prevent tokens from being forever locked.
* @param from cannot be the zero address.
* @param to cannot be the zero address.
* @param tokenId token must exist and be owned by `from`.
*/
function safeTransferFrom(
address from,
address to,
uint256 tokenId
) external payable {
safeTransferFrom(from, to, tokenId, "");
}
/**
* @notice Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
* @dev Since it's not being used, the _data variable is commented out to avoid compiler warnings.
* are aware of the ERC721 protocol to prevent tokens from being forever locked.
* @param from cannot be the zero address.
* @param to cannot be the zero address.
* @param tokenId token must exist and be owned by `from`.
*/
function safeTransferFrom(
address from,
address to,
uint256 tokenId,
bytes memory data
) public payable {
require(_isApproved(msg.sender, tokenId), "ERC721: not approved sender");
if (_isEventRegistered(HolographERC721Event.beforeSafeTransfer)) {
require(SourceERC721().beforeSafeTransfer(from, to, tokenId, data));
}
_transferFrom(from, to, tokenId);
if (_isContract(to)) {
require(
(ERC165(to).supportsInterface(ERC165.supportsInterface.selector) &&
ERC165(to).supportsInterface(ERC721TokenReceiver.onERC721Received.selector) &&
ERC721TokenReceiver(to).onERC721Received(address(this), from, tokenId, data) ==
ERC721TokenReceiver.onERC721Received.selector),
"ERC721: onERC721Received fail"
);
}
if (_isEventRegistered(HolographERC721Event.afterSafeTransfer)) {
require(SourceERC721().afterSafeTransfer(from, to, tokenId, data));
}
}
/**
* @notice Adds a new approved operator.
* @dev Allows platforms to sell/transfer all your NFTs. Used with proxy contracts like OpenSea/Rarible.
* @param to The address to approve.
* @param approved Turn on or off approval status.
*/
function setApprovalForAll(address to, bool approved) external {
require(to != msg.sender, "ERC721: cannot approve self");
if (_isEventRegistered(HolographERC721Event.beforeApprovalAll)) {
require(SourceERC721().beforeApprovalAll(to, approved));
}
_operatorApprovals[msg.sender][to] = approved;
emit ApprovalForAll(msg.sender, to, approved);
if (_isEventRegistered(HolographERC721Event.afterApprovalAll)) {
require(SourceERC721().afterApprovalAll(to, approved));
}
}
/**
* @dev Allows for source smart contract to burn a token.
* Note: this is put in place to make sure that custom logic could be implemented for merging, gamification, etc.
* Note: token cannot be burned if it's locked by bridge.
*/
function sourceBurn(uint256 tokenId) external onlySource {
address wallet = _tokenOwner[tokenId];
_burn(wallet, tokenId);
}
/**
* @dev Allows for source smart contract to mint a token.
*/
function sourceMint(address to, uint224 tokenId) external onlySource {
// uint32 is reserved for chain id to be used
// we need to get current chain id, and prepend it to tokenId
// this will prevent possible tokenId overlap if minting simultaneously on multiple chains is possible
uint256 token = uint256(bytes32(abi.encodePacked(_chain(), tokenId)));
require(!_burnedTokens[token], "ERC721: can't mint burned token");
_mint(to, token);
}
/**
* @dev Allows source to get the prepend for their tokenIds.
*/
function sourceGetChainPrepend() external view onlySource returns (uint256) {
return uint256(bytes32(abi.encodePacked(_chain(), uint224(0))));
}
/**
* @dev Allows for source smart contract to mint a batch of tokens.
*/
// function sourceMintBatch(address to, uint224[] calldata tokenIds) external onlySource {
// require(tokenIds.length < 1000, "ERC721: max batch size is 1000");
// uint32 chain = _chain();
// uint256 token;
// for (uint256 i = 0; i < tokenIds.length; i++) {
// require(!_burnedTokens[token], "ERC721: can't mint burned token");
// token = uint256(bytes32(abi.encodePacked(chain, tokenIds[i])));
// require(!_burnedTokens[token], "ERC721: can't mint burned token");
// _mint(to, token);
// }
// }
/**
* @dev Allows for source smart contract to mint a batch of tokens.
*/
// function sourceMintBatch(address[] calldata wallets, uint224[] calldata tokenIds) external onlySource {
// require(wallets.length == tokenIds.length, "ERC721: array length missmatch");
// require(tokenIds.length < 1000, "ERC721: max batch size is 1000");
// uint32 chain = _chain();
// uint256 token;
// for (uint256 i = 0; i < tokenIds.length; i++) {
// token = uint256(bytes32(abi.encodePacked(chain, tokenIds[i])));
// require(!_burnedTokens[token], "ERC721: can't mint burned token");
// _mint(wallets[i], token);
// }
// }
/**
* @dev Allows for source smart contract to mint a batch of tokens.
*/
// function sourceMintBatchIncremental(
// address to,
// uint224 startingTokenId,
// uint256 length
// ) external onlySource {
// uint32 chain = _chain();
// uint256 token;
// for (uint256 i = 0; i < length; i++) {
// token = uint256(bytes32(abi.encodePacked(chain, startingTokenId)));
// require(!_burnedTokens[token], "ERC721: can't mint burned token");
// _mint(to, token);
// startingTokenId++;
// }
// }
/**
* @dev Allows for source smart contract to transfer a token.
* Note: this is put in place to make sure that custom logic could be implemented for merging, gamification, etc.
* Note: token cannot be transfered if it's locked by bridge.
*/
function sourceTransfer(address to, uint256 tokenId) external onlySource {
address wallet = _tokenOwner[tokenId];
_transferFrom(wallet, to, tokenId);
}
/**
* @notice Transfers `tokenId` token from `msg.sender` to `to`.
* @dev WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
* @param to cannot be the zero address.
* @param tokenId token must be owned by `from`.
*/
function transfer(address to, uint256 tokenId) external payable {
transferFrom(msg.sender, to, tokenId, "");
}
/**
* @notice Transfers `tokenId` token from `from` to `to`.
* @dev WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
* @param from cannot be the zero address.
* @param to cannot be the zero address.
* @param tokenId token must be owned by `from`.
*/
function transferFrom(
address from,
address to,
uint256 tokenId
) public payable {
transferFrom(from, to, tokenId, "");
}
/**
* @notice Transfers `tokenId` token from `from` to `to`.
* @dev WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
* @dev Since it's not being used, the _data variable is commented out to avoid compiler warnings.
* @param from cannot be the zero address.
* @param to cannot be the zero address.
* @param tokenId token must be owned by `from`.
* @param data additional data to pass.
*/
function transferFrom(
address from,
address to,
uint256 tokenId,
bytes memory data
) public payable {
require(_isApproved(msg.sender, tokenId), "ERC721: not approved sender");
if (_isEventRegistered(HolographERC721Event.beforeTransfer)) {
require(SourceERC721().beforeTransfer(from, to, tokenId, data));
}
_transferFrom(from, to, tokenId);
if (_isEventRegistered(HolographERC721Event.afterTransfer)) {
require(SourceERC721().afterTransfer(from, to, tokenId, data));
}
}
/**
* @notice Get total number of tokens owned by wallet.
* @dev Used to see total amount of tokens owned by a specific wallet.
* @param wallet Address for which to get token balance.
* @return uint256 Returns an integer, representing total amount of tokens held by address.
*/
function balanceOf(address wallet) public view returns (uint256) {
require(wallet != address(0), "ERC721: zero address");
return _ownedTokensCount[wallet];
}
function burned(uint256 tokenId) public view returns (bool) {
return _burnedTokens[tokenId];
}
/**
* @notice Decimal places to have for totalSupply.
* @dev Since ERC721s are single, we use 0 as the decimal places to make sure a round number for totalSupply.
* @return uint256 Returns the number of decimal places to have for totalSupply.
*/
function decimals() external pure returns (uint256) {
return 0;
}
function exists(uint256 tokenId) public view returns (bool) {
return _tokenOwner[tokenId] != address(0);
}
/**
* @notice Gets the approved address for the token.
* @dev Single operator set for a specific token. Usually used for one-time very specific authorisations.
* @param tokenId Token id to get approved operator for.
* @return address Approved address for token.
*/
function getApproved(uint256 tokenId) external view returns (address) {
return _tokenApprovals[tokenId];
}
/**
* @notice Checks if the address is approved.
* @dev Includes references to OpenSea and Rarible marketplace proxies.
* @param wallet Address of the wallet.
* @param operator Address of the marketplace operator.
* @return bool True if approved.
*/
function isApprovedForAll(address wallet, address operator) external view returns (bool) {
return _operatorApprovals[wallet][operator];
}
/**
* @notice Checks who the owner of a token is.
* @dev The token must exist.
* @param tokenId The token to look up.
* @return address Owner of the token.
*/
function ownerOf(uint256 tokenId) external view returns (address) {
address tokenOwner = _tokenOwner[tokenId];
require(tokenOwner != address(0), "ERC721: token does not exist");
return tokenOwner;
}
/**
* @notice Get token by index.
* @dev Used in conjunction with totalSupply function to iterate over all tokens in collection.
* @param index Index of token in array.
* @return uint256 Returns the token id of token located at that index.
*/
function tokenByIndex(uint256 index) external view returns (uint256) {
require(index < _allTokens.length, "ERC721: index out of bounds");
return _allTokens[index];
}
/**
* @notice Get set length list, starting from index, for all tokens.
* @param index The index to start enumeration from.
* @param length The length of returned results.
* @return tokenIds uint256[] Returns a set length array of token ids minted.
*/
function tokens(uint256 index, uint256 length) external view returns (uint256[] memory tokenIds) {
uint256 supply = _allTokens.length;
if (index + length > supply) {
length = supply - index;
}
tokenIds = new uint256[](length);
for (uint256 i = 0; i < length; i++) {
tokenIds[i] = _allTokens[index + i];
}
}
/**
* @notice Get token from wallet by index instead of token id.
* @dev Helpful for wallet token enumeration where token id info is not yet available. Use in conjunction with balanceOf function.
* @param wallet Specific address for which to get token for.
* @param index Index of token in array.
* @return uint256 Returns the token id of token located at that index in specified wallet.
*/
function tokenOfOwnerByIndex(address wallet, uint256 index) external view returns (uint256) {
require(index < balanceOf(wallet), "ERC721: index out of bounds");
return _ownedTokens[wallet][index];
}
/**
* @notice Total amount of tokens in the collection.
* @dev Ignores burned tokens.
* @return uint256 Returns the total number of active (not burned) tokens.
*/
function totalSupply() external view returns (uint256) {
return _allTokens.length;
}
/**
* @notice Empty function that is triggered by external contract on NFT transfer.
* @dev We have this blank function in place to make sure that external contract sending in NFTs don't error out.
* @dev Since it's not being used, the _operator variable is commented out to avoid compiler warnings.
* @dev Since it's not being used, the _from variable is commented out to avoid compiler warnings.
* @dev Since it's not being used, the _tokenId variable is commented out to avoid compiler warnings.
* @dev Since it's not being used, the _data variable is commented out to avoid compiler warnings.
* @return bytes4 Returns the interfaceId of onERC721Received.
*/
function onERC721Received(
address _operator,
address _from,
uint256 _tokenId,
bytes calldata _data
) external returns (bytes4) {
require(_isContract(_operator), "ERC721: operator not contract");
if (_isEventRegistered(HolographERC721Event.beforeOnERC721Received)) {
require(SourceERC721().beforeOnERC721Received(_operator, _from, address(this), _tokenId, _data));
}
try HolographERC721Interface(_operator).ownerOf(_tokenId) returns (address tokenOwner) {
require(tokenOwner == address(this), "ERC721: contract not token owner");
} catch {
revert("ERC721: token does not exist");
}
if (_isEventRegistered(HolographERC721Event.afterOnERC721Received)) {
require(SourceERC721().afterOnERC721Received(_operator, _from, address(this), _tokenId, _data));
}
return ERC721TokenReceiver.onERC721Received.selector;
}
/**
* @dev Add a newly minted token into managed list of tokens.
* @param to Address of token owner for which to add the token.
* @param tokenId Id of token to add.
*/
function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private {
_ownedTokensIndex[tokenId] = _ownedTokensCount[to];
_ownedTokensCount[to]++;
_ownedTokens[to].push(tokenId);
_allTokensIndex[tokenId] = _allTokens.length;
_allTokens.push(tokenId);
}
/**
* @notice Burns the token.
* @dev All validation needs to be done before calling this function.
* @param wallet Address of current token owner.
* @param tokenId The token to burn.
*/
function _burn(address wallet, uint256 tokenId) private {
_clearApproval(tokenId);
_tokenOwner[tokenId] = address(0);
emit Transfer(wallet, address(0), tokenId);
_removeTokenFromOwnerEnumeration(wallet, tokenId);
_burnedTokens[tokenId] = true;
}
/**
* @notice Deletes a token from the approval list.
* @dev Removes from count.
* @param tokenId T.
*/
function _clearApproval(uint256 tokenId) private {
delete _tokenApprovals[tokenId];
}
/**
* @notice Mints an NFT.
* @dev Can to mint the token to the zero address and the token cannot already exist.
* @param to Address to mint to.
* @param tokenId The new token.
*/
function _mint(address to, uint256 tokenId) private {
require(tokenId > 0, "ERC721: token id cannot be zero");
require(to != address(0), "ERC721: minting to burn address");
require(!_exists(tokenId), "ERC721: token already exists");
require(!_burnedTokens[tokenId], "ERC721: token has been burned");
_tokenOwner[tokenId] = to;
emit Transfer(address(0), to, tokenId);
_addTokenToOwnerEnumeration(to, tokenId);
}
function _removeTokenFromAllTokensEnumeration(uint256 tokenId) private {
uint256 lastTokenIndex = _allTokens.length - 1;
uint256 tokenIndex = _allTokensIndex[tokenId];
uint256 lastTokenId = _allTokens[lastTokenIndex];
_allTokens[tokenIndex] = lastTokenId;
_allTokensIndex[lastTokenId] = tokenIndex;
delete _allTokensIndex[tokenId];
delete _allTokens[lastTokenIndex];
_allTokens.pop();
}
/**
* @dev Remove a token from managed list of tokens.
* @param from Address of token owner for which to remove the token.
* @param tokenId Id of token to remove.
*/
function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId) private {
_removeTokenFromAllTokensEnumeration(tokenId);
_ownedTokensCount[from]--;
uint256 lastTokenIndex = _ownedTokensCount[from];
uint256 tokenIndex = _ownedTokensIndex[tokenId];
if (tokenIndex != lastTokenIndex) {
uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];
_ownedTokens[from][tokenIndex] = lastTokenId;
_ownedTokensIndex[lastTokenId] = tokenIndex;
}
if (lastTokenIndex == 0) {
delete _ownedTokens[from];
} else {
delete _ownedTokens[from][lastTokenIndex];
_ownedTokens[from].pop();
}
}
/**
* @dev Primary private function that handles the transfer/mint/burn functionality.
* @param from Address from where token is being transferred. Zero address means it is being minted.
* @param to Address to whom the token is being transferred. Zero address means it is being burned.
* @param tokenId Id of token that is being transferred/minted/burned.
*/
function _transferFrom(
address from,
address to,
uint256 tokenId
) private {
require(_tokenOwner[tokenId] == from, "ERC721: token not owned");
require(to != address(0), "ERC721: use burn instead");
_clearApproval(tokenId);
_tokenOwner[tokenId] = to;
emit Transfer(from, to, tokenId);
_removeTokenFromOwnerEnumeration(from, tokenId);
_addTokenToOwnerEnumeration(to, tokenId);
}
function _chain() private view returns (uint32) {
uint32 currentChain = HolographInterface(HolographerInterface(payable(address(this))).getHolograph())
.getHolographChainId();
if (currentChain != HolographerInterface(payable(address(this))).getOriginChain()) {
return currentChain;
}
return uint32(0);
}
/**
* @notice Checks if the token owner exists.
* @dev If the address is the zero address no owner exists.
* @param tokenId The affected token.
* @return bool True if it exists.
*/
function _exists(uint256 tokenId) private view returns (bool) {
address tokenOwner = _tokenOwner[tokenId];
return tokenOwner != address(0);
}
/**
* @notice Checks if the address is an approved one.
* @dev Uses inlined checks for different usecases of approval.
* @param spender Address of the spender.
* @param tokenId The affected token.
* @return bool True if approved.
*/
function _isApproved(address spender, uint256 tokenId) private view returns (bool) {
require(_exists(tokenId), "ERC721: token does not exist");
address tokenOwner = _tokenOwner[tokenId];
return (spender == tokenOwner || _tokenApprovals[tokenId] == spender || _operatorApprovals[tokenOwner][spender]);
}
function _isContract(address contractAddress) private view returns (bool) {
bytes32 codehash;
assembly {
codehash := extcodehash(contractAddress)
}
return (codehash != 0x0 && codehash != precomputekeccak256(""));
}
/**
* @dev Get the source smart contract as bridgeable interface.
*/
function SourceERC721() private view returns (HolographedERC721 sourceContract) {
assembly {
sourceContract := sload(_sourceContractSlot)
}
}
/**
* @dev Get the interfaces contract address.
*/
function _interfaces() private view returns (address) {
return _holograph().getInterfaces();
}
function owner() public view override returns (address) {
Ownable ownableContract;
assembly {
ownableContract := sload(_sourceContractSlot)
}
return ownableContract.owner();
}
function _holograph() private view returns (HolographInterface holograph) {
assembly {
holograph := sload(_holographSlot)
}
}
/**
* @dev Get the bridge contract address.
*/
function _royalties() private view returns (address) {
return
HolographRegistryInterface(_holograph().getRegistry()).getContractTypeAddress(
0x0000000000000000000000000000000000000000000000000000000050413144
);
}
/**
* @dev Purposefully left empty, to prevent running out of gas errors when receiving native token payments.
*/
receive() external payable {}
/**
* @notice Fallback to the source contract.
* @dev Any function call that is not covered here, will automatically be sent over to the source contract.
*/
fallback() external payable {
// we check if royalties support the function, send there, otherwise revert to source
address _target;
if (HolographInterfacesInterface(_interfaces()).supportsInterface(InterfaceType.PA1D, msg.sig)) {
_target = _royalties();
assembly {
calldatacopy(0, 0, calldatasize())
let result := delegatecall(gas(), _target, 0, calldatasize(), 0, 0)
returndatacopy(0, 0, returndatasize())
switch result
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
}
} else {
assembly {
calldatacopy(0, 0, calldatasize())
mstore(calldatasize(), caller())
let result := call(gas(), sload(_sourceContractSlot), callvalue(), 0, add(calldatasize(), 32), 0, 0)
returndatacopy(0, 0, returndatasize())
switch result
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
}
}
}
function _isEventRegistered(HolographERC721Event _eventName) private view returns (bool) {
return ((_eventConfig >> uint256(_eventName)) & uint256(1) == 1 ? true : false);
}
}