Blockchain: What are you Token about?

Tokens, NFT, ERC, EIP… these terms have been in our vocabulary for quite a while. Many people are excited about how innovative they are and what they can bring to decentralize the economy and trading. Others hate the concept as a whole with prejudice for, perhaps, what it delivers. But what exactly is this thing that you’re acquiring? In this article, I intend to introduce you to the many patterns that nominate blockchain tradables and kill the mysticism that roams this concept.

Tokens, ERC and EIP

When browsing about blockchain, you will certainly bump into these terms. EIP stand for Ethereum Improvement Proposal and wraps many suggestions on how the network should behave, like the very recent (at the time of writing this) Merge to Proof of Stake described at the EIP-3675 [1], ERC is a subcategory that stands for Ethereum Request for Comments and describes specifically contract standards and thus, Tokens.

Quickly explaining what is fungibility: you may imagine that if one unit of a thing is summable or exchanged by another unit of thing, as they have no identification itself or intrinsic unitary value, it is fungible. The most common example is money. One unit of some currency is worthy and represents the same thing as any unit of that currency. When something is not fungible, it means that for some reason this unit of thing is unique. It may be an item, a rare coin, a car, or your official identification document. Each piece of those is unique and potentially does not have the same worth as a similar piece.

So Coins and Non-Fungible-Tokens (NFT) are all Contracts on a blockchain. What we usually call Coin, implements the ERC-20 Standard [2], and what we call NFT implements the ERC-721 Standard [3]. There is also a third case that is not very well known that implements a both scenarios in the same contract. This one is the ERC-1155 Standard [4] that holds the registry of both fungible and non-fungible in one place.

What are the Tokens and how are they stored?

Knowing that there are standards to follow when creating your own Token, and being the nature of the blockchain the exposure of source code so you can trust that contract, for a programmer to discover how tokens work is quite easy. Some get mindblown when they discover that the Wallet concept does not even exist. Well, we can assume that it exists for practical purposes, but for technical purposes, your coins are not in your wallet!

To answer the title’s question, Tokens are only references between your address and the amount you have, stored in the state of the contract. Nothing more than that. Inside the contracts you may encounter what in Solidity language is called a mapping. It is a hashmap where the key is your address and the value is the amount. Let’s take a look first at the contract that implements the Coins.

    mapping(address => uint256) private _balances;

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol

The contract above is an example of implementation for an ERC-20. There are two points of interest that you can look to: the function balanceOf and the mapping _balances. The first wraps the second. Notice that the format of the value of the mapping is an unsigned integer. That means that you cannot have a negative balance, but sometimes badly programmed math can do crazy things. Also, usually, ERC-20 contracts determine 18 decimals of precision, and with that, obtaining 1 unity of a token will point your address to a 1 000 000 000 000 000 000 number.

Let’s take a look at the NFT Contract then:

    // Mapping from token ID to owner address
    mapping(uint256 => address) private _owners;

    // Mapping owner address to token count
    mapping(address => uint256) private _balances;

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol

Look at the first mapping. It maps the Token ID, which is an unsigned integer, to your address. Being Non-Fungible means that each ID is a different token. Look at the second mapping. It counts how many tokens you have in this contract, summing every transaction that gave a token to you. So you can have a balance of 3, but that won’t mean that you can trade 0.25 of your tokens. Only integers are transferrable. Only integers are transferrable. Integers.

Integers are transferrable

The repetition on the last paragraph was to emphasize that tradability. People usually acquire an NFT thinking that they acquired an asset, like an image or a game item, but what they actually did was registering that a certain index on a certain contract belongs to them. But what do I do with an index?

With that index, you’re granted in the contract’s universe that whatever the contract emitter said the index represents is yours. So if an artist emitted a collection of nice pictures and posted them on a gallery so people can bid and acquire, he assigned a token ID to a picture. Now you need to trust the contract emitter that related the token ID with something to not change that relation. “But isn’t the blockchain immutable?” Yes, it is.
Untill now, I did not say where this relation is being maintained. The immutable part so far is the mapping that relates your address to a token ID. There is another property if you look deeper the ERC-721 contract that will provide a Token URI. It means that the metadata of your Token ID is hosted elsewhere. That is because hosting enormous amounts of bytes on the blockchain is really expensive. People prefer outsourcing NFT metadata to either the Inter-Planetary File System (IPFS) or the well known S3 Bucket. The first is immutable, the second is not. The best solution would really depend on what the NFT represents. If I acquired an image, I would prefer it to never stop existing because its project halted, but let’s say that my metadata means the experience points of my game character. That is supposed to change often.

Why should I even care about acquiring a number then?

Other than the Coins that have clear uses, NFT are often still being misused. People don’t understand what they are acquiring and some kinds of metadata don’t deserve to be NFT’ed. In my journey, the most exciting use cases for those are granting someone an asset that can be used inside a universe. Calling (a bit erroneously) the blockchain a distributed database, a consortium can make use of it to make several games that each player’s equipment is stored at some contract. If a game needs specific resources, it can emit another contract and register the player’s belongings on it whereas other games can inspire themselves to make use of the state registered in that contract.

There are also many who advocate that your house can be NFT’ed, but analyzing closely, we need to trust a central source to emit the contract in the first place. And in how many “central sources” can we register some land before being caught? Well, I’m really curious to find some more nice use cases where NFT are used to do something instead of just a static asset.

Proving your ownership

At this point, I guess you have an idea of what the NFT is at the Contract level. But how can someone not fake being me and make use of my tokens? The ownership proof uses a cryptographic signature to verify that you are really you. Or at least the bearer of a private key. The concept is called Digital Signature [5]. Suppose you want to send from address A to B some tokens. To do that, you write a message to the contract in an expected format (that is passing parameters to a function) and sign it with your Private Key. The contract receives the message and the signature can decode the signature, verifying that your private key signed the message. Only then the contract allows you to transfer your tokens.

Knowing this, it also sort of explains why Wallet is not really a thing. There is nothing in your Wallet itself, but your Wallet reads given contracts, expecting balanceOf methods to implement a default interface, so it can show you that you have tokens. Your Wallet is only the key to accessing your balances in many contracts.

Conclusion

We’ve seen that there is no actual pocket or slot where you place your tokens and that tokens are only numbers in the state of someone’s else contract. We trust they implemented the contract somewhat as the spec says and that they did not place any backdoors. We also trust, for the case of NFT, that what they mean won’t change, at least within the time I care about my belongings. Not forgetting to mention, there are many more contract types that aim to solve many concepts, like the ERC-1155 which accepts both fungible and non-fungible tokens in one contract. I recommend everyone to take a deeper look at all those mentioned in this article and wonder about use cases that really would add value to a project.

[1] https://eips.ethereum.org/EIPS/eip-3675
[2] https://eips.ethereum.org/EIPS/eip-20
[3] https://eips.ethereum.org/EIPS/eip-721
[4] https://eips.ethereum.org/EIPS/eip-1155
[5] https://en.wikipedia.org/wiki/Digital_signature

We are hiring new talent. Check out our careers portal!