< Summary

Class:DCL.Wallet.WalletSectionHUDController
Assembly:WalletHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WalletHUD/WalletSectionHUDController.cs
Covered lines:73
Uncovered lines:13
Coverable lines:86
Total lines:193
Line coverage:84.8% (73 of 86)
Covered branches:0
Total branches:0
Covered methods:12
Total methods:12
Method coverage:100% (12 of 12)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WalletSectionHUDController(...)0%110100%
Dispose()0%110100%
ConfigureWalletSectionInFullscreenMenuChanged(...)0%110100%
OnCurrentEthereumManaBalanceChanged(...)0%110100%
OnCurrentPolygonManaBalanceChanged(...)0%110100%
OnWalletSectionVisible(...)0%3.583060%
RequestManaAsync()0%23.1216069.7%
OnProfileUpdated(...)0%2.032080%
CopyWalletAddress()0%2.062075%
GoToManaPurchaseUrl(...)0%110100%
GoToLearnMoreUrl()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WalletHUD/WalletSectionHUDController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Browser;
 3using DCL.Helpers;
 4using DCL.MyAccount;
 5using DCL.Tasks;
 6using System;
 7using System.Threading;
 8using UnityEngine;
 9
 10namespace DCL.Wallet
 11{
 12    public class WalletSectionHUDController
 13    {
 14        private const string URL_MANA_INFO = "https://docs.decentraland.org/examples/get-a-wallet?utm_source=dcl_explore
 15        private const string URL_MANA_PURCHASE = "https://account.decentraland.org?utm_source=dcl_explorer";
 16
 17        private readonly IWalletSectionHUDComponentView view;
 18        private readonly DataStore dataStore;
 19        private readonly IUserProfileBridge userProfileBridge;
 20        private readonly IClipboard clipboard;
 21        private readonly IBrowserBridge browserBridge;
 22        private readonly ITheGraph theGraph;
 23        private readonly IMyAccountAnalyticsService myAccountAnalyticsService;
 24
 25        private CancellationTokenSource fetchEthereumManaCancellationToken;
 26        private CancellationTokenSource fetchPolygonManaCancellationToken;
 27
 4928        private UserProfile ownUserProfile => userProfileBridge.GetOwn();
 29
 1130        public WalletSectionHUDController(
 31            IWalletSectionHUDComponentView view,
 32            DataStore dataStore,
 33            IUserProfileBridge userProfileBridge,
 34            IClipboard clipboard,
 35            IBrowserBridge browserBridge,
 36            ITheGraph theGraph,
 37            IMyAccountAnalyticsService myAccountAnalyticsService)
 38        {
 1139            this.view = view;
 1140            this.dataStore = dataStore;
 1141            this.userProfileBridge = userProfileBridge;
 1142            this.clipboard = clipboard;
 1143            this.browserBridge = browserBridge;
 1144            this.theGraph = theGraph;
 1145            this.myAccountAnalyticsService = myAccountAnalyticsService;
 46
 1147            dataStore.exploreV2.configureWalletSectionInFullscreenMenu.OnChange += ConfigureWalletSectionInFullscreenMen
 1148            ConfigureWalletSectionInFullscreenMenuChanged(dataStore.exploreV2.configureWalletSectionInFullscreenMenu.Get
 49
 1150            dataStore.wallet.isInitialized.Set(true);
 51
 1152            dataStore.wallet.currentEthereumManaBalance.OnChange += OnCurrentEthereumManaBalanceChanged;
 1153            OnCurrentEthereumManaBalanceChanged(dataStore.wallet.currentEthereumManaBalance.Get(), 0f);
 1154            dataStore.wallet.currentPolygonManaBalance.OnChange += OnCurrentPolygonManaBalanceChanged;
 1155            OnCurrentPolygonManaBalanceChanged(dataStore.wallet.currentPolygonManaBalance.Get(), 0f);
 56
 1157            dataStore.wallet.isWalletSectionVisible.OnChange += OnWalletSectionVisible;
 58
 1159            ownUserProfile.OnUpdate += OnProfileUpdated;
 1160            OnProfileUpdated(ownUserProfile);
 61
 1162            view.OnCopyWalletAddress += CopyWalletAddress;
 1163            view.OnBuyManaClicked += GoToManaPurchaseUrl;
 1164            view.OnLearnMoreClicked += GoToLearnMoreUrl;
 1165        }
 66
 67        public void Dispose()
 68        {
 1169            dataStore.exploreV2.configureWalletSectionInFullscreenMenu.OnChange -= ConfigureWalletSectionInFullscreenMen
 1170            dataStore.wallet.currentEthereumManaBalance.OnChange -= OnCurrentEthereumManaBalanceChanged;
 1171            dataStore.wallet.currentPolygonManaBalance.OnChange -= OnCurrentPolygonManaBalanceChanged;
 1172            dataStore.wallet.isWalletSectionVisible.OnChange -= OnWalletSectionVisible;
 1173            ownUserProfile.OnUpdate -= OnProfileUpdated;
 1174            view.OnBuyManaClicked -= GoToManaPurchaseUrl;
 1175            view.OnLearnMoreClicked -= GoToLearnMoreUrl;
 76
 1177            fetchEthereumManaCancellationToken.SafeCancelAndDispose();
 1178            fetchPolygonManaCancellationToken.SafeCancelAndDispose();
 1179        }
 80
 81        private void ConfigureWalletSectionInFullscreenMenuChanged(Transform currentParentTransform, Transform _) =>
 1182            view.SetAsFullScreenMenuMode(currentParentTransform);
 83
 84        private void OnCurrentEthereumManaBalanceChanged(double currentBalance, double _) =>
 1485            view.SetEthereumManaBalance(currentBalance);
 86
 87        private void OnCurrentPolygonManaBalanceChanged(double currentBalance, double _) =>
 1488            view.SetPolygonManaBalance(currentBalance);
 89
 90        private void OnWalletSectionVisible(bool isVisible, bool _)
 91        {
 292            if (ownUserProfile.isGuest)
 093                return;
 94
 295            if (isVisible)
 96            {
 297                fetchEthereumManaCancellationToken = fetchEthereumManaCancellationToken.SafeRestart();
 298                fetchPolygonManaCancellationToken = fetchPolygonManaCancellationToken.SafeRestart();
 299                RequestManaAsync(TheGraphNetwork.Ethereum, fetchEthereumManaCancellationToken.Token).Forget();
 2100                RequestManaAsync(TheGraphNetwork.Polygon, fetchPolygonManaCancellationToken.Token).Forget();
 101            }
 102            else
 103            {
 0104                fetchEthereumManaCancellationToken.SafeCancelAndDispose();
 0105                fetchPolygonManaCancellationToken.SafeCancelAndDispose();
 106            }
 0107        }
 108
 109        private async UniTask RequestManaAsync(TheGraphNetwork network, CancellationToken cancellationToken)
 110        {
 111            while (true)
 112            {
 12113                await UniTask.WaitUntil(() =>
 4114                        ownUserProfile != null && !string.IsNullOrEmpty(ownUserProfile.userId),
 115                    cancellationToken: cancellationToken);
 116
 4117                double ethereumManaBalanceResult = dataStore.wallet.currentEthereumManaBalance.Get();
 4118                double polygonManaBalanceResult = dataStore.wallet.currentPolygonManaBalance.Get();
 119
 120                try
 121                {
 4122                    if (network == TheGraphNetwork.Ethereum)
 2123                        view.SetEthereumManaLoadingActive(true);
 124                    else
 2125                        view.SetPolygonManaLoadingActive(true);
 126
 4127                    Promise<double> promise = theGraph.QueryMana(ownUserProfile.userId, network);
 128
 4129                    if (promise != null)
 130                    {
 4131                        await promise;
 132
 4133                        if (network == TheGraphNetwork.Ethereum)
 2134                            ethereumManaBalanceResult = promise.value;
 135                        else
 2136                            polygonManaBalanceResult = promise.value;
 137                    }
 4138                }
 0139                catch (OperationCanceledException) { }
 0140                catch (Exception)
 141                {
 0142                    Debug.LogError(network == TheGraphNetwork.Ethereum ?
 143                        "Error requesting Ethereum MANA balance from TheGraph!" :
 144                        "Error requesting Polygon MANA balance from TheGraph!");
 0145                }
 146                finally
 147                {
 4148                    if (network == TheGraphNetwork.Ethereum)
 149                    {
 2150                        dataStore.wallet.currentEthereumManaBalance.Set(ethereumManaBalanceResult);
 2151                        view.SetEthereumManaLoadingActive(false);
 152                    }
 153                    else
 154                    {
 2155                        dataStore.wallet.currentPolygonManaBalance.Set(polygonManaBalanceResult);
 2156                        view.SetPolygonManaLoadingActive(false);
 157                    }
 158                }
 159
 12160                await UniTask.Delay(TimeSpan.FromSeconds(WalletUtils.FETCH_MANA_INTERVAL), cancellationToken: cancellati
 161
 0162                if (cancellationToken.IsCancellationRequested)
 0163                    break;
 164            }
 0165        }
 166
 167        private void OnProfileUpdated(UserProfile userProfile)
 168        {
 11169            if (string.IsNullOrEmpty(userProfile.userId))
 0170                return;
 171
 11172            view.SetWalletAddress(userProfile.userId);
 11173            view.SetWalletSectionAsGuest(userProfile.isGuest);
 11174        }
 175
 176        private void CopyWalletAddress()
 177        {
 1178            if (string.IsNullOrEmpty(ownUserProfile.userId))
 0179                return;
 180
 1181            clipboard.WriteText(ownUserProfile.userId);
 1182        }
 183
 184        private void GoToManaPurchaseUrl(bool isPolygonNetwork)
 185        {
 2186            browserBridge.OpenUrl(URL_MANA_PURCHASE);
 2187            myAccountAnalyticsService.SendPlayerWalletBuyManaAnalytic(isPolygonNetwork);
 2188        }
 189
 190        private void GoToLearnMoreUrl() =>
 1191            browserBridge.OpenUrl(URL_MANA_INFO);
 192    }
 193}