| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Browser; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.MyAccount; |
| | 5 | | using DCL.Tasks; |
| | 6 | | using System; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace 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 | |
|
| 49 | 28 | | private UserProfile ownUserProfile => userProfileBridge.GetOwn(); |
| | 29 | |
|
| 11 | 30 | | public WalletSectionHUDController( |
| | 31 | | IWalletSectionHUDComponentView view, |
| | 32 | | DataStore dataStore, |
| | 33 | | IUserProfileBridge userProfileBridge, |
| | 34 | | IClipboard clipboard, |
| | 35 | | IBrowserBridge browserBridge, |
| | 36 | | ITheGraph theGraph, |
| | 37 | | IMyAccountAnalyticsService myAccountAnalyticsService) |
| | 38 | | { |
| 11 | 39 | | this.view = view; |
| 11 | 40 | | this.dataStore = dataStore; |
| 11 | 41 | | this.userProfileBridge = userProfileBridge; |
| 11 | 42 | | this.clipboard = clipboard; |
| 11 | 43 | | this.browserBridge = browserBridge; |
| 11 | 44 | | this.theGraph = theGraph; |
| 11 | 45 | | this.myAccountAnalyticsService = myAccountAnalyticsService; |
| | 46 | |
|
| 11 | 47 | | dataStore.exploreV2.configureWalletSectionInFullscreenMenu.OnChange += ConfigureWalletSectionInFullscreenMen |
| 11 | 48 | | ConfigureWalletSectionInFullscreenMenuChanged(dataStore.exploreV2.configureWalletSectionInFullscreenMenu.Get |
| | 49 | |
|
| 11 | 50 | | dataStore.wallet.isInitialized.Set(true); |
| | 51 | |
|
| 11 | 52 | | dataStore.wallet.currentEthereumManaBalance.OnChange += OnCurrentEthereumManaBalanceChanged; |
| 11 | 53 | | OnCurrentEthereumManaBalanceChanged(dataStore.wallet.currentEthereumManaBalance.Get(), 0f); |
| 11 | 54 | | dataStore.wallet.currentPolygonManaBalance.OnChange += OnCurrentPolygonManaBalanceChanged; |
| 11 | 55 | | OnCurrentPolygonManaBalanceChanged(dataStore.wallet.currentPolygonManaBalance.Get(), 0f); |
| | 56 | |
|
| 11 | 57 | | dataStore.wallet.isWalletSectionVisible.OnChange += OnWalletSectionVisible; |
| | 58 | |
|
| 11 | 59 | | ownUserProfile.OnUpdate += OnProfileUpdated; |
| 11 | 60 | | OnProfileUpdated(ownUserProfile); |
| | 61 | |
|
| 11 | 62 | | view.OnCopyWalletAddress += CopyWalletAddress; |
| 11 | 63 | | view.OnBuyManaClicked += GoToManaPurchaseUrl; |
| 11 | 64 | | view.OnLearnMoreClicked += GoToLearnMoreUrl; |
| 11 | 65 | | } |
| | 66 | |
|
| | 67 | | public void Dispose() |
| | 68 | | { |
| 11 | 69 | | dataStore.exploreV2.configureWalletSectionInFullscreenMenu.OnChange -= ConfigureWalletSectionInFullscreenMen |
| 11 | 70 | | dataStore.wallet.currentEthereumManaBalance.OnChange -= OnCurrentEthereumManaBalanceChanged; |
| 11 | 71 | | dataStore.wallet.currentPolygonManaBalance.OnChange -= OnCurrentPolygonManaBalanceChanged; |
| 11 | 72 | | dataStore.wallet.isWalletSectionVisible.OnChange -= OnWalletSectionVisible; |
| 11 | 73 | | ownUserProfile.OnUpdate -= OnProfileUpdated; |
| 11 | 74 | | view.OnBuyManaClicked -= GoToManaPurchaseUrl; |
| 11 | 75 | | view.OnLearnMoreClicked -= GoToLearnMoreUrl; |
| | 76 | |
|
| 11 | 77 | | fetchEthereumManaCancellationToken.SafeCancelAndDispose(); |
| 11 | 78 | | fetchPolygonManaCancellationToken.SafeCancelAndDispose(); |
| 11 | 79 | | } |
| | 80 | |
|
| | 81 | | private void ConfigureWalletSectionInFullscreenMenuChanged(Transform currentParentTransform, Transform _) => |
| 11 | 82 | | view.SetAsFullScreenMenuMode(currentParentTransform); |
| | 83 | |
|
| | 84 | | private void OnCurrentEthereumManaBalanceChanged(double currentBalance, double _) => |
| 14 | 85 | | view.SetEthereumManaBalance(currentBalance); |
| | 86 | |
|
| | 87 | | private void OnCurrentPolygonManaBalanceChanged(double currentBalance, double _) => |
| 14 | 88 | | view.SetPolygonManaBalance(currentBalance); |
| | 89 | |
|
| | 90 | | private void OnWalletSectionVisible(bool isVisible, bool _) |
| | 91 | | { |
| 2 | 92 | | if (ownUserProfile.isGuest) |
| 0 | 93 | | return; |
| | 94 | |
|
| 2 | 95 | | if (isVisible) |
| | 96 | | { |
| 2 | 97 | | fetchEthereumManaCancellationToken = fetchEthereumManaCancellationToken.SafeRestart(); |
| 2 | 98 | | fetchPolygonManaCancellationToken = fetchPolygonManaCancellationToken.SafeRestart(); |
| 2 | 99 | | RequestManaAsync(TheGraphNetwork.Ethereum, fetchEthereumManaCancellationToken.Token).Forget(); |
| 2 | 100 | | RequestManaAsync(TheGraphNetwork.Polygon, fetchPolygonManaCancellationToken.Token).Forget(); |
| | 101 | | } |
| | 102 | | else |
| | 103 | | { |
| 0 | 104 | | fetchEthereumManaCancellationToken.SafeCancelAndDispose(); |
| 0 | 105 | | fetchPolygonManaCancellationToken.SafeCancelAndDispose(); |
| | 106 | | } |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | private async UniTask RequestManaAsync(TheGraphNetwork network, CancellationToken cancellationToken) |
| | 110 | | { |
| | 111 | | while (true) |
| | 112 | | { |
| 12 | 113 | | await UniTask.WaitUntil(() => |
| 4 | 114 | | ownUserProfile != null && !string.IsNullOrEmpty(ownUserProfile.userId), |
| | 115 | | cancellationToken: cancellationToken); |
| | 116 | |
|
| 4 | 117 | | double ethereumManaBalanceResult = dataStore.wallet.currentEthereumManaBalance.Get(); |
| 4 | 118 | | double polygonManaBalanceResult = dataStore.wallet.currentPolygonManaBalance.Get(); |
| | 119 | |
|
| | 120 | | try |
| | 121 | | { |
| 4 | 122 | | if (network == TheGraphNetwork.Ethereum) |
| 2 | 123 | | view.SetEthereumManaLoadingActive(true); |
| | 124 | | else |
| 2 | 125 | | view.SetPolygonManaLoadingActive(true); |
| | 126 | |
|
| 4 | 127 | | Promise<double> promise = theGraph.QueryMana(ownUserProfile.userId, network); |
| | 128 | |
|
| 4 | 129 | | if (promise != null) |
| | 130 | | { |
| 4 | 131 | | await promise; |
| | 132 | |
|
| 4 | 133 | | if (network == TheGraphNetwork.Ethereum) |
| 2 | 134 | | ethereumManaBalanceResult = promise.value; |
| | 135 | | else |
| 2 | 136 | | polygonManaBalanceResult = promise.value; |
| | 137 | | } |
| 4 | 138 | | } |
| 0 | 139 | | catch (OperationCanceledException) { } |
| 0 | 140 | | catch (Exception) |
| | 141 | | { |
| 0 | 142 | | Debug.LogError(network == TheGraphNetwork.Ethereum ? |
| | 143 | | "Error requesting Ethereum MANA balance from TheGraph!" : |
| | 144 | | "Error requesting Polygon MANA balance from TheGraph!"); |
| 0 | 145 | | } |
| | 146 | | finally |
| | 147 | | { |
| 4 | 148 | | if (network == TheGraphNetwork.Ethereum) |
| | 149 | | { |
| 2 | 150 | | dataStore.wallet.currentEthereumManaBalance.Set(ethereumManaBalanceResult); |
| 2 | 151 | | view.SetEthereumManaLoadingActive(false); |
| | 152 | | } |
| | 153 | | else |
| | 154 | | { |
| 2 | 155 | | dataStore.wallet.currentPolygonManaBalance.Set(polygonManaBalanceResult); |
| 2 | 156 | | view.SetPolygonManaLoadingActive(false); |
| | 157 | | } |
| | 158 | | } |
| | 159 | |
|
| 12 | 160 | | await UniTask.Delay(TimeSpan.FromSeconds(WalletUtils.FETCH_MANA_INTERVAL), cancellationToken: cancellati |
| | 161 | |
|
| 0 | 162 | | if (cancellationToken.IsCancellationRequested) |
| 0 | 163 | | break; |
| | 164 | | } |
| 0 | 165 | | } |
| | 166 | |
|
| | 167 | | private void OnProfileUpdated(UserProfile userProfile) |
| | 168 | | { |
| 11 | 169 | | if (string.IsNullOrEmpty(userProfile.userId)) |
| 0 | 170 | | return; |
| | 171 | |
|
| 11 | 172 | | view.SetWalletAddress(userProfile.userId); |
| 11 | 173 | | view.SetWalletSectionAsGuest(userProfile.isGuest); |
| 11 | 174 | | } |
| | 175 | |
|
| | 176 | | private void CopyWalletAddress() |
| | 177 | | { |
| 1 | 178 | | if (string.IsNullOrEmpty(ownUserProfile.userId)) |
| 0 | 179 | | return; |
| | 180 | |
|
| 1 | 181 | | clipboard.WriteText(ownUserProfile.userId); |
| 1 | 182 | | } |
| | 183 | |
|
| | 184 | | private void GoToManaPurchaseUrl(bool isPolygonNetwork) |
| | 185 | | { |
| 2 | 186 | | browserBridge.OpenUrl(URL_MANA_PURCHASE); |
| 2 | 187 | | myAccountAnalyticsService.SendPlayerWalletBuyManaAnalytic(isPolygonNetwork); |
| 2 | 188 | | } |
| | 189 | |
|
| | 190 | | private void GoToLearnMoreUrl() => |
| 1 | 191 | | browserBridge.OpenUrl(URL_MANA_INFO); |
| | 192 | | } |
| | 193 | | } |