STEPS TO CREATE, TEST AND DEPLOY XINFIN SMART CONTRACT

Dhriti Misra
9 min readNov 2, 2020

--

Ever since XinFin main-net was launched in the blockchain space with its presence in 2019, the invention by Ritesh kakkad and Atul Khekade has brought the new decentralized applications (dApps). However, XinFin success is significantly attributed to the EVM Compatible implementation of smart contracts.

Many people believe that smart contracts are a new concept and have been invented with the Ethereum Blockchain Platform. But smart contracts date back to 1996 when computer scientist, Nick Szabo coined the term “smart contracts” and explained them as:

I call these new contracts by XinFin “smart”, because they are far more functional than their inanimate paper-based ancestors. No use of artificial intelligence is implied. A smart contract is a set of promises, specified in digital form, including protocols within which the parties perform on these promises.

Before we go deeper into the creation and deployment of the Xinfin smart contract, it is essential to understand the XinFin network and how it works.

XinFin as a Blockchain Platform for developing decentralized applications

Blockchain platforms allow developers to create and run smart contracts. XinFin is also one of the blockchain platforms that can execute arbitrary code so that you can execute any program on XinFin XDPoS Network.

The XinFin Blockchain is a potential distributed infrastructure that facilitates you to complete projects using smart contracts.

  • Create your cryptocurrencies
    Using XinFin XDPoS Network, you can create a tradable token that you can use as a new currency. Tokens created with the XinFin platform use a standard coin API to be compatible with any XinFin blockchain wallet.
  • Develop virtual organizations
    You can write a smart contract to build a blockchain-based organization. You can then add more people to your organization and enable voting rules. Members of your organization can vote and if it reaches the required number of votes, the smart contract executes automatically.
  • Build dApps
    XinFin allows developers to develop secure and fault-tolerant decentralized apps that eliminate intermediaries and offer transparency.
  • Raise funds
    XinFin smart contracts can also be used for fundraising. With XinFin, you can write a smart contract and a deadline. In case you fail to accomplish the goal, all donations will automatically be recompensed to donors without disputes or commissions.

How XinFin Blockchain Platform executes Smart Contracts?

Before discussing how to create a smart contract on the XinFin platform, you need to understand how it runs smart contracts. So, let’s understand the execution environment first.

XinFin is an Ethereum Virtual Machine (EVM) compatible blockchain.

The purpose of EVM is to serve as a runtime environment for smart contracts built on XinFin. Consider it as a global supercomputer that executes all the smart contracts.

As the name indicates, Ethereum Virtual Machine is not physical but a virtual machine. The functionality of EVM is restricted than that of virtual machines; for example, it cannot make delayed calls on the internet or produce random numbers. Therefore, it is considered as a simple state machine. Writing programs in assembly language does not make any sense, so XinFin required a programming language for the EVM.

Gas

In the Ethereum Virtual Machine, gas is a measurement unit used for assigning fees to each transaction with a smart contract. Each computation happening in the EVM needs some amount of gas. The more complex the computation is, the more the gas is required to run the smart contracts.

Transaction fee = Total gas used*gas price

Solidity

Solidity is a smart contract programming language on XinFin. Developed on the top of the EVM, it is similar to the object-oriented programming language that uses class and methods. It allows you to perform arbitrary computations, but it is used to send and receive tokens and store states. When it comes to syntax, Solidity is greatly influenced by C++, Python and Javascript so that developers can understand its syntax quickly.

It would be best to have a good understanding of Solidity programming language to write an XinFin Smart Contract efficiently.

What is a Smart contract?

Smart Contracts are the business logic or a protocol according to which all the transactions on a Blockchain happen. The general objective of the smart contract is to satisfy common contractual conditions like if we want to create our own token on XinFin, we need to develop smart contracts according to which all the calculations on our token would happen.

It is a stand-alone script written in Solidity and compiled into JSON and deployed to a particular address on the blockchain. Just like we can call a URL endpoint of a RESTful API to run some logic through an HttpRequest, we can execute a deployed smart contract similarly at a particular address by entering the accurate data along with XinFin to call the compiled and deployed Solidity function.

Smart contracts can be deployed to the decentralized database for a fee proportional to the storage size of containing code. It can also be defined as a collection of code stored in the blockchain network, defining conditions to which all parties within the contract should agree upon.

We will be sharing the example of XinFin smart contract creation using Solidity programming language. So, it is first essential to understand what Solidity is.

What is Solidity?

Solidity is a Javascript-like language developed specifically for creating smart contracts. It is typed statically and supports libraries, inheritance and complex user-defined types.

Solidity compiler converts code into EVM bytecode which is sent to the XinFin network as a deployment transaction. Such transactions have more fees as compared to smart contracts interactions and the owner of the contract must pay it.

Here’s a step-by-step guide to creating and deploying XinFin Smart Contracts with Solidity

Installing Prerequisites

XinPay Chrome Extension

XinPay acts both as an XinFin browser and a wallet. It allows you to interact with smart contracts and dApps on the web without downloading the blockchain or installing any software. You only need to add XinPay as a Chrome Extension, create a wallet and submit XDC.

Though XinPay is currently available for Google Chrome browser, it is expected to launch for Firefox too in the coming years.

Download the XinPay chrome extension before you start writing smart contracts.

Once it is downloaded and added as a Chrome extension, you can either import an already created wallet or create a new wallet. You must have some amount of XDC in your Xinpay wallet to deploy XinFin smart contracts on the network.

Steps to develop an XinFin Smart Contract

Step 1: Create a wallet at XinPay

Install XinPay in your Chrome browser and enable it. Once it is installed, click on its icon on the top right of the browser page.

Click on “Create Wallet” and agree to the terms and conditions by clicking “I agree” to proceed further. It will ask you to create a password.

After you create a password, it will send you a secret backup phrase that can be used for backing up and restoring the account. Do not disclose it or share it with someone as this phrase can take away your XDC.

You should either write this phrase on a piece of paper securely or store it safely on an external encrypted hard drive where no one could find it.

The next step is to ensure that you are in the “ XinFin Main-Net.”

Step 2: Select any one test network

You might also find the following Apothem test networks in your XinPay wallet:

The above Apothem network are for testing purposes only; note that the XDC of these networks have no real value.

Step 3: Add some dummy XDC in your wallet

In case you want to test the smart contract, you must have some dummy XDC in your XinPay wallet.

For example, if you want to test a contract using Apothem test network, select it and you will find 0 XDC as initial balance in your account.

To add a dummy XDC please visit faucet and just paste your XDC address there and you will receive 1000 XDC to your address for the testing of smart contract.

Once the dummy XDC are added to the XinPay wallet, you can start writing smart contracts on theXinFin Remix Browser IDE in Solidity programming language.

Step 4: Use editor XinFin remix to write the smart contract in Solidity

We will useXinFin Remix Browser IDE to write our Solidity code. XinFin Remix is the best option for writing smart contracts as it comes with a handful of features and offers comprehensive development experience.

It is usually used for writing smaller sized contracts. XinFin Remix’s features include:

  • Warnings like gas cost, unsafe code, checks for overlapping variable names, whether functions can be constant or not
  • Syntax and error highlighting
  • Functions with injected Web3 objects
  • Static analysis
  • Integrated debugger
  • Integrated testing and deployment environment
  • Deploy directly to XinPay

Let’s start writing smart contract code by visiting https://remix.xinfin.network/

Step 5: Create a .sol extension file

Open XinFin Remix Browser and click on the plus icon on the top left side, next to the browser to create a .sol extension file.

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

XRC20.sol is a standard template for 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 viewreturns(uint256){return amount[to_who];}function transfer(address to_a,uint256 _value) publicreturns(bool){require(_value<=amount[msg.sender]);amount[msg.sender]=amount[msg.sender]-_value;amount[to_a]=amount[to_a]+_value;return true;}}

Select a version of the compiler from XinFin Remix to compile the solidity XinFin smart contract code.

Step 7: Deploy your contract

Deploy the smart contract at the XinFin Apothem test network by pressing the deploy button at the left-hand side of the XinFin Remix window.

Wait until the transaction is complete.

After the transaction commits successfully, the address of the smart contract would be visible at the left-hand side of remix

Window.

At first, all the XRC20 token will be stored in the wallet of the user, who is deploying the smart contract.

To check the tokens in your wallet, go to the XinPay window, click add tokens, enter the smart contract address and click ok. You would be able to see the number of tokens there.

Steps to test an XinFin smart contract

  1. Try to run all the methods of your smart contract like transfer, totalSuppy and balanceOf(in above smart contract example). These methods are present at the left-hand side of the XinFin remix window and you can run all the methods from there itself.
  2. Try to transfer some tokens to another XinFin wallet address and then check the balance of that address by calling balanceOf method.
  3. Try to get totalSupply by running the totalSupply method.
  4. Steps to deploy XinFin Smart Contracts
  5. To make your smart contract live, switch to the main XinFin network at XinPay.
  6. Add some real XDC.
  7. Now again deploy your smart contract using XinFin remix as mentioned in the above steps.
  8. When a smart contract is deployed successfully, visit https://explorer.xinfin.network and search your smart contract address there. Select your smart contract.
  9. Now you need to verify your smart contract here, click
    “verify contract”
  10. Copy your smart contract code and paste it at XinFin Explorer.Select the same compiler version that you selected at XinFin remix to compile your code.
  11. Check “optimization” to Yes, if you had selected optimization at XinFin remix; otherwise, select No.
  12. Click Verify.
  13. It will take a few minutes and your smart contract will be live if no issue occurs.
  14. You can now run your smart contract methods at XinFinScan

For Telegram Support Join: https://t.me/xinfintech

Follow XinFin on:

Twitter: ( @ ) XinFin_Official

LinkedIn: https://www.linkedin.com/company/xinfin/

Telegram: https://t.me/xinfintalk

--

--