Skip to content

Commit 8f28543

Browse files
committed
Fix AccountController causing issues on WebGL
1 parent 324894a commit 8f28543

4 files changed

Lines changed: 31 additions & 45 deletions

File tree

Packages/com.walletconnect.web3modal/Plugins/Web3Modal.jslib

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ mergeInto(LibraryManager.library, {
3838
let result = await callFn(_web3ModalConfig, methodName, parameterObj);
3939

4040
if (result === undefined || result === null) {
41-
const error = new Error("Result is null or undefined");
42-
let errorJson = JSON.stringify(error, ['name', 'message']);
43-
let errorStrPtr = stringToNewUTF8(errorJson);
44-
{{{makeDynCall('viii', 'callbackPtr')}}} (id, undefined, errorStrPtr);
41+
{{{makeDynCall('viii', 'callbackPtr')}}} (id, undefined, undefined);
4542
return;
4643
}
4744

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public string BalanceSymbol
6565
private NetworkController _networkController;
6666
private BlockchainApiController _blockchainApiController;
6767

68-
private UnityHttpClient _httpClient = new();
68+
private readonly UnityHttpClient _httpClient = new();
6969

7070
private string _address;
7171
private string _accountId;
@@ -87,9 +87,11 @@ public async Task InitializeAsync(ConnectorController connectorController, Netwo
8787
_connectorController = connectorController ?? throw new ArgumentNullException(nameof(connectorController));
8888
_networkController = networkController ?? throw new ArgumentNullException(nameof(networkController));
8989
_blockchainApiController = blockchainApiController ?? throw new ArgumentNullException(nameof(blockchainApiController));
90-
90+
91+
#if !UNITY_WEBGL || UNITY_EDITOR
9192
_connectorController.AccountConnected += ConnectorAccountConnectedHandler;
9293
_connectorController.AccountChanged += ConnectorAccountChangedHandler;
94+
#endif
9395
}
9496

9597
private async void ConnectorAccountConnectedHandler(object sender, Connector.AccountConnectedEventArgs e)
@@ -124,6 +126,9 @@ await Task.WhenAll(
124126

125127
public async Task UpdateProfile()
126128
{
129+
if (string.IsNullOrWhiteSpace(Address))
130+
return;
131+
127132
var identity = await _blockchainApiController.GetIdentityAsync(Address);
128133
ProfileName = string.IsNullOrWhiteSpace(identity.Name)
129134
? Address.Truncate()
@@ -151,8 +156,19 @@ public async Task UpdateProfile()
151156

152157
public async Task UpdateBalance()
153158
{
159+
if (string.IsNullOrWhiteSpace(Address))
160+
return;
161+
154162
var response = await _blockchainApiController.GetBalanceAsync(Address);
155-
var balance = response.Balances.FirstOrDefault(x => x.chainId == ChainId && string.IsNullOrWhiteSpace(x.address));
163+
164+
if (response.Balances.Length == 0)
165+
{
166+
Balance = "0.000";
167+
BalanceSymbol = _networkController.ActiveChain.NativeCurrency.symbol;
168+
return;
169+
}
170+
171+
var balance = Array.Find(response.Balances,x => x.chainId == ChainId && string.IsNullOrWhiteSpace(x.address));
156172

157173
if (string.IsNullOrWhiteSpace(balance.quantity.numeric))
158174
{

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Threading.Tasks;
45
using UnityEngine;
56
using WalletConnect.Web3Modal.Http;
@@ -24,6 +25,9 @@ public class BlockchainApiController
2425

2526
public async Task<GetIdentityResponse> GetIdentityAsync(string address)
2627
{
28+
if (string.IsNullOrWhiteSpace(address))
29+
throw new ArgumentNullException(nameof(address));
30+
2731
var projectId = ProjectConfiguration.Load().Id;
2832
var path = $"identity/{address}?projectId={projectId}";
2933

@@ -45,6 +49,9 @@ public async Task<GetIdentityResponse> GetIdentityAsync(string address)
4549

4650
public async Task<GetBalanceResponse> GetBalanceAsync(string address)
4751
{
52+
if (string.IsNullOrWhiteSpace(address))
53+
throw new ArgumentNullException(nameof(address));
54+
4855
var projectId = ProjectConfiguration.Load().Id;
4956
return await _httpClient.GetAsync<GetBalanceResponse>($"account/{address}/balance?projectId={projectId}&currency=usd", headers: _getBalanceHeaders);
5057
}

Samples/W3M Basic Sample/Assets/Scripts/Dapp.cs

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public async void OnPersonalSignButton()
151151
{
152152
var account = await Web3Modal.GetAccountAsync();
153153

154-
const string message = "Hello from the service!";
154+
const string message = "Hello from Unity!";
155155
var signature = await Web3Modal.Evm.SignMessageAsync(message);
156156
var isValid = await Web3Modal.Evm.VerifyMessageSignatureAsync(account.Address, message, signature);
157157

@@ -305,42 +305,8 @@ private TypedData<Domain> GetMailTypedDefinition()
305305
};
306306
}
307307

308-
309-
public const string CryptoPunksAbi = @"[{""constant"":true,""inputs"":[],""name"":""name"",""outputs"":[{""name"":"""",""type"":""string""}],""payable"":false,""type"":""function""},
310-
{""constant"":true,""inputs"":[{""name"":"""",""type"":""uint256""}],""name"":""punksOfferedForSale"",""outputs"":[{""name"":""isForSale"",""type"":""bool""},{""name"":""punkIndex"",""type"":""uint256""},{""name"":""seller"",""type"":""address""},{""name"":""minValue"",""type"":""uint256""},{""name"":""onlySellTo"",""type"":""address""}],""payable"":false,""type"":""function""},
311-
{""constant"":false,""inputs"":[{""name"":""punkIndex"",""type"":""uint256""}],""name"":""enterBidForPunk"",""outputs"":[],""payable"":true,""type"":""function""},
312-
{""constant"":true,""inputs"":[],""name"":""totalSupply"",""outputs"":[{""name"":"""",""type"":""uint256""}],""payable"":false,""type"":""function""},
313-
{""constant"":false,""inputs"":[{""name"":""punkIndex"",""type"":""uint256""},{""name"":""minPrice"",""type"":""uint256""}],""name"":""acceptBidForPunk"",""outputs"":[],""payable"":false,""type"":""function""},
314-
{""constant"":true,""inputs"":[],""name"":""decimals"",""outputs"":[{""name"":"""",""type"":""uint8""}],""payable"":false,""type"":""function""},
315-
{""constant"":false,""inputs"":[{""name"":""addresses"",""type"":""address[]""},{""name"":""indices"",""type"":""uint256[]""}],""name"":""setInitialOwners"",""outputs"":[],""payable"":false,""type"":""function""},
316-
{""constant"":false,""inputs"":[],""name"":""withdraw"",""outputs"":[],""payable"":false,""type"":""function""},
317-
{""constant"":true,""inputs"":[],""name"":""imageHash"",""outputs"":[{""name"":"""",""type"":""string""}],""payable"":false,""type"":""function""},
318-
{""constant"":true,""inputs"":[],""name"":""nextPunkIndexToAssign"",""outputs"":[{""name"":"""",""type"":""uint256""}],""payable"":false,""type"":""function""},
319-
{""constant"":true,""inputs"":[{""name"":"""",""type"":""uint256""}],""name"":""punkIndexToAddress"",""outputs"":[{""name"":"""",""type"":""address""}],""payable"":false,""type"":""function""},
320-
{""constant"":true,""inputs"":[],""name"":""standard"",""outputs"":[{""name"":"""",""type"":""string""}],""payable"":false,""type"":""function""},
321-
{""constant"":true,""inputs"":[{""name"":"""",""type"":""uint256""}],""name"":""punkBids"",""outputs"":[{""name"":""hasBid"",""type"":""bool""},{""name"":""punkIndex"",""type"":""uint256""},{""name"":""bidder"",""type"":""address""},{""name"":""value"",""type"":""uint256""}],""payable"":false,""type"":""function""},
322-
{""constant"":true,""inputs"":[{""name"":"""",""type"":""address""}],""name"":""balanceOf"",""outputs"":[{""name"":"""",""type"":""uint256""}],""payable"":false,""type"":""function""},
323-
{""constant"":false,""inputs"":[],""name"":""allInitialOwnersAssigned"",""outputs"":[],""payable"":false,""type"":""function""},
324-
{""constant"":true,""inputs"":[],""name"":""allPunksAssigned"",""outputs"":[{""name"":"""",""type"":""bool""}],""payable"":false,""type"":""function""},
325-
{""constant"":false,""inputs"":[{""name"":""punkIndex"",""type"":""uint256""}],""name"":""buyPunk"",""outputs"":[],""payable"":true,""type"":""function""},
326-
{""constant"":false,""inputs"":[{""name"":""to"",""type"":""address""},{""name"":""punkIndex"",""type"":""uint256""}],""name"":""transferPunk"",""outputs"":[],""payable"":false,""type"":""function""},
327-
{""constant"":true,""inputs"":[],""name"":""symbol"",""outputs"":[{""name"":"""",""type"":""string""}],""payable"":false,""type"":""function""},
328-
{""constant"":false,""inputs"":[{""name"":""punkIndex"",""type"":""uint256""}],""name"":""withdrawBidForPunk"",""outputs"":[],""payable"":false,""type"":""function""},
329-
{""constant"":false,""inputs"":[{""name"":""to"",""type"":""address""},{""name"":""punkIndex"",""type"":""uint256""}],""name"":""setInitialOwner"",""outputs"":[],""payable"":false,""type"":""function""},
330-
{""constant"":false,""inputs"":[{""name"":""punkIndex"",""type"":""uint256""},{""name"":""minSalePriceInWei"",""type"":""uint256""},{""name"":""toAddress"",""type"":""address""}],""name"":""offerPunkForSaleToAddress"",""outputs"":[],""payable"":false,""type"":""function""},
331-
{""constant"":true,""inputs"":[],""name"":""punksRemainingToAssign"",""outputs"":[{""name"":"""",""type"":""uint256""}],""payable"":false,""type"":""function""},
332-
{""constant"":false,""inputs"":[{""name"":""punkIndex"",""type"":""uint256""},{""name"":""minSalePriceInWei"",""type"":""uint256""}],""name"":""offerPunkForSale"",""outputs"":[],""payable"":false,""type"":""function""},
333-
{""constant"":false,""inputs"":[{""name"":""punkIndex"",""type"":""uint256""}],""name"":""getPunk"",""outputs"":[],""payable"":false,""type"":""function""},
334-
{""constant"":true,""inputs"":[{""name"":"""",""type"":""address""}],""name"":""pendingWithdrawals"",""outputs"":[{""name"":"""",""type"":""uint256""}],""payable"":false,""type"":""function""},
335-
{""constant"":false,""inputs"":[{""name"":""punkIndex"",""type"":""uint256""}],""name"":""punkNoLongerForSale"",""outputs"":[],""payable"":false,""type"":""function""},
336-
{""inputs"":[],""payable"":true,""type"":""constructor""},
337-
{""anonymous"":false,""inputs"":[{""indexed"":true,""name"":""to"",""type"":""address""},{""indexed"":false,""name"":""punkIndex"",""type"":""uint256""}],""name"":""Assign"",""type"":""event""},
338-
{""anonymous"":false,""inputs"":[{""indexed"":true,""name"":""from"",""type"":""address""},{""indexed"":true,""name"":""to"",""type"":""address""},{""indexed"":false,""name"":""value"",""type"":""uint256""}],""name"":""Transfer"",""type"":""event""},
339-
{""anonymous"":false,""inputs"":[{""indexed"":true,""name"":""from"",""type"":""address""},{""indexed"":true,""name"":""to"",""type"":""address""},{""indexed"":false,""name"":""punkIndex"",""type"":""uint256""}],""name"":""PunkTransfer"",""type"":""event""},
340-
{""anonymous"":false,""inputs"":[{""indexed"":true,""name"":""punkIndex"",""type"":""uint256""},{""indexed"":false,""name"":""minValue"",""type"":""uint256""},{""indexed"":true,""name"":""toAddress"",""type"":""address""}],""name"":""PunkOffered"",""type"":""event""},
341-
{""anonymous"":false,""inputs"":[{""indexed"":true,""name"":""punkIndex"",""type"":""uint256""},{""indexed"":false,""name"":""value"",""type"":""uint256""},{""indexed"":true,""name"":""fromAddress"",""type"":""address""}],""name"":""PunkBidEntered"",""type"":""event""},
342-
{""anonymous"":false,""inputs"":[{""indexed"":true,""name"":""punkIndex"",""type"":""uint256""},{""indexed"":false,""name"":""value"",""type"":""uint256""},{""indexed"":true,""name"":""fromAddress"",""type"":""address""}],""name"":""PunkBidWithdrawn"",""type"":""event""},
343-
{""anonymous"":false,""inputs"":[{""indexed"":true,""name"":""punkIndex"",""type"":""uint256""},{""indexed"":false,""name"":""value"",""type"":""uint256""},{""indexed"":true,""name"":""fromAddress"",""type"":""address""},{""indexed"":true,""name"":""toAddress"",""type"":""address""}],""name"":""PunkBought"",""type"":""event""},
344-
{""anonymous"":false,""inputs"":[{""indexed"":true,""name"":""punkIndex"",""type"":""uint256""}],""name"":""PunkNoLongerForSale"",""type"":""event""}]";
308+
public const string CryptoPunksAbi =
309+
@"[{""constant"":true,""inputs"":[{""name"":""_owner"",""type"":""address""}],""name"":""balanceOf"",""outputs"":[{""name"":""balance"",""type"":""uint256""}],""payable"":false,""stateMutability"":""view"",""type"":""function""},
310+
{""constant"":true,""inputs"":[],""name"":""name"",""outputs"":[{""name"":"""",""type"":""string""}],""payable"":false,""stateMutability"":""view"",""type"":""function""}]";
345311
}
346312
}

0 commit comments

Comments
 (0)