Deploying a smart contract on the Ropsten test network using a virtual wallet (Metamask), Solidity, Hardhat, and Alchemy
Making a request on ethereum using Alchemy as we cant afford to spin up our own node. Create a API key on Alchemy on Ropsten network
Change to Ropsten on your test Ethereum account from metamask and make sure your have Ropsten ETH in wallet
Inside the project folder, we’ll use npm init to initialize the project. This will create a package.json for us
Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. It helps developers when building smart contracts and dApps locally before deploying to the live chain.
Inside the project run:
npm install --save-dev hardhat
To create your Hardhat project run npx hardhat
in your project folder:
This will generate a hardhat.config.js file for us which is where we’ll specify all of the set up for our project
- contracts/ is where we’ll keep our smart contract code file
- scripts/ is where we’ll keep scripts to deploy and interact with our contract
Every transaction sent from your virtual wallet requires a signature using your unique private key. To provide our program with this permission, we can safely store our private key (and Alchemy API key) in an environment file.
Install the dotenv package in your project directory:
npm install dotenv --save
Then, create a .env file in the root directory of our project, and add your Metamask private key and HTTP Alchemy API URL to it.
To actually connect these to our code, we’ll reference these variables in our hardhat.config.js
Ethers.js is a library that makes it easier to interact and make requests to Ethreum by wrapping standard JSON-RPC methods with more user friendly methods.
Hardhat makes it super easy to integrate Plugins for additional tooling and extended functionality. We’ll be taking advantage of the Ethers plugin for contract deployment (Ethers.js has some super clean contract deployment methods).
In your project directory type:
npm install --save-dev @nomiclabs/hardhat-ethers "ethers@^5.0.0"
We’ve added several dependencies and plugins so far, now we need to update hardhat.config.js so that our project knows about all of them.
npx hardhat compile
to make sure that everything is working so far
Now that our contract is written and our configuration file is good to go, it’s time to write our contract deploy script.
A ContractFactory in ethers.js is an abstraction used to deploy new smart contracts, so HelloWorld here is a factory for instances of our hello world contract. When using the hardhat-ethers plugin ContractFactory and Contract instances are connected to the first signer by default.
const hello_world = await HelloWorld.deploy();
Calling deploy() on a ContractFactory will start the deployment, and return a Promise that resolves to a Contract. This is the object that has a method for each of our smart contract functions.
Deploying our contract:
npx hardhat run scripts/deploy.js --network ropsten
If we go to the Ropsten etherscan and search for our contract address we should able to see that it has been deployed successfully.
Here you’ll see a handful of JSON-RPC calls that Hardhat/Ethers made under the hood for us when we called the .deploy() function. Two important ones to call out here are eth_sendRawTransaction, which is the request to actually write our contract onto the Ropsten chain, and eth_getTransactionByHash which is a request to read information about our transaction given the hash (a typical pattern when sending transactions).