@@ -43,34 +43,56 @@ const accounts = web3.eth.accounts.wallet.add(privatekey);
4343/*
4444 -- Deploy Contract --
4545*/
46+ // 添加错误处理和日志
4647const Deploy = async ( ) => {
47- // Create contract instance
48- const deployContract = new web3 . eth . Contract ( abi ) ;
49-
50- // Create Tx
51- const deployTx = deployContract . deploy ( {
52- data : '0x' + bytecode ,
53- arguments : [ 0 ] , // Pass arguments to the contract constructor on deployment(_initialNumber in Incremental.sol)
54- } ) ;
55-
56- // optionally, estimate the gas that will be used for development and log it
57- const gas = await deployTx . estimateGas ( {
58- from : accounts ,
59- } ) ;
60- console . log ( 'estimated gas:' , gas ) ;
61-
6248 try {
63- // Deploy the contract to the Ganache network
64- // Your deployed contrac can be viewed at: https://sepolia.etherscan.io/address/${tx.options.address}
65- // You can change sepolia in above url to your selected testnet.
49+ // 添加部署前的检查
50+ if ( ! process . env . INFURA_ID ) {
51+ throw new Error ( 'Missing INFURA_ID environment variable' ) ;
52+ }
53+ if ( ! process . env . PRIVATE_KEY ) {
54+ throw new Error ( 'Missing PRIVATE_KEY environment variable' ) ;
55+ }
56+
57+ // Create contract instance
58+ const deployContract = new web3 . eth . Contract ( abi ) ;
59+
60+ // Create Tx
61+ const deployTx = deployContract . deploy ( {
62+ data : '0x' + bytecode ,
63+ arguments : [ 0 ] , // Pass arguments to the contract constructor on deployment(_initialNumber in Incremental.sol)
64+ } ) ;
65+
66+ // optionally, estimate the gas that will be used for development and log it
67+ const gas = await deployTx . estimateGas ( {
68+ from : accounts ,
69+ } ) ;
70+ console . log ( 'estimated gas:' , gas ) ;
71+
72+ // 添加更详细的日志
73+ console . log ( 'Contract deployment started...' ) ;
74+ console . log ( 'Network:' , await web3 . eth . net . getNetworkType ( ) ) ;
75+ console . log ( 'Account:' , accounts [ 0 ] . address ) ;
76+
6677 const tx = await deployTx . send ( {
6778 from : accounts [ 0 ] . address ,
6879 gas,
69- // gasPrice: 10000000000,
7080 } ) ;
71- console . log ( 'Contract deployed at address: ' + tx . options . address ) ;
81+
82+ console . log ( 'Contract deployed successfully!' ) ;
83+ console . log ( 'Contract address:' , tx . options . address ) ;
84+ console . log ( 'Transaction hash:' , tx . transactionHash ) ;
85+
86+ // 添加部署后的验证
87+ const code = await web3 . eth . getCode ( tx . options . address ) ;
88+ if ( code === '0x' ) {
89+ throw new Error ( 'Contract deployment failed - no code at address' ) ;
90+ }
91+
92+ return tx . options . address ;
7293 } catch ( error ) {
73- console . error ( error ) ;
94+ console . error ( 'Deployment failed:' , error . message ) ;
95+ // throw error;
7496 }
7597} ;
7698
0 commit comments