55
66namespace Thirdweb ;
77
8- public enum ExecutionMode
9- {
10- EOA ,
11- EIP7702 ,
12- }
13-
148/// <summary>
159/// Represents a 7702 delegated wallet with granular session key permissions and automatic session key execution.
1610/// </summary>
@@ -24,27 +18,27 @@ public class SmarterWallet : IThirdwebWallet
2418 internal IThirdwebWallet UserWallet { get ; }
2519 internal ThirdwebContract UserContract { get ; }
2620 internal BigInteger ChainId { get ; }
27- internal ExecutionMode ExecutionMode { get ; }
21+ internal bool SponsorGas { get ; }
2822
2923 private EIP7702Authorization ? Authorization { get ; set ; }
3024
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 )
3226 {
3327 this . Client = client ;
3428 this . ChainId = chainId ;
3529 this . UserWallet = userWallet ;
3630 this . UserContract = userContract ;
3731 this . Authorization = authorization ;
38- this . ExecutionMode = executionMode ;
32+ this . SponsorGas = sponsorGas ;
3933 }
4034
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 )
4236 {
4337 var userWalletAddress = await userWallet . GetAddress ( ) ;
4438 var userContract = await ThirdwebContract . Create ( client , userWalletAddress , chainId , Constants . MINIMAL_ACCOUNT_7702_ABI ) ;
4539 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 ) ;
4842 Utils . TrackConnection ( wallet ) ;
4943 return wallet ;
5044 }
@@ -145,66 +139,59 @@ public async Task<string> SendTransaction(ThirdwebTransactionInput transaction)
145139 }
146140 } ;
147141
148- switch ( this . ExecutionMode )
142+ if ( this . SponsorGas )
149143 {
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 )
195162 {
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 ) ;
197177 }
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 ) ;
208195 }
209196 }
210197
0 commit comments