| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Helpers.NFT; |
| | 3 | | using MainScripts.DCL.ServiceProviders.OpenSea.Interfaces; |
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Threading; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | public interface INFTInfoRetriever : IDisposable |
| | 10 | | { |
| | 11 | | public event Action<NFTInfo> OnFetchInfoSuccess; |
| | 12 | | public event Action OnFetchInfoFail; |
| | 13 | | void FetchNFTInfo(string chain, string contractAddress, string tokenId); |
| | 14 | |
|
| | 15 | | UniTask<NFTInfo?> FetchNFTInfoAsync(string chain, string contractAddress, string tokenId); |
| | 16 | | } |
| | 17 | |
|
| | 18 | | public class NFTInfoRetriever : INFTInfoRetriever |
| | 19 | | { |
| | 20 | | internal const string COULD_NOT_FETCH_DAR_URL = "Couldn't fetch DAR url '{0}' for NFTShape."; |
| | 21 | | internal const string ACCEPTED_URL_FORMAT = "The accepted format is 'ethereum://ContractAddress/TokenID'."; |
| | 22 | | internal const string SUPPORTED_PROTOCOL = "The only protocol currently supported is 'ethereum'."; |
| | 23 | |
|
| | 24 | | public event Action<NFTInfo> OnFetchInfoSuccess; |
| | 25 | | public event Action OnFetchInfoFail; |
| | 26 | | internal Coroutine fetchCoroutine; |
| | 27 | | private CancellationTokenSource tokenSource; |
| | 28 | |
|
| | 29 | | public void Dispose() |
| | 30 | | { |
| | 31 | | // Note: When we delete de old component NFTShape, we should remove the coroutine part |
| 3 | 32 | | if (fetchCoroutine != null) |
| 2 | 33 | | CoroutineStarter.Stop(fetchCoroutine); |
| | 34 | |
|
| 3 | 35 | | fetchCoroutine = null; |
| 3 | 36 | | tokenSource?.Cancel(); |
| 3 | 37 | | tokenSource?.Dispose(); |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | public void FetchNFTInfo(string chain, string contractAddress, string tokenId) |
| | 41 | | { |
| 2 | 42 | | if (fetchCoroutine != null) |
| 0 | 43 | | CoroutineStarter.Stop(fetchCoroutine); |
| | 44 | |
|
| 2 | 45 | | fetchCoroutine = CoroutineStarter.Start(FetchNFTInfoCoroutine(chain, contractAddress, tokenId)); |
| 2 | 46 | | } |
| | 47 | |
|
| | 48 | | public async UniTask<NFTInfo?> FetchNFTInfoAsync(string chain, string contractAddress, string tokenId) |
| | 49 | | { |
| 0 | 50 | | tokenSource = new CancellationTokenSource(); |
| 0 | 51 | | tokenSource.Token.ThrowIfCancellationRequested(); |
| | 52 | |
|
| 0 | 53 | | NFTInfo? nftInformation = null; |
| | 54 | |
|
| 0 | 55 | | var rutine = NFTUtils.FetchNFTInfo(chain, contractAddress, tokenId, |
| | 56 | | (info) => |
| | 57 | | { |
| 0 | 58 | | nftInformation = info; |
| 0 | 59 | | }, |
| | 60 | | (error) => |
| | 61 | | { |
| 0 | 62 | | Debug.LogError($"Couldn't fetch NFT: '{contractAddress}/{tokenId}' {error}"); |
| 0 | 63 | | }); |
| | 64 | |
|
| 0 | 65 | | await rutine.WithCancellation(tokenSource.Token); |
| 0 | 66 | | return nftInformation; |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | private IEnumerator FetchNFTInfoCoroutine(string chain, string contractAddress, string tokenId) |
| | 70 | | { |
| 2 | 71 | | yield return NFTUtils.FetchNFTInfo(chain, contractAddress, tokenId, |
| 0 | 72 | | (info) => { OnFetchInfoSuccess?.Invoke(info); }, |
| | 73 | | (error) => |
| | 74 | | { |
| 0 | 75 | | Debug.LogError($"Couldn't fetch NFT: '{contractAddress}/{tokenId}' {error}"); |
| 0 | 76 | | OnFetchInfoFail?.Invoke(); |
| 0 | 77 | | }); |
| 0 | 78 | | } |
| | 79 | | } |