< Summary

Class:NFTInfoRetriever
Assembly:DCL.Components.NFT
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/LoadableShapes/NFTShape/NFTAsset/NFTInfoLoadHelper.cs
Covered lines:9
Uncovered lines:18
Coverable lines:27
Total lines:79
Line coverage:33.3% (9 of 27)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:4
Method coverage:75% (3 of 4)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Dispose()0%4.074083.33%
FetchNFTInfo(...)0%2.062075%
FetchNFTInfoAsync()0%12300%
FetchNFTInfoCoroutine()0%4.123050%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/LoadableShapes/NFTShape/NFTAsset/NFTInfoLoadHelper.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Helpers.NFT;
 3using MainScripts.DCL.ServiceProviders.OpenSea.Interfaces;
 4using System;
 5using System.Collections;
 6using System.Threading;
 7using UnityEngine;
 8
 9public 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
 18public 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
 332        if (fetchCoroutine != null)
 233            CoroutineStarter.Stop(fetchCoroutine);
 34
 335        fetchCoroutine = null;
 336        tokenSource?.Cancel();
 337        tokenSource?.Dispose();
 038    }
 39
 40    public void FetchNFTInfo(string chain, string contractAddress, string tokenId)
 41    {
 242        if (fetchCoroutine != null)
 043            CoroutineStarter.Stop(fetchCoroutine);
 44
 245        fetchCoroutine = CoroutineStarter.Start(FetchNFTInfoCoroutine(chain, contractAddress, tokenId));
 246    }
 47
 48    public async UniTask<NFTInfo?> FetchNFTInfoAsync(string chain, string contractAddress, string tokenId)
 49    {
 050        tokenSource = new CancellationTokenSource();
 051        tokenSource.Token.ThrowIfCancellationRequested();
 52
 053        NFTInfo? nftInformation = null;
 54
 055        var rutine = NFTUtils.FetchNFTInfo(chain, contractAddress, tokenId,
 56            (info) =>
 57            {
 058                nftInformation = info;
 059            },
 60            (error) =>
 61            {
 062                Debug.LogError($"Couldn't fetch NFT: '{contractAddress}/{tokenId}' {error}");
 063            });
 64
 065        await rutine.WithCancellation(tokenSource.Token);
 066        return nftInformation;
 067    }
 68
 69    private IEnumerator FetchNFTInfoCoroutine(string chain, string contractAddress, string tokenId)
 70    {
 271        yield return NFTUtils.FetchNFTInfo(chain, contractAddress, tokenId,
 072            (info) => { OnFetchInfoSuccess?.Invoke(info); },
 73            (error) =>
 74            {
 075                Debug.LogError($"Couldn't fetch NFT: '{contractAddress}/{tokenId}' {error}");
 076                OnFetchInfoFail?.Invoke();
 077            });
 078    }
 79}