diff --git a/Packages/io.chainsafe.web3-unity.web3auth/Runtime/Web3AuthWallet.cs b/Packages/io.chainsafe.web3-unity.web3auth/Runtime/Web3AuthWallet.cs index d88f21521..37683cd3b 100644 --- a/Packages/io.chainsafe.web3-unity.web3auth/Runtime/Web3AuthWallet.cs +++ b/Packages/io.chainsafe.web3-unity.web3auth/Runtime/Web3AuthWallet.cs @@ -8,6 +8,7 @@ using ChainSafe.Gaming.Web3.Analytics; using ChainSafe.Gaming.Web3.Core; using ChainSafe.Gaming.Web3.Core.Evm; +using ChainSafe.Gaming.Web3.Environment; using Nethereum.Signer; using UnityEngine; using TWeb3Auth = Web3Auth; @@ -25,6 +26,7 @@ public class Web3AuthWallet : ISigner, ITransactionExecutor, ILifecycleParticipa private TWeb3Auth coreInstance; private InProcessSigner signer; private InProcessTransactionExecutor transactionExecutor; + private IMainThreadRunner mainThreadRunner; private readonly IAnalyticsClient analyticsClient; /// @@ -33,10 +35,11 @@ public class Web3AuthWallet : ISigner, ITransactionExecutor, ILifecycleParticipa /// The configuration for the Web3Auth wallet. /// The configuration for the target blockchain. /// The RPC provider for blockchain interaction. - public Web3AuthWallet(Web3AuthWalletConfig config, IRpcProvider rpcProvider, IAnalyticsClient analyticsClient) + public Web3AuthWallet(Web3AuthWalletConfig config, IRpcProvider rpcProvider, IMainThreadRunner mainThreadRunner, IAnalyticsClient analyticsClient) { this.config = config; this.rpcProvider = rpcProvider; + this.mainThreadRunner = mainThreadRunner; this.analyticsClient = analyticsClient; } @@ -69,7 +72,7 @@ public async ValueTask WillStartAsync() var signerConfig = new InProcessSignerConfig { PrivateKey = privateKey }; signer = new InProcessSigner(signerConfig); - transactionExecutor = new InProcessTransactionExecutor(signer, analyticsClient.ChainConfig, rpcProvider, new RpcClientWrapper(analyticsClient.ChainConfig)); + transactionExecutor = new InProcessTransactionExecutor(signer, analyticsClient.ChainConfig, rpcProvider, mainThreadRunner, new RpcClientWrapper(analyticsClient.ChainConfig)); void Web3Auth_OnLogin(Web3AuthResponse response) { diff --git a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/ChainSafe.Gaming.InProcessTransactionExecutor.Unity.csproj b/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/ChainSafe.Gaming.InProcessTransactionExecutor.Unity.csproj index 73d85b1d2..36dd894ec 100644 --- a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/ChainSafe.Gaming.InProcessTransactionExecutor.Unity.csproj +++ b/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/ChainSafe.Gaming.InProcessTransactionExecutor.Unity.csproj @@ -6,14 +6,13 @@ True True ../../global.ruleset - enable Debug;Release;Test AnyCPU ChainSafe.Gaming.InProcessTransactionExecutor.Unity - + diff --git a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs b/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs index 86062770e..ed40e4e8a 100644 --- a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs +++ b/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs @@ -1,5 +1,7 @@ using System; +using System.Net.Http; using ChainSafe.Gaming.Web3; +using ChainSafe.Gaming.Web3.Environment; using Nethereum.JsonRpc.Client; using Nethereum.Unity.Rpc; @@ -7,11 +9,13 @@ namespace ChainSafe.Gaming.InProcessTransactionExecutor.Unity { public class RpcClientWrapper : IRpcClientWrapper { + private readonly IChainConfig chainConfig; + public RpcClientWrapper(IChainConfig chainConfig) { - Client = new UnityWebRequestRpcTaskClient(new Uri(chainConfig.Rpc)); + this.chainConfig = chainConfig; } - public IClient Client { get; private set; } + public IClient Client => new RpcClient(new Uri(chainConfig.Rpc)); } } \ No newline at end of file diff --git a/src/ChainSafe.Gaming.InProcessTransactionExecutor/ChainSafe.Gaming.InProcessTransactionExecutor.csproj b/src/ChainSafe.Gaming.InProcessTransactionExecutor/ChainSafe.Gaming.InProcessTransactionExecutor.csproj index 1a2fca6ad..080af97b0 100644 --- a/src/ChainSafe.Gaming.InProcessTransactionExecutor/ChainSafe.Gaming.InProcessTransactionExecutor.csproj +++ b/src/ChainSafe.Gaming.InProcessTransactionExecutor/ChainSafe.Gaming.InProcessTransactionExecutor.csproj @@ -6,7 +6,6 @@ True True ../../global.ruleset - enable Debug;Release;Test AnyCPU ChainSafe.Gaming.InProcessTransactionExecutor diff --git a/src/ChainSafe.Gaming.InProcessTransactionExecutor/InProcessTransactionExecutor.cs b/src/ChainSafe.Gaming.InProcessTransactionExecutor/InProcessTransactionExecutor.cs index 2402435c7..403780e4e 100644 --- a/src/ChainSafe.Gaming.InProcessTransactionExecutor/InProcessTransactionExecutor.cs +++ b/src/ChainSafe.Gaming.InProcessTransactionExecutor/InProcessTransactionExecutor.cs @@ -5,6 +5,7 @@ using ChainSafe.Gaming.Evm.Transactions; using ChainSafe.Gaming.Web3; using ChainSafe.Gaming.Web3.Core.Evm; +using ChainSafe.Gaming.Web3.Environment; using Nethereum.Hex.HexTypes; using Nethereum.RPC.Eth.DTOs; using Nethereum.Web3.Accounts; @@ -18,19 +19,21 @@ namespace ChainSafe.Gaming.InProcessTransactionExecutor /// public class InProcessTransactionExecutor : ITransactionExecutor { - private readonly NWeb3 web3; private readonly IRpcProvider rpcProvider; private readonly string accountAddress; + private NWeb3 web3; + /// /// Initializes a new instance of the class. /// /// Injected . /// Injected . /// Injected . + /// Injected . /// Injected . /// Throws exception if initializing instance fails. - public InProcessTransactionExecutor(ISigner signer, IChainConfig chainConfig, IRpcProvider rpcProvider, IRpcClientWrapper rpcClientWrapper) + public InProcessTransactionExecutor(ISigner signer, IChainConfig chainConfig, IRpcProvider rpcProvider, IMainThreadRunner mainThreadRunner, IRpcClientWrapper rpcClientWrapper) { // It should be possible to set up other signers to work with this as well. // 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 throw new Web3Exception($"{nameof(InProcessTransactionExecutor)} only supports {nameof(InProcessSigner.InProcessSigner)}"); accountAddress = privateKey.GetPublicAddress(); var account = new Account(privateKey); - if (chainConfig.Rpc is not null && !string.Empty.Equals(chainConfig.Rpc)) - { - web3 = new NWeb3(account, rpcClientWrapper.Client); - } - else if (chainConfig.Ipc is not null && !string.Empty.Equals(chainConfig.Ipc)) - { - var client = new NIpcClient(chainConfig.Rpc); - web3 = new NWeb3(client); - } - else + + // Initialize Web3 on the main thread. + mainThreadRunner.Enqueue(() => { - throw new Web3Exception($"{nameof(IChainConfig)} should have at least one communication method set."); - } + if (chainConfig.Rpc is not null && !string.Empty.Equals(chainConfig.Rpc)) + { + web3 = new NWeb3(account, rpcClientWrapper.Client); + } + else if (chainConfig.Ipc is not null && !string.Empty.Equals(chainConfig.Ipc)) + { + var client = new NIpcClient(chainConfig.Rpc); + web3 = new NWeb3(client); + } + else + { + throw new Web3Exception($"{nameof(IChainConfig)} should have at least one communication method set."); + } + }); this.rpcProvider = rpcProvider; } diff --git a/src/ChainSafe.Gaming.Unity/UnityDispatcherAdapter.cs b/src/ChainSafe.Gaming.Unity/UnityDispatcherAdapter.cs index 671ff6cb4..ac064c7a8 100644 --- a/src/ChainSafe.Gaming.Unity/UnityDispatcherAdapter.cs +++ b/src/ChainSafe.Gaming.Unity/UnityDispatcherAdapter.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using ChainSafe.Gaming.Evm.Unity; +using ChainSafe.Gaming.Web3.Environment; namespace ChainSafe.Gaming.Unity { diff --git a/src/ChainSafe.Gaming.Unity/IMainThreadRunner.cs b/src/ChainSafe.Gaming/Web3/Core/Environment/IMainThreadRunner.cs similarity index 85% rename from src/ChainSafe.Gaming.Unity/IMainThreadRunner.cs rename to src/ChainSafe.Gaming/Web3/Core/Environment/IMainThreadRunner.cs index 5b7e6a38b..5392dc597 100644 --- a/src/ChainSafe.Gaming.Unity/IMainThreadRunner.cs +++ b/src/ChainSafe.Gaming/Web3/Core/Environment/IMainThreadRunner.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; -namespace ChainSafe.Gaming.Unity +namespace ChainSafe.Gaming.Web3.Environment { public interface IMainThreadRunner {