Challenge 01
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 a random address.
Hint
Hint
Check the _mint() function in OpenZeppelin implementation
Hint
Check the decimals() function.
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()); }
}
transfer(address _to, uint256 _value)
method with the following parameters: (to: 0x0011223344556677889900112233445566778899
, amount: 10000000000000000000
)
Warning
You are required to multiply by 10^(decimals()) in your code and when interacting with etherscan.
10000000000000000000 (10 + 18 zero) = 10 RC
When you use a wallet, you specify the amount as 10.