Now to deploy our contract, let’s create a deploy.js file in a new directory named scripts.
Copy-paste the following into your deploy.js file:
async function main() {
const [deployer] = await ethers.getSigners();
console.log(
"Deploying contracts with the account:",
deployer.address
);
console.log("Account balance:", (await deployer.getBalance()).toString());
const HelloWorld = await ethers.getContractFactory("HelloWorld");
const contract = await HelloWorld.deploy();
console.log("Contract deployed at:", contract.address);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
async function main() {
const [deployer] = await ethers.getSigners();
console.log(
"Deploying contracts with the account:",
deployer.address
);
console.log("Account balance:", (await deployer.getBalance()).toString());
const HelloWorld = await ethers.getContractFactory("HelloWorld");
const contract = await HelloWorld.deploy();
console.log("Contract deployed at:", contract.address);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
async function main() {
const [deployer] = await ethers.getSigners();
console.log(
"Deploying contracts with the account:",
deployer.address
);
console.log("Account balance:", (await deployer.getBalance()).toString());
const HelloWorld = await ethers.getContractFactory("HelloWorld");
const contract = await HelloWorld.deploy();
console.log("Contract deployed at:", contract.address);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
Explanation of the code above.
Line 1: Starting an async function.
Line 3: Getting the Ethereum address to sign the transaction and storing it in the deployer address.
Line 5-8: Printing the Ethereum address along with a string to the console.
Line 10: Printing the balance of the Ethereum address in wei to console. Wei is the smallest unit of ether, one Wei = 10^−18 ETH.
Line 12: Calling the ethers.js method
ContractFactory. This will look for the "HelloWorld.sol" file, and return an instance that we can use ContractFactory methods on.
Line 13: Calling deploy on ContractFactory to deploy the contract.
Line 15: Printing the address of the deployed contract to the console.
Line 18: Invoking the function "main".
Line 19-22: Checking for error, printing if any error, and exiting the process.
Save the file and run the following to deploy the contract.
npx hardhat run scripts/deploy.js --network ropsten
npx hardhat run scripts/deploy.js --network ropsten
npx hardhat run scripts/deploy.js --network ropsten
Note: The network flag can be skipped if deploying to the mainnet.
On the successful deployment of the contract you will see an output containing the deployed contract’s address.
We can verify the deployment by copying and pasting the deployed contract’s address in
Ropsten Etherscan’s search bar. This will display information about the deployed contract.