-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetNFTURI.sol
128 lines (111 loc) · 4.65 KB
/
GetNFTURI.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
// SPDX-License-Identifier: MIT
/**
<<<<<<< HEAD:GetNFTURI.sol
* @title NFTIndexAndUri
* @author ProDesert22 and Ita
=======
* @title NFTIndexAndUrl
* @author ProDesert22 and itamarcps
>>>>>>> 8ab4de796abb668518ecee8602433bc873426f68:GetNFTURL.sol
*/
// https://github.com/prodesert22/NFTIndexAndUri
pragma solidity ^0.8.0;
import "./OpenZeppelin/token/ERC721/IERC721Enumerable.sol";
import "./OpenZeppelin/token/ERC721/IERC721Metadata.sol";
contract NFTIndexAndUri {
struct Nft {
address contract_address;
uint256 tokenId;
string uri;
}
function getNFTIndex(address contractAddress, address userAddress) public view returns (uint256[] memory _tokensId) {
//Returns the token's indexes owned by the address
IERC721Enumerable NFTContract = IERC721Enumerable(contractAddress);
uint256 userBalance = NFTContract.balanceOf(userAddress);
if (userBalance == 0) {
// Return an empty array
return new uint256[](0);
}
uint256[] memory result = new uint256[](userBalance);
uint256 index;
for (index = 0; index < userBalance; index++) {
try NFTContract.tokenOfOwnerByIndex(userAddress, index) returns (uint256 tokenId){
result[index] = tokenId;
} catch {
return new uint256[](0);
}
}
return result;
}
function getNFTIndexAndURI(address contractAddress, address userAddress) public view returns (uint256[] memory _tokensId, string[] memory _uris) {
//Returns the token's indexes owned by the address and uri of nft
IERC721Enumerable NFTContract = IERC721Enumerable(contractAddress);
IERC721Metadata NFTConctractUri = IERC721Metadata(contractAddress);
uint256 userBalance = NFTContract.balanceOf(userAddress);
if (userBalance == 0) {
// Return an empty array
return (new uint256[](0), new string[](0));
}
uint256[] memory result = new uint256[](userBalance);
string[] memory resulturi = new string[](userBalance);
uint256 index;
for (index = 0; index < userBalance; index++) {
try NFTContract.tokenOfOwnerByIndex(userAddress, index) returns (uint256 tokenId){
result[index] = tokenId;
try NFTConctractUri.tokenURI(tokenId) returns (string memory uri){
resulturi[index] = uri;
} catch {
return (new uint256[](0), new string[](0));
}
} catch {
return (new uint256[](0), new string[](0));
}
}
return (result, resulturi);
}
function getNFTsIndexAndUri(address[] calldata contractsAddresses, address userAddress) public view returns (Nft[] memory _nft){
uint256 index;
uint256 sum = 0;
uint256 n_contracts = contractsAddresses.length;
for (index = 0; index < n_contracts; index++) {
IERC721Enumerable NFTContract = IERC721Enumerable(contractsAddresses[index]);
IERC721Metadata NFTConctractUri = IERC721Metadata(contractsAddresses[index]);
uint256 userBalance = NFTContract.balanceOf(userAddress);
try NFTContract.tokenOfOwnerByIndex(userAddress, 0) returns (uint256 tokenId){
try NFTConctractUri.tokenURI(tokenId){
sum = sum + userBalance;
}catch{
continue;
}
}catch{
continue;
}
}
if (sum == 0){
return new Nft[](0);
}
Nft[] memory nfts = new Nft[](sum);
uint256 j = 0;
for (index = 0; index < n_contracts; index++) {
address contractAddress = contractsAddresses[index];
IERC721Enumerable NFTContract = IERC721Enumerable(contractAddress);
IERC721Metadata NFTConctractUri = IERC721Metadata(contractAddress);
uint256 userBalance = NFTContract.balanceOf(userAddress);
if (userBalance > 0){
for (uint256 i = 0; i < userBalance; i++) {
try NFTContract.tokenOfOwnerByIndex(userAddress, i) returns (uint256 tokenId){
try NFTConctractUri.tokenURI(tokenId) returns (string memory uri){
nfts[j] = Nft(contractAddress, tokenId, uri);
j++;
} catch {
break;
}
} catch {
break;
}
}
}
}
return nfts;
}
}