Skip to content

Commit d55882e

Browse files
authored
Merge pull request #19 from WalletConnect/fix/webgl-solidity-data-types
fix: interaction with some smart contract functions is broken in WebGL
2 parents cda06d2 + 3921a9a commit d55882e

9 files changed

Lines changed: 103 additions & 51 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ mergeInto(LibraryManager.library, {
3737
// Call the method using the provided function
3838
let result = await callFn(_web3ModalConfig, methodName, parameterObj);
3939

40-
if (!result) {
40+
if (result === undefined || result === null) {
4141
{{{makeDynCall('viii', 'callbackPtr')}}} (id, undefined, undefined);
4242
return;
4343
}
@@ -69,7 +69,7 @@ mergeInto(LibraryManager.library, {
6969
const enableOnramp = parameters.enableOnramp;
7070

7171
// Load the scripts and initialize the configuration
72-
import("https://cdn.jsdelivr.net/npm/@web3modal/cdn@5.0.1/dist/wagmi.js").then(CDNW3M => {
72+
import("https://cdn.jsdelivr.net/npm/@web3modal/cdn@5.0.11/dist/wagmi.js").then(CDNW3M => {
7373
const WagmiCore = CDNW3M['WagmiCore'];
7474
const Chains = CDNW3M['Chains'];
7575
const Web3modal = CDNW3M['Web3modal'];

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
}

Packages/com.walletconnect.web3modal/Runtime/Evm/EvmService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ public Task<string> WriteContractAsync(string contractAddress, string contractAb
8989
return WriteContractAsync(contractAddress, contractAbi, methodName, default, default, arguments);
9090
}
9191

92+
public Task<string> WriteContractAsync(string contractAddress, string contractAbi, string methodName, BigInteger gas = default, params object[] arguments)
93+
{
94+
return WriteContractAsyncCore(contractAddress, contractAbi, methodName, default, gas, arguments);
95+
}
96+
9297
public Task<string> WriteContractAsync(string contractAddress, string contractAbi, string methodName, BigInteger value = default, BigInteger gas = default, params object[] arguments)
9398
{
9499
return WriteContractAsyncCore(contractAddress, contractAbi, methodName, value, gas, arguments);

Packages/com.walletconnect.web3modal/Runtime/Evm/NethereumEvmService.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,12 @@ protected override async Task<string> WriteContractAsyncCore(string contractAddr
124124
var contract = Web3.Eth.GetContract(contractAbi, contractAddress);
125125
var function = contract.GetFunction(methodName);
126126

127-
var receipt = await function.SendTransactionAndWaitForReceiptAsync(
128-
from: null, // will be automatically filled by interceptor
129-
gas: new HexBigInteger(gas),
130-
value: new HexBigInteger(value),
131-
receiptRequestCancellationToken: CancellationToken.None,
127+
return await function.SendTransactionAsync(
128+
null, // will be automatically filled by interceptor
129+
new HexBigInteger(gas),
130+
new HexBigInteger(value),
132131
arguments
133132
);
134-
135-
return receipt.TransactionHash;
136133
}
137134

138135

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
using WalletConnectSharp.Common.Utils;
4+
5+
namespace WalletConnect.Web3Modal.Utils
6+
{
7+
/// <summary>
8+
/// Converts byte array to hex string and vice versa.
9+
/// </summary>
10+
/// <remarks>
11+
/// The default behavior of Newtonsoft.Json is to convert byte arrays to base64 strings.
12+
/// </remarks>
13+
public class ByteArrayJsonConverter : JsonConverter
14+
{
15+
public override bool CanConvert(Type objectType)
16+
{
17+
return objectType == typeof(byte[]);
18+
}
19+
20+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
21+
{
22+
if (reader.TokenType == JsonToken.Null)
23+
return null;
24+
25+
if (reader.TokenType == JsonToken.String)
26+
{
27+
var hexString = (string)reader.Value;
28+
return hexString.HexToByteArray();
29+
}
30+
31+
throw new JsonSerializationException("Expected byte array object value");
32+
}
33+
34+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
35+
{
36+
if (value == null)
37+
{
38+
writer.WriteNull();
39+
return;
40+
}
41+
42+
var byteArray = (byte[])value;
43+
var hexString = byteArray.ToHex(true);
44+
45+
writer.WriteValue(hexString);
46+
}
47+
}
48+
}

Packages/com.walletconnect.web3modal/Runtime/Utils/ByteArrayJsonConverter.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/WebGL/InteropService.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using AOT;
77
using Newtonsoft.Json;
88
using UnityEngine;
9+
using WalletConnect.Web3Modal.Utils;
910

1011
namespace WalletConnect.Web3Modal.WebGl
1112
{
@@ -15,6 +16,11 @@ public class InteropService
1516

1617
private readonly ExternalMethod _externalMethod;
1718

19+
private readonly JsonConverter[] _jsonConverts =
20+
{
21+
new ByteArrayJsonConverter()
22+
};
23+
1824
public InteropService(ExternalMethod externalMethod)
1925
{
2026
_externalMethod = externalMethod;
@@ -46,9 +52,13 @@ public async Task<TRes> InteropCallAsync<TReq, TRes>(string methodName, TReq req
4652
if (!Equals(requestParameter, default(TReq)))
4753
{
4854
if (typeof(TReq) == typeof(string))
55+
{
4956
paramStr = requestParameter as string;
57+
}
5058
else
51-
paramStr = JsonConvert.SerializeObject(requestParameter);
59+
{
60+
paramStr = JsonConvert.SerializeObject(requestParameter, _jsonConverts);
61+
}
5262
}
5363

5464
_externalMethod(id, methodName, paramStr, TcsCallback);

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)