Challenge 04
Creating and manipulating fungible tokens (ERC20).
Task
-
use openzeppelin's implementation of ERC20 to create a fungible token called "Sollearn Demo" and with a symbol of "SLD".
-
increase your balance to 100 of the newly created token.
-
Deploy the contract
-
send 10 tokens to me.
-
allow me to transfer 20 tokens.
-
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.
Solution
- open remix-ide
- 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
- call the
transfer(address _to, uint256 _value)
method with the following parameters: (to: 0x000, amount: 10_000_000_000_000_000_000) - call the
approve(address _spender, uint256 _value)
method with the following parameters: (to: 0x000, amount: 20_000_000_000_000_000_000) - 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) - 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)