Ethereum Deployment Script: What went wrong?

When it comes to deploying smart contracts on the Ethereum blockchain, a well-crafted deployment script is crucial to success. In this article, we’ll delve into an issue that can arise when you try to use console.log statements after exporting your module.
The Problem
In your example code snippet, you use the module.exports command to export your deployed contract. However, you’ve run into an issue with the console log statements in this script.
const { network } = require('"hardhat"');
const {
development chains,
networkConfig,
INITIAL_DEPLOYMENT,
} = require('"../helper-hardhat-config"');
module.exports = {
// Your deployed contract code here
};
When you run your deployment script, console.log statements will not be executed because they are placed outside of an export object.
The Fix
To resolve this issue, you need to wrap your console log statements in an import statement or conditional block that allows them to be evaluated. Here is an updated version of your code snippet:
const { network } = require('"hardhat"');
const {
development chains,
networkConfig,
START_DEPLOYMENT,
} = require('"../helper-hardhat-config"');
module.exports = () => {
console.log('Deployment started!');
if (network.name === 'mainnet') {
console.log('Deployed to mainnet!');
} else if (developmentChains.includes(network.name)) {
console.log('Deployed to development chain!');
}
const { accounts, getContractAddress } = require('../helper-hardhat-config');
// Your deployed contract code here
};
In this updated version:
- We wrap the
console.logexpressions in an arrow function (() => {}), which allows us to evaluate them.
- We use an import statement to access
accounts' andgetContractAddress’.
- The conditional block checks whether you are deploying to a specific network (mainnet or development chain) before exporting your contract code.
Best Practices
To avoid similar issues in the future, make sure to:
- Use export statements in functions or other blocks that allow evaluation.
- Avoid console logging statements in import statements or when evaluating expressions.
- Consider using a separate module for your deployed contract logic to keep it organized and maintainable.
