Skip to content

Commit 90cd2f5

Browse files
rob1997kantagara
andauthored
unity RPC Client issue fix (#996)
* fix for main thread issue * circular dependency fix by moving IMainThreadRunner to ChainSafe.Gaming * referenced IMainThreadRunner in Web3AuthWallet * client now initialized * Reverting back to rpcclient --------- Co-authored-by: Nikola Garabandic <[email protected]>
1 parent dc6a5e3 commit 90cd2f5

File tree

7 files changed

+36
-22
lines changed

7 files changed

+36
-22
lines changed

Packages/io.chainsafe.web3-unity.web3auth/Runtime/Web3AuthWallet.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using ChainSafe.Gaming.Web3.Analytics;
99
using ChainSafe.Gaming.Web3.Core;
1010
using ChainSafe.Gaming.Web3.Core.Evm;
11+
using ChainSafe.Gaming.Web3.Environment;
1112
using Nethereum.Signer;
1213
using UnityEngine;
1314
using TWeb3Auth = Web3Auth;
@@ -25,6 +26,7 @@ public class Web3AuthWallet : ISigner, ITransactionExecutor, ILifecycleParticipa
2526
private TWeb3Auth coreInstance;
2627
private InProcessSigner signer;
2728
private InProcessTransactionExecutor transactionExecutor;
29+
private IMainThreadRunner mainThreadRunner;
2830
private readonly IAnalyticsClient analyticsClient;
2931

3032
/// <summary>
@@ -33,10 +35,11 @@ public class Web3AuthWallet : ISigner, ITransactionExecutor, ILifecycleParticipa
3335
/// <param name="config">The configuration for the Web3Auth wallet.</param>
3436
/// <param name="chainConfig">The configuration for the target blockchain.</param>
3537
/// <param name="rpcProvider">The RPC provider for blockchain interaction.</param>
36-
public Web3AuthWallet(Web3AuthWalletConfig config, IRpcProvider rpcProvider, IAnalyticsClient analyticsClient)
38+
public Web3AuthWallet(Web3AuthWalletConfig config, IRpcProvider rpcProvider, IMainThreadRunner mainThreadRunner, IAnalyticsClient analyticsClient)
3739
{
3840
this.config = config;
3941
this.rpcProvider = rpcProvider;
42+
this.mainThreadRunner = mainThreadRunner;
4043
this.analyticsClient = analyticsClient;
4144
}
4245

@@ -69,7 +72,7 @@ public async ValueTask WillStartAsync()
6972
var signerConfig = new InProcessSignerConfig { PrivateKey = privateKey };
7073
signer = new InProcessSigner(signerConfig);
7174

72-
transactionExecutor = new InProcessTransactionExecutor(signer, analyticsClient.ChainConfig, rpcProvider, new RpcClientWrapper(analyticsClient.ChainConfig));
75+
transactionExecutor = new InProcessTransactionExecutor(signer, analyticsClient.ChainConfig, rpcProvider, mainThreadRunner, new RpcClientWrapper(analyticsClient.ChainConfig));
7376

7477
void Web3Auth_OnLogin(Web3AuthResponse response)
7578
{

src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/ChainSafe.Gaming.InProcessTransactionExecutor.Unity.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
77
<EnableNETAnalyzers>True</EnableNETAnalyzers>
88
<CodeAnalysisRuleSet>../../global.ruleset</CodeAnalysisRuleSet>
9-
<Nullable>enable</Nullable>
109
<Configurations>Debug;Release;Test</Configurations>
1110
<Platforms>AnyCPU</Platforms>
1211
<RootNamespace>ChainSafe.Gaming.InProcessTransactionExecutor.Unity</RootNamespace>
1312
</PropertyGroup>
1413

1514
<ItemGroup>
16-
<ProjectReference Include="..\ChainSafe.Gaming.InProcessTransactionExecutor\ChainSafe.Gaming.InProcessTransactionExecutor.csproj" />
15+
<ProjectReference Include="../ChainSafe.Gaming.InProcessTransactionExecutor/ChainSafe.Gaming.InProcessTransactionExecutor.csproj" />
1716
</ItemGroup>
1817

1918
<ItemGroup>
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
using System;
2+
using System.Net.Http;
23
using ChainSafe.Gaming.Web3;
4+
using ChainSafe.Gaming.Web3.Environment;
35
using Nethereum.JsonRpc.Client;
46
using Nethereum.Unity.Rpc;
57

68
namespace ChainSafe.Gaming.InProcessTransactionExecutor.Unity
79
{
810
public class RpcClientWrapper : IRpcClientWrapper
911
{
12+
private readonly IChainConfig chainConfig;
13+
1014
public RpcClientWrapper(IChainConfig chainConfig)
1115
{
12-
Client = new UnityWebRequestRpcTaskClient(new Uri(chainConfig.Rpc));
16+
this.chainConfig = chainConfig;
1317
}
1418

15-
public IClient Client { get; private set; }
19+
public IClient Client => new RpcClient(new Uri(chainConfig.Rpc));
1620
}
1721
}

src/ChainSafe.Gaming.InProcessTransactionExecutor/ChainSafe.Gaming.InProcessTransactionExecutor.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
77
<EnableNETAnalyzers>True</EnableNETAnalyzers>
88
<CodeAnalysisRuleSet>../../global.ruleset</CodeAnalysisRuleSet>
9-
<Nullable>enable</Nullable>
109
<Configurations>Debug;Release;Test</Configurations>
1110
<Platforms>AnyCPU</Platforms>
1211
<RootNamespace>ChainSafe.Gaming.InProcessTransactionExecutor</RootNamespace>

src/ChainSafe.Gaming.InProcessTransactionExecutor/InProcessTransactionExecutor.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using ChainSafe.Gaming.Evm.Transactions;
66
using ChainSafe.Gaming.Web3;
77
using ChainSafe.Gaming.Web3.Core.Evm;
8+
using ChainSafe.Gaming.Web3.Environment;
89
using Nethereum.Hex.HexTypes;
910
using Nethereum.RPC.Eth.DTOs;
1011
using Nethereum.Web3.Accounts;
@@ -18,19 +19,21 @@ namespace ChainSafe.Gaming.InProcessTransactionExecutor
1819
/// </summary>
1920
public class InProcessTransactionExecutor : ITransactionExecutor
2021
{
21-
private readonly NWeb3 web3;
2222
private readonly IRpcProvider rpcProvider;
2323
private readonly string accountAddress;
2424

25+
private NWeb3 web3;
26+
2527
/// <summary>
2628
/// Initializes a new instance of the <see cref="InProcessTransactionExecutor"/> class.
2729
/// </summary>
2830
/// <param name="signer">Injected <see cref="ISigner"/>.</param>
2931
/// <param name="chainConfig">Injected <see cref="IChainConfig"/>.</param>
3032
/// <param name="rpcProvider">Injected <see cref="IRpcProvider"/>.</param>
33+
/// <param name="mainThreadRunner">Injected <see cref="IMainThreadRunner"/>.</param>
3134
/// <param name="rpcClientWrapper">Injected <see cref="IRpcClientWrapper"/>.</param>
3235
/// <exception cref="Web3Exception">Throws exception if initializing instance fails.</exception>
33-
public InProcessTransactionExecutor(ISigner signer, IChainConfig chainConfig, IRpcProvider rpcProvider, IRpcClientWrapper rpcClientWrapper)
36+
public InProcessTransactionExecutor(ISigner signer, IChainConfig chainConfig, IRpcProvider rpcProvider, IMainThreadRunner mainThreadRunner, IRpcClientWrapper rpcClientWrapper)
3437
{
3538
// It should be possible to set up other signers to work with this as well.
3639
// However, does it make sense to let a remote wallet sign a transaction, but
@@ -39,19 +42,24 @@ public InProcessTransactionExecutor(ISigner signer, IChainConfig chainConfig, IR
3942
throw new Web3Exception($"{nameof(InProcessTransactionExecutor)} only supports {nameof(InProcessSigner.InProcessSigner)}");
4043
accountAddress = privateKey.GetPublicAddress();
4144
var account = new Account(privateKey);
42-
if (chainConfig.Rpc is not null && !string.Empty.Equals(chainConfig.Rpc))
43-
{
44-
web3 = new NWeb3(account, rpcClientWrapper.Client);
45-
}
46-
else if (chainConfig.Ipc is not null && !string.Empty.Equals(chainConfig.Ipc))
47-
{
48-
var client = new NIpcClient(chainConfig.Rpc);
49-
web3 = new NWeb3(client);
50-
}
51-
else
45+
46+
// Initialize Web3 on the main thread.
47+
mainThreadRunner.Enqueue(() =>
5248
{
53-
throw new Web3Exception($"{nameof(IChainConfig)} should have at least one communication method set.");
54-
}
49+
if (chainConfig.Rpc is not null && !string.Empty.Equals(chainConfig.Rpc))
50+
{
51+
web3 = new NWeb3(account, rpcClientWrapper.Client);
52+
}
53+
else if (chainConfig.Ipc is not null && !string.Empty.Equals(chainConfig.Ipc))
54+
{
55+
var client = new NIpcClient(chainConfig.Rpc);
56+
web3 = new NWeb3(client);
57+
}
58+
else
59+
{
60+
throw new Web3Exception($"{nameof(IChainConfig)} should have at least one communication method set.");
61+
}
62+
});
5563

5664
this.rpcProvider = rpcProvider;
5765
}

src/ChainSafe.Gaming.Unity/UnityDispatcherAdapter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Threading.Tasks;
33
using ChainSafe.Gaming.Evm.Unity;
4+
using ChainSafe.Gaming.Web3.Environment;
45

56
namespace ChainSafe.Gaming.Unity
67
{

src/ChainSafe.Gaming.Unity/IMainThreadRunner.cs renamed to src/ChainSafe.Gaming/Web3/Core/Environment/IMainThreadRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Threading.Tasks;
33

4-
namespace ChainSafe.Gaming.Unity
4+
namespace ChainSafe.Gaming.Web3.Environment
55
{
66
public interface IMainThreadRunner
77
{

0 commit comments

Comments
 (0)