| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Helpers.NFT.Markets; |
| | 4 | |
|
| | 5 | | namespace DCL.Helpers.NFT |
| | 6 | | { |
| | 7 | | public static class NFTHelper |
| | 8 | | { |
| 1 | 9 | | static INFTMarket market = new OpenSea(); |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// Fetch NFT from owner |
| | 13 | | /// </summary> |
| | 14 | | /// <param name="address">owner address</param> |
| | 15 | | /// <param name="onSuccess">success callback</param> |
| | 16 | | /// <param name="onError">error callback</param> |
| | 17 | | /// <returns>IEnumerator</returns> |
| | 18 | | public static IEnumerator FetchNFTsFromOwner(string address, Action<NFTOwner> onSuccess, Action<string> onError) |
| | 19 | | { |
| 55 | 20 | | INFTMarket selectedMarket = null; |
| 110 | 21 | | yield return GetMarket(address, (mkt) => selectedMarket = mkt); |
| | 22 | |
|
| 2 | 23 | | if (selectedMarket != null) |
| | 24 | | { |
| 2 | 25 | | yield return selectedMarket.FetchNFTsFromOwner(address, onSuccess, onError); |
| 0 | 26 | | } |
| | 27 | | else |
| | 28 | | { |
| 0 | 29 | | onError?.Invoke($"Market not found for asset {address}"); |
| | 30 | | } |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Fetch NFT. Request is added to a batch of requests to reduce the amount of request to the api. |
| | 35 | | /// NOTE: for ERC1155 result does not contain the array of owners and sell price for this asset |
| | 36 | | /// </summary> |
| | 37 | | /// <param name="assetContractAddress">asset contract address</param> |
| | 38 | | /// <param name="tokenId">asset token id</param> |
| | 39 | | /// <param name="onSuccess">success callback</param> |
| | 40 | | /// <param name="onError">error callback</param> |
| | 41 | | /// <returns>IEnumerator</returns> |
| | 42 | | public static IEnumerator FetchNFTInfo(string assetContractAddress, string tokenId, Action<NFTInfo> onSuccess, A |
| | 43 | | { |
| 11 | 44 | | INFTMarket selectedMarket = null; |
| 22 | 45 | | yield return GetMarket(assetContractAddress, tokenId, (mkt) => selectedMarket = mkt); |
| | 46 | |
|
| 8 | 47 | | if (selectedMarket != null) |
| | 48 | | { |
| 8 | 49 | | yield return selectedMarket.FetchNFTInfo(assetContractAddress, tokenId, onSuccess, onError); |
| 0 | 50 | | } |
| | 51 | | else |
| | 52 | | { |
| 0 | 53 | | onError?.Invoke($"Market not found for asset {assetContractAddress}/{tokenId}"); |
| | 54 | | } |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Fetch NFT. Request is fetch directly to the api instead of batched with other requests in a single query. |
| | 59 | | /// Please try to use `FetchNFTInfo` if ownership info is not relevant for your use case. |
| | 60 | | /// NOTE: result it does contain the array of owners for ERC1155 NFTs |
| | 61 | | /// </summary> |
| | 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 assetContractAddress, string tokenId, Action<NFTInfoSin |
| | 68 | | { |
| 1 | 69 | | INFTMarket selectedMarket = null; |
| 2 | 70 | | yield return GetMarket(assetContractAddress, tokenId, (mkt) => selectedMarket = mkt); |
| | 71 | |
|
| 0 | 72 | | if (selectedMarket != null) |
| | 73 | | { |
| 0 | 74 | | yield return selectedMarket.FetchNFTInfoSingleAsset(assetContractAddress, tokenId, onSuccess, onError); |
| 0 | 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 | | static public IEnumerator GetMarket(string assetContractAddress, string tokenId, Action<INFTMarket> onSuccess) |
| | 84 | | { |
| 12 | 85 | | onSuccess?.Invoke(market); |
| 12 | 86 | | yield break; |
| | 87 | | } |
| | 88 | |
|
| | 89 | | static public IEnumerator GetMarket(string assetContractAddress, Action<INFTMarket> onSuccess) |
| | 90 | | { |
| 55 | 91 | | onSuccess?.Invoke(market); |
| 55 | 92 | | yield break; |
| | 93 | | } |
| | 94 | | } |
| | 95 | | } |