What It Is
Truffle is a world-class development environment, testing framework and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM), aiming to make life as a developer easier.
Truffle is widely considered the most popular tool for blockchain application development with over 1.5 million lifetime downloads. Truffle supports developers across the full lifecycle of their projects, whether they are looking to build on Ethereum, Hyperledger, Quorum, or one of an ever-growing list of other supported platforms. Paired with Ganache, a personal blockchain, and Drizzle, a front-end dApp development kit, the full Truffle suite of tools promises to be an end-to-end dApp development platform.
- Built-in smart contract compilation, linking, deployment and binary management.
- Automated contract testing for rapid development.
- Scriptable, extensible deployment & migrations framework.
- Network management for deploying to any number of public & private networks.
- Package management with EthPM & NPM, using the ERC190 standard.
- Interactive console for direct contract communication.
- Configurable build pipeline with support for tight integration.
- External script runner that executes scripts within a Truffle environment.
How it Works
Truffle Suite
Truffle is the most popular development tooling for Ethereum programmers. Easily deploy smart contracts and communicate with their underlying state without heavy client side programming. An especially useful library for the testing and iteration of Ethereum smart contracts.
Connecting with Truffle
Kaleido environments are provisioned with two pre-configured Truffle boxes for the easy construction and deployment of a basic decentralized application. Truffle is a particularly useful development framework, as it abstracts many of the web3 and Ethereum complexities that typically exist with blockchain app development and allows for rapid lightweight testing and iteration. This is a great way to get started without needing deep expertise around client libraries and JSON/RPC APIs.
The first box truffle-kaleido-box compiles and instantiates the simple storage smart contract. Simple storage uses a single global variable – storedData – specified as an unsigned integer and a single method – set – for state updates.
It is accompanied by the drizzle-kaleido-box which extends the initial library with a React and Redux front end for secure interaction with your Kaleido environment.
What is Hardhat?
What is Hardhat? Hardhat is an environment developers use to test, compile, deploy and debug dApps based on the Ethereum blockchain. As such, it helps coders and developers to manage many of the tasks that are inherent to developing dApps and smart contracts. Along with providing developers with the proper tools to manage this process, Hardhat also helps automate some of these steps and provides developers with new, helpful functions.
Hardhat comes with a pre-built local Ethereum network designed with development at its core. This network focuses on Solidity debugging and features stack traces, messages when transactions of the dApps fail, etc. This provides the developers with essential tools to understand where an application fails and provides them with the answer to solve them.
The environment is characterized by plugins which a lot of the functionality originates from. This means that the developers can choose exactly which plugins they would like to include in their development process. However, it comes with built-in defaults, but they can all be overridden. This means that the Hardhat software does not care which tools that the developers end up using.
Hardhat Runner
Hardhat Runner is the command-line interface (CLI) command used to interact with Hardhat. This is a task runner, and that provides the users with many different options and alternatives. The design of the task runner revolves around plugins and tasks. This further means that every time a developer runs Hardhat from the CLI, they run a task. An example here would be:
Running this command would start the built-in compile task. Furthermore, a task can call another task which means that it is possible to develop and define complex workflows. It is also possible for Hardhat users to override tasks that already exist, meaning that the workflows are extendable and customizable.
Hardhat Network
As was mentioned in the previous section, the development environment comes with a pre-built network. This is a local Ethereum network that allows for Hardhat Ethereum development. This will enable developers to test, run, debug, and deploy the code and their written contracts.
How does the network function?
The network mines a block with every transaction and without any delay. Along with this, the network is backed by an EMV implementation, which is the same implementation utilized by other applications such as Remix, Ethereum Studio, and ganache. Furthermore, the network also supports various hard forks such as Byzantium, Constantinople, and Petersburg.
How can the network be utilized?
The network has some default behaviors; one example is that Hardhat always starts an instance if ”defaultNetwork” is set to ”hardhat” or the network is empty. Furthermore, we can use the network to run tests, tasks, and scripts.
It is also possible to add plugins to the Hardhat Ethereum Network. Some of the usable plugins are ethers.js, Waffle, Truffle, among other things, and they can connect directly to the provider.
It is also possible for external clients to connect to the network; an example of an external client is MetaMask. However, we will discover how Hardhat can connect to MetaMask later on in the article. Meanwhile, if you want to learn more about MetaMask, we recommend reading the following article for our full MetaMask breakdown.
Solidity Stack Traces
The network supports Solidity with a first-class standard. Therefore, the network knows the contracts that are running, what they intend to do, and if they fail, also provides a reason.
If a call or a transaction were to fail, the network would provide the users with an exception. This exception will give a combined Solidity and JavaScript stack trace. This means that the stack trace starts in JavaScript up to the call of a contract and then follows up with a full Solidity call stack. This means that the stack traces that the network provides will give the developer the reason why the contract or transaction failed.
Automatic Error Messages
As the network has the ability to always know what transactions or calls fail and why they are unsuccessful, it utilizes this information to make the debugging process a lot easier.
When a transaction fails without a known reason, the network will provide the users with a clear message explaining the error. Here are some examples of cases where the network will display an error message to help with the debugging process:
- When someone sends ETH to a contract without a receive function or a payable fallback
- Calling a function without the right parameters
- Calling an external function on a non-contract account
- Trying to send an insufficient amount of ETH.
- Calling a precompiled contract incorrectly.
- Trying to deploy contracts on the network that exceeds the bitcode size limit.
Furthermore, you can find more information regarding the Hardhat Network in their documentation.
How to Install Hardhat
Installing Hardhat for Ethereum development is an easy task to manage, and it only takes a few moments. Hardhat is utilized through a local installation in a developer’s projects. One of the reasons for this is that the developer will reproduce the environment and avoid future version conflicts.
The first step in the installation process is to create an npm project. We can accomplish this by locating an empty folder, run the ”npm init” command, and follow the instructions. As we finalize a project and it is ready for development, the next step is to run the following command:
As it is installed, the way to use the local installation is through the ”npx” command. An example here would be ”npx hardhat”.
How to Start Using Hardhat
In this section, we will explore the basics of how to get a project going. When exploring what is possible with Hardhat and how we do things, we will use a sample project as our basis.
As soon as we finalize the installation of the barebones version of the environment, it is possible to run tasks, compile code, run tests, use the Hardhat Network, and deploy contracts without any plugins installed.
To get a project going, we need to create a project by running ”npx hardhat” in the project folder. After we complete this task, we can create a sample project that we will use to explore how we get started with Hardhat. The sample project will ask to install ”hardhat-waffle” and ”hardhat-ethers”, enabling the project to run Waffle tests.
Tasks
The first thing that we are going to look at is what tasks are available. Running the ”npx hardhat” command will provide us with a list of all available tasks. As we are using a barebones version of Hardhat without any plugins, only a few limited tasks are built-in. However, as we add plugins, more tasks will appear in this list.
An example of a task in the sample project is the ”accounts” task. To run this task, simply input ”npx hardhat accounts”, and the outcome will be a list of all the accounts connected to the project.
Compile
The next helpful function that Hardhat provides for Ethereum development is the ability to compile code. For this, we can look at the ”contracts/” folder to explore the smart contracts of the project. In the sample project, we will find ”Greeter.sol”. To compile this, it is possible just to run the following command:
Testing Contracts
Previously we installed Waffle and Ethers.js to the sample project, which provides a few tests. However, it is also possible to use other tests from other libraries if desired. For the sample project, if we look at the ”test/” folder, we will find a sample test. It is possible to run this test by inputting the following command:
Deploying Contracts
To deploy contracts once we finalize the tests, it is possible to do so through a script. Inside the ”scripts/” folder, if we are using the sample project, we will find a sample script: ”sample-script.js”. We can run the script by simply using the following command:
Connecting Hardhat to MetaMask
One of the great things about Moralis is all the different solutions integrated with the development platform. One example of this is how easy it is to connect Hardhat to Moralis. However, before connecting Hardhat to Moralis, we must first connect it to a wallet or a dApp. In this case, we will take a closer look at how to connect Hardhat to MetaMask.
The first thing you need to do to connect Hardhat to MetaMask is to follow the steps above on how to install the development environment.
Once the installation process is complete, the next step is to run a node with the following command: ”npx hardhat node”. By doing this, you will create a local Ethereum node with some additions, such as the ability to utilize console.log().
Once the command has been run, you will be provided with an address right below the inputted command. Copy this address and move on to MetaMask. You can use the drop-down menu and select ”Custom RPC,” and use this address when creating the network.
As we finish creating the network, the account will have 0 ETH. We can move back to the console and see that several different accounts have been created. We can take the private key from any of them, import an account, and see that the account will have been provided with some ETH. However, this is just ETH on the local network which means that they are not real ETH. But it is now possible to use these ETH to test contracts and so on.