Now that you have completed the development of your DApp, it’s time to deploy it to the Ethereum network and test it out. In this chapter, we will cover the following topics:
- Deploying the DApp to a test network
- Interacting with the DApp from a web browser
- Testing the DApp using Hardhat’s built-in testing tools
Deploying the DApp to a Test Network
To deploy your DApp to a test network, you can use Hardhat’s network
command. This command allows you to specify a set of deployment configurations for each network that you want to deploy to.
To deploy your DApp to a test network, you will need to create a configuration file in the hardhat.config.js
file in the root of your project. This file should export an object containing the configuration for each network that you want to deploy to.
For example, to deploy your DApp to the Rinkeby test network, you can use the following configuration:
module.exports = {
networks: {
rinkeby: {
url: "https://rinkeby.infura.io/v3/YOUR-INFURA-API-KEY",
accounts: [
{
privateKey: "YOUR-PRIVATE-KEY",
balance: "1000000000000000000000000",
},
],
},
},
};
In this configuration, we are specifying the URL of the Rinkeby test network and providing the private key of an account that has a balance of at least 1 Ether. This account will be used to deploy your contracts.
To deploy your DApp to the Rinkeby test network, you can run the following command:
npx hardhat network --network rinkeby
This command will compile your contracts, deploy them to the Rinkeby test network, and save the deployment addresses to a file called hardhat-network.json
.
Interacting with the DApp from a Web Browser
To interact with your DApp from a web browser, you will need to use a web3 provider. A web3 provider is a JavaScript library that allows you to access the Ethereum network from a web browser.
There are several web3 providers available, but the most popular one is MetaMask. MetaMask is a browser extension that allows you to interact with the Ethereum network from within your web browser.
To use MetaMask, you will need to install the extension in your browser and create an account. Once you have created an account, you will need to import the private key of your account into MetaMask.
To import the private key of your account, click on the MetaMask icon in your browser and select “Import Account”. You will be prompted to enter your private key and create a password for your account.
Once you have imported your account, you will need to connect MetaMask to the Rinkeby test network. To do this, click on the MetaMask icon and select “Custom RPC”. In the “New Network” form, enter the following URL: https://rinkeby.infura.io/v3/YOUR-INFURA-API-KEY
.
After you have connected MetaMask to the Rinkeby test network, you can start interacting with your DApp.
Testing the DApp using Hardhat’s Built-In Testing Tools
Hardhat provides several tools for testing your DApp, including a testing framework called truffle-test-utils
and a testing library called mocha
.
To use truffle-test-utils
, you will need to install it as a dependency of your project:
npm install truffle-test-utils --save-dev
truffle-test-utils
provides several utility functions for testing your DApp, including the createMockProvider
function, which allows you to create a mock Ethereum provider for testing purposes.
To use mocha
, you will need to install it as a dependency of your project:
npm install mocha --save-dev
mocha
is a popular testing framework that allows you to write and run tests for your DApp. To use mocha
, you will need to create a test
directory in the root of your project and add a test.js
file with your tests.
For example, to test the setValue
function of your DApp, you could write the following test:
const { accounts, contract } = require("@openzeppelin/test-environment");
const MyContract = contract.fromArtifact("MyContract");
describe("MyContract", () => {
it("should set the value to 42", async () => {
const instance = await MyContract.new();
await instance.setValue(42);
assert.equal(await instance.value(), 42);
});
});
To run this test, you can use the following command:
npx mocha
This command will run all of the tests in the test
directory and display the results in the console.
Conclusion
In conclusion, deploying and testing your DApp is an important step in the development process. Hardhat provides several tools and features to help you do this efficiently and effectively. By using Hardhat’s built-in testing tools, you can ensure that your DApp is working as intended before deploying it to the Ethereum network.
Exercises
To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.
Write a test to ensure that the setValue
function of your DApp sets the value to the correct number.
const { accounts, contract } = require("@openzeppelin/test-environment");
const MyContract = contract.fromArtifact("MyContract");
describe("MyContract", () => {
it("should set the value to 42", async () => {
const instance = await MyContract.new();
await instance.setValue(42);
assert.equal(await instance.value(), 42);
});
});
Write a test to ensure that the addTwo
function of your DApp correctly adds two numbers.
const { accounts, contract } = require("@openzeppelin/test-environment");
const MyContract = contract.fromArtifact("MyContract");
describe("MyContract", () => {
it("should correctly add two numbers", async () => {
const instance = await MyContract.new();
const result = await instance.addTwo(40, 2);
assert.equal(result, 42);
});
});
Write a test to ensure that the subtractTwo
function of your DApp correctly subtracts two numbers.
const { accounts, contract } = require("@openzeppelin/test-environment");
const MyContract = contract.fromArtifact("MyContract");
describe("MyContract", () => {
it("should correctly subtract two numbers", async () => {
const instance = await MyContract.new();
const result = await instance.subtractTwo(44, 2);
assert.equal(result, 42);
});
});
Write a test to ensure that the multiplyTwo
function of your DApp correctly multiplies two numbers.
const { accounts, contract } = require("@openzeppelin/test-environment");
const MyContract = contract.fromArtifact("MyContract");
describe("MyContract", () => {
it("should correctly multiply two numbers", async () => {
const instance = await MyContract.new();
const result = await instance.multiplyTwo(7, 6);
assert.equal(result, 42);
});
});
Write a test to ensure that the divideTwo
function of your DApp correctly divides two numbers.
const { accounts, contract } = require("@openzeppelin/test-environment");
const MyContract = contract.fromArtifact("MyContract");
describe("MyContract", () => {
it("should correctly divide two numbers", async () => {
const instance = await MyContract.new();
const result = await instance.divideTwo(84, 2);
assert.equal(result, 42);
});
});