My Image
CoursesQuizzesProblemsContestsSmartBooks
Contest!

No results found

LOGINREGISTER
My ProgressCoursesQuizzesProblemsContestsSmartbooks
Published on 18 Oct 2021
Send ERC token from one address to another address
ERC20 Token
img
Anurag Saini
0
Like
160

Introduction

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.

Solution

Heading Files

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()

Interface declaration

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 declaration

contract MyContract  {

}

If you want to change this contract name then only change " MyContract".

Variables

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.

Function declaration

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.

  • override means allows a property or method in a class to be overridden in a derived class.
  • public is our visibility
  • And it will return some unsigned integer value.

checkBalance function

    function checkBalance() override public view returns(uint){
        return balances[owner];
    }

check the current amount. 

 

Code:

// 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];
    }
    
}
    


Enjoyed the SmartBook?
Like
logo
contact@dapp-world.com
Katraj, Pune, Maharashtra, India - 411048

Follow Us

linkedintwitteryoutubediscordinstagram

Products

  • SmartBooks
  • Courses
  • Quizzes
  • Assessments

Support

  • Contact Us
  • FAQ
  • Privacy Policy
  • T&C

Backed By

ah! ventures

Copyright 2023 - All Rights Reserved.

Recommended from DAppWorld
img
1 May 2021
How to connect Ganache with Metamask and deploy Smart contracts on remix without
Set up your development environment with (Metamask + Ganache + Remix) and skip truffle :)
3 min read
11494
5
img
8 Jul 2021
How to interact with smart contarct from backend node js
call and send functions from backend server side using nodejs
3 min read
8069
2
img
18 Aug 2021
Send transaction with web3 using python
Introduction to web3.py and sending transaction on testnet
3 min read
6215
5
img
5 Aug 2021
Deploy Smart Contract on Polygon POS using Hardhat
how to deploy smart contracts on polygon pos chain using hardhat both mainnet and testnet ?
3 min read
5533
3