Transferfrom function in ERC20

Hello

Fistly source link

Could anybody explain this function ?
Function takes “_from” and “_to
but
require(_value <= _allowed[_from][msg.sender], ‘ERC20: from not allowed’);

Who is _from?
Who is _to?
Who is msg.sender ?

 function transferFrom( address _from, address _to, uint256 _value) public override whenNotPaused  returns (bool)

    {

        require(_from != address(0), 'ERC20: from address is not valid');

         require(_to != address(0), 'ERC20: to address is not valid');
 
        require(_value <= _balances[_from], 'ERC20: insufficient balance');
 
         require(_value <= _allowed[_from][msg.sender], 'ERC20: from not allowed');
 
         _balances[_from] = SafeMath.sub(_balances[_from], _value);
 
         _balances[_to] = SafeMath.add(_balances[_to], _value);
 
         _allowed[_from][msg.sender] = SafeMath.sub(_allowed[_from][msg.sender], _value);
1 Like

hey @veridelisi. ok so address_from is the owner, namely the address who owns the tokens, the _to address, is the address that you want to send the tokens to., msg.sender is the account or address of the person calling the transferFrom function. This may be failing be cause your going to have to approve the _to address as the _from address first before calling transferFrom

Dear mcgrane

I’m 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2
I approve to the 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 as spender.
No problem

now I would like to sent my token to the 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db

from 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 to the 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db

but dont work

ss22

1 Like

ok so your address 0XAb… and you want to send tokens to 0X4B… to do this you need to approve 0X4B… and in the transferFrom you need to set the from address as 0xAb and the to address as 0X4B. try this

I approve 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db
it works

Butt

transfer from
from:0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2
to: 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db

doesnt work

1 Like

can you share your code ill get it working now for you

//SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.6.8;

import 'https://github.com/freitasgouvea/token-erc-20/blob/master/contracts/interfaces/IERC20.sol';

import 'https://github.com/freitasgouvea/token-erc-20/blob/master/contracts/libraries/SafeMath.sol';

import "https://github.com/freitasgouvea/token-erc-20/blob/master/contracts/ownership/Ownable.sol";

import "https://github.com/freitasgouvea/token-erc-20/blob/master/contracts/lifecycle/Pausable.sol";

contract ERC20 is IERC20, Ownable, Pausable {

    using SafeMath for uint;

    string internal _name;

    string internal _symbol;

    uint8 internal _decimals;

    uint256 internal _totalSupply;

    mapping (address => uint256) internal _balances;

    mapping (address => mapping (address => uint256)) internal _allowed;

    event Mint(address indexed minter, address indexed account, uint256 amount);

    event Burn(address indexed burner, address indexed account, uint256 amount);

    constructor (

        string memory name,

        string memory symbol,

        uint8 decimals,

        uint256 totalSupply

    ) public

    {

        _symbol = symbol;

        _name = name;

        _decimals = decimals;

        _totalSupply = totalSupply;

        _balances[msg.sender] = totalSupply;

    }

    function name() public view returns (string memory)

    {

        return _name;

    }

    function symbol() public view returns (string memory)

    {

        return _symbol;

    }

    function decimals() public view returns (uint8)

    {

        return _decimals;

    }

    function totalSupply() public view returns (uint256)

    {

        return _totalSupply;

    }

    function transfer(address _to,uint256 _value ) public override whenNotPaused returns (bool)

    {

        require(_to != address(0), 'ERC20: to address is not valid');

        require(_value <= _balances[msg.sender], 'ERC20: insufficient balance');

       

        _balances[msg.sender] = SafeMath.sub(_balances[msg.sender], _value);

        _balances[_to] = SafeMath.add(_balances[_to], _value);

       

        emit Transfer(msg.sender, _to, _value);

       

        return true;

    }

   function balanceOf(address _owner) public override view returns (uint256 )

    {

        return _balances[_owner];

    }

    function approve(address _spender,  uint256 _value) public override whenNotPaused returns (bool)

    {

        _allowed[msg.sender][_spender] = _value;

       

        emit Approval(msg.sender, _spender, _value);

       

        return true;

   }

   function transferFrom( address _from, address _to, uint256 _value) public override whenNotPaused  returns (bool)

    {

        require(_from != address(0), 'ERC20: from address is not valid');

        require(_to != address(0), 'ERC20: to address is not valid');

        require(_value <= _balances[_from], 'ERC20: insufficient balance');

        require(_value <= _allowed[_from][msg.sender], 'ERC20: from not allowed');

        _balances[_from] = SafeMath.sub(_balances[_from], _value);

        _balances[_to] = SafeMath.add(_balances[_to], _value);

        _allowed[_from][msg.sender] = SafeMath.sub(_allowed[_from][msg.sender], _value);

       

        emit Transfer(_from, _to, _value);

       

        return true;

   }

    function allowance(address _owner,address _spender) public override view whenNotPaused returns (uint256)

    {

        return _allowed[_owner][_spender];

    }

    function increaseApproval( address _spender, uint _addedValue) public whenNotPaused returns (bool)

    {

        _allowed[msg.sender][_spender] = SafeMath.add(_allowed[msg.sender][_spender], _addedValue);

       

        emit Approval(msg.sender, _spender, _allowed[msg.sender][_spender]);

       

        return true;

    }

    function decreaseApproval( address _spender, uint _subtractedValue) public whenNotPaused returns (bool)

    {

        uint oldValue = _allowed[msg.sender][_spender];

       

        if (_subtractedValue > oldValue) {

            _allowed[msg.sender][_spender] = 0;

        } else {

            _allowed[msg.sender][_spender] = SafeMath.sub(oldValue, _subtractedValue);

        }

       

        emit Approval(msg.sender, _spender, _allowed[msg.sender][_spender]);

       

        return true;

   }

    function mintTo(

        address _to,

        uint _amount

    ) public

        whenNotPaused

        onlyOwner

    {

        require(_to != address(0), 'ERC20: to address is not valid');

        require(_amount > 0, 'ERC20: amount is not valid');

        _totalSupply = _totalSupply.add(_amount);

        _balances[_to] = _balances[_to].add(_amount);

        emit Mint(msg.sender, _to, _amount);

    }

    function burnFrom(

        address _from,

        uint _amount

    ) public

        whenNotPaused

        onlyOwner

    {

        require(_from != address(0), 'ERC20: from address is not valid');

        require(_balances[_from] >= _amount, 'ERC20: insufficient balance');

       

        _balances[_from] = _balances[_from].sub(_amount);

        _totalSupply = _totalSupply.sub(_amount);

        emit Burn(msg.sender, _from, _amount);

    }

    function getadress() view public returns(address) {

        return address(this);

    }

    function getBalance() public view returns(uint256){

        return msg.sender.balance;

    }

}

ok so the logic in your transferFrom function was incorrect. The reason is because of this

require(_value <= _allowed[_from][msg.sender], 'ERC20: from not allowed');

here msg.sender and _from are the same address. you need to change this to

require(_value <= _allowed[_from][_to], 'ERC20: from not allowed');

so the fix was changing all instances of allowed[fromAddress][toAddress] to

_allowed[_from][_to];

the fixed transferFrom function is below

function transferFrom( address _from, address _to, uint256 _value) public override whenNotPaused  returns (bool)

    {

        require(_from != address(0), 'ERC20: from address is not valid');

        require(_to != address(0), 'ERC20: to address is not valid');

        require(_value <= _balances[_from], 'ERC20: insufficient balance');

        require(_value <= _allowed[_from][_to], 'ERC20: from not allowed');

        _balances[_from] = SafeMath.sub(_balances[_from], _value);

        _balances[_to] = SafeMath.add(_balances[_to], _value);

        _allowed[_from][_to] = SafeMath.sub(_allowed[_from][_to], _value);

       

        emit Transfer(_from, _to, _value);

       

        return true;

   }

didnt work mcgrane
11 ![111|270x168]
111

(upload://60uF4O1EjBguKHOrVIZmVshmxWA.png) ![1111|690x98]
(upload://371kCjlcsycYStoRi0H88SssHo6.png)

1 Like

ok so now the issue is that your putting in the wrong args to thos functions. let me explain

if i deploy the contract as 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2. i am th eowner. Now i want to send tokens to 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db.

the first thing i need to do is to call the approve function as 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2 and pass in 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db as the address i want to approve. run this function it will work.

No i want to send funds to 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db. so now i call the transferFrom function as 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2 . the from saddres is 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2 and the to address is 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db

have u got it resolved

1 Like

thank you mcgrane
now it is clear

1 Like

glad to help you out

Dear mcgrane

This is openzeppelin ERC20 Code

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol

I dont understand why this code uses the following assigment

address spender = _msgSender();

1 Like

how do u mean your not sure what it means. this check is used to make sure the spender of the tokens has sufficent allowance. if you search the spendAllowance function you will see that it checks this.

1 Like

thank you mcgrane5 for your kindly answer

1 Like

no worries if you have any other doubts be sure to message here in the forums im always on standyby