14
14
using Nethereum . Contracts ;
15
15
using Nethereum . Hex . HexConvertors . Extensions ;
16
16
using Thirdweb . Redcode . Awaiting ;
17
+ using System . Threading ;
18
+ using System . Collections . Concurrent ;
17
19
18
20
namespace Thirdweb . AccountAbstraction
19
21
{
@@ -34,24 +36,19 @@ public class UserOperationHexified
34
36
35
37
public class SmartWallet
36
38
{
39
+ private bool _deployed ;
40
+ private bool _deploying ;
41
+ private bool _initialized ;
42
+
37
43
public List < string > Accounts { get ; internal set ; }
38
44
public string PersonalAddress { get ; internal set ; }
39
45
public Web3 PersonalWeb3 { get ; internal set ; }
40
46
public ThirdwebSDK . SmartWalletConfig Config { get ; internal set ; }
47
+ public bool IsDeployed => _deployed ;
48
+ public bool IsDeploying => _deploying ;
41
49
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 > > ( ) ;
55
52
56
53
public SmartWallet ( Web3 personalWeb3 , ThirdwebSDK . SmartWalletConfig config )
57
54
{
@@ -141,14 +138,36 @@ internal async Task<RpcResponseMessage> Request(RpcRequestMessage requestMessage
141
138
{
142
139
ThirdwebDebug . Log ( "Requesting: " + requestMessage . Method + "..." ) ;
143
140
144
- if ( requestMessage . Method == "eth_chainId " )
141
+ if ( requestMessage . Method == "eth_sendTransaction " )
145
142
{
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 ;
148
166
}
149
- else if ( requestMessage . Method == "eth_sendTransaction " )
167
+ else if ( requestMessage . Method == "eth_chainId " )
150
168
{
151
- return await CreateUserOpAndSend ( requestMessage ) ;
169
+ var chainId = await PersonalWeb3 . Eth . ChainId . SendRequestAsync ( ) ;
170
+ return new RpcResponseMessage ( requestMessage . Id , chainId . HexValue ) ;
152
171
}
153
172
else
154
173
{
@@ -227,22 +246,19 @@ private async Task<RpcResponseMessage> CreateUserOpAndSend(RpcRequestMessage req
227
246
}
228
247
ThirdwebDebug . Log ( "Tx Hash: " + txHash ) ;
229
248
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
245
250
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
+ }
246
262
return new RpcResponseMessage ( requestMessage . Id , txHash ) ;
247
263
}
248
264
0 commit comments