| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Text.RegularExpressions; |
| | 4 | | using System.Threading; |
| | 5 | | using Cysharp.Threading.Tasks; |
| | 6 | | using DCL.Helpers.NFT; |
| | 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 address, string id); |
| | 14 | | UniTask<NFTInfo> FetchNFTInfo(string src); |
| | 15 | | } |
| | 16 | |
|
| | 17 | | public class NFTInfoRetriever : INFTInfoRetriever |
| | 18 | | { |
| | 19 | | internal const string COULD_NOT_FETCH_DAR_URL = "Couldn't fetch DAR url '{0}' for NFTShape."; |
| | 20 | | internal const string ACCEPTED_URL_FORMAT = "The accepted format is 'ethereum://ContractAddress/TokenID'."; |
| | 21 | | internal const string SUPPORTED_PROTOCOL = "The only protocol currently supported is 'ethereum'."; |
| | 22 | |
|
| | 23 | | public event Action<NFTInfo> OnFetchInfoSuccess; |
| | 24 | | public event Action OnFetchInfoFail; |
| | 25 | | internal Coroutine fetchCoroutine; |
| | 26 | | private CancellationTokenSource tokenSource; |
| | 27 | |
|
| | 28 | | public void Dispose() |
| | 29 | | { |
| | 30 | | // Note: When we delete de old component NFTShape, we should remove the coroutine part |
| 11 | 31 | | if (fetchCoroutine != null) |
| 2 | 32 | | CoroutineStarter.Stop(fetchCoroutine); |
| | 33 | |
|
| 11 | 34 | | fetchCoroutine = null; |
| 11 | 35 | | tokenSource?.Cancel(); |
| 11 | 36 | | tokenSource?.Dispose(); |
| 3 | 37 | | } |
| | 38 | |
|
| | 39 | | public void FetchNFTInfo(string address, string id) |
| | 40 | | { |
| 2 | 41 | | if (fetchCoroutine != null) |
| 0 | 42 | | CoroutineStarter.Stop(fetchCoroutine); |
| | 43 | |
|
| 2 | 44 | | fetchCoroutine = CoroutineStarter.Start(FetchNFTInfoCoroutine(address, id)); |
| 2 | 45 | | } |
| | 46 | |
|
| | 47 | | public async UniTask<NFTInfo> FetchNFTInfo(string src) |
| | 48 | | { |
| 3 | 49 | | tokenSource = new CancellationTokenSource(); |
| 3 | 50 | | tokenSource.Token.ThrowIfCancellationRequested(); |
| | 51 | | // Check the src follows the needed format e.g.: 'ethereum://0x06012c8cf97BEaD5deAe237070F9587f8E7A266d/558536' |
| 3 | 52 | | var regexMatches = Regex.Matches(src, "(?<protocol>[^:]+)://(?<registry>0x([A-Fa-f0-9])+)(?:/(?<asset>.+))?"); |
| 3 | 53 | | if (regexMatches.Count == 0 || regexMatches[0].Groups["protocol"] == null || regexMatches[0].Groups["registry"] |
| | 54 | | { |
| 3 | 55 | | string errorMessage = string.Format(COULD_NOT_FETCH_DAR_URL + " " + ACCEPTED_URL_FORMAT, src); |
| 3 | 56 | | Debug.Log(errorMessage); |
| 3 | 57 | | OnFetchInfoFail?.Invoke(); |
| 3 | 58 | | return null; |
| | 59 | | } |
| | 60 | |
|
| 0 | 61 | | Match match = regexMatches[0]; |
| 0 | 62 | | string darURLProtocol = match.Groups["protocol"].ToString(); |
| 0 | 63 | | if (darURLProtocol != "ethereum") |
| | 64 | | { |
| 0 | 65 | | string errorMessage = string.Format(COULD_NOT_FETCH_DAR_URL + " " + SUPPORTED_PROTOCOL + " " + ACCEPTED_URL_ |
| 0 | 66 | | Debug.Log(errorMessage); |
| 0 | 67 | | OnFetchInfoFail?.Invoke(); |
| 0 | 68 | | return null; |
| | 69 | | } |
| | 70 | |
|
| 0 | 71 | | string darURLRegistry = match.Groups["registry"].ToString(); |
| 0 | 72 | | string darURLAsset = match.Groups["asset"].ToString(); |
| | 73 | |
|
| 0 | 74 | | NFTInfo nftInformation = null; |
| | 75 | |
|
| 0 | 76 | | var rutine = NFTUtils.FetchNFTInfo(darURLRegistry, darURLAsset, |
| | 77 | | (info) => |
| | 78 | | { |
| 0 | 79 | | nftInformation = info; |
| 0 | 80 | | }, |
| | 81 | | (error) => |
| | 82 | | { |
| 0 | 83 | | Debug.LogError($"Couldn't fetch NFT: '{darURLRegistry}/{darURLAsset}' {error}"); |
| 0 | 84 | | }); |
| | 85 | |
|
| 0 | 86 | | await rutine.WithCancellation(tokenSource.Token); |
| | 87 | |
|
| 0 | 88 | | return nftInformation; |
| 3 | 89 | | } |
| | 90 | |
|
| | 91 | | private IEnumerator FetchNFTInfoCoroutine(string address, string id) |
| | 92 | | { |
| 2 | 93 | | yield return NFTUtils.FetchNFTInfo(address, id, |
| 0 | 94 | | (info) => { OnFetchInfoSuccess?.Invoke(info); }, |
| | 95 | | (error) => |
| | 96 | | { |
| 0 | 97 | | Debug.LogError($"Couldn't fetch NFT: '{address}/{id}' {error}"); |
| 0 | 98 | | OnFetchInfoFail?.Invoke(); |
| 0 | 99 | | }); |
| 0 | 100 | | } |
| | 101 | | } |