XRC20
Introduction
XDC-based tokens can represent anything virtually. For example:
Reputation points in an online platform
A fiat currency like USD
Lottery tickets
Skills of a character in a game
Financial assets, like shares in a company
An ounce of gold
XRC20 is a standard that allows developers to build token applications that are interoperable with other products and services.
XRC20 is a standard for fungible tokens, which implements an API for the tokens in smart contracts. Fungible tokens are interchangeable because they are identical to each other and thus equal in value. They can be put to many uses, such as voting, staking, and storing or exchanging value. The XRC20 standard can do much more than perform basic functions, like transferring tokens from one address to another and retrieving token balances. For example, it can determine the number of tokens a third party can spend.
Constructing an XRC20 Token Contract
An XRC20 token can easily be created using contracts.
These XRC20 tokens can now be used to track an in-game asset or currency like Diamonds (DMD).
The DMD token might look like this:
For the most part, our contracts are used via inheritance. Here, XRC20 is reused for implementing basic standards and optional extensions such as name, symbol, and decimals.
To query the deployer's balance, once the contract is deployed, run the following code:
To transfer the tokens to other accounts, run the following code:
It is important to note that decimals
is only used to display decimal numbers. The arithmetical operations within the contract are still performed with integers, and the displayed values will need to be adjusted to decimals
for different user interfaces (exchanges, wallets, etc.). The balance of each account and total token supply are not specified in DMD
. It is necessary to divide by 10^decimals
to obtain the actual DMD
amount. When minting or transferring tokens, the actual amount is num DMD*10^decimals
.
To send five tokens, run the following code:
Read methods
name() → string
Returns the name of the token.
symbol() → string
Returns the token symbol, which is usually just an abbreviation of the name.
decimals() → uint8
It gives the number of decimals used. For example, a balance of 705 tokens is displayed to users as 7.05 (705 / 10 * 2) if decimals
equals 2.
totalSupply() → uint256
Returns the number of tokens in existence.
balanceOf(address account) → uint256
Provides the number of tokens associated with an account.
allowance(address owner, address spender) → uint256
Gives the number of tokens that the spender will spend on behalf of the owner usingtransferFrom
. It is zero by default.
Write Methods
transfer(address recipient, uint256 amount)
Moves tokens from the caller’s account to the recipient.
approve(address spender, uint256 amount)
Sets amount as the spender's allowance over the caller’s tokens.
transferFrom(address sender, address recipient, uint256 amount)
Transfers tokens from sender to receiver through allowance mechanism.
increaseAllowance(address spender, uint256 addedValue)
Increases the allowance provided to the spender by the caller.
decreaseAllowance(address spender, uint256 addedValue)
Decreases the allowance provided to the spender by the caller.
Usage
When the user wants to use the SDKPHP library he/ she need to follow the below steps
Create a folder User convient (eg : sdk)
Inside the folder open the command prompt and type composer require xdc3/php
Create a file according to User convient (eg : index.php) Step4 :- After creating user need to add the path , import the classes and create the obj
After creating user need to add the path , import the classes and create the object
(Optional) If user wants to change the URL Goto vendor -> XDC3 folder -> PHP folder -> .env file
Important
This library provides simple way to interact with XDC XRC20 & XRC721 tokens. Before Installing This Library
Install php version 7.4.25 in the System
Go to PHP Folder in the system, open php.ini file, then enable gmp and curl extensions
You will see ";extension=curl", just remove ";" to enable this extension.
Enable ";curl.cainfo =" and add ssl certificatepath after the "="
To download the certificate go to https://curl.se/docs/caextract.html then click on cacert.pem on the top, then copy the path of the certificate and paste it in the php.ini file
ex: curl.cainfo = "C:\Users\HP\Downloads\cacert.pem"
Install openssl in the system by chocolately
Run the following command in admin powershell to install openssl, "choco install openssl"
In XDC library inside the vendor folder -> xdc3base -> php we can find the all called functions of the XRC20 methods . (including contract file , address calling from apothem file etc)
In the PHPS library inside the vendor folder -> xdc3 -> php we can find all the calling functions from the XDC libarary. (ex : in modules xrc20 folder contains every calling methods)
Folder Info
In XDC library inside the vendor folder -> xdc3base -> php we can find the all called functions of the XRC20 methods . (including contract file , address calling from apothem file etc). If you want to change the URL Goto vendor -> XDC3 folder -> PHP folder -> .env file.
Do the following if you want to create objects to call XRC20 functions and create a wallet for the user
Example
Last updated