Select Page

HOW TO CREATE, TEST, AND DEPLOY SMART CONTRACTS ON XDC NETWORK?

Create, Test and Deply - XDC Network

While the most basic implementation of blockchain technology is to timestamp digital records on a secure and distributed network, enterprises worldwide continue to launch innovative blockchain use cases and introduce the world to the power of blockchain.

Blockchain infrastructure needs constant evolution to support the creation of advanced blockchain projects. For instance, Bitcoin was the first crypto-based implementation of decentralized technology, and later Ethereum entered the space with smart contract functionality.

Likewise, the need for a private blockchain emerged after people realized the limitations associated with Bitcoin, and now enterprises are interested in using hybrid blockchain networks. However, the selection of blockchain depends on the project type and the purpose it intends to serve.

The concept of hybrid blockchain is still in its infancy, and hence, there are only a few hybrid blockchains available in the blockchain space. These blockchains hold better capabilities than public and permissioned blockchains and open better possibilities for building feasible decentralized solutions.

XDC Network is a prominent hybrid blockchain with smart contract functionality, powering various blockchain-based solutions and thus getting traction across diverse industries. With more blockchain projects seeking to benefit from the hybrid blockchain’s infrastructure, various projects are being developed on the XDC Network.

Let’s understand how to build a smart contract on the XDC Network to understand creating and deploying decentralized apps (dApps) on the top of this blockchain. Before we go in-depth about creating, testing, and deploying the XDC smart contract, it is essential first to understand the XDC blockchain, its features, and how it works.

What is XDC Network?

XDC Network is a hybrid enterprise-ready blockchain that combines the power of both public and private blockchain, offering the benefits of open-source infrastructure and interoperable smart contracts. This blockchain has the potential to address the challenges of the public and private blockchains and provide a core blockchain for better utility and application.

As a major upgrade by the XDC community, the XDC blockchain officially has been designed to empower global trade and finance. The network has been unveiling considerable upgrades and new products since then. The XDC Network leverages the power of XDC delegated proof of stake (XDPoS) consensus, and it offers EVM compatibility, allowing developers to build robust dApps and blockchain-powered solutions.

XDC Network adopts a “stakeholder approval” approach to ensure high-end security and resolve issues with the consensus mechanism. The platform enables tokenization, digitization, and quick settlement of trade transactions. Reducing reliance on Forex exchange (FX) infrastructures and increasing efficiency for greater flexibility in liquidity management across financial institutions. For an in-depth analysis of the XDC Network, check out this insight.

How do smart contracts work on the XDC Network?

Smart contract on any blockchain automates the executions based on certain predetermined rules besides acting as the backend for the decentralized applications. With XDC smart contract, you can do the following:

Create cryptocurrencies

The XDPoS network allows you to create a tradable token used as a currency. Tokens minted with the XDC platform use a standard coin API compatible with any XDC blockchain wallet.

Develop dApps

Developers can build secure and fault-tolerant dApps on the XDC platform that bring transparency and remove intermediaries from the ecosystem.

Raise funds

You can also use XDC smart contracts for fundraising. For example, you can create a smart contract and set a deadline. If you fail to achieve the goal,

Build virtual organizations

You can create a smart contract to develop a blockchain-based organization. By adding people to your organization, you can enable voting rules. The smart contract executes automatically when members of the organization vote and reach the needed number of votes.

Why should you choose XDC Network?

Self-proclaimed as a third-generation blockchain protocol, XDC Network is designed to tackle the challenges of first and second-generation blockchains, such as low throughput, high transaction cost, and complicated development approach. Following are the unique features of XDC Network:

Consensus mechanism

XDPoS is a unique consensus algorithm that intends to make the XDC Network more efficient for solving scalability issues and other blockchain-based problems without consuming high energy like other mainstream blockchains.

Low computation resources

XDC blockchain, through its XDPoS, consumes low computation resources of 0.0000074 TWh. In comparison, Ethereum and Bitcoin are 20.61 and 71.12, respectively. The platform’s robust smart contracts ensure a fast transaction confirmation time of two seconds, and it offers a 2000+ TPS. Such capability of this network is considerably higher than Bitcoin and Ethereum.

Easy verification

Even the most prominent blockchains refuse to allow consumer-grade hardware to run a masternode. In contrast, XDC Network can easily run a 108-Masternode system to support ingenious techniques like double validation, staking using smart contracts, and true randomization. XDPoS also offers a self-KYC feature that every public masternode holder must use to perform their identity verification to participate in consensus.

Interoperable smart contracts

By interoperable smart contracts, XDC Network refers to computer programs encoded on the blockchain accessible across multiple blockchain networks. For instance, there’s no need to build separate smart contracts to regulate dApps on Ethereum and the XDC blockchain since a single contract can regulate both.

How does the XDC blockchain execute smart contracts?

Before discussing smart contract creation, let’s first understand the execution environment of the XDC Network and how it runs smart contracts.

XDC Network is an Ethereum Virtual Machine (EVM) compatible blockchain platform. EVM is a runtime environment for smart contracts written on the XDC network. As the name suggests, it is not physical; but a virtual machine. The functionality of EVM is limited compared to that of the virtual machine; for example, it cannot generate random numbers or make delayed calls on the internet.

Hence, coding smart contract programs in assembly language do not make any sense, and the XDC Network needed a programming language for EVM, which is Solidity. Now, let’s have a walkthrough of the creation and deployment of Smart Contracts on the XDC Network using Solidity.

How to create and deploy smart contracts on the XDC Network?

Install prerequisites

  • The network for running and deploying contracts
  • Wallet CLI: Wallet-CLI is the XDC Network’s command line wallet. Developers can use wallet-cli to deploy contracts on the mainnet and execute other operations.
  • XDCPay Chrome Extension

XDCPay acts as both a wallet and XDC browser. With XDCPay, you can interact with dApps and smart contracts without installing any software or blockchain. You need to add XDCPay Chrome Extension to your browser, create a wallet and submit XDC. Before writing smart contracts, you must have some amount of XDC tokens in your XDCPay wallet.

Step1: Create a wallet with XDCPay

Create Wallet With XDCPay

After creating and confirming your password, you’ll be given a secret backup phrase. That phrase is required to backup or restores your XDCPay account even when you forget the login credentials since it is derived from a unique hash associated with your account.

Created Vault

Keep this phrase safe either on an external encrypted hard drive or on a piece of paper. The next step is to ensure that you are in the XDC Network’s Mainnet.

Step2: Select a test network

You need to select the “XDC Apothem Testnet” to test the smart contract before deploying it on the mainnet. Note that this Apothem network is solely for testing purposes and the XDC provided to this network is a dummy with no real value.

Select Test Network

Step3: Add test XDC to your wallet

To test the smart contract, you need to have some dummy XDC in your XDCPay wallet. For example, if you need to test a contract using a test network, select that network, and you will see 0 XDC as the initial balance in your account.
To add the dummy XDC, visit the faucet and enter your XDC address. It will provide you with 1000 XDC for testing smart contracts.

xinfin smart contract demo

After receiving 1000 XDC, you will view pop up like this:

xinfin smart contract tutorial

Once you have dummy XDC in the XDCPay wallet, you can start creating smart contracts and coding on the Remix Browser IDE in Solidity programming language.

Step4: Use Remix Editor to write smart contracts

We are using Remix Browser IDE to write smart contracts in Solidity language. This IDE is used for writing small-sized contracts. Features of Remix Browser IDE include:

  • Syntax and error highlighting
  • Integrated debugger
  • Deploy to XDCPay
  • Static analysis
  • Integrated testing and deployment environment
  • Functions with injected web3 objects
  • Warnings, including gas cost, checks for overlapping variable names, unsafe code

Step5: Creating a .sol extension file

Open XDC Remix Browser and click on the plus icon on the top left side to create a .sol extension file.

Step:6 A smart contract code sample to create XRC20 tokens.

XRC20.sol is a standard template for creating XRC20 tokens

pragma solidity ^0.4.0;
import “./XRC20.sol”;
contract myToken is XRC20{
mapping(address =>uint256) public amount;
uint256 totalAmount;
string tokenName;
string tokenSymbol;
uint256 decimal;
constructor() public{
totalAmount = 10000 * 10**18;
amount[msg.sender]=totalAmount;
tokenName=”Mytoken”;
tokenSymbol=”Mytoken”;
decimal=18;
}
function totalSupply() public view returns(uint256){
return totalAmount;
}
function balanceOf(address to_who) public view
returns(uint256){
return amount[to_who];}
function transfer(address to_a,uint256 _value) public
returns(bool){
require(_value<=amount[msg.sender]);
amount[msg.sender]=amount[msg.sender]-_value;
amount[to_a]=amount[to_a]+_value;
return true;
}
}

Now, select a compiler version from Remix for compiling the Solidity XDC smart contract code.

Step:7 Deploy your contract.

Deploy smart contract at XDC Apothem Test Network by clicking the “deploy” button on the left side of the Remix IDE Window.

Wait until the transaction is processed. After committing the transaction, the address of smart contracts can be seen on the left side of the Remix Window.

Firstly, the user’s wallet will store all the XRC tokens.

Go to the XDCPay window to check tokens in the wallet. Click “add tokens”, enter the smart contract’s address, and click Ok. You would see the number of tokens here.

Now, let’s discuss the steps for testing smart contracts on the XDC Network.

Here are the steps to test the Smart Contract on the XDC Network

  • Ensure to run all methods of smart contracts, including totalSupply, balanceOf and transfer. These methods can be found on the left-hand side of the Remix Window, and run all methods from there.
  • Make sure if tokens can be transferred to another XDC wallet address and check the balance of that address by calling balanceOf method.
  • Switch to the main XDC Network at XDCPay to make smart contracts live.
  • Add real XDC to your account.
  • Now, you need to deploy the smart contract again using Remix.
  • Once the smart contract is deployed successfully, visit XDC Observatory and find your smart contract address. Then, select your smart contract.
  • Click “verify contract” to verify your smart contract.
  • Copy the smart contract code and paste it into XDC Explorer. Select the compiler version you chose at Remix to compile the code.
  • Also, check “optimization” to yes if you selected optimization at Remix. Otherwise, check “optimization” to No.
  • Click verify, and it will take a few minutes, and smart contracts will be live if no issue occurs.

Conclusion

We have used Remix to create and deploy smart contracts. However, there are different functions and tools that we can explore to write, test and deploy smart contracts.

If you are looking for smart contracts development services, we can help you. We have a team of experienced blockchain developers who can create and deploy smart contracts on various blockchain platforms, including XDC Network, Tezos, Ethereum, etc.

Webinar Details

Author’s Bio

Akash Takyar
Akash Takyar
CEO LeewayHertz
Akash Takyar is the founder and CEO at LeewayHertz. The experience of building over 100+ platforms for startups and enterprises allows Akash to rapidly architect and design solutions that are scalable and beautiful.
Akash's ability to build enterprise-grade technology solutions has attracted over 30 Fortune 500 companies, including Siemens, 3M, P&G and Hershey’s. Akash is an early adopter of new technology, a passionate technology enthusiast, and an investor in AI and IoT startups.

Start a conversation by filling the form

Once you let us know your requirement, our technical expert will schedule a call and discuss your idea in detail post sign of an NDA.
All information will be kept confidential.

Follow Us