DEV Community

Cover image for Web3 - The Ultimate Guide to Create your own Cryptocurrency Token with Solidity
Matheus Costa
Matheus Costa

Posted on

Web3 - The Ultimate Guide to Create your own Cryptocurrency Token with Solidity

Old or new to the crypto world, you have probably already met with some cryptocurrencies. But how about creating one? That's what we're going to do in this tutorial.

ERC-20

First, what is ERC-20? Some kind of android? No! Besides the difficult name, it is just a protocol to define what basic structure, rules and functionalities your fungible token (like crypto currencies) must have to properly interact with the world. But good news! You don't have to write it from scratch, because OpenZeppelin already has a bunch of secure boilerplate smart contracts written and audited by the community developers, so you just have to import it in your code and extend your contract so it inherits the ERC-20 basic properties and methods.

The basic properties and methods

To properly interact with the world your token needs some basic rules, such as:

  • Name (like Ethereum or Dogecoin)
  • Symbol (like ETH for Ethereum)
  • Decimals (like 2 for US Dollars and 18 for Ethereum)

And also basic functionalities, such as:

  • totalSupply (to indicate the total supply of the token)
  • balanceOf (to check the balance of the indicated wallet)
  • transfer (to send or receive the token)
  • allowance (which allows a 3rd party to move your tokens)
  • mint (to mint new tokens)
  • burn (to burn tokens)

And a couple more you can check at the OpenZeppelin ERC-20 smart contract

The code

Let's go to the fun part: coding. For this part, I'm assuming you already have basic Solidity experience. Open your Remix IDE, create a new file and set your Pragma to the newest available version. Import the ERC-20 smart contract from here and extend your token smart contract. You should have something like this:

pragma solidity ^0.8.7;

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

contract CakeCoin is ERC20 {

}
Enter fullscreen mode Exit fullscreen mode

Now we need to add the constructor function, so we can assign values to the name and symbol properties, and also mint some starting tokens on the smart contract creation. You can choose to either mint a specific quantity of your token on the contract creation, with no possibility to mint more than the initial supply, or create a function so you can mint on demand. It depends on your strategy.

pragma solidity ^0.8.7;

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

contract CakeCoin is ERC20 {
    address public owner;

    constructor() ERC20('CakeCoin', 'CAKE') {
        _mint(msg.sender, 100000 * 10 ** 18);
        owner = msg.sender;
    }
}
Enter fullscreen mode Exit fullscreen mode

Here we defined our token as CakeCoin, with its symbol being CAKE. We also set some initial coins, defined 18 decimals and registered the owner address, so we can create functions like mint which can only be used by the contract creator. Now let's create the mint function.

pragma solidity ^0.8.7;

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

contract CakeCoin is ERC20 {
    address public owner;

    constructor() ERC20('CakeCoin', 'CAKE') {
        _mint(msg.sender, 100000 * 10 ** 18);
        owner = msg.sender;
    }

    function mint(address to, uint amount) external {
        require(msg.sender == owner, "Only the coin owner can mint more coins");
        _mint(to, amount);
    }
}
Enter fullscreen mode Exit fullscreen mode

With the mint function, the contract owner can mint new tokens and send it to any address. That's it for the basic funcionalities of our token, but you can create any other functionality to interact with it as you want. Sky is the limit :)

Deploying the smart contract to a local development blockchain

To test our smart contract, we can deploy it to a local development blockchain with Remix as the example below:

Local Development Environment

Just set the environment to JavaScript VM (London), choose any test account and click deploy. After that, if your contract doesn't have any errors, you will be able to interact with your smart contract. You can see that we can use not only our functions, but also every function inherited from the ERC-20 smart contract.

Contract Functions

Deploying the contract to the mainnet

We can also deploy our contract in the Ethereum mainnet with Remix, but you'll need MetaMask and some funds to pay for the fees.

Mainnet Environment

For the deployment we'll select the Injected Web3 option, which will trigger a MetaMask popup to connect your account. After that, we'll need to pick the correct account and click deploy.

Thats it folks, hope you enjoyed the tutorial and reach me out if you have any questions!

Discussion (1)

Collapse
_lehmoura profile image
lele

Thanks for the article man you are TOP #estourado