1- import { Address , Hex , PrivateKeyAccount , publicActions } from 'viem'
1+ import { Address , Hex , hexToBigInt , PrivateKeyAccount , publicActions } from 'viem'
22import { BaseAction , FunctionCallAction , prepareCallWithRuntimeVars , VoucherRequestAction } from '../actions/index.js'
33import { CrossChainBuilder } from './CrossChainBuilder.js'
44import { CrossChainVoucherCoordinator } from './CrossChainVoucherCoordinator.js'
5- import { InternalConfig } from './InternalConfig .js'
5+ import { NetworkEnvironment } from './NetworkEnvironment .js'
66import {
7+ ICrossChainBuilder ,
78 InternalVoucherInfo ,
89 isCall ,
910 isValidAddress ,
@@ -15,9 +16,8 @@ import {
1516} from '../types/index.js'
1617import { appendPaymasterSignature , getUserOpHash } from '../index.js'
1718import { assert } from '../sdkUtils/SdkUtils.js'
18- import { IMultiChainSmartAccount } from '../account/index.js'
1919import { Asset } from '../../contractTypes/Asset.js'
20- import { abiEncodePaymasterData } from '../../utils/index.js'
20+ import { abiEncodePaymasterData , nowSeconds } from '../../utils/index.js'
2121
2222/**
2323 * Return the minimum amount for an asset.
@@ -56,14 +56,18 @@ export class BatchBuilder {
5656 private _vars : Set < string > = new Set ( )
5757
5858 constructor (
59+ private readonly parentBuilder : ICrossChainBuilder ,
5960 private readonly ephemeralSigner : PrivateKeyAccount ,
6061 private readonly coordinator : CrossChainVoucherCoordinator ,
61- readonly config : InternalConfig ,
62- private readonly smartAccount : IMultiChainSmartAccount ,
62+ readonly config : NetworkEnvironment ,
6363 readonly paymaster : `0x${string } `,
6464 readonly chainId : bigint
6565 ) { }
6666
67+ endBatch ( ) : ICrossChainBuilder {
68+ return this . parentBuilder
69+ }
70+
6771 /**
6872 * Add a new dynamic runtime variable to the batch.
6973 * Variables can be used to store values that are not known at the time of batch creation.
@@ -127,7 +131,7 @@ export class BatchBuilder {
127131 } else {
128132 assert ( req . sourceChainId == this . chainId , `Voucher request sourceChainId ${ req . sourceChainId } does not match batch chainId ${ this . chainId } ` )
129133 }
130- assert ( ! this . coordinator . has ( req ) , `Voucher request ${ req } already exists in this BatchBuilder` )
134+ assert ( ! this . coordinator . has ( req ) , `Voucher request ${ req . ref } already exists in this BatchBuilder` )
131135
132136 req . tokens . forEach ( token => {
133137 if ( ! isValidAddress ( req . sourceChainId ! , token . token ) ) {
@@ -138,7 +142,7 @@ export class BatchBuilder {
138142 }
139143 } )
140144
141- this . coordinator . set ( req , {
145+ this . coordinator . set ( {
142146 voucher : req ,
143147 sourceBatch : this ,
144148 } )
@@ -155,13 +159,14 @@ export class BatchBuilder {
155159 /**
156160 * Use the {@link SdkVoucherRequest} created in an earlier batch to move tokens to this chain.
157161 */
158- useVoucher ( voucher : SdkVoucherRequest ) : this {
162+ useVoucher ( refId : string ) : this {
159163 this . assertNotBuilt ( )
160- const internalVoucherInfo = this . coordinator . getVoucherInternalInfo ( voucher )
161- assert ( internalVoucherInfo != null , `Voucher request ${ voucher } not found in action builder` )
164+ const internalVoucherInfo = this . coordinator . getVoucherInternalInfo ( refId )
165+ const voucher = internalVoucherInfo ?. voucher !
166+ assert ( internalVoucherInfo != null , `Voucher request ${ refId } not found in action builder` )
162167 assert ( this . userOpOverrides ?. paymaster == null && this . userOpOverrides ?. paymasterData == null ,
163168 `Cannot override paymaster or paymasterData in a batch that uses vouchers.` )
164- assert ( internalVoucherInfo . destBatch == undefined , `Voucher request ${ voucher } already used` )
169+ assert ( internalVoucherInfo . destBatch == undefined , `Voucher request ${ refId } already used` )
165170 assert ( voucher . destinationChainId == this . chainId ,
166171 `Voucher request is for chain ${ voucher . destinationChainId } , but batch is for chain ${ this . chainId } ` )
167172 internalVoucherInfo . destBatch = this
@@ -179,7 +184,7 @@ export class BatchBuilder {
179184 for ( const v of allVoucherRequests ) {
180185 if ( v . destinationChainId === this . chainId ) {
181186 added = true
182- this . useVoucher ( v )
187+ this . useVoucher ( v . ref )
183188 }
184189 }
185190 assert ( added , `No voucher requests found for chain ${ this . chainId } ` )
@@ -194,7 +199,8 @@ export class BatchBuilder {
194199 async createUserOp ( ) : Promise < UserOperation > {
195200 const chainId = this . chainId
196201
197- const smartAccount = this . smartAccount . contractOn ( chainId )
202+ const mcAccount = this . parentBuilder . getAccount ( )
203+ const smartAccount = mcAccount . contractOn ( chainId )
198204 const allCalls = await Promise . all ( this . actions . map ( ( action ) => {
199205 return action . encodeCall ( this )
200206 } ) )
@@ -209,14 +215,15 @@ export class BatchBuilder {
209215 smartAccount . getAddress ( ) ,
210216 smartAccount . getNonce ( ) ,
211217 smartAccount . getFactoryArgs ( ) ,
212- calls . length == 0 ? '0x' : this . smartAccount . encodeCalls ( chainId , calls )
218+ calls . length == 0 ? '0x' : mcAccount . encodeCalls ( chainId , calls )
213219 ] )
214220
221+ const nonce1 = BigInt ( nowSeconds ( ) ) << 64n
215222 const { maxFeePerGas, maxPriorityFeePerGas } = await smartAccount . client . extend ( publicActions ) . estimateFeesPerGas ( )
216223 let userOp = {
217224 chainId,
218225 sender,
219- nonce,
226+ nonce : nonce1 ,
220227 factory,
221228 factoryData,
222229 callData,
@@ -312,7 +319,7 @@ export class BatchBuilder {
312319 }
313320
314321 getVoucherInternalInfo ( voucher : SdkVoucherRequest ) : InternalVoucherInfo | undefined {
315- return this . coordinator . getVoucherInternalInfo ( voucher )
322+ return this . coordinator . getVoucherInternalInfo ( voucher . ref )
316323 }
317324
318325 getOutVoucherRequests ( ) : SdkVoucherRequest [ ] {
0 commit comments