Skip to content

Commit

Permalink
feat: mint fox
Browse files Browse the repository at this point in the history
  • Loading branch information
nattb8 committed May 29, 2024
1 parent 6c8ace0 commit 7de7830
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
51 changes: 47 additions & 4 deletions Assets/Shared/Scripts/UI/MintScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Net.Http;
using Immutable.Passport;
using Cysharp.Threading.Tasks;

namespace HyperCasual.Runner
{
Expand Down Expand Up @@ -50,7 +53,7 @@ public void OnEnable()
Mint();
}

private void Mint()
private async void Mint()
{
try
{
Expand All @@ -60,11 +63,15 @@ private void Mint()
ShowNextButton(false);

// Mint
bool mintedFox = await MintFox();

ShowMintedMessage();
if (mintedFox)
{
ShowMintedMessage();
}
ShowLoading(false);
ShowError(false);
ShowNextButton(true);
ShowError(!mintedFox);
ShowNextButton(mintedFox);
}
catch (Exception ex)
{
Expand All @@ -76,6 +83,42 @@ private void Mint()
}
}

/// <summary>
/// Gets the wallet address of the player.
/// </summary>
private async UniTask<string> GetWalletAddress()
{
List<string> accounts = await Passport.Instance.ZkEvmRequestAccounts();
return accounts[0]; // Get the first wallet address
}

/// <summary>
/// Mints a fox (i.e. Immutable Runner Fox) to the player's wallet
/// </summary>
/// <returns>True if minted a fox successfully to player's wallet. Otherwise, false.</returns>
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)
{
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 a 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;
}

private void OnNextButtonClicked()
{
m_NextEvent.Raise();
Expand Down
2 changes: 1 addition & 1 deletion Assets/Shared/Scripts/UI/SetupWalletScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private async void SetupWallet()

private void OnNextButtonClicked()
{
m_NextEvent.Raise();
m_MintEvent.Raise();
}

private void ShowNextButton(bool show)
Expand Down

0 comments on commit 7de7830

Please sign in to comment.