XDC Android SDK

XRC20 Token Integration

  • An XRC20 token is a blockchain-based asset with similar functionalities to ether; it can hold value and can be sent and received.
  • XRC20 tokens are stored and transferred using XDC addresses and transactions. It uses gas to cover transaction fees.

Read methods

name() β†’ string Returns the name of the token.
symbol() β†’ string Returns the symbol of the token, usually a shorter version of the name.
decimals() β†’ uint8 Returns the number of decimals used to get its user representation. For example, if decimal equals 2, a balance of 505 tokens should be displayed to a user as 5,05 (505 / 10 ** 2). Tokens usually opt for a value of 18, imitating the relationship between Ether and Wei.
totalSupply() β†’ uint256 Returns the number of tokens in existence.
balanceOf(address account) β†’ uint256 Returns the number of tokens owned by the account.
allowance(address owner, address spender) β†’ uint256 Returns the remaining number of tokens that the spender will be allowed to spend on behalf of the owner through transferFrom. It is zero by default.

Write methods

approve(address spender, uint256 amount) Sets amount as the allowance of spender over the caller’s tokens.
transfer(address recipient, uint256 amount) Moves tokens from the caller’s account to the recipient.
transferFrom(address sender, address recipient, uint256 amount) Moves tokens from sender to recipient using the allowance mechanism. The amount is deducted from the caller’s allowance.
increaseAllowance(address spender, uint256 addedValue) Automatically increases the allowance granted to spender by the caller.
decreaseAllowance(address spender, uint256 addedValue) Automatically decreases the allowance granted to spender by the caller.

AAR/JAR file

Add XDC_SDK to your project:
To add module dependency to your project, follow the steps below :
  • Go to file menu β€” project structure β€” modules β€” click β€˜+’ β€” select import Gradle Project β€” select the sub-module folder (git repo which we create) β€” select β€œXDC-sdkβ€œ folder β€” finish.
  • You can see the folder in your android project.
  • Now, we have to add Module Dependency in the application project builg.gradle file.
  • Put β€œimplementation project(":xdc_sdk")β€œ in dependencies.
  • Now, we can access the methods of the XDC-android library in the Android app.
Accessing methods of XRC20 in our project
1
XDCClient.getInstance().getTokenInfo(token_address, new TokenDetailCallback() {
2
3
@Override
4
public void success(TokenDetailsResponse tokenDetailsResponse)
5
{
6
// you can proceed as you like after getting success response.
7
}
8
9
@Override
10
public void failure(Throwable t)
11
{
12
Toast.makeText(MainActivity.this,t.getMessage(),Toast.LENGTH_LONG).show();
13
}
14
​
15
@Override
16
public void failure(String message)
17
{
18
Toast.makeText(MainActivity.this,message,Toast.LENGTH_LONG).show();
19
}
20
});
Copied!
Now, we can interact with the XRC20 read methods.
  • name() β†’ string Returns the name of the token.
tokenResponse.getName();
  • balanceOf(address token,address account) β†’ uint256 Returns the number of tokens owned by account.
tokenResponse.getBalance();
For write methods, we need to create an instance of XDC and need a token owner private key.
Credentials creds = org.web3j.crypto.Credentials.create(PRIVATE_KEY);
  • transfer(address token, address recipient, uint256 amount) β†’ Moves tokens from the caller’s account to the recipient. It will return a transaction hash.
String trasaction_hash = XDCClient.getInstance().TransferXdc(private_key, sender_address, receiver_address, new BigInteger(String.valueOf(token_totransfer), Long.parseLong(gasprice), Long.parseLong(gaslimit;
  • approve(address token, address spender, uint256 amount) β†’ Sets amount as the allowance of spender over the caller’s tokens. It will return a transaction hash.
approved_hash = XDCClient.getInstance().approveXRC20Token(Spender_address, privatekey, allownce_spender, value_approve);
To increase Allowance and decrease Allowance, we need an instance of XRC20 and the private key of the owner:
decreaseAllowance(XDCAccount account, address token, address owner, address spender, uint256 subtractedValue) Automatically decreases the allowance granted to spender by the caller.
It is an alternative to approve.
Emits an Approval event indicating the updated allowance.approved_hash = XDCClient.getInstance().decreaseAllowance(decrease_owner, decrease_spender, privatekey, decrease_allowance, Spender_address);

XRC721 Token Integration

What is XRC721 Token?
The XRC-721 introduces a standard for NFT. In other words, this type of token is unique and can have a different value than other tokens from the same Smart Contract, which may be due to its age, rarity or even its visual.
All NFTs have a uint256 variable called tokenId. So, for any XRC721 contract, the pair contract address and uint256 tokenId must be globally unique.

Read Methods

name() β†’ string Returns the name of the token.
symbol() β†’ string Returns the symbol of the token, usually a shorter version of the name.
totalSupply() β†’ uint256 Gets the total amount of tokens stored by the contract.
balanceOf(address account) β†’ uint256 Returns the number of NFTs in the owner's account.
ownerOf(tokenId) β†’ address Returns the owner of the NFT specified by tokenId.
tokenURI(uint256 tokenId) β†’ string Returns the URI for a given tokenID. May return an empty string.
tokenByIndex(uint256 index) β†’ uint256 Gets the token ID at a given index of all the tokens in the contract. Reverts if the index is greater or equal to the total number of tokens.
tokenOfOwnerByIndex(address owner, uint256 index) β†’ uint256 Gets the token ID at a given index of the tokens list of the requested owner.
supportInterface(tokenId) β†’ bool Returns true if the contract implements the interface defined by interfaceId.
getApproved(tokenId) β†’ address Gets the approved address for a token ID or zero if no address is set. Reverts if the token ID does not exist.
isApprovedForAll (address owner, address operator) β†’ bool Tells whether an operator is approved by a given owner.

Write methods

transferFrom(address from, address to, uint256 tokenId) Transfers the ownership of a given token ID to another address.
safeTransferFrom(address from, address to, uint256 tokenId) Safely transfers the ownership of a given token ID to another address.
setApprovalForAll(address operator, bool approved) Sets or unsets the approval of a given operator. An operator is allowed to transfer all tokens of the sender on their behalf.
approve(address to, uint256 tokenId) Approves another address to transfer the given token ID. The zero address indicates there is no approved address. There can only be one approved address per token at a given time. It can only be called by the token owner or an approved operator.

AAR/JAR file

Add XDC_SDK to your project:
To add module dependency in your project, follow the steps below:
  • Go to file menu β€” project structure β€” modules β€” click β€˜+’ β€” select import Gradle Project β€” select the sub-module folder (git repo which we create) β€” select β€œXDC-sdkβ€œ folder β€” finish.
  • You can see the folder in your android project.
  • Now, we have to add Module Dependency in the application project builg.gradle file.
  • Put β€œimplementation project(":xdc_sdk")β€œ in dependencies.
  • Now, we can access the methods of the XDC-android library in the Android app.
Accessing methods of XRC721 in our project
1
XDC721Client.getInstance().getTokenInfo(token_address, new Token721DetailCallback() {
2
@Override
3
public void success(Token721DetailsResponse tokenDetailsResponse) {
4
// success
5
}
6
​
7
@Override
8
public void failure(Throwable t) {
9
​
10
}
11
​
12
@Override
13
public void failure(String message) {
14
// error message
15
​
16
}
17
});
18
}
19
else
20
{
21
// error message
22
}
23
}
24
});
Copied!
Now, we can interact with the XRC721 read methods.
  • name() β†’ string Returns the name of the token. tokenResponse.getName();
  • balanceOf(address token,address account) β†’ uint256 Returns the number of tokens owned by account. String balance = XDC721Client.getInstance().getBalance(tokenAddress, ownerAddress);
For write methods, we need to create an instance of XDC and we need a token owner private key.
Credentials creds = org.web3j.crypto.Credentials.create(PRIVATE_KEY);
  • transferFrom(address from, address to, uint256 tokenId) β†’ Transfers the ownership of a given token ID to another address. String transferFrom = XDC721Client.getInstance().transferfrom(tokenAddress, privatekey, receiverAddress, tokenid);
  • approve(address to, uint256 tokenId) β†’ Approves another address to transfer the given token ID The zero address indicates there is no approved address. There can only be one approved address per token at a given time. Can only be called by the token owner or an approved operator. String approve = XDC721Client.getInstance().approve(tokenAddress, privatekey,String tokenid, receiverAddress);
  • safetransferFrom(address from, address to, uint256 tokenId) β†’ Safely transfers the ownership of a given token ID to another address. String safeTransferFrom = XDC721Client.getInstance().safeTransferfrom(tokenAddress, privatekey, receiverAddress, tokenid);
  • setApprovalForAll(address to, uint256 tokenId) β†’ Sets or unsets the approval of a given operator An operator is allowed to transfer all tokens of the sender on their behalf. String setapproveforall = XDC721Client.getInstance().setApprovalForAll(tokenAddress, privatekey, OperatorAddress, booleanvalue);