Skip to content

Commit ec18d92

Browse files
authored
Merge pull request #1281 from brandon2014-art/docs
docs: translate document to help read
2 parents 3b4e9f8 + 89941b4 commit ec18d92

File tree

11 files changed

+751
-295
lines changed

11 files changed

+751
-295
lines changed

basic/01-web3js-deploy/index.js

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,56 @@ const accounts = web3.eth.accounts.wallet.add(privatekey);
4343
/*
4444
-- Deploy Contract --
4545
*/
46+
// 添加错误处理和日志
4647
const 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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
networks: {
3+
sepolia: {
4+
http: `https://sepolia.infura.io/v3/${process.env.INFURA_ID}`,
5+
ws: `wss://sepolia.infura.io/ws/v3/${process.env.INFURA_ID}`,
6+
},
7+
// 可添加其他网络配置
8+
},
9+
gas: {
10+
limit: 8000000,
11+
// 可添加 gasPrice 策略
12+
},
13+
contracts: {
14+
incrementer: {
15+
initialValue: 5,
16+
},
17+
},
18+
};

0 commit comments

Comments
 (0)