How to Migrate a dApp from Ethereum to the XDC Network Using Truffle
🧭 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.
dApps use truffle and hardhat to work with decentralized networks like Ethereum and XDC.
What you will learn
This guide aims at teaching on how to migrate a dApp from ethereum to the XDC network using truffle.
What you will do
Install and set up Truffle
Create a Truffle project
Write a smart contract
Compile and migrate the smart contract from the Ethereum Network to the XDC Network
Check the deployment status on xinfin.network.
🚀 Setting up the development environment
There are a few technical requirements before you start. Please install the following:
Node.js v8+ LTS and npm (comes with Node)
Once you have those installed, 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
Start by setting up your folder. As we are creating a project called Pizza
, create a new Pizza
folder by running this on terminal:
Next, you have to run truffle init
. If truffle is correctly installed on your local environment, you should see the following message:
And your folder files will look like this:
📝 Writing our first Smart Contract
Lets create a simple Pizza.sol
in the contracts folder
. 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! 😋
Write the following code to Pizza.sol
:
🍕 Compiling
Now try compiling the Pizza.sol
contract by running:
If everything is correctly configured and there are no errors, you should see the following message on your console:
Your folder should look like this:
If you see an error while compiling on a Mac, you can use
sudo truffle compile
In order to get started deploying new contracts on XDC Mainnet and/or Apothem(testnet), you 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, you can use either npm
or yarn
.
If you never used yarn
before, you might need to install it first. ‼️You can skip this step if you already have yarn installed‼️.
Creating a .env file
Initialize your package manager on your folder and install the required dependencies through your terminal:
You will also need a 24-Word Mnemonic Phrase. To configure your wallet, create a new .env
file in the 'root' of your project and write your mnemonic in there:
Remember to change the 24-Word Mnemonic above to 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! 🚨
🔀 Migrating from Ethereum to XDC network
For this step we need to change the truffle-config.js
file.
Your file for ethereum would look like this:
To migrate it to XDC network, you'll need to add a network for XDC Network Mainnet and XDC testnet(apothem)
🍕 Deploying
In order to deploy our newly compiled contract artifacts to the blockchain, you'll have to create a deployment script in the migrations folder.
Create a file 1_pizza_migration.js
in the migrations folder and write the following migration script:
If the migration script have no errors, we can go ahead and run the command.
Make sure you change the network name from 'goerli' to 'xinfin' for the following command.
For deployment on XDC testnet:
For deployment on XDC testnet:
For deployment on the XDC Apothem Testnet. In either case, you need to 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
The truffle console
CLI is another amazing tool that allows us to try out our contracts straight from our development environment.
To start interacting with you smart contracts you can start running:
Once the console opens, we can instantiate our Pizza
contract by running:
We can 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 the following:
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, may find it interesting 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:
There is a Pizza
contract deployed on XDC Mainnet at the 0x4FA229354CdF9c49FD2752e3869150C24c6A80c7
. You can search for this newly deployed contract on XinFin Block Explorer:
Click in the Verify And Publish
Option.
We 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
:
In 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 XDC 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