How To deploy your first smart contract on the XDC Network using Truffle
Use Truffle to deploy a Smart Contract.
🧭 Table of contents
📰 Overview
Truffle is a blockchain development environment, which you can use to create and test smart contracts by leveraging an Ethereum Virtual Machine.
What you will learn
This guide aims at teaching how to create a smart contract using Truffle and deploying it on the XDC Network.
What you will do
Install and set up Truffle
Deploy a contract on the XDC Network
Check the deployment status on xinfin.network.
🚀 Setting up the development environment
There are a few technical requirements before we start. Please install the following:
Node.js v8+ LTS and npm (comes with Node)
Once you have installed those, you only need one command to install Truffle:
To verify that Truffle is installed properly, type truffle version
on a terminal. You should see something like:
If you see an error instead, make sure that your npm modules are added to your path.
⚒ Starting a new Truffle Project
You can start by setting up your folder. In this example, we are creating a project called Pizza
. Create your new Pizza
folder by running the following on terminal:
Next, you can run truffle init
. If truffle is correctly installed on your local environment, you should see the following message:
Your folder files will look like this:
⚒ Configuring XDC Mainnet and Apothem Testnet on Truffle
In order to get started deploying new contracts on the XDC Mainnet and/or Apothem, you will 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 in 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 the XDC Network by writing:
⚒ Adding Testnet XDC to Development Wallet
It is possible to list all XDC addresses bound to your mnemonic on Truffle by accessing the Truffle console:
Once the truffle console CLI opens, you can run:
And the console should log all accounts bound to your mnemonic phrase as follow:
These accounts are on the Ethereum standard format starting with 0x
, but we can simply switch 0x
for xdc
. By default, the deployment account is the first account from the list above: xdcA4e66f4Cc17752f331eaC6A20C00756156719519
.
With this account, we can head to the Apothem Faucet and claim some TXDC for development purposes:
🍕 Writing our first Smart Contract
Lets create a simple Pizza.sol
contract on Solidity, the Pizza contract should have:
a
constructor
where the deployer can define the pizza size,a
eatSlice
method to consume slices available,a
bakeNewPizza
method to refill all slices only if the previous pizza have been entirely eaten! 😋
You can start by creating the Pizza.sol
file:
And write the following code to Pizza.sol
:
🍕 Compiling
Next, try compiling the Pizza.sol
contract by running:
If everything is correctly configured and there are no errors, you will see the following message on your console:
And your folder should look like this:
🍕 Deploying
In order to deploy our newly-compiled contract artifacts to the blockchain, we need to create a deployment script in the migrations folder.
And write the following migration script:
If the migration script has no errors, you can run the command:
for deployment on XDC mainet. Or you can run:
for deployment on the XDC Apothem Testnet. In either case, you must have enough funds to pay for gas fees on the address that is being used for development.
If the deployment is sucessful, the console should log the following message after migrations complete processing:
🍕 Interacting with your contract using Truffle Console
Another amazing tool that allow us to try out our contracts straight from our development environment is the truffle console
CLI.
To start interacting with you smart contracts you can run:
Once the console opens, you can instantiate your Pizza
contract by running:
Check if this instance
points to our deployment on chain by writing:
or simply run our eatSlice()
method:
It should log a transaction confirmation (or rejection) object like this:
This transaction is immediately reflected in the corresponding block explorer, as seen here!
🔍 Veryfing Contracts on the Block Explorer
Once you have successfully deployed your smart contract to the blockchain, you may want to verify your contract on XinFin Block Explorer.
First lets check the address our contract is deployed to by running:
If you have a contract already deployed, the console should log something like this:
If your Pizza
contract is deployed on the XDC Mainnet at 0xF899E5C79ccfa144fc76261Ad9A9F0300708FF24
. You can search for our newly deployed contract on XinFin Block Explorer:
Click in the Verify And Publish
option.
You will be redirected to the contract verification page where we need to fill out:
Contract Name: Pizza
Compiler: Check your
truffle-config.js
file for Compiler VersionContract Code: Just paste everything from your
Pizza.sol
file
Once everything is filled out, press Submit!
If everything is correctly filled out, your contract page on the block explorer should display a new tab called Contract
:
On this page you can read from, write to, or simply read the information tied to your smart contract on the blockchain:
For more information about Truffle Suite, Please Visit Truffle Suite Documentation. For more information about XinFin Network, Please Visit XDC Network Documentation on GitBook. Resources used during the deployment of the Pizza smart contract can be found at The Pizza Contract Folder.
Last updated