Skip to content

Commit

Permalink
feat: mint coins for each level
Browse files Browse the repository at this point in the history
  • Loading branch information
nattb8 committed May 29, 2024
1 parent d468f72 commit 542ea6e
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 37 deletions.
83 changes: 78 additions & 5 deletions Assets/Shared/Scripts/UI/LevelCompleteScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}

/// <summary>
/// Mints collected coins (i.e. Immutable Runner Token) to the player's wallet
/// </summary>
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<string> 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<KeyValuePair<string, string>>
{
// Set 'to' to the player's wallet address
new KeyValuePair<string, string>("to", address),
// Set 'quanity' to the number of coins collected
new KeyValuePair<string, string>("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()
Expand Down Expand Up @@ -156,8 +228,9 @@ private async void OnContinueWithPassportButtonClicked()
}
}

private void OnTryAgainButtonClicked()
private async void OnTryAgainButtonClicked()
{
await MintCoins();
}

private void OnNextButtonClicked()
Expand Down
81 changes: 49 additions & 32 deletions Assets/Shared/Scripts/UI/MintScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -119,24 +119,31 @@ private async UniTask<string> GetWalletAddress()
private async UniTask<bool> 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<KeyValuePair<string, string>>
string address = await GetWalletAddress(); // Get the player's wallet address to mint the fox to

if (address != null)
{
var nvc = new List<KeyValuePair<string, string>>
{
// Set 'to' to the player's wallet address
new KeyValuePair<string, string>("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;
}
}

/// <summary>
Expand All @@ -146,32 +153,42 @@ private async UniTask<bool> MintFox()
private async UniTask<bool> 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<KeyValuePair<string, string>>
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<KeyValuePair<string, string>>
{
// Set 'to' to the player's wallet address
new KeyValuePair<string, string>("to", address),
// Set 'quanity' to the number of coins collected
new KeyValuePair<string, string>("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()
Expand Down

0 comments on commit 542ea6e

Please sign in to comment.