在Rinkeby测试网部署合约时遭遇Gas Limit相关未处理错误
Let's walk through why you're hitting these errors and how to resolve them step by step:
Understanding the Two Errors
- First error ("The contract code couldn't be stored, please check your gas limit"): Your initial gas limit of
1000000was too low. Deploying a contract requires gas to execute the constructor and store the contract bytecode on-chain, and your contract needs more than 1 million gas to complete this process. - Second error ("exceeds block gas limit"): Rinkeby testnet has a maximum gas limit per block (this is a network-wide constraint). When you set the gas limit to
10000000, you exceeded this block-level cap, so the network rejected your transaction.
Solutions to Deploy Successfully
1. Estimate Exact Gas Requirements
Instead of guessing gas limits, use Web3's built-in estimateGas method to calculate exactly how much gas your contract deployment needs. Add a small buffer (10-20% is safe) to account for any unexpected network variance.
Here's how to modify your code:
// First estimate the required gas const gasEstimate = await new web3.eth.Contract(JSON.parse(interface)) .deploy({data: bytecode, arguments: [100, accounts[0]]}) .estimateGas({from: accounts[0]}); // Add a buffer (e.g., 100,000 gas) to avoid estimation shortfalls const safeGasLimit = gasEstimate + 100000; // Now deploy with the safe gas limit const result = await new web3.eth.Contract(JSON.parse(interface)) .deploy({data: bytecode, arguments: [100, accounts[0]]}) .send({gas: safeGasLimit, from: accounts[0]});
2. Verify the Rinkeby Block Gas Limit
To confirm you're not exceeding the network's block gas limit, you can fetch the latest block's gas limit with this code:
const latestBlock = await web3.eth.getBlock("latest"); console.log("Current Rinkeby block gas limit:", latestBlock.gasLimit);
Ensure your safeGasLimit is below this value. Rinkeby's block gas limit is typically around 10-15 million, but it can fluctuate—so checking dynamically is a good practice.
3. Audit Your Contract's Constructor
If the estimated gas is unexpectedly high, check your contract's constructor. Common culprits include:
- Large arrays or structs being initialized in storage
- Complex calculations or loops that run during deployment
- Multiple external calls to other contracts in the constructor
Simplifying these operations can reduce your deployment gas cost.
4. Handle Promise Rejections Properly
You're seeing UnhandledPromiseRejectionWarning because you're not catching errors in your async code. Wrap your deployment in a try/catch block to handle failures gracefully:
try { const gasEstimate = await new web3.eth.Contract(JSON.parse(interface)) .deploy({data: bytecode, arguments: [100, accounts[0]]}) .estimateGas({from: accounts[0]}); const safeGasLimit = gasEstimate + 100000; const result = await new web3.eth.Contract(JSON.parse(interface)) .deploy({data: bytecode, arguments: [100, accounts[0]]}) .send({gas: safeGasLimit, from: accounts[0]}); console.log("Contract deployed successfully at:", result.options.address); } catch (error) { console.error("Deployment failed with error:", error); }
内容的提问来源于stack exchange,提问作者user3289405

