| | 1 | | using MainScripts.DCL.ServiceProviders.OpenSea.Interfaces; |
| | 2 | | using System; |
| | 3 | | using System.Collections; |
| | 4 | |
|
| | 5 | | namespace DCL.Helpers.NFT |
| | 6 | | { |
| | 7 | | public static class NFTUtils |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Fetch NFT from owner |
| | 11 | | /// </summary> |
| | 12 | | /// <param name="address">owner address</param> |
| | 13 | | /// <param name="onSuccess">success callback</param> |
| | 14 | | /// <param name="onError">error callback</param> |
| | 15 | | /// <returns>IEnumerator</returns> |
| | 16 | | public static IEnumerator FetchNFTsFromOwner(string address, Action<OwnNFTInfo> onSuccess, Action<string> onErro |
| | 17 | | { |
| 0 | 18 | | IOpenSea selectedMarket = null; |
| 0 | 19 | | yield return GetMarket(address, (mkt) => selectedMarket = mkt); |
| | 20 | |
|
| 0 | 21 | | if (selectedMarket != null) |
| | 22 | | { |
| 0 | 23 | | yield return selectedMarket.FetchNFTsFromOwner(address, onSuccess, onError); |
| | 24 | | } |
| | 25 | | else |
| | 26 | | { |
| 0 | 27 | | onError?.Invoke($"Market not found for asset {address}"); |
| | 28 | | } |
| 0 | 29 | | } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Fetch NFT. Request is added to a batch of requests to reduce the amount of request to the api. |
| | 33 | | /// NOTE: for ERC1155 result does not contain the array of owners and sell price for this asset |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="chain">chain network id</param> |
| | 36 | | /// <param name="assetContractAddress">asset contract address</param> |
| | 37 | | /// <param name="tokenId">asset token id</param> |
| | 38 | | /// <param name="onSuccess">success callback</param> |
| | 39 | | /// <param name="onError">error callback</param> |
| | 40 | | /// <returns>IEnumerator</returns> |
| | 41 | | public static IEnumerator FetchNFTInfo(string chain, string assetContractAddress, string tokenId, Action<NFTInfo |
| | 42 | | { |
| 2 | 43 | | IOpenSea selectedMarket = null; |
| 4 | 44 | | yield return GetMarket(chain, assetContractAddress, tokenId, (mkt) => selectedMarket = mkt); |
| | 45 | |
|
| 0 | 46 | | if (selectedMarket != null) |
| | 47 | | { |
| 0 | 48 | | yield return selectedMarket.FetchNFTInfo(chain, assetContractAddress, tokenId, onSuccess, onError); |
| | 49 | | } |
| | 50 | | else |
| | 51 | | { |
| 0 | 52 | | onError?.Invoke($"Market not found for asset {assetContractAddress}/{tokenId}"); |
| | 53 | | } |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Fetch NFT. Request is fetch directly to the api instead of batched with other requests in a single query. |
| | 58 | | /// Please try to use `FetchNFTInfo` if ownership info is not relevant for your use case. |
| | 59 | | /// NOTE: result it does contain the array of owners for ERC1155 NFTs |
| | 60 | | /// </summary> |
| | 61 | | /// <param name="chain">chain network id</param> |
| | 62 | | /// <param name="assetContractAddress">asset contract address</param> |
| | 63 | | /// <param name="tokenId">asset token id</param> |
| | 64 | | /// <param name="onSuccess">success callback</param> |
| | 65 | | /// <param name="onError">error callback</param> |
| | 66 | | /// <returns>IEnumerator</returns> |
| | 67 | | public static IEnumerator FetchNFTInfoSingleAsset(string chain, string assetContractAddress, string tokenId, Act |
| | 68 | | { |
| 2 | 69 | | IOpenSea selectedMarket = null; |
| 4 | 70 | | yield return GetMarket(chain, assetContractAddress, tokenId, (mkt) => selectedMarket = mkt); |
| | 71 | |
|
| 0 | 72 | | if (selectedMarket != null) |
| | 73 | | { |
| 0 | 74 | | yield return selectedMarket.FetchNFTInfo(chain, assetContractAddress, tokenId, onSuccess, onError); |
| | 75 | | } |
| | 76 | | else |
| | 77 | | { |
| 0 | 78 | | onError?.Invoke($"Market not found for asset {assetContractAddress}/{tokenId}"); |
| | 79 | | } |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | // NOTE: this method doesn't make sense now, but it will when support for other market is added |
| | 83 | | public static IEnumerator GetMarket(string chain, string assetContractAddress, string tokenId, Action<IOpenSea> |
| | 84 | | { |
| 4 | 85 | | IServiceProviders serviceProviders = Environment.i.platform.serviceProviders; |
| 4 | 86 | | IOpenSea openSea = null; |
| | 87 | |
|
| 4 | 88 | | if (serviceProviders != null) |
| 0 | 89 | | openSea = serviceProviders.openSea; |
| | 90 | |
|
| 4 | 91 | | onSuccess?.Invoke(openSea); |
| 4 | 92 | | yield break; |
| | 93 | | } |
| | 94 | |
|
| | 95 | | public static IEnumerator GetMarket(string assetContractAddress, Action<IOpenSea> onSuccess) |
| | 96 | | { |
| 0 | 97 | | IServiceProviders serviceProviders = Environment.i.platform.serviceProviders; |
| 0 | 98 | | IOpenSea openSea = null; |
| | 99 | |
|
| 0 | 100 | | if (serviceProviders != null) |
| 0 | 101 | | openSea = serviceProviders.openSea; |
| | 102 | |
|
| 0 | 103 | | onSuccess?.Invoke(openSea); |
| 0 | 104 | | yield break; |
| | 105 | | } |
| | 106 | |
|
| | 107 | | /// <summary> |
| | 108 | | /// Parses URNs with the format "urn:decentraland:{CHAIN}:{CONTRACT_STANDARD}:{CONTRACT_ADDRESS}:{TOKEN_ID}" |
| | 109 | | /// and if successful stores the contract address and token ID in their corresponding reference parameters. |
| | 110 | | /// example: urn:decentraland:ethereum:erc721:0x00...000:123 |
| | 111 | | /// </summary> |
| | 112 | | /// <param name="urn">the raw URN of the NFT asset</param> |
| | 113 | | /// <param name="chain">if successful the chain network id is stored in this reference parameter</param> |
| | 114 | | /// <param name="contractAddress">if successful the contract address is stored in this reference parameter</para |
| | 115 | | /// <param name="tokenId">if successful the token id is stored in this reference parameter</param> |
| | 116 | | /// <returns>bool</returns> |
| | 117 | | // TODO: update this method when support for wearables/emotes/etc urn is added |
| | 118 | | public static bool TryParseUrn(string urn, out string chain, out string contractAddress, out string tokenId) |
| | 119 | | { |
| | 120 | | const char SEPARATOR = ':'; |
| | 121 | | const string DCL_URN_ID = "urn:decentraland"; |
| | 122 | | const string COLLECTIONS_THIRDPARTY = "collections-thirdparty"; |
| | 123 | |
|
| 13 | 124 | | contractAddress = string.Empty; |
| 13 | 125 | | tokenId = string.Empty; |
| 13 | 126 | | chain = string.Empty; |
| | 127 | |
|
| | 128 | | try |
| | 129 | | { |
| 13 | 130 | | var urnSpan = urn.AsSpan(); |
| | 131 | |
|
| | 132 | | // 1: "urn:decentraland" |
| 13 | 133 | | if (!urnSpan.Slice(0, DCL_URN_ID.Length).Equals(DCL_URN_ID, StringComparison.Ordinal)) |
| 1 | 134 | | return false; |
| 10 | 135 | | urnSpan = urnSpan.Slice(DCL_URN_ID.Length + 1); |
| | 136 | |
|
| | 137 | | // 2: chain/network |
| 10 | 138 | | var chainSpan = urnSpan.Slice(0, urnSpan.IndexOf(SEPARATOR)); |
| 10 | 139 | | urnSpan = urnSpan.Slice(chainSpan.Length + 1); |
| | 140 | |
|
| | 141 | | // 3: contract standard |
| 10 | 142 | | var contractStandardSpan = urnSpan.Slice(0, urnSpan.IndexOf(SEPARATOR)); |
| 10 | 143 | | urnSpan = urnSpan.Slice(contractStandardSpan.Length + 1); |
| | 144 | |
|
| | 145 | | // check if wearables is third-party |
| 10 | 146 | | if (contractStandardSpan.ToString().Equals(COLLECTIONS_THIRDPARTY, StringComparison.Ordinal)) |
| | 147 | | { |
| | 148 | | // 4: contract address |
| 0 | 149 | | var contractAddressSpan = urnSpan; |
| 0 | 150 | | contractAddress = contractAddressSpan.ToString(); |
| | 151 | |
|
| | 152 | | // NOTE: Third Party wearables do not have token id at the moment |
| | 153 | | } |
| | 154 | | else |
| | 155 | | { |
| | 156 | | // 4: contract address |
| 10 | 157 | | var contractAddressSpan = urnSpan.Slice(0, urnSpan.IndexOf(SEPARATOR)); |
| 7 | 158 | | urnSpan = urnSpan.Slice(contractAddressSpan.Length + 1); |
| | 159 | |
|
| | 160 | | // 5: token id |
| 7 | 161 | | var tokenIdSpan = urnSpan; |
| 7 | 162 | | contractAddress = contractAddressSpan.ToString(); |
| 7 | 163 | | tokenId = tokenIdSpan.ToString(); |
| | 164 | | } |
| | 165 | |
|
| 7 | 166 | | chain = chainSpan.ToString(); |
| 7 | 167 | | return true; |
| | 168 | | } |
| 5 | 169 | | catch (Exception e) |
| | 170 | | { |
| | 171 | | // ignored |
| 5 | 172 | | } |
| | 173 | |
|
| 5 | 174 | | return false; |
| 8 | 175 | | } |
| | 176 | | } |
| | 177 | | } |