Truffle
What Truffle is, what it is used for, and how to use it for development on the XDC Network.
🧭 Table of contents
📰 Overview
Truffle is a development environment to compile, deploy, test, and debug your Ethereum software.
What you will learn
This article will teach you what Truffle is, what Truffle is used for, and how to use the features of Truffle for development on the XDC Network.
What you will do
Learn what Truffle is
Learn how to create and configure Truffle project
Explore Truffle commands
Explore Truffle boxes
📰 About Truffle
Truffle is development environment which lets you compile and deploy smart contracts on Ethereum-like networks. Truffle has extensible tooling platform, solidity debugger, plugin ecosystem, Typescript support and more. It is one of the most popular development environments along with Remix and Truffle.
Why do you need development environment
Deploying smart contracts is a very complex task and can cost you a lot of money if you do it wrong, that's why you need to use development environment like Truffle.
Truffle comes with lots of tools out of the box which make you life easier and increase your productivity as dApp developer. Truffle debugging and testing tools allow you to weed out bugs and vulnerabilities in your smart contract before deploying it to live network.
⚒ Create Truffle Project
Installation of Truffle:
If truffle is not present on your system, you can use npm
to install it.
To create new Truffle simply run this command:
This will create truffle-config.js
as well as three folders: contracts
, migrations
and test
.
⚒ Truffle project structure
Truffle project has the following folders:
build
: contains contract compilation artifactscontracts
: folder for smart contract source codemigrations
: folder where migration files are stored. A migration is a script which manages deploying smart contractstest
: place to store both Javascript and Solidity tests
⚒ Truffle Config
Truffle config is stored in truffle-config.js
. Config defines which network and accounts Truffle will use, which version of compiler it will run and more.
Here is an example of Truffle config:
networks
: list of blockchain networks you will useprovider
: default web3 provider
compilers
: list of compiler configurationssolc
: solidity compiler optionssettings
: compiler settings with the same schema assettings
in Solidity JSON input interface
For the full documentation of truffle-config.js
options visit Truffle Configuration.
⚒ Truffle commands
Truffle comes with a bunch of predefined commands which automate tasks like compiling, testing, deploying and so on.
Open truffle console:
This will connect to development network, if one is defined in config.
Or you can use custom predefined network
Compile contracts:
Running tests:
Run migration script:
Add --network
parameter to deploy on specific network
To see addresses for all deployed contracts on each network, run:
You can explore more built-in tasks by running:
Or get full information on a specific task by running
⚒ Truffle Boxes
Frameworks like truffle require writing lots of boilerplate code. Each new project need truffle-config.js
, migration files, tests and more. To avoid writing this boilerplate code each time, Truffle provides boxes - a simple way to reuse already existing code.
To create new project using truffle boxes, simply run this in new folder:
Then it will take few minutes to initialize the project. This project has two main folders client
and truffle
.
truffle
folders comes with lots of boilerplate code for contracts, testing and migrating.
Most popular boxes are:
react-box
: provides basic scaffolding for dApp projectdrizzle-box
: provides everything you need to start developing react app with Drizzle, a popular dApp librarypet-shop-box
: provides code for a Truffle tutorial
You can explore the full list of Truffle boxes here or on official Truffle box page.
⚒ Using Truffle to develop on XDC
Truffle development experience on XDC is mostly indistinguishable from any other Ethereum-like network, but there are still some things you need to learn before starting developing on XDC.
⚒ Configuring XDC Mainnet and Apothem Testnet on Truffle
In order to get started deploying new contracts on XDC Mainnet and/or Apothem, we need to install two new dependencies that will be used in the truffle-config.js
file. These dependencies are @truffle/hdwallet-provider
and dotenv
. First choose your preferred package manager. In this example we are using yarn
but you can also use npm
.
If you never used yarn
before, you might need to install it first.
‼️You can skip this step if you already have yarn installed‼️
Initialize your package manager on your folder and install the required dependencies:
You will also need a 24-Word Mnemonic Phrase. To configure your wallet, create a new .env
file and write your mnemonic by running:
Remember to change the 24-Word Mnemonic above for your own mnemonic. The contents of your .env
file should read as follow:
🚨 Do not use the mnemonic in the example above in production or you can risk losing your assets and/or the ownership of your smart contracts! 🚨
And finally, we can configure the truffle-config.js
file for both Apothem and XinFin Networks by writting:
For more information about Truffle, Please Visit Truffle Documentation. For more information about XinFin Network, Please Visit XDC Network Documentation on GitBook.
Last updated