Skip to content

Commit

Permalink
feat: show USDC balance, update swap URL
Browse files Browse the repository at this point in the history
  • Loading branch information
nattb8 committed Jan 8, 2025
1 parent 05995fa commit 775d8d5
Show file tree
Hide file tree
Showing 14 changed files with 1,482 additions and 113 deletions.
476 changes: 475 additions & 1 deletion Assets/Shared/Prefabs/UI/Balance.prefab

Large diffs are not rendered by default.

895 changes: 802 additions & 93 deletions Assets/Shared/Prefabs/UI/Inventory/InventoryAssetDetailsView.prefab

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions Assets/Shared/Prefabs/UI/Inventory/InventoryScreen.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 4986908211239487415}
m_HandleRect: {fileID: 2602276242752267194}
m_Direction: 2
m_Value: 1
m_Size: 0.7685185
m_Value: 0
m_Size: 1
m_NumberOfSteps: 0
m_OnValueChanged:
m_PersistentCalls:
Expand Down Expand Up @@ -2849,13 +2849,11 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
m_BackButton: {fileID: 608994780706620231}
m_AddButton: {fileID: 0}
m_BackEvent: {fileID: 11400000, guid: 58f804667e2cb4eb789b6d2dfb3c4ce3, type: 2}
m_Balance: {fileID: 7413814428584860172}
m_TypeDropdown: {fileID: 3902948968200711973}
m_InventoryObj: {fileID: 9215591978314449373}
m_ScrollView: {fileID: 6011150636714096262}
m_AddFunds: {fileID: 278197972233077694}
--- !u!1 &7652146041976592896
GameObject:
m_ObjectHideFlags: 0
Expand Down
119 changes: 116 additions & 3 deletions Assets/Shared/Prefabs/UI/Marketplace/MarketplaceScreen.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ MonoBehaviour:
m_HandleRect: {fileID: 2602276242752267194}
m_Direction: 2
m_Value: 1
m_Size: 0.7685185
m_Size: 0.9259259
m_NumberOfSteps: 0
m_OnValueChanged:
m_PersistentCalls:
Expand Down Expand Up @@ -4027,13 +4027,11 @@ MonoBehaviour:
m_EditorClassIdentifier:
m_BackButton: {fileID: 608994780706620231}
m_BackEvent: {fileID: 11400000, guid: 58f804667e2cb4eb789b6d2dfb3c4ce3, type: 2}
m_AddButton: {fileID: 0}
m_Balance: {fileID: 2947439975678462892}
m_ColoursDropdown: {fileID: 8004575839892644515}
m_SpeedDropdown: {fileID: 2828885607642083255}
m_ScrollView: {fileID: 2120143446010795323}
m_StackObj: {fileID: 6351333064208322446}
m_AddFunds: {fileID: 4985113869925412450}
m_CustomDialog: {fileID: 1192901970226320393}
--- !u!1 &7518897395523842868
GameObject:
Expand Down Expand Up @@ -6152,6 +6150,61 @@ PrefabInstance:
propertyPath: m_AnchoredPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1717019873544408673, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchorMax.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1717019873544408673, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchorMin.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1717019873544408673, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_SizeDelta.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1717019873544408673, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_SizeDelta.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1717019873544408673, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchoredPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1717019873544408673, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchoredPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1781770977802883007, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchorMax.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1781770977802883007, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchorMin.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1781770977802883007, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_SizeDelta.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1781770977802883007, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_SizeDelta.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1781770977802883007, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchoredPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1955016790985467476, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_Material
Expand Down Expand Up @@ -6467,6 +6520,36 @@ PrefabInstance:
propertyPath: m_AddFunds
value:
objectReference: {fileID: 4985113869925412450}
- target: {fileID: 5271269552739479919, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchorMax.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5271269552739479919, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchorMin.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5271269552739479919, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_SizeDelta.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5271269552739479919, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_SizeDelta.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5271269552739479919, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchoredPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5271269552739479919, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchoredPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6693625691478856431, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchorMax.y
Expand Down Expand Up @@ -6537,6 +6620,36 @@ PrefabInstance:
propertyPath: m_Material
value:
objectReference: {fileID: 0}
- target: {fileID: 8452721319882028120, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchorMax.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8452721319882028120, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchorMin.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8452721319882028120, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_SizeDelta.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8452721319882028120, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_SizeDelta.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8452721319882028120, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchoredPosition.x
value: 108
objectReference: {fileID: 0}
- target: {fileID: 8452721319882028120, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchoredPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8607960660092029228, guid: df2ad67e481ca43feb8579d2885cb381,
type: 3}
propertyPath: m_AnchorMax.y
Expand Down
21 changes: 21 additions & 0 deletions Assets/Shared/Scripts/Domain/GetTokenBalanceUseCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Immutable.Orderbook.Api;
using Immutable.Orderbook.Client;
using Immutable.Passport;
using Nethereum.Web3;
using UnityEngine;

namespace HyperCasual.Runner
Expand Down Expand Up @@ -49,5 +50,25 @@ public async UniTask<string> GetImxBalance()

return roundedValue.ToString("F2");
}

/// <summary>
/// Gets the user's USDC balance
/// </summary>
public async UniTask<string> GetUsdcBalance()
{
var web3 = new Web3(Config.RPC_URL);

var abi = @"[{'constant':true,'inputs':[{'name':'account','type':'address'}],'name':'balanceOf','outputs':[{'name':'','type':'uint256'}],'payable':false,'stateMutability':'view','type':'function'}]";

var contract = web3.Eth.GetContract(abi, Contract.USDC);

var balanceOfFunction = contract.GetFunction("balanceOf");

var balance = await balanceOfFunction.CallAsync<BigInteger>(SaveManager.Instance.WalletAddress);

var quantity = Web3.Convert.FromWei(balance, 6);

return quantity.ToString("F2");
}
}
}
7 changes: 5 additions & 2 deletions Assets/Shared/Scripts/Model/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class Config

public const string CLIENT_ID = "mp6rxfMDwwZDogcdgNrAaHnG0qMlXuMK"; // Testnet
// public const string CLIENT_ID = "UnB98ngnXIZIEJWGJOjVe1BpCx5ix7qc"; // WebGL

public const string ENVIRONMENT = "sandbox";

#if (UNITY_ANDROID && !UNITY_EDITOR_WIN) || (UNITY_IPHONE && !UNITY_EDITOR_WIN) || UNITY_STANDALONE_OSX
Expand All @@ -33,13 +33,16 @@ public static class Config

public const string CHAIN_NAME = "imtbl-zkevm-testnet";
public const string BASE_URL = "https://api.sandbox.immutable.com";
public const string RPC_URL = "https://rpc.testnet.immutable.com";
public const int PAGE_SIZE = 20;
}

public static class Contract
{
public const string SKIN = "0xc8df1b1693e2beffd2e484a825a357c6a3d998f2"; // Testnet team
public const string TOKEN = "0xb237501b35dfdcad274299236a141425469ab9ba"; // Testnet team
public const string PACK = "0x8525b5e782f3fbe6460057460be020146b63ed0f"; // Testnet team\
public const string PACK = "0x8525b5e782f3fbe6460057460be020146b63ed0f"; // Testnet team

public const string USDC = "0x3B2d8A1931736Fc321C24864BceEe981B11c3c57"; // Transak Testnet USDC
}
}
15 changes: 8 additions & 7 deletions Assets/Shared/Scripts/UI/AddFunds/AddFunds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ public void Show(Action onDismiss)

private async void OnFiatButtonClicked()
{
string environment = Immutable.Passport.Model.Environment.SANDBOX;
string email = await Passport.Instance.GetEmail();
List<string> walletAddress = await Passport.Instance.ZkEvmRequestAccounts();
OnRamp onRamp = new OnRamp(environment, email, walletAddress.FirstOrDefault());
string link = await onRamp.GetLink();
const string environment = Immutable.Passport.Model.Environment.SANDBOX;
var email = await Passport.Instance.GetEmail();
var walletAddress = await Passport.Instance.ZkEvmRequestAccounts();

var onRamp = new OnRamp(environment, email, walletAddress.FirstOrDefault());
var link = await onRamp.GetLink();
Debug.Log($"onRamp.GetOnRampLink: {link}");

m_TransakView.Show($"https://global-stg.transak.com/?apiKey=d14b44fb-0f84-4db5-affb-e044040d724b&network=immutablezkevm&defaultPaymentMethod=credit_debit_card&disablePaymentMethods=&productsAvailed=buy&exchangeScreenTitle=Buy&themeColor=0D0D0D&defaultCryptoCurrency=IMX&defaultFiatAmount=50&defaultFiatCurrency=usd&walletAddress={walletAddress.FirstOrDefault()}&cryptoCurrencyList=imx%2Ceth%2Cusdc", () =>
m_TransakView.Show(link, () =>
{
m_Options.gameObject.SetActive(true);
m_TransakView.gameObject.SetActive(false);
Expand All @@ -58,7 +59,7 @@ private async void OnFiatButtonClicked()

private void OnTokenButtonClicked()
{
Application.OpenURL($"https://checkout-playground.sandbox.immutable.com/embedded/add-funds/?publishableKey=pk_imapik-test-Xdera%40&passportClientId={Config.CLIENT_ID}&redirectUri={Config.REDIRECT_URI}");
Application.OpenURL($"https://checkout-playground.sandbox.immutable.com/checkout/swap/?publishableKey=pk_imapik-test-7-hfC5T$W$eEDE8Mc5mp&fromTokenAddress={Contract.USDC}&toTokenAddress={Contract.TOKEN}");
}

private void OnCancelButtonClicked()
Expand Down
23 changes: 22 additions & 1 deletion Assets/Shared/Scripts/UI/BalanceObject.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable
using System;
using System.Linq;
using Cysharp.Threading.Tasks;
using TMPro;
using UnityEngine;
Expand All @@ -15,6 +16,7 @@ public class BalanceObject : MonoBehaviour
[SerializeField] private GameObject m_Panel;
[SerializeField] private TextMeshProUGUI m_ImrValue;
[SerializeField] private TextMeshProUGUI m_ImxValue;
[SerializeField] private TextMeshProUGUI m_UsdcValue;

[SerializeField] private HyperCasualButton m_AddFundsButton;
[SerializeField] private AddFunds m_AddFunds;
Expand All @@ -23,11 +25,19 @@ public class BalanceObject : MonoBehaviour

private void Start()
{
Debug.Log("Balance object start");
gameObject.SetActive(false);
m_Panel.SetActive(false);

// Cache root Canvas
m_RootCanvas = FindObjectOfType<Canvas>();
var rootCanvas = FindObjectsOfType<Canvas>().FirstOrDefault(c => c.name == "Canvas");
if (rootCanvas == null)
{
gameObject.SetActive(false);
return;
}

m_RootCanvas = rootCanvas;

// Wallet address
var address = SaveManager.Instance.WalletAddress;
Expand Down Expand Up @@ -59,6 +69,11 @@ private void TogglePanel()
m_Panel.SetActive(false);
return;
}

// Update balance
#pragma warning disable CS4014
UpdateBalance();
#pragma warning restore CS4014

// Position the panel
m_Panel.transform.SetParent(m_RootCanvas.transform, false);
Expand All @@ -82,6 +97,8 @@ private void ShowAddFunds()
#pragma warning disable CS4014
m_AddFunds.Show(() => UpdateBalance());
#pragma warning restore CS4014

TogglePanel();
}

/// <summary>
Expand All @@ -101,6 +118,10 @@ private void ShowAddFunds()
// IMX balance
var imxBalance = await GetTokenBalanceUseCase.Instance.GetImxBalance();
m_ImxValue.text = imxBalance;

// USDC balance
var usdcBalance = await GetTokenBalanceUseCase.Instance.GetUsdcBalance();
m_UsdcValue.text = usdcBalance;

return imrBalance == "0.0" ? "0" : imrBalance;
}
Expand Down
2 changes: 2 additions & 0 deletions Assets/Shared/Scripts/UI/Inventory/InventoryScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ private void OnCreateItemView(int index, GameObject item)
clickable.ClearAllSubscribers();
clickable.OnClick += () =>
{
m_Balance.ClosePanel();

var view = UIManager.Instance.GetView<InventoryAssetDetailsView>();
UIManager.Instance.Show(view);
view.Initialise(asset);
Expand Down
2 changes: 2 additions & 0 deletions Assets/Shared/Scripts/UI/Marketplace/MarketplaceScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ private void OnCreateItemView(int index, GameObject item)
clickable.ClearAllSubscribers();
clickable.OnClick += () =>
{
m_Balance.ClosePanel();

var view = UIManager.Instance.GetView<MarketplaceAssetDetailsView>();
UIManager.Instance.Show(view);
view.Initialise(stack);
Expand Down
2 changes: 2 additions & 0 deletions Assets/Shared/Scripts/UI/Shop/ShopScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ private void OnCreateItemView(int index, GameObject item)
clickable.ClearAllSubscribers();
clickable.OnClick += () =>
{
m_Balance.ClosePanel();

var view = UIManager.Instance.GetView<PackDetailsView>();
UIManager.Instance.Show(view);
view.Initialise(pack);
Expand Down
10 changes: 9 additions & 1 deletion Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"com.unity.modules.video": "1.0.0",
"com.unity.modules.vr": "1.0.0",
"com.unity.modules.wind": "1.0.0",
"com.unity.modules.xr": "1.0.0"
"com.unity.modules.xr": "1.0.0",
"com.nethereum.unity": "4.19.2"
},
"scopedRegistries": [
{
Expand All @@ -60,6 +61,13 @@
"scopes": [
"com.unity.uiextensions"
]
},
{
"name": "package.openupm.com",
"url": "https://package.openupm.com",
"scopes": [
"com.nethereum.unity"
]
}
]
}
Loading

0 comments on commit 775d8d5

Please sign in to comment.