I'm trying to create a market contract using the token I created as the currency. The market works by placing asks and bids and then using transferFrom function to hold onto the tokens. However the function fails even though I have approved the contract address. Here is my "Ask" method.

function addAsk (uint _amount, uint _price) public {
    string memory _timestamp = uint2str(block.timestamp);
    Ask storage ask = asks[msg.sender];
    ask.asker = msg.sender;
    ask.amount = _amount;
    ask.price = _price;
    ask.timestamp = _timestamp;
    //Works until here
    credits.transferFrom(msg.sender, address(this), _amount);
    emit AskPlaced(msg.sender, _amount, _price, _timestamp, tick);

Here is how I implemented my ERC20 token in the contract:

IERC20 public credits;

constructor() {
credits = new EnergyCredits();

I am getting this error every single time. enter image description here

I have pinpointed the problem to the following location: enter image description here

It seems the transferFrom function is having trouble when doing math operations to the "balances" mapping. What could be the reason for this?

  • Take a look at your EnergyCredits code,Have your tokens been allocated? Take a look at your front-end code, there may be a problem.
    – zmy
    Aug 2, 2021 at 2:21
  • How are you approving the tokens? Aug 2, 2021 at 9:51
  • ` function approve(address spender, uint256 tokens) public override returns (bool success) { allowed[msg.sender][spender] = tokens; emit Approval(msg.sender, spender, tokens); return true; } ` This is the method I use to approve the spender. I use Remix IDE with solidity 0.8.6 to approve the contract address as a spender. Aug 2, 2021 at 12:17
  • @BatuBüktel Please indicate where do you call approve, and how are you calling it (parameters used). In solc v0.8 airthmetic operations are checked for overflow.
    – Ismael
    Aug 4, 2021 at 4:10

1 Answer 1


I eventually solved this problem by using Open Zeppelin library to create my token. For some reason, I couldn't get my contract to work with my token contract.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.