openzeppelin – Solving Undefined Identifier Issue with _setupDecimals()

openzeppelin

I'm trying to specify the amount of decimals of a token using OpenZeppelin. This will cause a compilation error:

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20 {
   //........

   constructor() ERC20("my token", "a")  {
       // _mint(..... )
     _setupDecimals(4); //  – undefined identifier
 }

//.........

What's the matter?

Best Answer

_setupDecimals is not a function of the normal OpenZepplin ERC20 contract.

To change the decimals you can override the decimals function and return another value.

Edit: This is also mentioned in the constructor documentation of the ERC20 contract:

The default value of {decimals} is 18. To select a different value for {decimals} you should overload it.

Related Topic