@@ -17,13 +17,77 @@ console.log(chalk.blue('Running anvil base node'), {
1717 NEXT_PUBLIC_BASE_CHAIN_ID : $ . env . NEXT_PUBLIC_BASE_CHAIN_ID ,
1818} )
1919
20- const baseBaseFee = await $ `cast base-fee --rpc-url $ANVIL_BASE_FORK_URL`
21- const baseGasPrice = await $ `cast gas-price --rpc-url $ANVIL_BASE_FORK_URL`
20+ const parseCastBigInt = ( stdout : string , label : string ) : bigint => {
21+ const tokens = stdout
22+ . split ( / \s + / )
23+ . map ( ( token ) => token . replace ( / [ , ; ] + $ / , '' ) )
24+ . filter ( Boolean )
25+
26+ for ( let i = tokens . length - 1 ; i >= 0 ; i -- ) {
27+ const candidate = tokens [ i ]
28+ if ( / ^ - ? (?: 0 x ) ? [ 0 - 9 a - f A - F ] + $ / . test ( candidate ) ) {
29+ try {
30+ return BigInt ( candidate )
31+ } catch ( error ) {
32+ console . warn ( chalk . yellow ( `Failed to parse ${ label } token "${ candidate } " as BigInt` ) , error )
33+ }
34+ }
35+ }
36+
37+ throw new Error (
38+ `Unable to parse numeric output for ${ label } from cast command. Raw stdout: ${ stdout } `
39+ )
40+ }
41+
42+ const runCastBigInt = async (
43+ label : string ,
44+ fn : ( ) => ProcessPromise < ProcessOutput >
45+ ) : Promise < bigint > => {
46+ const maxAttempts = 3
47+ let lastError : unknown
48+
49+ for ( let attempt = 1 ; attempt <= maxAttempts ; attempt ++ ) {
50+ try {
51+ const result = await fn ( )
52+ const stdout = result . stdout ?. trim ?.( ) ? result . stdout . trim ( ) : `${ result } ` . trim ( )
53+ if ( ! stdout ) {
54+ throw new Error ( `cast returned empty stdout for ${ label } ` )
55+ }
56+ return parseCastBigInt ( stdout , label )
57+ } catch ( error ) {
58+ lastError = error
59+ console . warn (
60+ chalk . yellow ( `Attempt ${ attempt } /${ maxAttempts } to fetch ${ label } failed. Retrying...` ) ,
61+ error
62+ )
63+ await new Promise ( ( resolve ) => setTimeout ( resolve , attempt * 1000 ) )
64+ }
65+ }
66+
67+ throw new Error ( `Failed to resolve ${ label } after ${ maxAttempts } attempts` , {
68+ cause : lastError ,
69+ } )
70+ }
71+
72+ const baseBaseFee = (
73+ await runCastBigInt (
74+ 'base base fee' ,
75+ ( ) => $ `cast base-fee --rpc-url ${ $ . env . ANVIL_BASE_FORK_URL } `
76+ )
77+ ) . toString ( )
78+ const baseGasPrice = (
79+ await runCastBigInt (
80+ 'base gas price' ,
81+ ( ) => $ `cast gas-price --rpc-url ${ $ . env . ANVIL_BASE_FORK_URL } `
82+ )
83+ ) . toString ( )
2284
2385// do not fork from the absolute latest block
24- const blockHeight = await $ `cast bn --rpc-url $ANVIL_BASE_FORK_URL` . then (
25- ( r ) => BigInt ( r . stdout . trim ( ) ) - 30n
86+ const remoteBlockHeight = await runCastBigInt (
87+ 'latest block' ,
88+ ( ) => $ `cast bn --rpc-url ${ $ . env . ANVIL_BASE_FORK_URL } `
2689)
90+ const blockHeight = remoteBlockHeight > 30n ? remoteBlockHeight - 30n : remoteBlockHeight
2791
2892await $ `docker rm -f sendapp-anvil-base`
2993
0 commit comments