Building a full-stack Decentralised Application on the Ethereum blockchain
Part Three: Deploying the smart contract to a local network
This blog series is broken down into five parts:
- Part One: Building the smart contract
- Part Two: Testing the smart contract
- Part Three: Deploying the smart contract to a local network
- Part Four: Deploying the smart contract to a live Ethereum network
- Part Five: Creating a front-end for the Decentralised Application
Solidity is a language that can only be run on the Ethereum Virtual Machine (EVM), which is part of all Ethereum nodes, thus for us to use our code, it needs to be deployed to the blockchain. In this step we’re going to initialise a local ethereum blockchain and then deploy our smart contract to it.
Building a deployment script
Hardhat’s task runner is going to make deploying our smart contract a piece of cake and we’re now going to write a deployment script with JavaScript for the task runner to execute. Firstly, create a new folder in the root directory of our project called scripts, then within this folder create a file called deployInbox.js. Within this file, paste the following code:
async function main() {
const Inbox = await ethers.getContractFactory("Inbox");
const inbox = await Inbox.deploy("Perry Fardella was here!");
console.log("The inbox smart contract deployed to:", inbox.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
You should recognise the first few lines here from our test file, that’s because the instantiation logic used within our test to deploy our smart contract can be used here also. Again, feel free to change the message in the constructor to anything you’d like. We’re using a console.log() function here to print to the terminal the address the smart contract gets deployed to. We are also error handling when we call the main function on deployment here so that if any errors are encountered they will be handled appropriately and printed to the terminal.
Adding deployment and local testnet scripts to our config file
It’s time to open our package.json file again and add some more scripts that we’ll be using. Add the following two scripts:
"local-testnet": "hardhat node"
This script will establish a local ethereum testnet, upon which we can deploy our smart contract.
"deploy:local": "hardhat run --network localhost scripts/deployInbox.js"
This script will deploy our ethereum smart contract onto our local ethereum testnet.
Your package.json file should now look similar to this:
{
"name": "inbox-solidity-project",
"version": "1.0.0",
"description": "A simple smart contract",
"main": "index.js",
"scripts": {
"test": "hardhat coverage",
"build": "hardhat compile",
"deploy:local": "hardhat run --network localhost scripts/deployIndex.js",
"local-testnet": "hardhat node"
},
"keywords": [],
"author": "Perry Fardella",
"license": "MIT",
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.3",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"chai": "^4.3.4",
"ethereum-waffle": "^3.4.0",
"ethers": "^5.5.2",
"hardhat": "^2.7.1",
"solidity-coverage": "^0.7.17"
}
}
Deploying our smart contract to a local testnet
We now have everything we need to run a local testnet and deploy our smart contract locally. Go ahead and open up two terminals in our project's root directory. In the first terminal we will initialise our local test net with:
npm run local-testnet
You should then see an output similar to:
> inbox-solidity-project@1.0.0 local-testnet
> hardhat node
Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/
Accounts
========
WARNING: These accounts, and their private keys, are publicly known.
Any funds sent to them on Mainnet or any other live network WILL BE LOST.
Account #0: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 (10000 ETH)
Private Key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
Account #1: 0x70997970c51812dc3a010c7d01b50e0d17dc79c8 (10000 ETH)
Private Key: 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
Account #2: 0x3c44cdddb6a900fa2b585dd299e03d12fa4293bc (10000 ETH)
Private Key: 0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a
Account #3: 0x90f79bf6eb2c4f870365e785982e1f101e93b906 (10000 ETH)
Private Key: 0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6
Account #4: 0x15d34aaf54267db7d7c367839aaf71a00a2c6a65 (10000 ETH)
Private Key: 0x47e179ec197488593b187f80a00eb0da91f1b9d0b13f8733639f19c30a34926a
Account #5: 0x9965507d1a55bcc2695c58ba16fb37d819b0a4dc (10000 ETH)
Private Key: 0x8b3a350cf5c34c9194ca85829a2df0ec3153be0318b5e2d3348e872092edffba
Account #6: 0x976ea74026e726554db657fa54763abd0c3a0aa9 (10000 ETH)
Private Key: 0x92db14e403b83dfe3df233f83dfa3a0d7096f21ca9b0d6d6b8d88b2b4ec1564e
Account #7: 0x14dc79964da2c08b23698b3d3cc7ca32193d9955 (10000 ETH)
Private Key: 0x4bbbf85ce3377467afe5d46f804f221813b2bb87f24d81f60f1fcdbf7cbf4356
Account #8: 0x23618e81e3f5cdf7f54c3d65f7fbc0abf5b21e8f (10000 ETH)
Private Key: 0xdbda1821b80551c9d65939329250298aa3472ba22feea921c0cf5d620ea67b97
Account #9: 0xa0ee7a142d267c1f36714e4a8f75612f20a79720 (10000 ETH)
Private Key: 0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6
Account #10: 0xbcd4042de499d14e55001ccbb24a551f3b954096 (10000 ETH)
Private Key: 0xf214f2b2cd398c806f84e317254e0f0b801d0643303237d97a22a48e01628897
Account #11: 0x71be63f3384f5fb98995898a86b02fb2426c5788 (10000 ETH)
Private Key: 0x701b615bbdfb9de65240bc28bd21bbc0d996645a3dd57e7b12bc2bdf6f192c82
Account #12: 0xfabb0ac9d68b0b445fb7357272ff202c5651694a (10000 ETH)
Private Key: 0xa267530f49f8280200edf313ee7af6b827f2a8bce2897751d06a843f644967b1
Account #13: 0x1cbd3b2770909d4e10f157cabc84c7264073c9ec (10000 ETH)
Private Key: 0x47c99abed3324a2707c28affff1267e45918ec8c3f20b8aa892e8b065d2942dd
Account #14: 0xdf3e18d64bc6a983f673ab319ccae4f1a57c7097 (10000 ETH)
Private Key: 0xc526ee95bf44d8fc405a158bb884d9d1238d99f0612e9f33d006bb0789009aaa
Account #15: 0xcd3b766ccdd6ae721141f452c550ca635964ce71 (10000 ETH)
Private Key: 0x8166f546bab6da521a8369cab06c5d2b9e46670292d85c875ee9ec20e84ffb61
Account #16: 0x2546bcd3c84621e976d8185a91a922ae77ecec30 (10000 ETH)
Private Key: 0xea6c44ac03bff858b476bba40716402b03e41b8e97e276d1baec7c37d42484a0
Account #17: 0xbda5747bfd65f08deb54cb465eb87d40e51b197e (10000 ETH)
Private Key: 0x689af8efa8c651a91ad287602527f3af2fe9f6501a7ac4b061667b5a93e037fd
Account #18: 0xdd2fd4581271e230360230f9337d5c0430bf44c0 (10000 ETH)
Private Key: 0xde9be858da4a475276426320d5e9262ecfc3ba460bfac56360bfa6c4c28b4ee0
Account #19: 0x8626f6940e2eb28930efb4cef49b2d1f2c9c1199 (10000 ETH)
Private Key: 0xdf57089febbacf7ba0bc227dafbffa9fc08a93fdc68e1e42411a14efcf23656e
WARNING: These accounts, and their private keys, are publicly known.
Any funds sent to them on Mainnet or any other live network WILL BE LOST.
We have now created a local ethereum network that has 20 accounts/addresses each with 10000ETH. We will now deploy our smart contract to this network by running the deployment script we created earlier. Within your second terminal, run:
npm run deploy:local
Within our first terminal you should see output similar to:
web3_clientVersion (2)
eth_chainId
eth_accounts
eth_blockNumber
eth_chainId (2)
eth_estimateGas
eth_getBlockByNumber
eth_feeHistory
eth_sendTransaction
Contract deployment: Inbox
Contract address: 0x5fbdb2315678afecb367f032d93f642f64180aa3
Transaction: 0xff2936b26312e159407beb63e05555af89ce222bfda367ba24b4ba07322f23cb
From: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
Value: 0 ETH
Gas used: 345609 of 345609
Block #1: 0x000d5d652420ae7d8f100400c5127eeddec0d1495d3279b2acff3e20c590c47d
eth_chainId
eth_getTransactionByHash
Within the second terminal you should see output similar to:
> inbox-solidity-project@1.0.0 deploy:local
> hardhat run --network localhost scripts/deployInbox.js
The inbox smart contract deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
And just like that our smart contract has been deployed locally! The outputs from our terminals show us the basic details of the deployment including:
- The smart contract address
- The transaction hash
- The value of the smart contracts wallet at launch
- The gas used
- The block the smart contract was deployed as part of
Now our smart contract has been deployed locally, let’s deploy it onto a live Ethereum test network so that it can be seen and used by the whole world.