Skip to content

docs: translate document to help read #1281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 43 additions & 21 deletions basic/01-web3js-deploy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,56 @@ const accounts = web3.eth.accounts.wallet.add(privatekey);
/*
-- Deploy Contract --
*/
// 添加错误处理和日志
const Deploy = async () => {
// Create contract instance
const deployContract = new web3.eth.Contract(abi);

// Create Tx
const deployTx = deployContract.deploy({
data: '0x' + bytecode,
arguments: [0], // Pass arguments to the contract constructor on deployment(_initialNumber in Incremental.sol)
});

// optionally, estimate the gas that will be used for development and log it
const gas = await deployTx.estimateGas({
from: accounts,
});
console.log('estimated gas:', gas);

try {
// Deploy the contract to the Ganache network
// Your deployed contrac can be viewed at: https://sepolia.etherscan.io/address/${tx.options.address}
// You can change sepolia in above url to your selected testnet.
// 添加部署前的检查
if (!process.env.INFURA_ID) {
throw new Error('Missing INFURA_ID environment variable');
}
if (!process.env.PRIVATE_KEY) {
throw new Error('Missing PRIVATE_KEY environment variable');
}

// Create contract instance
const deployContract = new web3.eth.Contract(abi);

// Create Tx
const deployTx = deployContract.deploy({
data: '0x' + bytecode,
arguments: [0], // Pass arguments to the contract constructor on deployment(_initialNumber in Incremental.sol)
});

// optionally, estimate the gas that will be used for development and log it
const gas = await deployTx.estimateGas({
from: accounts,
});
console.log('estimated gas:', gas);

// 添加更详细的日志
console.log('Contract deployment started...');
console.log('Network:', await web3.eth.net.getNetworkType());
console.log('Account:', accounts[0].address);

const tx = await deployTx.send({
from: accounts[0].address,
gas,
// gasPrice: 10000000000,
});
console.log('Contract deployed at address: ' + tx.options.address);

console.log('Contract deployed successfully!');
console.log('Contract address:', tx.options.address);
console.log('Transaction hash:', tx.transactionHash);

// 添加部署后的验证
const code = await web3.eth.getCode(tx.options.address);
if (code === '0x') {
throw new Error('Contract deployment failed - no code at address');
}

return tx.options.address;
} catch (error) {
console.error(error);
console.error('Deployment failed:', error.message);
// throw error;
}
};

Expand Down
18 changes: 18 additions & 0 deletions basic/02-web3js-transaction/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
networks: {
sepolia: {
http: `https://sepolia.infura.io/v3/${process.env.INFURA_ID}`,
ws: `wss://sepolia.infura.io/ws/v3/${process.env.INFURA_ID}`,
},
// 可添加其他网络配置
},
gas: {
limit: 8000000,
// 可添加 gasPrice 策略
},
contracts: {
incrementer: {
initialValue: 5,
},
},
};
Loading