In this smart book, we were discussing the ERC20 token send and receive the actual token amount. And check the balance for a particular address.
In every smart contract, we declare firstly over license and pragma files.
Licence is not the must but it will show warning like this:
Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.
--> New.sol
You can resolve this warning like this:
// SPDX-License-Identifier: GPL-3.0
Pragma files declare in top of the smart contract it mainly use to compile the contract based on different versions.
I'm using version 0.4.5 to 0.9.0.
pragma solidity >=0.4.5 <0.9.0;
Using this because we want to access all the keywords, global variables and much more. If we're using any particular version like only 0.4.5 then we're not access other version functions like: call()
we define a interface, it's meaning we store some different functions and we're using this function same as inside the " contract " functions.
interface ERC {
function transfer(address receiver, uint amount) external returns(bool);
function checkBalance(address add) external returns(uint);
event Transfer(address from, address to, uint value) ;
}
I'm using only two function one is transfer and the other one is checkbalance. But inside ERC20 token they've multiple function like balanceOf(), transfer(), allowance, approve(), transferFrom(), etc.
contract MyContract {
}
If you want to change this contract name then only change " MyContract".
We declare two variables totalSupply and owner. Declare totalSupply value 1000. It will be uint means unsigned integer it must me positive value. And we connect smart contract with interface. And add mapping to our address to unsigned integer value.
contract MyContract is ERC {
uint totalSupply = 1000;
address owner;
mapping( address => unit ) balances;
}
Means our key is address and the particular key store some values in integer.
Don't panic it will show error like this:
TypeError: Contract "MyContract" should be marked as abstract.
--> New.sol:14:1:
|
14 | contract MyContract is ERC{
| ^ (Relevant source part starts here and spans across multiple lines).
Note: Missing implementation:
--> New.sol:8:5:
|
8 | function checkBalance(address add) external returns(uint);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Note: Missing implementation:
--> New.sol:7:5:
|
7 | function transfer(address receiver, uint amount) external returns(bool);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Because you've declared interface but forget to use function then if we define the same functions then it will automatically resolve error.
We declare two function who's name same as the interface function name.
First function transfer is used to send the amount of totalSupply to other address.
This function has two parameter one for the accepted or to and the other is how much supply you send.
function transfer(address _to, uint _value) override public returns(bool){
require( balances[owner] >= _value, "Not enough amount" );
balances[owner] -= _value;
balances[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
}
Inside this they've require statement check the condition if the condition is true then go to next line otherwise show the error message.
function checkBalance() override public view returns(uint){
return balances[owner];
}
check the current amount.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.5 <0.9.0;
interface ERC {
function transfer(address receiver, uint amount) external returns(bool);
function checkBalance(address add) external returns(uint);
event Transfer(address from, address to, uint value) ;
}
contract MyContract is ERC{
uint totalSupply = 1000;
address owner;
mapping ( address => uint ) balances;
constructor(){
owner = msg.sender;
balances[owner] = totalSupply;
}
function transfer(address _to, uint _value) override public returns(bool){
require( balances[owner] >= _value, "Not enough amount" );
balances[owner] -= _value;
balances[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
}
function checkBalance(address _add) override public view returns(uint){
return balances[_add];
}
}