TransWikia.com

cannot transfer token from EOA account to a contract from function

Ethereum Asked by Shahzain Tariq on December 13, 2021

i was making a contract for staking where user transfer its token to a contract and i will give him interest but i got stuck to this problem i have been searching for 3-4 days but for it is not working i dont know what i am doing wrong

    pragma solidity ^0.5.0;

interface ERC20TokenInterface {
    function totalSupply() external view returns (uint);
    function name() external view returns (string memory);
    function symbol() external view returns (string memory);
    function decimals() external view returns (uint);
    function balanceOf(address tokenOwner) external view returns (uint256);
    function allowance(address tokenOwner, address spender) external view returns (uint remaining);
    function transfer(address to, uint tokens) external returns (bool success);
    function approve(address spender, uint tokens) external returns (bool success);
    function transferFrom(address from, address to, uint tokens) external returns (bool success);

    event Transfer(address indexed from, address indexed to, uint tokens);
    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
contract Stake{
    
    
    mapping(address => uint256) stakeAmount;
    
    address tokenAddress = 0x3a13Cb36dAd5d083449729Ea9dE53d68121a2c02;
    string tokens = "no";

    
    function stakeCoin(uint256 _amount) public {
        ERC20TokenInterface token = ERC20TokenInterface(tokenAddress);
        token.approve(address(this), _amount);
        token.transferFrom(msg.sender,address(this), _amount);
    }
    
    function stake1(uint256 _amount) public {
        ERC20TokenInterface token = ERC20TokenInterface(tokenAddress);
        token.transferFrom(msg.sender,address(this),_amount);
    }
    
    function tokenBalance() view public returns(uint256){
        ERC20TokenInterface token = ERC20TokenInterface(tokenAddress);
        return token.balanceOf(address(this));
    }
    
    function supply() view public returns(uint256){
        ERC20TokenInterface token = ERC20TokenInterface(tokenAddress);
        return token.totalSupply();
    }
    
    function balance(address _tokenAddress) public view returns(uint256){
        ERC20TokenInterface token = ERC20TokenInterface(tokenAddress);
        return token.balanceOf(_tokenAddress);
    }
    
}

when i run stake function transaction got even though msg.sender has tokens please me out with this problem

enter image description here
enter image description here
https://ropsten.etherscan.io/tx/0x7af622aa96a91ec2ad73d22c47fef6a5f029039d3620e0e97aae054a7c0d561e

One Answer

Transaction status: Fail with error 'ERC20: transfer amount exceeds allowance'

function stake1(uint256 _amount) public {
  ERC20TokenInterface token = ERC20TokenInterface(tokenAddress);
  token.transferFrom(msg.sender,address(this),_amount);
}

In order for this function to work, you first need to approve for contract transfer tokens behalf of owner (msg.sender). Those, before calling the stake1, you must call token.approve(CONTRACT_ADDRESS, amount) from owner account.

Answered by MentatX on December 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP