5
5
6
6
namespace Thirdweb ;
7
7
8
- public enum ExecutionMode
9
- {
10
- EOA ,
11
- EIP7702 ,
12
- }
13
-
14
8
/// <summary>
15
9
/// Represents a 7702 delegated wallet with granular session key permissions and automatic session key execution.
16
10
/// </summary>
@@ -24,27 +18,27 @@ public class SmarterWallet : IThirdwebWallet
24
18
internal IThirdwebWallet UserWallet { get ; }
25
19
internal ThirdwebContract UserContract { get ; }
26
20
internal BigInteger ChainId { get ; }
27
- internal ExecutionMode ExecutionMode { get ; }
21
+ internal bool SponsorGas { get ; }
28
22
29
23
private EIP7702Authorization ? Authorization { get ; set ; }
30
24
31
- internal SmarterWallet ( ThirdwebClient client , BigInteger chainId , IThirdwebWallet userWallet , ThirdwebContract userContract , EIP7702Authorization ? authorization , ExecutionMode executionMode )
25
+ internal SmarterWallet ( ThirdwebClient client , BigInteger chainId , IThirdwebWallet userWallet , ThirdwebContract userContract , EIP7702Authorization ? authorization , bool sponsorGas )
32
26
{
33
27
this . Client = client ;
34
28
this . ChainId = chainId ;
35
29
this . UserWallet = userWallet ;
36
30
this . UserContract = userContract ;
37
31
this . Authorization = authorization ;
38
- this . ExecutionMode = executionMode ;
32
+ this . SponsorGas = sponsorGas ;
39
33
}
40
34
41
- public static async Task < SmarterWallet > Create ( ThirdwebClient client , BigInteger chainId , IThirdwebWallet userWallet , ExecutionMode executionMode )
35
+ public static async Task < SmarterWallet > Create ( ThirdwebClient client , BigInteger chainId , IThirdwebWallet userWallet , bool sponsorGas = true )
42
36
{
43
37
var userWalletAddress = await userWallet . GetAddress ( ) ;
44
38
var userContract = await ThirdwebContract . Create ( client , userWalletAddress , chainId , Constants . MINIMAL_ACCOUNT_7702_ABI ) ;
45
39
var needsDelegation = ! await Utils . IsDelegatedAccount ( client , chainId , userWalletAddress ) ;
46
- EIP7702Authorization ? authorization = needsDelegation ? await userWallet . SignAuthorization ( chainId , Constants . MINIMAL_ACCOUNT_7702 , willSelfExecute : executionMode == ExecutionMode . EOA ) : null ;
47
- var wallet = new SmarterWallet ( client , chainId , userWallet , userContract , authorization , executionMode ) ;
40
+ EIP7702Authorization ? authorization = needsDelegation ? await userWallet . SignAuthorization ( chainId , Constants . MINIMAL_ACCOUNT_7702 , willSelfExecute : ! sponsorGas ) : null ;
41
+ var wallet = new SmarterWallet ( client , chainId , userWallet , userContract , authorization , sponsorGas ) ;
48
42
Utils . TrackConnection ( wallet ) ;
49
43
return wallet ;
50
44
}
@@ -145,66 +139,59 @@ public async Task<string> SendTransaction(ThirdwebTransactionInput transaction)
145
139
}
146
140
} ;
147
141
148
- switch ( this . ExecutionMode )
142
+ if ( this . SponsorGas )
149
143
{
150
- case ExecutionMode . EIP7702 :
151
- var wrappedCalls = new WrappedCalls ( ) { Calls = calls , Uid = Guid . NewGuid ( ) . ToByteArray ( ) . PadTo32Bytes ( ) } ;
152
- var signature = await EIP712 . GenerateSignature_SmartAccount_7702_WrappedCalls ( "MinimalAccount" , "1" , this . ChainId , userWalletAddress , wrappedCalls , this . UserWallet ) ;
153
- var response = await BundlerClient . TwExecute (
154
- client : this . Client ,
155
- // url: $"{this.ChainId}.bundler.thirdweb.com",
156
- url : "http://localhost:8787?chain=11155111" ,
157
- requestId : 7702 ,
158
- eoaAddress : userWalletAddress ,
159
- wrappedCalls : wrappedCalls ,
160
- signature : signature ,
161
- authorization : this . Authorization != null && ! await Utils . IsDelegatedAccount ( this . Client , this . ChainId , userWalletAddress ) ? this . Authorization : null
162
- ) ;
163
- var queueId = response ? . QueueId ;
164
- string txHash = null ;
165
- var ct = new CancellationTokenSource ( this . Client . FetchTimeoutOptions . GetTimeout ( TimeoutType . Other ) ) ;
166
- try
167
- {
168
- while ( txHash == null )
169
- {
170
- ct . Token . ThrowIfCancellationRequested ( ) ;
171
-
172
- var hashResponse = await BundlerClient
173
- . TwGetTransactionHash (
174
- client : this . Client ,
175
- // url: $"{this.ChainId}.bundler.thirdweb.com",
176
- url : "http://localhost:8787?chain=11155111" ,
177
- requestId : 7702 ,
178
- queueId
179
- )
180
- . ConfigureAwait ( false ) ;
181
-
182
- txHash = hashResponse ? . TransactionHash ;
183
- await ThirdwebTask . Delay ( 100 , ct . Token ) . ConfigureAwait ( false ) ;
184
- }
185
- return txHash ;
186
- }
187
- catch ( OperationCanceledException )
188
- {
189
- throw new Exception ( $ "EIP-7702 sponsored transaction timed out with queue id: { queueId } ") ;
190
- }
191
- case ExecutionMode . EOA :
192
- // Add up values of all calls
193
- BigInteger totalValue = 0 ;
194
- foreach ( var call in calls )
144
+ var wrappedCalls = new WrappedCalls ( ) { Calls = calls , Uid = Guid . NewGuid ( ) . ToByteArray ( ) . PadTo32Bytes ( ) } ;
145
+ var signature = await EIP712 . GenerateSignature_SmartAccount_7702_WrappedCalls ( "MinimalAccount" , "1" , this . ChainId , userWalletAddress , wrappedCalls , this . UserWallet ) ;
146
+ var response = await BundlerClient . TwExecute (
147
+ client : this . Client ,
148
+ // url: $"{this.ChainId}.bundler.thirdweb.com",
149
+ url : "http://localhost:8787?chain=11155111" ,
150
+ requestId : 7702 ,
151
+ eoaAddress : userWalletAddress ,
152
+ wrappedCalls : wrappedCalls ,
153
+ signature : signature ,
154
+ authorization : this . Authorization != null && ! await Utils . IsDelegatedAccount ( this . Client , this . ChainId , userWalletAddress ) ? this . Authorization : null
155
+ ) ;
156
+ var queueId = response ? . QueueId ;
157
+ string txHash = null ;
158
+ var ct = new CancellationTokenSource ( this . Client . FetchTimeoutOptions . GetTimeout ( TimeoutType . Other ) ) ;
159
+ try
160
+ {
161
+ while ( txHash == null )
195
162
{
196
- totalValue += call . Value ;
163
+ ct . Token . ThrowIfCancellationRequested ( ) ;
164
+
165
+ var hashResponse = await BundlerClient
166
+ . TwGetTransactionHash (
167
+ client : this . Client ,
168
+ // url: $"{this.ChainId}.bundler.thirdweb.com",
169
+ url : "http://localhost:8787?chain=11155111" ,
170
+ requestId : 7702 ,
171
+ queueId
172
+ )
173
+ . ConfigureAwait ( false ) ;
174
+
175
+ txHash = hashResponse ? . TransactionHash ;
176
+ await ThirdwebTask . Delay ( 100 , ct . Token ) . ConfigureAwait ( false ) ;
197
177
}
198
- // Prepare a tx using the user wallet as the executor
199
- var finalTx = await this . UserContract . Prepare ( wallet : this . UserWallet , method : "execute" , weiValue : totalValue , parameters : new object [ ] { calls } ) ;
200
- finalTx . Input . AuthorizationList = this . Authorization != null ? new List < EIP7702Authorization > ( ) { this . Authorization . Value } : null ;
201
-
202
- // Append authorization if not delegated yet
203
-
204
- // Send the transaction and return the
205
- return await ThirdwebTransaction . Send ( finalTx ) ;
206
- default :
207
- throw new NotImplementedException ( $ "Execution mode { this . ExecutionMode } is not supported.") ;
178
+ return txHash ;
179
+ }
180
+ catch ( OperationCanceledException )
181
+ {
182
+ throw new Exception ( $ "EIP-7702 sponsored transaction timed out with queue id: { queueId } ") ;
183
+ }
184
+ }
185
+ else
186
+ {
187
+ BigInteger totalValue = 0 ;
188
+ foreach ( var call in calls )
189
+ {
190
+ totalValue += call . Value ;
191
+ }
192
+ var finalTx = await this . UserContract . Prepare ( wallet : this . UserWallet , method : "execute" , weiValue : totalValue , parameters : new object [ ] { calls } ) ;
193
+ finalTx . Input . AuthorizationList = this . Authorization != null ? new List < EIP7702Authorization > ( ) { this . Authorization . Value } : null ;
194
+ return await ThirdwebTransaction . Send ( finalTx ) ;
208
195
}
209
196
}
210
197
0 commit comments