How to migrate any Dapp from Ethereum to XDC Network using Truffle

  • Setting up Truffle, the most popular development framework for Ethereum, which also works perfectly for XDC Network.
  • Creating a Truffle project
  • Creating an XDC Network wallet
  • Writing a smart contract.
  • Compiling and migrating the smart contract from Ethereum Network to XDC Network
  • Install Node.js & npm (“Node.js Package Manager”)
  • Install Git
npm install -g truffle
mkdir pet-shop-tutorialcd pet-shop-tutorial
truffle unbox pet-shop
0x30BC0cd9c595742048b8c1bb5f97F72EE60a8961
Garden shadow dash husband scan bonus nerve bright fashion dose balcony circle
  1. We’ll start our DApp by writing the smart contract that acts as the back-end logic and storage.
pragma solidity ^0.5.0;contract Adoption {address[16] public adopters;// Adopting a petfunction adopt(uint petId) public returns (uint) {// check that petId is in range of our adopters arrayrequire(petId >= 0 && petId <= 15);// add the address who called this function to our adopter arrayadopters[petId] = msg.sender;// return the petId provided as a confirmationreturn petId;}// Retrieving the adoptersfunction getAdopters() public view returns (address[16] memory) {return adopters;}}
truffle compile
Compiling ./contracts/Migrations.sol…Compiling ./contracts/Adoption.sol…Writing artifacts to ./build/contracts
  1. Create a new file named 2_deploy_contracts.js in the migrations/ directory.
  2. Add the following content to the 2_deploy_contracts.js file
var Adoption = artifacts.require(“Adoption”);module.exports = function(deployer) {deployer.deploy(Adoption);};
‘use strict’var HDWalletProvider = require(“truffle-hdwallet-provider”); var mnemonic = ‘arm derive cupboard decade course garlic journey blast tribe describe curve obey’; module.exports = {networks: {development: {provider: () => new HDWalletProvider(mnemonic,“http://127.0.0.1:8545",),host: “127.0.0.1”,port: “8545”,network_id: “*”, // Match any network id},xinfinmainnet: {provider: () => new HDWalletProvider(mnemonic,“https://erpc.xinfin.network",0,1,true,“m/44'/889'/0'/0/”),network_id: “50”,gas: 2000000,gasPrice: 25000000000},xinfintestnet: {provider: () => new HDWalletProvider(mnemonic,“https://rpc.apothem.network",0,1,true,“m/44'/889'/0'/0/”,),network_id: “51”,gas: 2000000,gasPrice: 10000000000000,}}};
truffle migrate — network xinfinmainnet
truffle migrate — network xinfinmainnetCompiling your contracts…===========================> Compiling .\contracts\Adoption.sol> Compiling .\contracts\Migrations.sol> Artifacts written to C:\Users\IndSoft-PC\Desktop\pet-shop-tutorial\build\contracts> Compiled successfully using:- solc: 0.5.16+commit.9c3226ce.Emscripten.clangStarting migrations…======================> Network name: ‘xinfinmainnet’> Network id: 50> Block gas limit: 420000000 (0x1908b100)1_initial_migration.js======================Deploying ‘Migrations’ — — — — — — — — — — — > transaction hash: 0xd1cf1315a0963b1bb04dbaee9122ee7aba5a6f9b02153e944a2c9bc3018f6e6d> Blocks: 2 Seconds: 5> contract address: 0xA444e1180B898AB62f8D01FB3715eb55f2E17825> block number: 50457694> block timestamp: 1663416685> account: 0x6BE9772969A2E853452124D460A6fd57653808bB> balance: 0.094236125> gas used: 222155 (0x363cb)> gas price: 25 gwei> value sent: 0 ETH> total cost: 0.005553875 ETH> Saving migration to chain.> Saving artifacts — — — — — — — — — — — — — — — — — — -> Total cost: 0.005553875 ETH2_deploy_contracts.js=====================Deploying ‘Adoption’ — — — — — — — — — — > transaction hash: 0x44b6c25709539d8a7ea578269b3a21111bca7187136fcc0724b73cda2d756bbd> Blocks: 2 Seconds: 5> contract address: 0xBc6EF4D97880D44e68C4f6cEB694234560174A94> block number: 50457703> block timestamp: 1663416703> account: 0x6BE9772969A2E853452124D460A6fd57653808bB> balance: 0.0871733> gas used: 239915 (0x3a92b)> gas price: 25 gwei> value sent: 0 ETH> total cost: 0.005997875 ETH> Saving migration to chain.> Saving artifacts — — — — — — — — — — — — — — — — — — -> Total cost: 0.005997875 ETHSummary=======> Total deployments: 2> Final cost: 0.01155175 ETHThe transaction ID is: 0x31fb3d5aef9d9c67852ba31ca0049158783ef32e6a61a45f6af3130d617790c3

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store