55 waitAndVerify ,
66 loadOutput ,
77 saveOutput ,
8- DeploymentAddresses
8+ DeploymentAddresses ,
9+ checkSufficientBalance ,
10+ isContractDeployed
911} from '../deployUtils' ;
1012import { setupBigBlocksForV4Deployment } from './enableBigBlocks' ;
1113import { isBigBlocksSupported } from '../config/bigBlocksConfig' ;
@@ -39,6 +41,69 @@ async function main() {
3941 `🚀 Deployer: ${ deployerAddress } (nonce: ${ currentNonce } ) on chain ${ chainId } `
4042 ) ;
4143
44+ // Pre-deployment balance check - estimate total cost for all contracts
45+ console . log ( '\n--- Checking deployer balance ---' ) ;
46+
47+ // Estimate gas costs for all potential deployments
48+ let totalEstimatedCost = 0n ;
49+
50+ // Only estimate if we need to deploy (not already deployed)
51+ if ( ! output . walletImplementation || ! ( await isContractDeployed ( output . walletImplementation ) ) ) {
52+ const WalletSimple = await ethers . getContractFactory ( chainConfig . walletImplementationContractName ) ;
53+ const walletGas = await deployer . estimateGas ( {
54+ ...await WalletSimple . getDeployTransaction ( ...[ ] , gasOverrides ) ,
55+ from : deployerAddress
56+ } ) ;
57+ totalEstimatedCost += walletGas ;
58+ }
59+
60+ if ( ! output . walletFactory || ! ( await isContractDeployed ( output . walletFactory ) ) ) {
61+ const WalletFactory = await ethers . getContractFactory ( chainConfig . walletFactoryContractName ) ;
62+ const factoryGas = await deployer . estimateGas ( {
63+ ...await WalletFactory . getDeployTransaction ( deployerAddress , gasOverrides ) , // Use deployer address as placeholder
64+ from : deployerAddress
65+ } ) ;
66+ totalEstimatedCost += factoryGas ;
67+ }
68+
69+ if ( ! output . forwarderImplementation || ! ( await isContractDeployed ( output . forwarderImplementation ) ) ) {
70+ const ForwarderV4 = await ethers . getContractFactory ( chainConfig . forwarderContractName ) ;
71+ const forwarderGas = await deployer . estimateGas ( {
72+ ...await ForwarderV4 . getDeployTransaction ( gasOverrides ) ,
73+ from : deployerAddress
74+ } ) ;
75+ totalEstimatedCost += forwarderGas ;
76+ }
77+
78+ if ( ! output . forwarderFactory || ! ( await isContractDeployed ( output . forwarderFactory ) ) ) {
79+ const ForwarderFactory = await ethers . getContractFactory ( chainConfig . forwarderFactoryContractName ) ;
80+ const forwarderFactoryGas = await deployer . estimateGas ( {
81+ ...await ForwarderFactory . getDeployTransaction ( deployerAddress , gasOverrides ) , // Use deployer address as placeholder
82+ from : deployerAddress
83+ } ) ;
84+ totalEstimatedCost += forwarderFactoryGas ;
85+ }
86+
87+ if ( totalEstimatedCost > 0n ) {
88+ // Get gas price for cost calculation
89+ const feeData = await ethers . provider . getFeeData ( ) ;
90+ let gasPrice : bigint ;
91+
92+ if ( gasOverrides ?. gasPrice ) {
93+ gasPrice = gasOverrides . gasPrice ;
94+ } else if ( gasOverrides ?. maxFeePerGas ) {
95+ gasPrice = gasOverrides . maxFeePerGas ;
96+ } else {
97+ gasPrice = feeData . gasPrice || 1_000_000_000n ; // 1 gwei fallback
98+ }
99+
100+ const estimatedCost = totalEstimatedCost * gasPrice ;
101+
102+ await checkSufficientBalance ( deployerAddress , estimatedCost , 'all V4 contracts' ) ;
103+ } else {
104+ console . log ( '✅ All contracts already deployed, skipping balance check' ) ;
105+ }
106+
42107 // Deploy Wallet Implementation
43108 const walletAddress = await deployIfNeededAtNonce (
44109 output . walletImplementation ,
@@ -69,7 +134,8 @@ async function main() {
69134 output . walletImplementation = contract . target as string ;
70135 saveOutput ( output ) ;
71136 return contract . target as string ;
72- }
137+ } ,
138+ gasOverrides
73139 ) ;
74140
75141 // Deploy Wallet Factory
@@ -96,7 +162,8 @@ async function main() {
96162 output . walletFactory = contract . target as string ;
97163 saveOutput ( output ) ;
98164 return contract . target as string ;
99- }
165+ } ,
166+ gasOverrides
100167 ) ;
101168
102169 // Deploy Forwarder
@@ -118,7 +185,8 @@ async function main() {
118185 output . forwarderImplementation = contract . target as string ;
119186 saveOutput ( output ) ;
120187 return contract . target as string ;
121- }
188+ } ,
189+ gasOverrides
122190 ) ;
123191
124192 // Deploy Forwarder Factory
@@ -148,7 +216,8 @@ async function main() {
148216 output . forwarderFactory = contract . target as string ;
149217 saveOutput ( output ) ;
150218 return contract . target as string ;
151- }
219+ } ,
220+ gasOverrides
152221 ) ;
153222
154223 console . log ( `🎉 All contracts deployed and verified!` ) ;
0 commit comments