@@ -130,126 +130,111 @@ describe('ComposableBatch — add, length, clear, toCalldata', () => {
130130 expect ( batch . length ) . toBe ( 0 ) ;
131131 } ) ;
132132
133- it ( 'add(single call) increments length by 1' , async ( ) => {
133+ it ( 'add(single call) increments length by 1' , ( ) => {
134134 const batch = createComposableBatch ( publicClient , ACCOUNT ) ;
135- const call = await batch
136- . contract ( USDC , erc20Abi )
137- . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ;
138- batch . add ( call ) ;
135+ batch . add ( batch . contract ( USDC , erc20Abi ) . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ) ;
139136 expect ( batch . length ) . toBe ( 1 ) ;
140137 } ) ;
141138
142- it ( 'add(array of calls) increments length by the array size' , async ( ) => {
139+ it ( 'add(array of calls) increments length by the array size' , ( ) => {
143140 const batch = createComposableBatch ( publicClient , ACCOUNT ) ;
144141 const token = batch . contract ( USDC , erc20Abi ) ;
145- const calls = await Promise . all ( [
142+ batch . add ( [
146143 token . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ,
147144 token . write ( { functionName : 'approve' , args : [ WETH , 1n ] } ) ,
148145 ] ) ;
149- batch . add ( calls ) ;
150146 expect ( batch . length ) . toBe ( 2 ) ;
151147 } ) ;
152148
153- it ( 'add() can be called multiple times and accumulates calls' , async ( ) => {
149+ it ( 'add() can be called multiple times and accumulates calls' , ( ) => {
154150 const batch = createComposableBatch ( publicClient , ACCOUNT ) ;
155151 const token = batch . contract ( USDC , erc20Abi ) ;
156- batch . add ( await token . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ) ;
157- batch . add ( await token . write ( { functionName : 'approve' , args : [ WETH , 1n ] } ) ) ;
152+ batch . add ( token . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ) ;
153+ batch . add ( token . write ( { functionName : 'approve' , args : [ WETH , 1n ] } ) ) ;
158154 expect ( batch . length ) . toBe ( 2 ) ;
159155 } ) ;
160156
161- it ( 'clear() resets length to 0' , async ( ) => {
157+ it ( 'clear() resets length to 0' , ( ) => {
162158 const batch = createComposableBatch ( publicClient , ACCOUNT ) ;
163- const call = await batch
164- . contract ( USDC , erc20Abi )
165- . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ;
166- batch . add ( call ) ;
159+ batch . add ( batch . contract ( USDC , erc20Abi ) . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ) ;
167160 batch . clear ( ) ;
168161 expect ( batch . length ) . toBe ( 0 ) ;
169162 } ) ;
170163
171164 it ( 'toCalldata() returns a hex string' , async ( ) => {
172165 const batch = createComposableBatch ( publicClient , ACCOUNT ) ;
173- const call = await batch
174- . contract ( USDC , erc20Abi )
175- . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ;
176- batch . add ( call ) ;
177- const calldata = await batch . toCalldata ( ) ;
178- expect ( calldata ) . toMatch ( / ^ 0 x [ 0 - 9 a - f A - F ] + $ / ) ;
166+ batch . add ( batch . contract ( USDC , erc20Abi ) . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ) ;
167+
168+ expect ( await batch . toCalldata ( ) ) . toMatch ( / ^ 0 x [ 0 - 9 a - f A - F ] + $ / ) ;
179169 } ) ;
180170
181171 it ( 'toCalldata() with multiple calls returns a hex string' , async ( ) => {
182172 const batch = createComposableBatch ( publicClient , ACCOUNT ) ;
183173 const token = batch . contract ( USDC , erc20Abi ) ;
184- batch . add ( await token . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ) ;
185- batch . add ( await token . write ( { functionName : 'approve' , args : [ WETH , 1_000_000n ] } ) ) ;
186- const calldata = await batch . toCalldata ( ) ;
187- expect ( calldata ) . toMatch ( / ^ 0 x [ 0 - 9 a - f A - F ] + $ / ) ;
174+ batch . add ( token . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ) ;
175+ batch . add ( token . write ( { functionName : 'approve' , args : [ WETH , 1_000_000n ] } ) ) ;
176+
177+ expect ( await batch . toCalldata ( ) ) . toMatch ( / ^ 0 x [ 0 - 9 a - f A - F ] + $ / ) ;
188178 } ) ;
189179
190180 it ( 'toCalldata() produces different output for different calls' , async ( ) => {
191181 const batchA = createComposableBatch ( publicClient , ACCOUNT ) ;
192182 const batchB = createComposableBatch ( publicClient , ACCOUNT ) ;
193- const [ transferCall , approveCall ] = await Promise . all ( [
183+ batchA . add (
194184 batchA . contract ( USDC , erc20Abi ) . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ,
185+ ) ;
186+ batchB . add (
195187 batchB . contract ( USDC , erc20Abi ) . write ( { functionName : 'approve' , args : [ WETH , 1n ] } ) ,
196- ] ) ;
197- batchA . add ( transferCall ) ;
198- batchB . add ( approveCall ) ;
188+ ) ;
199189 const [ a , b ] = await Promise . all ( [ batchA . toCalldata ( ) , batchB . toCalldata ( ) ] ) ;
200190 expect ( a ) . not . toBe ( b ) ;
201191 } ) ;
202192} ) ;
203193
204194// ---------------------------------------------------------------------------
205- // ComposableBatch — calls getter
195+ // ComposableBatch — toCalls
206196// ---------------------------------------------------------------------------
207197
208- describe ( 'ComposableBatch — calls getter ' , ( ) => {
209- it ( 'calls is empty on a fresh batch' , ( ) => {
198+ describe ( 'ComposableBatch — toCalls ' , ( ) => {
199+ it ( 'toCalls() returns empty array on a fresh batch' , async ( ) => {
210200 const batch = createComposableBatch ( publicClient , ACCOUNT ) ;
211- expect ( batch . calls ) . toHaveLength ( 0 ) ;
201+ expect ( await batch . toCalls ( ) ) . toHaveLength ( 0 ) ;
212202 } ) ;
213203
214- it ( 'calls reflects added single call' , async ( ) => {
204+ it ( 'toCalls() reflects a single added call' , async ( ) => {
215205 const batch = createComposableBatch ( publicClient , ACCOUNT ) ;
216- const call = await batch
206+ const writePromise = batch
217207 . contract ( USDC , erc20Abi )
218208 . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ;
219- batch . add ( call ) ;
220- expect ( batch . calls ) . toHaveLength ( 1 ) ;
221- expect ( batch . calls [ 0 ] . functionSig ) . toBe ( call . functionSig ) ;
209+ batch . add ( writePromise ) ;
210+ const calls = await batch . toCalls ( ) ;
211+ expect ( calls ) . toHaveLength ( 1 ) ;
212+ expect ( calls [ 0 ] . functionSig ) . toBe ( ( await writePromise ) . functionSig ) ;
222213 } ) ;
223214
224- it ( 'calls reflects added array of calls' , async ( ) => {
215+ it ( 'toCalls() reflects an added array of calls' , async ( ) => {
225216 const batch = createComposableBatch ( publicClient , ACCOUNT ) ;
226217 const token = batch . contract ( USDC , erc20Abi ) ;
227- batch . add (
228- await Promise . all ( [
229- token . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ,
230- token . write ( { functionName : 'approve' , args : [ WETH , 1n ] } ) ,
231- ] ) ,
232- ) ;
233- expect ( batch . calls ) . toHaveLength ( 2 ) ;
218+ batch . add ( [
219+ token . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ,
220+ token . write ( { functionName : 'approve' , args : [ WETH , 1n ] } ) ,
221+ ] ) ;
222+ expect ( await batch . toCalls ( ) ) . toHaveLength ( 2 ) ;
234223 } ) ;
235224
236- it ( 'calls is empty after clear()' , async ( ) => {
225+ it ( 'toCalls() returns empty array after clear()' , async ( ) => {
237226 const batch = createComposableBatch ( publicClient , ACCOUNT ) ;
238- batch . add (
239- await batch . contract ( USDC , erc20Abi ) . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ,
240- ) ;
227+ batch . add ( batch . contract ( USDC , erc20Abi ) . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ) ;
241228 batch . clear ( ) ;
242- expect ( batch . calls ) . toHaveLength ( 0 ) ;
229+ expect ( await batch . toCalls ( ) ) . toHaveLength ( 0 ) ;
243230 } ) ;
244231
245- it ( 'calls returns a copy — mutating it does not affect the batch' , async ( ) => {
232+ it ( 'toCalls() returns a copy — mutating it does not affect the batch' , async ( ) => {
246233 const batch = createComposableBatch ( publicClient , ACCOUNT ) ;
247- batch . add (
248- await batch . contract ( USDC , erc20Abi ) . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ,
249- ) ;
250- const snapshot = batch . calls ;
234+ batch . add ( batch . contract ( USDC , erc20Abi ) . write ( { functionName : 'transfer' , args : [ WETH , 1n ] } ) ) ;
235+ const snapshot = await batch . toCalls ( ) ;
251236 snapshot . pop ( ) ;
252- expect ( batch . calls ) . toHaveLength ( 1 ) ;
237+ expect ( await batch . toCalls ( ) ) . toHaveLength ( 1 ) ;
253238 } ) ;
254239} ) ;
255240
0 commit comments