Skip to content

Commit e951be4

Browse files
committed
AccountView: show ens name and balance
1 parent 416b5c4 commit e951be4

30 files changed

Lines changed: 515 additions & 47 deletions

Packages/com.walletconnect.web3modal/Resources/WalletConnect/Web3Modal/Views/AccountView.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#account-view__profile{
2+
display: flex;
3+
flex-direction: column;
4+
align-items: center;
5+
justify-content: center;
6+
margin-bottom: 20px;
7+
padding-top: 40px;
8+
}
9+
10+
#account-view__profile-address {
11+
margin-top: 20px;
12+
padding: 0;
13+
-unity-text-align: middle-center;
14+
color: var(--wui-color-fg-100);
15+
font-size: var(--wui-font-size-medium-title);
16+
}
17+
18+
#account-view__profile-balance-container {
19+
flex-direction: row;
20+
}
21+
22+
#account-view__profile-balance-container > Label {
23+
margin-top: 2px;
24+
padding: 0;
25+
-unity-text-align: middle-center;
26+
color: var(--wui-color-fg-200);
27+
font-size: var(--wui-font-size-paragraph);
28+
}

Packages/com.walletconnect.web3modal/Resources/WalletConnect/Web3Modal/Views/AccountView/AccountView.uss.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False">
2+
<Style src="AccountView.uss"/>
3+
<ui:VisualElement name="account-view__profile">
4+
<ui:Label name="account-view__profile-address"/>
5+
<ui:VisualElement name="account-view__profile-balance-container">
6+
<ui:Label name="account-view__profile-balance-value"/>
7+
<ui:Label name="account-view__profile-balance-symbol"/>
8+
</ui:VisualElement>
9+
</ui:VisualElement>
10+
<ui:VisualElement name="account-view__buttons"/>
11+
</ui:UXML>

Packages/com.walletconnect.web3modal/Resources/WalletConnect/Web3Modal/Views/AccountView/AccountView.uxml.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.walletconnect.web3modal/Runtime/Connectors/WalletConnect/WalletConnectConnector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ protected override async Task ChangeActiveChainAsyncCore(Chain chain)
140140

141141
await WalletConnectInstance.SignClient.AddressProvider.SetDefaultChainIdAsync(chain.ChainId);
142142
OnChainChanged(new ChainChangedEventArgs(chain.ChainId));
143+
OnAccountChanged(new AccountChangedEventArgs(GetCurrentAccount()));
143144
}
144145
}
145146

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Linq;
5+
using System.Runtime.CompilerServices;
6+
using System.Threading.Tasks;
7+
using UnityEngine;
8+
using WalletConnect.Web3Modal.Utils;
9+
10+
namespace WalletConnect.Web3Modal
11+
{
12+
public class AccountController : INotifyPropertyChanged
13+
{
14+
public bool IsInitialized { get; set; }
15+
16+
public bool IsConnected
17+
{
18+
get => _connectorController.IsAccountConnected;
19+
}
20+
21+
public string Address
22+
{
23+
get => _address;
24+
set => SetField(ref _address, value);
25+
}
26+
27+
public string AccountId
28+
{
29+
get => _accountId;
30+
set => SetField(ref _accountId, value);
31+
}
32+
33+
public string ChainId
34+
{
35+
get => _chainId;
36+
set => SetField(ref _chainId, value);
37+
}
38+
39+
public string ProfileName
40+
{
41+
get => _profileName;
42+
set => SetField(ref _profileName, value);
43+
}
44+
45+
public string ProfileAvatar
46+
{
47+
get => _profileAvatar;
48+
set => SetField(ref _profileAvatar, value);
49+
}
50+
51+
public string Balance
52+
{
53+
get => _balance;
54+
set => SetField(ref _balance, value);
55+
}
56+
57+
public string BalanceSymbol
58+
{
59+
get => _balanceSymbol;
60+
set => SetField(ref _balanceSymbol, value);
61+
}
62+
63+
private ConnectorController _connectorController;
64+
private NetworkController _networkController;
65+
private BlockchainApiController _blockchainApiController;
66+
67+
private string _address;
68+
private string _accountId;
69+
private string _chainId;
70+
71+
private string _profileName;
72+
private string _profileAvatar;
73+
74+
private string _balance;
75+
private string _balanceSymbol;
76+
77+
public event PropertyChangedEventHandler PropertyChanged;
78+
79+
public async Task InitializeAsync(ConnectorController connectorController, NetworkController networkController, BlockchainApiController blockchainApiController)
80+
{
81+
if (IsInitialized)
82+
throw new Exception("Already initialized"); // TODO: use custom ex type
83+
84+
_connectorController = connectorController ?? throw new ArgumentNullException(nameof(connectorController));
85+
_networkController = networkController ?? throw new ArgumentNullException(nameof(networkController));
86+
_blockchainApiController = blockchainApiController ?? throw new ArgumentNullException(nameof(blockchainApiController));
87+
88+
_connectorController.AccountConnected += ConnectorAccountConnectedHandler;
89+
_connectorController.AccountChanged += ConnectorAccountChangedHandler;
90+
}
91+
92+
private async void ConnectorAccountConnectedHandler(object sender, Connector.AccountConnectedEventArgs e)
93+
{
94+
var account = await e.GetAccount();
95+
96+
if (account.AccountId == AccountId)
97+
return;
98+
99+
Address = account.Address;
100+
AccountId = account.AccountId;
101+
ChainId = account.ChainId;
102+
103+
await Task.WhenAll(
104+
UpdateProfile(),
105+
UpdateBalance()
106+
);
107+
}
108+
109+
private async void ConnectorAccountChangedHandler(object sender, Connector.AccountChangedEventArgs e)
110+
{
111+
Debug.Log("ConnectorAccountChangedHandler");
112+
if (e.Account.Address != Address)
113+
{
114+
Address = e.Account.Address;
115+
await UpdateProfile();
116+
}
117+
118+
AccountId = e.Account.AccountId;
119+
ChainId = e.Account.ChainId;
120+
121+
await UpdateBalance();
122+
}
123+
124+
public async Task UpdateProfile()
125+
{
126+
var identity = await _blockchainApiController.GetIdentityAsync(Address);
127+
ProfileName = string.IsNullOrWhiteSpace(identity.Name)
128+
? Address.Truncate()
129+
: identity.Name;
130+
ProfileAvatar = identity.Avatar;
131+
}
132+
133+
public async Task UpdateBalance()
134+
{
135+
var response = await _blockchainApiController.GetBalanceAsync(Address);
136+
var balance = response.Balances.FirstOrDefault(x => x.chainId == ChainId && string.IsNullOrWhiteSpace(x.address));
137+
138+
if (string.IsNullOrWhiteSpace(balance.quantity.numeric))
139+
{
140+
Balance = "0.000";
141+
BalanceSymbol = _networkController.ActiveChain.NativeCurrency.symbol;
142+
}
143+
else
144+
{
145+
Balance = balance.quantity.numeric;
146+
BalanceSymbol = balance.symbol;
147+
}
148+
}
149+
150+
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
151+
{
152+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
153+
}
154+
155+
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
156+
{
157+
if (EqualityComparer<T>.Default.Equals(field, value))
158+
return false;
159+
field = value;
160+
OnPropertyChanged(propertyName);
161+
return true;
162+
}
163+
}
164+
}

Packages/com.walletconnect.web3modal/Runtime/Controllers/AccountController.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.walletconnect.web3modal/Runtime/Controllers/ApiController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ namespace WalletConnect.Web3Modal
1010
{
1111
public class ApiController
1212
{
13+
private const string BasePath = "https://api.web3modal.com/";
14+
private const int TimoutSeconds = 5;
15+
1316
private readonly string _includedWalletIdsString = Web3Modal.Config.includedWalletIds is { Length: > 0 }
1417
? string.Join(",", Web3Modal.Config.includedWalletIds)
1518
: null;
@@ -20,9 +23,6 @@ public class ApiController
2023
private readonly UnityHttpClient _httpClient = new(new Uri(BasePath), TimeSpan.FromSeconds(TimoutSeconds),
2124
new Web3ModalApiHeaderDecorator()
2225
);
23-
24-
private const string BasePath = "https://api.web3modal.com/";
25-
private const int TimoutSeconds = 5;
2626

2727
private const string Platform =
2828
#if UNITY_ANDROID
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using UnityEngine;
5+
using WalletConnect.Web3Modal.Http;
6+
using WalletConnect.Web3Modal.Model.BlockchainApi;
7+
using WalletConnectUnity.Core;
8+
9+
namespace WalletConnect.Web3Modal
10+
{
11+
public class BlockchainApiController
12+
{
13+
private const string BasePath = "https://rpc.walletconnect.org/v1/";
14+
private const int TimoutSeconds = 5;
15+
16+
private readonly UnityHttpClient _httpClient = new(new Uri(BasePath), TimeSpan.FromSeconds(TimoutSeconds));
17+
18+
private readonly IDictionary<string, string> _getBalanceHeaders = new Dictionary<string, string>
19+
{
20+
{"x-sdk-version", SdkMetadata.Version}
21+
};
22+
23+
public async Task<GetIdentityResponse> GetIdentityAsync(string address)
24+
{
25+
Debug.Log("GetIdentityAsync");
26+
var projectId = ProjectConfiguration.Load().Id;
27+
return await _httpClient.GetAsync<GetIdentityResponse>($"identity/{address}?projectId={projectId}");
28+
}
29+
30+
public async Task<GetBalanceResponse> GetBalanceAsync(string address)
31+
{
32+
Debug.Log("GetBalanceAsync");
33+
var projectId = ProjectConfiguration.Load().Id;
34+
return await _httpClient.GetAsync<GetBalanceResponse>($"account/{address}/balance?projectId={projectId}&currency=usd", headers: _getBalanceHeaders);
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)