AnswerBun.com

transferFrom error. Exception thrown in smart contract transaction

I compiled smart contract in solidity on remix.ethereum.org. I made account via MetaMask and want to let other addresses send tokens to themselves using ropsten network. I mean – I am contract owner = A, transfer some tokens to B and let him transfer/withdraw his tokens to address C. There are two transfer functions:

  • transfer, send tokens from contract owner to receiver address and it’s working properly
  • transferFrom, is causing problems, send tokens from B to C.
    I tried to use it via remix, write contract on etherscan and with web3.py but all of them return error.

Firstly I deployed contract with 1000000 tokens (1000.000 with 3 decimals) and sent 1000 (1.000) tokens to B (balanceOf(B) returned 1000), then called approve function with 1000 tokens for B address (allowance returned 1000), and called transferFrom, which threw errors:

  • remix: “Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
    gas required exceeds allowance (8000029) or always failing transaction”
  • MetaMask on etherscan: “ALERT: Transaction Error. Exception thrown in contract code.”

Another weird thing is sending tokens via transferFrom function from contract owner’s address works properly like normal transfer. Also transfer exaclty 0 tokens from B to C works normally.
I can not figure out what is wrong, below is contract code:

pragma solidity >=0.4.22 <0.7.0;

contract AnotherCoin {

    string public constant name = "AnotherCoin";
    string public constant symbol = "ATC";
    uint8 public constant decimals = 3;  


    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
    event Transfer(address indexed from, address indexed to, uint tokens);


    mapping(address => uint256) balances;

    mapping(address => mapping (address => uint256)) allowed;

    uint256 totalSupply_;

    using SafeMath for uint256;


    constructor(uint256 total) public {  
        totalSupply_ = total;
        balances[msg.sender] = totalSupply_;
    }  

    function totalSupply() public view returns (uint256) {
        return totalSupply_;
    }

    function balanceOf(address tokenOwner) public view returns (uint) {
        return balances[tokenOwner];
    }

    function transfer(address receiver, uint numTokens) public returns (bool) {
        require(numTokens <= balances[msg.sender]);
        balances[msg.sender] = balances[msg.sender].sub(numTokens);
        balances[receiver] = balances[receiver].add(numTokens);
        emit Transfer(msg.sender, receiver, numTokens);
        return true;
    }

    function approve(address delegate, uint numTokens) public returns (bool) {
        allowed[msg.sender][delegate] = numTokens;
        emit Approval(msg.sender, delegate, numTokens);
        return true;
    }

    function allowance(address owner, address delegate) public view returns (uint) {
        return allowed[owner][delegate];
    }

    function transferFrom(address owner, address buyer, uint numTokens) public payable returns (bool) {
        require(numTokens <= balances[owner]);    
        require(numTokens <= allowed[owner][msg.sender]);

        balances[owner] = balances[owner].sub(numTokens);
        allowed[owner][msg.sender] = allowed[owner][msg.sender].sub(numTokens);
        balances[buyer] = balances[buyer].add(numTokens);
        emit Transfer(owner, buyer, numTokens);
        return true;
    }
}

library SafeMath { 
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
      assert(b <= a);
      return a - b;
    }

    function add(uint256 a, uint256 b) internal pure returns (uint256) {
      uint256 c = a + b;
      assert(c >= a);
      return c;
    }
}

I tried compile it on different compiler versions (4.x, 5.x, 6.x) and compilation was succesfull but this
single function throw errors. Similiar topics on stack didn’t bring the solution.

Ethereum Asked by WR10 on January 2, 2021

1 Answers

One Answer

So I tried the following thing:

Made your mappings public to see what was happening. After using the approve function where owner was 'A' and delegate was 'B', I checked the allowed mapping and it was still showing 0. So after using approve 2nd time, the mapping was updated(I don't know why) and then I used the transferFrom where owner was 'A', buyer was 'C' and amount was equal to approved amount. The address calling transferFrom was 'B' and it was successful.

You are getting this error because: The mapping is not being updated properly.

Hope this helped

Answered by Hackeerrrr on January 2, 2021

Add your own answers!

Related Questions

why web3.eth.getBlockNumber().then(console.log) returns 0

2  Asked on December 9, 2020 by shifted-five

     

Does getPastLogs require arvhived parity node?

1  Asked on December 6, 2020 by miao-zhicheng

   

Solidity & truffle: get elements from strings array

2  Asked on December 5, 2020 by paulo-n-carrillo-pea

     

Typechain and Batch Requests

0  Asked on December 2, 2020 by slider

   

Unhandled Rejection (TypeError): this.web3.eth is undefined

2  Asked on November 29, 2020 by chan-austin

     

Synchronizing test and web3 enabled nodejs service

1  Asked on November 26, 2020 by zach_is_my_name

     

getting the latest holders address:balance for a smart contract

1  Asked on November 21, 2020 by lucas-rodriguez-benitez

   

Token migration and IDEX smart contract problem

1  Asked on November 21, 2020 by crypto-lemma

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.