1414using Nethereum . Contracts ;
1515using Nethereum . Hex . HexConvertors . Extensions ;
1616using Thirdweb . Redcode . Awaiting ;
17+ using System . Threading ;
18+ using System . Collections . Concurrent ;
1719
1820namespace Thirdweb . AccountAbstraction
1921{
@@ -34,24 +36,19 @@ public class UserOperationHexified
3436
3537 public class SmartWallet
3638 {
39+ private bool _deployed ;
40+ private bool _deploying ;
41+ private bool _initialized ;
42+
3743 public List < string > Accounts { get ; internal set ; }
3844 public string PersonalAddress { get ; internal set ; }
3945 public Web3 PersonalWeb3 { get ; internal set ; }
4046 public ThirdwebSDK . SmartWalletConfig Config { get ; internal set ; }
47+ public bool IsDeployed => _deployed ;
48+ public bool IsDeploying => _deploying ;
4149
42- private bool _deployed ;
43- public bool IsDeployed
44- {
45- get { return _deployed ; }
46- }
47-
48- private bool _deploying ;
49- public bool IsDeploying
50- {
51- get { return _deploying ; }
52- }
53-
54- private bool _initialized ;
50+ private readonly SemaphoreSlim _semaphore = new SemaphoreSlim ( 1 , 1 ) ;
51+ private readonly ConcurrentQueue < TaskCompletionSource < RpcResponseMessage > > _responseQueue = new ConcurrentQueue < TaskCompletionSource < RpcResponseMessage > > ( ) ;
5552
5653 public SmartWallet ( Web3 personalWeb3 , ThirdwebSDK . SmartWalletConfig config )
5754 {
@@ -141,14 +138,36 @@ internal async Task<RpcResponseMessage> Request(RpcRequestMessage requestMessage
141138 {
142139 ThirdwebDebug . Log ( "Requesting: " + requestMessage . Method + "..." ) ;
143140
144- if ( requestMessage . Method == "eth_chainId " )
141+ if ( requestMessage . Method == "eth_sendTransaction " )
145142 {
146- var chainId = await PersonalWeb3 . Eth . ChainId . SendRequestAsync ( ) ;
147- return new RpcResponseMessage ( requestMessage . Id , chainId . HexValue ) ;
143+ var tcs = new TaskCompletionSource < RpcResponseMessage > ( ) ;
144+ _responseQueue . Enqueue ( tcs ) ;
145+
146+ await _semaphore . WaitAsync ( ) ;
147+
148+ if ( _responseQueue . TryDequeue ( out var dequeuedTcs ) && dequeuedTcs == tcs )
149+ {
150+ try
151+ {
152+ var response = await CreateUserOpAndSend ( requestMessage ) ;
153+ tcs . SetResult ( response ) ;
154+ }
155+ catch ( Exception ex )
156+ {
157+ tcs . SetException ( ex ) ;
158+ }
159+ finally
160+ {
161+ _semaphore . Release ( ) ;
162+ }
163+ }
164+
165+ return await tcs . Task ;
148166 }
149- else if ( requestMessage . Method == "eth_sendTransaction " )
167+ else if ( requestMessage . Method == "eth_chainId " )
150168 {
151- return await CreateUserOpAndSend ( requestMessage ) ;
169+ var chainId = await PersonalWeb3 . Eth . ChainId . SendRequestAsync ( ) ;
170+ return new RpcResponseMessage ( requestMessage . Id , chainId . HexValue ) ;
152171 }
153172 else
154173 {
@@ -227,22 +246,19 @@ private async Task<RpcResponseMessage> CreateUserOpAndSend(RpcRequestMessage req
227246 }
228247 ThirdwebDebug . Log ( "Tx Hash: " + txHash ) ;
229248
230- // // Check if successful
231-
232- // var receipt = await new Web3(ThirdwebManager.Instance.SDK.session.RPC).Eth.Transactions.GetTransactionReceipt.SendRequestAsync(txHash);
233- // var decodedEvents = receipt.DecodeAllEvents<EntryPointContract.UserOperationEventEventDTO>();
234- // if (decodedEvents[0].Event.Success == false)
235- // {
236- // ThirdwebDebug.Log("Transaction not successful, checking reason...");
237- // var reason = await new Web3(ThirdwebManager.Instance.SDK.session.RPC).Eth.GetContractTransactionErrorReason.SendRequestAsync(txHash);
238- // throw new Exception($"Transaction {txHash} reverted with reason: {reason}");
239- // }
240- // else
241- // {
242- // ThirdwebDebug.Log("Transaction successful");
243- // _deployed = true;
244- // }
249+ // Check if successful
245250
251+ var receipt = await Transaction . WaitForTransactionResultRaw ( txHash ) ;
252+ var decodedEvents = receipt . DecodeAllEvents < EntryPointContract . UserOperationEventEventDTO > ( ) ;
253+ if ( decodedEvents [ 0 ] . Event . Success == false )
254+ {
255+ throw new Exception ( $ "Transaction { txHash } execution reverted") ;
256+ }
257+ else
258+ {
259+ ThirdwebDebug . Log ( "Transaction successful" ) ;
260+ _deployed = true ;
261+ }
246262 return new RpcResponseMessage ( requestMessage . Id , txHash ) ;
247263 }
248264
0 commit comments