DEV Community

ASHDEEP SINGH
ASHDEEP SINGH

Posted on

ERC 20 Token

ERC-20 Token

ERC-20 is a standard for tokens on the Ethereum blockchain. It defines a set of rules that all Ethereum tokens must follow, ensuring interoperability between various tokens and Ethereum-based applications. The standard includes functions such as totalSupply, balanceOf, transfer, approve, and transferFrom.

Example:

pragma solidity ^0.8.0;

interface ERC20 {
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
}
Enter fullscreen mode Exit fullscreen mode

Tokens vs. Coins

Coins: Native assets of a blockchain. For example, Bitcoin (BTC) is a coin on the Bitcoin blockchain, and Ether (ETH) is a coin on the Ethereum blockchain.
Tokens: Assets created on an existing blockchain, leveraging its infrastructure. For example, ERC-20 tokens are created on the Ethereum blockchain and can represent anything from utility tokens to stablecoins.

Decimals

Decimals in the context of tokens refer to the number of decimal places the token can be divided into. This is defined in the ERC-20 standard to determine the smallest unit of the token.

Example: If a token has decimals set to 18, it means that 1 token can be divided into 10181018 smallest units.

uint8 public constant decimals = 18;
Enter fullscreen mode Exit fullscreen mode

Transfer vs. Call vs. Send

These are methods to send Ether in Solidity:

Transfer:

  • Sends 2300 gas.
  • Reverts on failure.
  • Simple and recommended for straightforward Ether transfers.
payable(recipient).transfer(amount);
Enter fullscreen mode Exit fullscreen mode

Call:

General-purpose function for calling other contracts.
Returns a boolean indicating success and the returned data.
Must handle potential reentrancy attacks.

(bool success, ) = recipient.call{value: amount}("");
require(success, "Transfer failed");
Enter fullscreen mode Exit fullscreen mode

Send:

  • Sends 2300 gas.
  • Returns a boolean indicating success or failure.
  • Considered less safe compared to transfer.
bool success = payable(recipient).send(amount);
require(success, "Transfer failed");
Enter fullscreen mode Exit fullscreen mode

Summary

ERC-20 Token: A standard for creating tokens on the Ethereum blockchain.
Tokens vs. Coins: Coins are native assets of blockchains; tokens are created on existing blockchains.
Decimals: Defines the smallest unit of a token.
Transfer vs. Call vs. Send: Methods to send Ether in Solidity, with different levels of complexity and safety.

Good to know

if you set decimal to 2 you can not send tokens having more than 2 decimal numbers (like 1.22 not 1.222) .When you set decimals to 2, it means the smallest unit of the token is 10^āˆ’2, or 0.01. This setting restricts you to transferring tokens with at most 2 decimal places. So you can transfer amounts like 1.22 or 0.01, but not amounts like 1.222 or 0.001, as these would exceed the precision defined by the decimals setting.

In other words, the decimals setting determines the granularity of the token, with 2 decimals allowing you to work with hundredths of a token.

That's all folks.
Stay tuned for more....

Top comments (0)