HomeCoinsQTUM (QTUM)The Token Standards Of Qtum Part 1 — QRC20 and QRC721 |...

The Token Standards Of Qtum Part 1 — QRC20 and QRC721 | by Qtum | Sep, 2021

- Advertisement -


Qtum
The Token Standards Of Qtum Part 1 — QRC20 and

Creating a one-size-fits-all token standard was crucial for the widespread adoption of Ethereum. The most popular token standards for Ethereum are ERC20, ERC721, and ERC1155. So, today in this first part, let’s look at QRC20 and QRC721 (along with their Ethereum counterparts). Then in the second part, we will be learning more about QRC1155.

Let’s do a quick thought experiment. Imagine that you live in country A. There is a lane in that country with 5 different stores accepting 5 different currencies. Can you imagine how confusing and chaotic that could be? Without having a set currency that’s accepted all over, A’s local economy will likely crumble. Now, let’s extend this logic to Ethereum.

Every single app running on top of Ethereum had its own token and token structure back in the day. However, the community soon realized that having so many different token standards would wreck system interoperability. This gave rise to the Ethereum token standards.

The ERC20 token standard is used to define fungible tokens. Fungibility is an essential property to define currencies since fungibility imparts the values of replicability and breakability. Back in 2017, during the ICO boom period, the ERC20 standard became popular due to the simplicity of deployment.

ERC stands for “Ethereum Request for Comment,” while the number ’20’ is the number assigned to this request. The building blocks of the ERC20 standard are as follows:

  • totalSupply

The QRC20 standard is the same as ERC20. So you can use the QRC20Token code to create your own QRC20 token on Qtum. If you want a proper step-by-step guide in developing your QRC20 tokens then check this.

When it comes to QRC20 contract interaction, there are certain guidelines to follow.

Getting Token Balance

qtum-cli callcontract

{TOKEN_CONTRACT_ADDRESS}

70a08231{to32bytesArg(addressToHash160($userAddress))}

In the code above, $userAddress is the deposit address. This code will return a JSON output. In it, look for “executionResult.output” which will be the token balance.

Withdraw

Qtum-cli sendtocontract

{TOKEN_CONTRACT_ADDRESS}

a9059cbb{to32bytesArg(addressToHash160($userAddress))}{to32bytesArg(addDecimals($amount))

0

{DEFAULT_GAS_LIMIT}

{DEFAULT_GAS_PRICE}

{MAIN_QRC_ADDRESS}

In the snippet above:

  • $userAddress is the withdraw address

This code returns the txid of the transaction. You can use the id to find information about this transaction.

Generate A Deposit Address

The user can use the same deposit for both Qtum and other QRC20 tokens. You may use this command to generate a deposit address:

Qtum-cli getnewaddress

Deposit and Withdraw Logs

Qtum-cli searchlogs

STARTING_BLOCK

999999999

‘{ “addresses”: [“TOKEN_CONTRACT_ADDRESS”]}’

‘{“topics”: [“ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef”]}’

While you can start looking from 0, but for better efficiency, you should start from $startingBlock.

event Transfer(address indexed _from, address indexed _to, uint256 _value)

The code above allows you to filter different event types. You may look through the logs to filter by to or from addresses.

Checking Confirmations

Given a transaction id $txid:

Qtum-cli gettransaction $txid

You may use the “confirmations” property in the output.

The second most prominent token standard that Ethereum has is ERC721 — used to define non-fungible tokens. In many ways, it is pretty similar to ERC20. Ethereum did this to make it simple for developers to incorporate it into their code easily. However, what truly defines NFTs are the Token Ownership functions.

Unlike ERC20 tokens, one ERC721 token is fundamentally different from another due to the ownership functions mentioned within the code. The ownership functions defined in ERC721 are as follows:

  • ownerOf()

ownerOf()

contract X {

mapping(uint256 => address) private tokenOwners;

mapping(uint256 => bool) private tokenExists;

function ownerOf(uint256 _tokenId)

constant returns (address owner) {

require(tokenExists[_tokenId]);

return tokenOwners[_tokenId];

}

}

This function returns the address of the token owner. This is very important since this is ownership is a core principle of NFTs.

approve()

contract X {

mapping(address => mapping (address => uint256)) allowed;

function approve(address _to, uint256 _tokenId){

require(msg.sender == ownerOf(_tokenId));

require(msg.sender != _to);

allowed[msg.sender][_to] = _tokenId;

Approval(msg.sender, _to, _tokenId);

}

}

Another thing to keep in mind about NFTs is that it allows you to transfer ownership from one entity to another. In a nutshell, the program ensures that one can transfer ownership if and when needed.

takeOwnership()

contract X {

function takeOwnership(uint256 _tokenId){

require(tokenExists[_tokenId]);

address oldOwner = ownerOf(_tokenId);

address newOwner = msg.sender;

require(newOwner != oldOwner);

require(allowed[oldOwner][newOwner] == _tokenId);

balances[oldOwner] -= 1;

tokenOwners[_tokenId] = newOwner;

balances[newOwner] += 1;

Transfer(oldOwner, newOwner, _tokenId);

}

}

The takeOwnership() function is sort of like a “withdraw” function. Any outside party can call the function to take tokens out of another user’s account.

transfer()

contract X {

mapping(address => mapping(uint256 => uint256)) private ownerTokens;

function removeFromTokenList(address owner, uint256 _tokenId) private {

for(uint256 i = 0;ownerTokens[owner][i] != _tokenId;i++){

ownerTokens[owner][i] = 0;

}

}

function transfer(address _to, uint256 _tokenId){

address currentOwner = msg.sender;

address newOwner = _to;

require(tokenExists[_tokenId]);

require(currentOwner == ownerOf(_tokenId));

require(currentOwner != newOwner);

require(newOwner != address(0));

removeFromTokenList(_tokenId);

balances[oldOwner] -= 1;

tokenOwners[_tokenId] = newOwner;

balances[newOwner] += 1;

Transfer(oldOwner, newOwner, _tokenId);

}

}

The transfer() function allows you to transfer the NFT from one owner to another.

tokenOfOwnerByIndex() [Optional]

contract X {

mapping(address => mapping(uint256 => uint256)) private ownerTokens;

function tokenOfOwnerByIndex(address _owner, uint256 _index) constant returns (uint tokenId){

return ownerTokens[_owner][_index];

}

}

This is an optional function. An owner can own multiple NFTs ar a time. To make it easier to keep track of a holder’s NFT, the keeps a record of the IDs of each token that each user owns.



Source link

- Advertisement -
Mr Bitcointehttps://www.bitcointe.com/
“Fact You Need To Know About Cryptocurrency - The first Bitcoin purchase was for pizza.” ― Mohsin Jameel
462FansLike
76FollowersFollow
4,567FollowersFollow
5,261FollowersFollow
1,507FollowersFollow
2,230SubscribersSubscribe

Most Popular

bitcoin
Bitcoin (BTC) $ 61,466.00
ethereum
Ethereum (ETH) $ 4,040.12
tether
Tether (USDT) $ 1.00
bitcoin-cash
Bitcoin Cash (BCH) $ 625.14
litecoin
Litecoin (LTC) $ 196.87
eos
EOS (EOS) $ 4.75
okb
OKB (OKB) $ 27.54
tezos
Tezos (XTZ) $ 6.75
leo-token
LEO Token (LEO) $ 3.41
cardano
Cardano (ADA) $ 2.16
monero
Monero (XMR) $ 266.79
stellar
Stellar (XLM) $ 0.374856
chainlink
Chainlink (LINK) $ 30.55
huobi-token
Huobi Token (HT) $ 10.06
tron
TRON (TRX) $ 0.100348
usd-coin
USD Coin (USDC) $ 1.00
dash
Dash (DASH) $ 196.88
neo
NEO (NEO) $ 44.39
iota
IOTA (MIOTA) $ 1.31
nem
NEM (XEM) $ 0.176614
zcash
Zcash (ZEC) $ 172.42
maker
Maker (MKR) $ 2,525.49
paxos-standard
Pax Dollar (USDP) $ 1.00
ethereum-classic
Ethereum Classic (ETC) $ 55.03
vechain
VeChain (VET) $ 0.137141
true-usd
TrueUSD (TUSD) $ 1.01
ftx-token
FTX Token (FTT) $ 62.83
kucoin-shares
KuCoin Token (KCS) $ 14.81
waves
Waves (WAVES) $ 28.46