From 765a7ac802b9f30e1dafcb05e5f9ceb4b6ba3946 Mon Sep 17 00:00:00 2001 From: rob1997 Date: Mon, 3 Jun 2024 16:22:39 +0300 Subject: [PATCH 1/5] fix for main thread issue --- ...nSafe.Gaming.InProcessTransactionExecutor.Unity.csproj | 4 ++-- .../RpcClientWrapper.cs | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) 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..ce5f3c3ed 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,14 @@ 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..ce4ab259a 100644 --- a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs +++ b/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs @@ -1,4 +1,5 @@ using System; +using ChainSafe.Gaming.Unity; using ChainSafe.Gaming.Web3; using Nethereum.JsonRpc.Client; using Nethereum.Unity.Rpc; @@ -7,9 +8,12 @@ namespace ChainSafe.Gaming.InProcessTransactionExecutor.Unity { public class RpcClientWrapper : IRpcClientWrapper { - public RpcClientWrapper(IChainConfig chainConfig) + public RpcClientWrapper(IChainConfig chainConfig, IMainThreadRunner mainThreadRunner) { - Client = new UnityWebRequestRpcTaskClient(new Uri(chainConfig.Rpc)); + mainThreadRunner.Enqueue(delegate + { + Client = new UnityWebRequestRpcTaskClient(new Uri(chainConfig.Rpc)); + }); } public IClient Client { get; private set; } From d61d5caf3a195f893ce2dfe07c87f362f41d54b0 Mon Sep 17 00:00:00 2001 From: rob1997 Date: Mon, 3 Jun 2024 16:31:42 +0300 Subject: [PATCH 2/5] circular dependency fix by moving IMainThreadRunner to ChainSafe.Gaming --- ...ChainSafe.Gaming.InProcessTransactionExecutor.Unity.csproj | 1 - .../RpcClientWrapper.cs | 4 ++-- src/ChainSafe.Gaming.Unity/UnityDispatcherAdapter.cs | 1 + .../Web3/Core/Environment}/IMainThreadRunner.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename src/{ChainSafe.Gaming.Unity => ChainSafe.Gaming/Web3/Core/Environment}/IMainThreadRunner.cs (85%) 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 ce5f3c3ed..36dd894ec 100644 --- a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/ChainSafe.Gaming.InProcessTransactionExecutor.Unity.csproj +++ b/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/ChainSafe.Gaming.InProcessTransactionExecutor.Unity.csproj @@ -13,7 +13,6 @@ - diff --git a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs b/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs index ce4ab259a..458c2c1ad 100644 --- a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs +++ b/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs @@ -1,6 +1,6 @@ using System; -using ChainSafe.Gaming.Unity; using ChainSafe.Gaming.Web3; +using ChainSafe.Gaming.Web3.Environment; using Nethereum.JsonRpc.Client; using Nethereum.Unity.Rpc; @@ -10,7 +10,7 @@ public class RpcClientWrapper : IRpcClientWrapper { public RpcClientWrapper(IChainConfig chainConfig, IMainThreadRunner mainThreadRunner) { - mainThreadRunner.Enqueue(delegate + mainThreadRunner.Enqueue(() => { Client = new UnityWebRequestRpcTaskClient(new Uri(chainConfig.Rpc)); }); 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 { From 8329a7155cb528b809614db2230c31d0decd41e7 Mon Sep 17 00:00:00 2001 From: rob1997 Date: Mon, 3 Jun 2024 16:35:43 +0300 Subject: [PATCH 3/5] referenced IMainThreadRunner in Web3AuthWallet --- .../Runtime/Web3AuthWallet.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Packages/io.chainsafe.web3-unity.web3auth/Runtime/Web3AuthWallet.cs b/Packages/io.chainsafe.web3-unity.web3auth/Runtime/Web3AuthWallet.cs index d88f21521..118b207f7 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, new RpcClientWrapper(analyticsClient.ChainConfig, mainThreadRunner)); void Web3Auth_OnLogin(Web3AuthResponse response) { From 4573ee448a2c774c263bd90ac134e6cce4e7a684 Mon Sep 17 00:00:00 2001 From: rob1997 Date: Mon, 3 Jun 2024 17:12:01 +0300 Subject: [PATCH 4/5] client now initialized --- .../Runtime/Web3AuthWallet.cs | 2 +- .../RpcClientWrapper.cs | 11 +++--- ...Gaming.InProcessTransactionExecutor.csproj | 1 - .../InProcessTransactionExecutor.cs | 36 +++++++++++-------- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/Packages/io.chainsafe.web3-unity.web3auth/Runtime/Web3AuthWallet.cs b/Packages/io.chainsafe.web3-unity.web3auth/Runtime/Web3AuthWallet.cs index 118b207f7..37683cd3b 100644 --- a/Packages/io.chainsafe.web3-unity.web3auth/Runtime/Web3AuthWallet.cs +++ b/Packages/io.chainsafe.web3-unity.web3auth/Runtime/Web3AuthWallet.cs @@ -72,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, mainThreadRunner)); + 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/RpcClientWrapper.cs b/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs index 458c2c1ad..46b42ce22 100644 --- a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs +++ b/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs @@ -8,14 +8,13 @@ namespace ChainSafe.Gaming.InProcessTransactionExecutor.Unity { public class RpcClientWrapper : IRpcClientWrapper { - public RpcClientWrapper(IChainConfig chainConfig, IMainThreadRunner mainThreadRunner) + private readonly IChainConfig chainConfig; + + public RpcClientWrapper(IChainConfig chainConfig) { - mainThreadRunner.Enqueue(() => - { - Client = new UnityWebRequestRpcTaskClient(new Uri(chainConfig.Rpc)); - }); + this.chainConfig = chainConfig; } - public IClient Client { get; private set; } + public IClient Client => new UnityWebRequestRpcTaskClient(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; } From f961b284feda9250f18eff81f1c4cc45d2756245 Mon Sep 17 00:00:00 2001 From: Nikola Garabandic Date: Mon, 3 Jun 2024 20:21:33 +0200 Subject: [PATCH 5/5] Reverting back to rpcclient --- .../RpcClientWrapper.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs b/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs index 46b42ce22..ed40e4e8a 100644 --- a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs +++ b/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs @@ -1,4 +1,5 @@ using System; +using System.Net.Http; using ChainSafe.Gaming.Web3; using ChainSafe.Gaming.Web3.Environment; using Nethereum.JsonRpc.Client; @@ -15,6 +16,6 @@ public RpcClientWrapper(IChainConfig chainConfig) this.chainConfig = chainConfig; } - public IClient Client => new UnityWebRequestRpcTaskClient(new Uri(chainConfig.Rpc)); + public IClient Client => new RpcClient(new Uri(chainConfig.Rpc)); } } \ No newline at end of file