| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Helpers.NFT.Markets; |
| | 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<NFTOwner> onSuccess, Action<string> onError) |
| | 17 | | { |
| 3 | 18 | | INFTMarket selectedMarket = null; |
| 6 | 19 | | yield return GetMarket(address, (mkt) => selectedMarket = mkt); |
| | 20 | |
|
| 3 | 21 | | if (selectedMarket != null) |
| | 22 | | { |
| 3 | 23 | | yield return selectedMarket.FetchNFTsFromOwner(address, onSuccess, onError); |
| 3 | 24 | | } |
| | 25 | | else |
| | 26 | | { |
| 0 | 27 | | onError?.Invoke($"Market not found for asset {address}"); |
| | 28 | | } |
| 3 | 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="assetContractAddress">asset contract address</param> |
| | 36 | | /// <param name="tokenId">asset token id</param> |
| | 37 | | /// <param name="onSuccess">success callback</param> |
| | 38 | | /// <param name="onError">error callback</param> |
| | 39 | | /// <returns>IEnumerator</returns> |
| | 40 | | public static IEnumerator FetchNFTInfo(string assetContractAddress, string tokenId, Action<NFTInfo> onSuccess, A |
| | 41 | | { |
| 5 | 42 | | INFTMarket selectedMarket = null; |
| 10 | 43 | | yield return GetMarket(assetContractAddress, tokenId, (mkt) => selectedMarket = mkt); |
| | 44 | |
|
| 3 | 45 | | if (selectedMarket != null) |
| | 46 | | { |
| 3 | 47 | | yield return selectedMarket.FetchNFTInfo(assetContractAddress, tokenId, onSuccess, onError); |
| 3 | 48 | | } |
| | 49 | | else |
| | 50 | | { |
| 0 | 51 | | onError?.Invoke($"Market not found for asset {assetContractAddress}/{tokenId}"); |
| | 52 | | } |
| 3 | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Fetch NFT. Request is fetch directly to the api instead of batched with other requests in a single query. |
| | 57 | | /// Please try to use `FetchNFTInfo` if ownership info is not relevant for your use case. |
| | 58 | | /// NOTE: result it does contain the array of owners for ERC1155 NFTs |
| | 59 | | /// </summary> |
| | 60 | | /// <param name="assetContractAddress">asset contract address</param> |
| | 61 | | /// <param name="tokenId">asset token id</param> |
| | 62 | | /// <param name="onSuccess">success callback</param> |
| | 63 | | /// <param name="onError">error callback</param> |
| | 64 | | /// <returns>IEnumerator</returns> |
| | 65 | | public static IEnumerator FetchNFTInfoSingleAsset(string assetContractAddress, string tokenId, Action<NFTInfoSin |
| | 66 | | { |
| 1 | 67 | | INFTMarket selectedMarket = null; |
| 2 | 68 | | yield return GetMarket(assetContractAddress, tokenId, (mkt) => selectedMarket = mkt); |
| | 69 | |
|
| 0 | 70 | | if (selectedMarket != null) |
| | 71 | | { |
| 0 | 72 | | yield return selectedMarket.FetchNFTInfoSingleAsset(assetContractAddress, tokenId, onSuccess, onError); |
| 0 | 73 | | } |
| | 74 | | else |
| | 75 | | { |
| 0 | 76 | | onError?.Invoke($"Market not found for asset {assetContractAddress}/{tokenId}"); |
| | 77 | | } |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | // NOTE: this method doesn't make sense now, but it will when support for other market is added |
| | 81 | | public static IEnumerator GetMarket(string assetContractAddress, string tokenId, Action<INFTMarket> onSuccess) |
| | 82 | | { |
| 6 | 83 | | IServiceProviders serviceProviders = Environment.i.platform.serviceProviders; |
| 6 | 84 | | INFTMarket openSea = null; |
| | 85 | |
|
| 6 | 86 | | if ( serviceProviders != null ) |
| 3 | 87 | | openSea = serviceProviders.openSea; |
| | 88 | |
|
| 6 | 89 | | onSuccess?.Invoke(openSea); |
| 6 | 90 | | yield break; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | public static IEnumerator GetMarket(string assetContractAddress, Action<INFTMarket> onSuccess) |
| | 94 | | { |
| 3 | 95 | | IServiceProviders serviceProviders = Environment.i.platform.serviceProviders; |
| 3 | 96 | | INFTMarket openSea = null; |
| | 97 | |
|
| 3 | 98 | | if ( serviceProviders != null ) |
| 3 | 99 | | openSea = serviceProviders.openSea; |
| | 100 | |
|
| 3 | 101 | | onSuccess?.Invoke(openSea); |
| 3 | 102 | | yield break; |
| | 103 | | } |
| | 104 | | } |
| | 105 | | } |