Hi @jaureguino,
In OpenZeppelin Contracts 4 the repository organization has changed, so some of the contracts are in new locations.
To set decimals
to a value other than 18, we need to override the function (see: https://docs.openzeppelin.com/contracts/4.x/api/token/erc20#ERC20-decimals--):
function decimals() public view virtual override returns (uint8) {
return 9;
}
The following haven’t been appropriately tested or audited. You should appropriately test and audit before using in production and check that they meet your needs.
You can try these out on Remix as Remix now supports @
imports.
Extending from ERC20PresetMinterPauser:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;
import "@openzeppelin/contracts/token/ERC20/presets/ERC20PresetMinterPauser.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";
contract FilmVaultToken is ERC20PresetMinterPauser, ERC20Capped {
constructor() ERC20PresetMinterPauser("FilmVault", "FilmVault") ERC20Capped(1000000 * (10 ** uint256(decimals()))) {
}
function decimals() public view virtual override returns (uint8) {
return 9;
}
function _mint(address account, uint256 amount) internal virtual override(ERC20, ERC20Capped) {
super._mint(account, amount);
}
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override(ERC20, ERC20PresetMinterPauser) {
super._beforeTokenTransfer(from, to, amount);
}
}
Alternatively extending from ERC20:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
contract FilmVault is ERC20, ERC20Burnable, ERC20Capped, Pausable, AccessControl {
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
constructor() ERC20("FilmVault", "FV") ERC20Capped(1000000 * (10 ** uint256(decimals()))) {
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
_setupRole(PAUSER_ROLE, msg.sender);
_setupRole(MINTER_ROLE, msg.sender);
}
function decimals() public view virtual override returns (uint8) {
return 9;
}
function _mint(address account, uint256 amount) internal virtual override(ERC20, ERC20Capped) {
super._mint(account, amount);
}
function _beforeTokenTransfer(address from, address to, uint256 amount)
internal
whenNotPaused
override
{
super._beforeTokenTransfer(from, to, amount);
}
function pause() public {
require(hasRole(PAUSER_ROLE, msg.sender));
_pause();
}
function unpause() public {
require(hasRole(PAUSER_ROLE, msg.sender));
_unpause();
}
function mint(address to, uint256 amount) public {
require(hasRole(MINTER_ROLE, msg.sender));
_mint(to, amount);
}
}