diff --git a/Assets/Shared/Scripts/UI/LevelCompleteScreen.cs b/Assets/Shared/Scripts/UI/LevelCompleteScreen.cs index 328700dff..92e2c5d7d 100644 --- a/Assets/Shared/Scripts/UI/LevelCompleteScreen.cs +++ b/Assets/Shared/Scripts/UI/LevelCompleteScreen.cs @@ -6,6 +6,9 @@ using System; using System.Collections.Generic; using Immutable.Passport; +using Cysharp.Threading.Tasks; +using System.Numerics; +using System.Net.Http; namespace HyperCasual.Runner { @@ -117,10 +120,79 @@ public async void OnEnable() m_TryAgainButton.RemoveListener(OnTryAgainButtonClicked); m_TryAgainButton.AddListener(OnTryAgainButtonClicked); - // Show 'Next' button if player is already logged into Passport - ShowNextButton(SaveManager.Instance.IsLoggedIn); - // Show "Continue with Passport" button if the player is not logged into Passport - ShowContinueWithPassportButton(!SaveManager.Instance.IsLoggedIn); + ShowError(false); + ShowLoading(false); + + // If player is logged into Passport mint coins to player + if (SaveManager.Instance.IsLoggedIn) + { + // Mint collected coins to player + await MintCoins(); + } + else + { + // Show 'Next' button if player is already logged into Passport + ShowNextButton(SaveManager.Instance.IsLoggedIn); + // Show "Continue with Passport" button if the player is not logged into Passport + ShowContinueWithPassportButton(!SaveManager.Instance.IsLoggedIn); + } + } + + /// + /// Mints collected coins (i.e. Immutable Runner Token) to the player's wallet + /// + private async UniTask MintCoins() + { + // This function is similar to MintCoins() in MintScreen.cs. Consider refactoring duplicate code in production. + Debug.Log("Minting coins..."); + bool success = false; + + // Show loading + ShowLoading(true); + ShowNextButton(false); + ShowError(false); + + try + { + // Don't mint any coins if player did not collect any + if (m_CoinCount == 0) + { + success = true; + } + else + { + // Get the player's wallet address to mint the coins to + List accounts = await Passport.Instance.ZkEvmRequestAccounts(); + string address = accounts[0]; + if (address != null) + { + // Calculate the quantity to mint + // Need to take into account Immutable Runner Token decimal value i.e. 18 + BigInteger quantity = BigInteger.Multiply(new BigInteger(m_CoinCount), BigInteger.Pow(10, 18)); + Debug.Log($"Quantity: {quantity}"); + var nvc = new List> + { + // Set 'to' to the player's wallet address + new KeyValuePair("to", address), + // Set 'quanity' to the number of coins collected + new KeyValuePair("quantity", quantity.ToString()) + }; + using var client = new HttpClient(); + string url = $"http://localhost:3000/mint/token"; // Endpoint to mint token + using var req = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(nvc) }; + using var res = await client.SendAsync(req); + success = res.IsSuccessStatusCode; + } + } + } + catch (Exception ex) + { + Debug.Log($"Failed to mint coins: {ex.Message}"); + } + + ShowLoading(false); + ShowNextButton(success); + ShowError(!success); } private async void OnContinueWithPassportButtonClicked() @@ -156,8 +228,9 @@ private async void OnContinueWithPassportButtonClicked() } } - private void OnTryAgainButtonClicked() + private async void OnTryAgainButtonClicked() { + await MintCoins(); } private void OnNextButtonClicked() diff --git a/Assets/Shared/Scripts/UI/MintScreen.cs b/Assets/Shared/Scripts/UI/MintScreen.cs index a01e3d27f..ab2aacb66 100644 --- a/Assets/Shared/Scripts/UI/MintScreen.cs +++ b/Assets/Shared/Scripts/UI/MintScreen.cs @@ -71,12 +71,12 @@ private async void Mint() ShowError(false); ShowNextButton(false); - // Mint fox if fox not minted yet + // Mint fox if not minted yet if (!mintedFox) { mintedFox = await MintFox(); } - // Mint coins if fox not minted yet + // Mint coins if not minted yet if (!mintedCoins) { mintedCoins = await MintCoins(); @@ -119,24 +119,31 @@ private async UniTask GetWalletAddress() private async UniTask MintFox() { Debug.Log("Minting fox..."); - // Get the player's wallet address to mint the fox to - string address = await GetWalletAddress(); - - if (address != null) + try { - var nvc = new List> + string address = await GetWalletAddress(); // Get the player's wallet address to mint the fox to + + if (address != null) + { + var nvc = new List> { // Set 'to' to the player's wallet address new KeyValuePair("to", address) }; - using var client = new HttpClient(); - string url = $"http://localhost:3000/mint/fox"; // Endpoint to mint fox - using var req = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(nvc) }; - using var res = await client.SendAsync(req); - return res.IsSuccessStatusCode; - } + using var client = new HttpClient(); + string url = $"http://localhost:3000/mint/fox"; // Endpoint to mint fox + using var req = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(nvc) }; + using var res = await client.SendAsync(req); + return res.IsSuccessStatusCode; + } - return false; + return false; + } + catch (Exception ex) + { + Debug.Log($"Failed to mint fox: {ex.Message}"); + return false; + } } /// @@ -146,32 +153,42 @@ private async UniTask MintFox() private async UniTask MintCoins() { Debug.Log("Minting coins..."); - // Get the player's wallet address to mint the fox to - string address = await GetWalletAddress(); - // Get number of coins collected - int coinsCollected = GetNumCoinsCollected(); - - if (address != null) + try { - // Calculate the quantity to mint - // Need to take into account Immutable Runner Token decimal value i.e. 18 - BigInteger quantity = BigInteger.Multiply(new BigInteger(coinsCollected), BigInteger.Pow(10, 18)); - Debug.Log($"Quantity: {quantity}"); - var nvc = new List> + int coinsCollected = GetNumCoinsCollected(); // Get number of coins collected + if (coinsCollected == 0) // Don't mint any coins if player did not collect any + { + return true; + } + + string address = await GetWalletAddress(); // Get the player's wallet address to mint the coins to + if (address != null) + { + // Calculate the quantity to mint + // Need to take into account Immutable Runner Token decimal value i.e. 18 + BigInteger quantity = BigInteger.Multiply(new BigInteger(coinsCollected), BigInteger.Pow(10, 18)); + Debug.Log($"Quantity: {quantity}"); + var nvc = new List> { // Set 'to' to the player's wallet address new KeyValuePair("to", address), // Set 'quanity' to the number of coins collected new KeyValuePair("quantity", quantity.ToString()) }; - using var client = new HttpClient(); - string url = $"http://localhost:3000/mint/token"; // Endpoint to mint token - using var req = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(nvc) }; - using var res = await client.SendAsync(req); - return res.IsSuccessStatusCode; - } + using var client = new HttpClient(); + string url = $"http://localhost:3000/mint/token"; // Endpoint to mint token + using var req = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(nvc) }; + using var res = await client.SendAsync(req); + return res.IsSuccessStatusCode; + } - return false; + return false; + } + catch (Exception ex) + { + Debug.Log($"Failed to mint coins: {ex.Message}"); + return false; + } } private void OnNextButtonClicked()