Error using ERC20 transferFrom in a buy function selling tokens

Hi @PreciousChicken,

transferFrom :

  • sender must have a balance of at least amount.
  • the caller must have allowance for sender's tokens of at least amount

Which in your case the sender doesn't have any tokens (except if they deployed the token) as they are the ones buying the tokens.

You need your contract selling tokens to have an amount of tokens to sell. You could transfer tokens from the deployer of the token contract to the selling contract or you could get the CoinDeploy contract to deploy the Coin token contract and then it would be the holder of the total supply.

Your CoinDeploy should then use transfer rather than transferFrom when selling tokens.


Taking a step back, you are effectively creating a Crowdsale for a token. Crowdsales are not part of OpenZeppelin Contracts v3.x but are in OpenZeppelin Contracts v2.x

I suggest looking at the Crowdsale documentation.


As an aside, coin is generally used for the native currency of a blockchain, whilst a token is not the native currency. A blockchain will have one type of coin but potentially lots of different tokens.


Finally I suggest looking at Points to consider when creating a fungible token (ERC20, ERC777)

1 Like