Skip to content

Commit db254fb

Browse files
committed
fee options window check if wallet has the required balance
1 parent bc2160d commit db254fb

6 files changed

Lines changed: 46 additions & 10 deletions

File tree

Packages/Sequence-Unity/Sequence/Samples~/Demo/Resources/Prefabs/EcosystemWallet/Transactions/EcosystemWalletTransactions.prefab

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,6 +2396,7 @@ MonoBehaviour:
23962396
_useFeeOptions: 1
23972397
_feeOptionAddress:
23982398
- 0xABAAd93EeE2a569cF0632f39B10A9f5D734777ca
2399+
- 0xaf88d065e77c8cC2239327C5EDb3A432268e5831
23992400
_transaction:
24002401
<To>k__BackingField: 0x33985d320809E26274a72E03268c8a29927Bc6dA
24012402
<FunctionSelector>k__BackingField: explicitEmit()

Packages/Sequence-Unity/Sequence/Samples~/Demo/Resources/Prefabs/EcosystemWallet/Transactions/FeeOptionTile.prefab

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ MonoBehaviour:
5757
_logoImage: {fileID: 288285889312663865}
5858
_nameText: {fileID: 4274670708990135750}
5959
_valueText: {fileID: 6114889452991144139}
60+
_outOfFundsColor: {r: 0.7735849, g: 0.09852261, b: 0.09852261, a: 1}
6061
--- !u!114 &300926986013208494
6162
MonoBehaviour:
6263
m_ObjectHideFlags: 0
@@ -334,9 +335,9 @@ MonoBehaviour:
334335
m_lineSpacingMax: 0
335336
m_paragraphSpacing: 0
336337
m_charWidthMaxAdj: 0
337-
m_enableWordWrapping: 1
338+
m_enableWordWrapping: 0
338339
m_wordWrappingRatios: 0.4
339-
m_overflowMode: 0
340+
m_overflowMode: 1
340341
m_linkedTextComponent: {fileID: 0}
341342
parentLinkedComponent: {fileID: 0}
342343
m_enableKerning: 1

Packages/Sequence-Unity/Sequence/SequenceBoilerplates/Scripts/BoilerplateFactory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Numerics;
34
using Sequence.Boilerplates.DailyRewards;
45
using Sequence.Boilerplates.InGameShop;
56
using Sequence.Boilerplates.Inventory;
@@ -66,10 +67,10 @@ public static EcosystemWalletTransactions OpenEcosystemWalletTransactions(Transf
6667
b => b.Show(wallet, onClose));
6768
}
6869

69-
public static FeeOptionWindow OpenFeeOptionSelection(Transform parent, Sequence.Relayer.FeeOption[] feeOptions, Action<Sequence.Relayer.FeeOption> onSelected)
70+
public static FeeOptionWindow OpenFeeOptionSelection(Transform parent, Address walletAddress, Sequence.Relayer.FeeOption[] feeOptions, Action<Sequence.Relayer.FeeOption> onSelected)
7071
{
7172
return GetOrSpawnBoilerplate<FeeOptionWindow>("EcosystemWallet/Transactions/FeeOptionsWindow", parent,
72-
b => b.WaitForSelection(feeOptions, onSelected));
73+
b => b.WaitForSelection(walletAddress, feeOptions, onSelected));
7374
}
7475

7576
/// <summary>

Packages/Sequence-Unity/Sequence/SequenceBoilerplates/Scripts/EcosystemWallet/FeeOptionTile.cs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,55 @@ public class FeeOptionTile : MonoBehaviour
1313
[SerializeField] private Image _logoImage;
1414
[SerializeField] private TMP_Text _nameText;
1515
[SerializeField] private TMP_Text _valueText;
16+
[SerializeField] private Color _outOfFundsColor = Color.red;
1617

18+
private bool _allowSelect;
1719
private FeeOption _feeOption;
1820
private Action<FeeOption> _onSelected;
1921

20-
public async void Load(FeeOption feeOption, Action<FeeOption> onSelected)
22+
public async void Load(Address walletAddress, FeeOption feeOption, Action<FeeOption> onSelected)
2123
{
2224
_feeOption = feeOption;
2325
_onSelected = onSelected;
2426
_nameText.text = $"{feeOption.token.name} ({feeOption.token.symbol})";
25-
_valueText.text = DecimalNormalizer.ReturnToNormalPrecise(BigInteger.Parse(feeOption.value), feeOption.token.decimals).ToString();
27+
_valueText.text = "...";
28+
_valueText.color = Color.white;
2629
_logoImage.sprite = await AssetHandler.GetSpriteAsync(feeOption.token.logoURL);
30+
_allowSelect = false;
31+
32+
var indexer = new ChainIndexer(feeOption.token.chainId);
33+
if (feeOption.token.contractAddress == null)
34+
{
35+
var nativeResponse = await indexer.GetNativeTokenBalance(walletAddress);
36+
FormatFeeBalanceText(nativeResponse.balanceWei, feeOption);
37+
}
38+
else
39+
{
40+
var balancesResponse = await indexer.GetTokenBalances(new GetTokenBalancesArgs(walletAddress, feeOption.token.contractAddress));
41+
var balance = balancesResponse.balances.Length == 0 ? 0 : balancesResponse.balances[0].balance;
42+
FormatFeeBalanceText(balance, feeOption);
43+
}
2744
}
2845

2946
public void Select()
3047
{
31-
_onSelected?.Invoke(_feeOption);
48+
if (_allowSelect)
49+
_onSelected?.Invoke(_feeOption);
50+
}
51+
52+
private void FormatFeeBalanceText(BigInteger balance, FeeOption feeOption)
53+
{
54+
var currentBalance = DecimalNormalizer
55+
.ReturnToNormalPrecise(balance, feeOption.token.decimals).ToString();
56+
57+
var requiredBalance = DecimalNormalizer
58+
.ReturnToNormalPrecise(BigInteger.Parse(feeOption.value), feeOption.token.decimals).ToString();
59+
60+
var hasRequiredBalance = balance >= BigInteger.Parse(feeOption.value);
61+
_allowSelect = hasRequiredBalance;
62+
63+
_valueText.color = hasRequiredBalance ? Color.white : _outOfFundsColor;
64+
_valueText.text = $"{requiredBalance} (You own: {currentBalance})";
3265
}
3366
}
3467
}

Packages/Sequence-Unity/Sequence/SequenceBoilerplates/Scripts/EcosystemWallet/FeeOptionWindow.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ public class FeeOptionWindow : MonoBehaviour
1010

1111
private Action<FeeOption> _onSelected;
1212

13-
public void WaitForSelection(FeeOption[] feeOptions, Action<FeeOption> onSelected)
13+
public void WaitForSelection(Address walletAddress, FeeOption[] feeOptions, Action<FeeOption> onSelected)
1414
{
1515
gameObject.SetActive(true);
1616
_onSelected = onSelected;
1717

1818
_tilePool.Cleanup();
1919
foreach (var feeOption in feeOptions)
20-
_tilePool.GetObject().Load(feeOption, SelectFee);
20+
_tilePool.GetObject().Load(walletAddress, feeOption, SelectFee);
2121
}
2222

2323
public void Close()

Packages/Sequence-Unity/Sequence/SequenceBoilerplates/Scripts/EcosystemWallet/TransactionButton.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public async void SendTransaction()
9696
try
9797
{
9898
var feeOptions = await _wallet.GetFeeOption(_chain, transaction);
99-
BoilerplateFactory.OpenFeeOptionSelection(_transactionsWindow.transform.parent, feeOptions, async feeOption =>
99+
BoilerplateFactory.OpenFeeOptionSelection(_transactionsWindow.transform.parent, _wallet.Address, feeOptions, async feeOption =>
100100
{
101101
if (feeOption != null)
102102
await TrySendTransaction(transaction, feeOption);

0 commit comments

Comments
 (0)