@@ -3,7 +3,7 @@ import { getAddress, parseUnits } from 'viem';
33import { baseSepolia } from 'viem/chains' ;
44import { beforeAll , beforeEach , describe , expect , it } from 'vitest' ;
55import { createComposableBatch } from '../../core/batch' ;
6- import { account , initNexus , publicClient } from '../utils' ;
6+ import { account , initNexus , publicClient , walletClient } from '../utils' ;
77import { RUNTIME_TRANSFER_ABI } from './abi/runtime-transfer' ;
88import { fundWithUsdc , USDC , usdcBalanceOf } from './helpers' ;
99
@@ -34,10 +34,21 @@ async function ensureScaBalance(): Promise<void> {
3434 }
3535}
3636
37- // Tops up the runtime transfer contract to TRANSFER_AMOUNT if it has less than that
37+ // Resets the runtime transfer contract balance to exactly TRANSFER_AMOUNT before each test.
38+ // Drains any excess (from a previously failed test that didn't sweep) back to the EOA,
39+ // then tops up if the balance is below TRANSFER_AMOUNT.
3840async function ensureRuntimeTransferContractBalance ( ) : Promise < void > {
41+ if ( ! walletClient ) throw new Error ( 'PRIVATE_KEY is not set in environment' ) ;
3942 const balance = await usdcBalanceOf ( RUNTIME_TRANSFER_CONTRACT ) ;
40- if ( balance < TRANSFER_AMOUNT ) {
43+ if ( balance > TRANSFER_AMOUNT ) {
44+ const hash = await walletClient . writeContract ( {
45+ abi : RUNTIME_TRANSFER_ABI ,
46+ address : RUNTIME_TRANSFER_CONTRACT ,
47+ functionName : 'transferFunds' ,
48+ args : [ USDC , walletClient . account . address , balance - TRANSFER_AMOUNT ] ,
49+ } ) ;
50+ await publicClient . waitForTransactionReceipt ( { hash, confirmations : 2 } ) ;
51+ } else if ( balance < TRANSFER_AMOUNT ) {
4152 await fundWithUsdc ( RUNTIME_TRANSFER_CONTRACT , TRANSFER_AMOUNT - balance ) ;
4253 }
4354}
@@ -108,7 +119,7 @@ describe('Integration — composable execution via runtime transfer contract (Ba
108119 } ) ;
109120
110121 const { hash } = await meeClient . executeQuote ( { quote } ) ;
111- await meeClient . waitForSupertransactionReceipt ( { hash, mode : 'fast-block' } ) ;
122+ await meeClient . waitForSupertransactionReceipt ( { hash } ) ;
112123
113124 // Runtime transfer contract should be swept to zero after the transfer
114125 const contractBalanceAfter = await usdcBalanceOf ( RUNTIME_TRANSFER_CONTRACT ) ;
@@ -162,7 +173,7 @@ describe('Integration — composable execution via runtime transfer contract (Ba
162173 } ) ;
163174
164175 const { hash } = await meeClient . executeQuote ( { quote } ) ;
165- await meeClient . waitForSupertransactionReceipt ( { hash, mode : 'fast-block' } ) ;
176+ await meeClient . waitForSupertransactionReceipt ( { hash } ) ;
166177
167178 const contractBalanceAfter = await usdcBalanceOf ( RUNTIME_TRANSFER_CONTRACT ) ;
168179 expect ( contractBalanceAfter ) . toEqual ( 0n ) ;
@@ -212,7 +223,7 @@ describe('Integration — composable execution via runtime transfer contract (Ba
212223 } ) ;
213224
214225 const { hash } = await meeClient . executeQuote ( { quote } ) ;
215- await meeClient . waitForSupertransactionReceipt ( { hash, mode : 'fast-block' } ) ;
226+ await meeClient . waitForSupertransactionReceipt ( { hash } ) ;
216227
217228 const contractBalanceAfter = await usdcBalanceOf ( RUNTIME_TRANSFER_CONTRACT ) ;
218229 expect ( contractBalanceAfter ) . toEqual ( 0n ) ;
@@ -262,7 +273,7 @@ describe('Integration — composable execution via runtime transfer contract (Ba
262273 } ) ;
263274
264275 const { hash } = await meeClient . executeQuote ( { quote } ) ;
265- await meeClient . waitForSupertransactionReceipt ( { hash, mode : 'fast-block' } ) ;
276+ await meeClient . waitForSupertransactionReceipt ( { hash } ) ;
266277
267278 const contractBalanceAfter = await usdcBalanceOf ( RUNTIME_TRANSFER_CONTRACT ) ;
268279 expect ( contractBalanceAfter ) . toEqual ( 0n ) ;
@@ -312,7 +323,7 @@ describe('Integration — composable execution via runtime transfer contract (Ba
312323 } ) ;
313324
314325 const { hash } = await meeClient . executeQuote ( { quote } ) ;
315- await meeClient . waitForSupertransactionReceipt ( { hash, mode : 'fast-block' } ) ;
326+ await meeClient . waitForSupertransactionReceipt ( { hash } ) ;
316327
317328 const contractBalanceAfter = await usdcBalanceOf ( RUNTIME_TRANSFER_CONTRACT ) ;
318329 expect ( contractBalanceAfter ) . toEqual ( 0n ) ;
@@ -361,7 +372,7 @@ describe('Integration — composable execution via runtime transfer contract (Ba
361372 } ) ;
362373
363374 const { hash } = await meeClient . executeQuote ( { quote } ) ;
364- await meeClient . waitForSupertransactionReceipt ( { hash, mode : 'fast-block' } ) ;
375+ await meeClient . waitForSupertransactionReceipt ( { hash } ) ;
365376
366377 const contractBalanceAfter = await usdcBalanceOf ( RUNTIME_TRANSFER_CONTRACT ) ;
367378 expect ( contractBalanceAfter ) . toEqual ( 0n ) ;
0 commit comments