Skip to content

Challenge 04

Creating and manipulating fungible tokens (ERC20).

Task

  1. use openzeppelin's implementation of ERC20 to create a fungible token called "Sollearn Demo" and with a symbol of "SLD".

  2. increase your balance to 100 of the newly created token.

  3. Deploy the contract

  4. send 10 tokens to me.

  5. allow me to transfer 20 tokens.

  6. i have created a fungible token called "Demo Token" and allowed you transfer 10 of them. send 7 of them to you and 3 of them to a random address.

Info

You can use remix-ide or hardhat or truffle to create the contract and deploy it.

Solution
  1. open remix-ide
  2. create a new file and add the following code:
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract SollearnCoin is ERC20 {

    constructor() ERC20("Sollearn Demo", "SLD"){
        // mint uses the smallest unit
        uint oneUnit = 10 ** decimals(); // the default value of decimals() is 18
        uint amount = 100 * oneUnit; // 100_000_000_000_000_000_000
        address to = msg.sender;
        // mint will create new tokens and transfer them to the deployer
        _mint(to, amount);
    }

    // or in one line
    // constructor() ERC20("Sollearn Demo", "SLD"){ _mint(msg.sender, 100 * 10 ** decimals()); }

}

Warning

10_000_000_000_000_000_000 = 10 RC

  1. call the transfer(address _to, uint256 _value) method with the following parameters: (to: 0x000, amount: 10_000_000_000_000_000_000)
  2. call the approve(address _spender, uint256 _value) method with the following parameters: (to: 0x000, amount: 20_000_000_000_000_000_000)
  3. call the transferFrom(address _from, address _to, uint256 _value) method with the following parameters: (from: 0x000, to: 0x000, amount: 7_000_000_000_000_000_000)
  4. call the transferFrom(address _from, address _to, uint256 _value) method with the following parameters: (from: 0x000, to: 0x000, amount: 3_000_000_000_000_000_000)

References

EIP-20: Token Standard