Skip to content

Commit

Permalink
feat(ERC20Mintable): add access control roles
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrickGT committed Jun 7, 2022
1 parent b7eacf4 commit d43a053
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
35 changes: 30 additions & 5 deletions contracts/test/ERC20Mintable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,35 @@

pragma solidity 0.8.6;

import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

/**
* @dev Extension of {ERC20} that adds a set of accounts with the {MinterRole},
* which have permission to mint (create) new tokens as they see fit.
*
* At construction, the deployer of the contract is the only minter.
* At construction, the deployer of the contract is the admin and the only minter.
*/
contract ERC20Mintable is ERC20 {
constructor(string memory _name, string memory _symbol) ERC20(_name, _symbol) {}
contract ERC20Mintable is ERC20, AccessControl {
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");

uint8 internal _decimals;

constructor(
string memory _name,
string memory _symbol,
uint8 decimals_,
address _owner
) ERC20(_name, _symbol) {
_decimals = decimals_;

_grantRole(DEFAULT_ADMIN_ROLE, _owner);
_grantRole(MINTER_ROLE, _owner);
}

function decimals() public view override returns (uint8) {
return _decimals;
}

/**
* @dev See {ERC20-_mint}.
Expand All @@ -21,10 +40,11 @@ contract ERC20Mintable is ERC20 {
* - the caller must have the {MinterRole}.
*/
function mint(address account, uint256 amount) public {
require(hasRole(MINTER_ROLE, msg.sender), "ERC20Mintable/caller-not-minter");
_mint(account, amount);
}

function burn(address account, uint256 amount) public returns (bool) {
function burn(address account, uint256 amount) public onlyAdminRole returns (bool) {
_burn(account, amount);
return true;
}
Expand All @@ -33,7 +53,12 @@ contract ERC20Mintable is ERC20 {
address from,
address to,
uint256 amount
) public {
) public onlyAdminRole {
_transfer(from, to, amount);
}

modifier onlyAdminRole() {
require(hasRole(DEFAULT_ADMIN_ROLE, msg.sender), "ERC20Mintable/caller-not-admin");
_;
}
}
22 changes: 16 additions & 6 deletions test/features/support/PoolEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ function PoolEnv() {
this.token = async function (wallet) {
const yieldSource = await this.yieldSource();
const tokenAddress = await yieldSource.depositToken();
return (
await ethers.getContractAt('contracts/test/ERC20Mintable.sol:ERC20Mintable', tokenAddress)
).connect(wallet);
const token = await ethers.getContractAt('contracts/test/ERC20Mintable.sol:ERC20Mintable', tokenAddress);

await token.grantRole(token.MINTER_ROLE(), yieldSource.address);

return token.connect(wallet);
};

this.ticket = async (wallet) => (await ethers.getContract('Ticket')).connect(wallet);
Expand Down Expand Up @@ -66,7 +68,7 @@ function PoolEnv() {
let balance = await token.balanceOf(wallet.address);

if (balance.lt(amount)) {
await token.mint(wallet.address, amount, this.overrides);
await token.connect(owner).mint(wallet.address, amount, this.overrides);
}

await token.approve(prizePool.address, amount, this.overrides);
Expand All @@ -80,7 +82,6 @@ function PoolEnv() {

this.buyTicketsForPrizeDistributor = async function ({ user, tickets, prizeDistributor }) {
debug(`Buying tickets...`);
const owner = await this.wallet(0);
let wallet = await this.wallet(user);

debug('wallet is ', wallet.address);
Expand Down Expand Up @@ -153,7 +154,16 @@ function PoolEnv() {

this.poolAccrues = async function ({ tickets }) {
debug(`poolAccrues(${tickets})...`);
const owner = await this.wallet(0);
const yieldSource = await this.yieldSource();
const tokenAddress = await yieldSource.depositToken();
const token = await ethers.getContractAt(
"contracts/test/ERC20Mintable.sol:ERC20Mintable",
tokenAddress
);

await token.connect(owner).grantRole(token.MINTER_ROLE(), yieldSource.address);

await yieldSource.yield(toWei(tickets));
};

Expand Down Expand Up @@ -191,7 +201,7 @@ function PoolEnv() {
const prizeDistributions = {
bitRangeSize,
matchCardinality,
expiryDuration,
expiryDuration,
startTimestampOffset,
endTimestampOffset,
numberOfPicks,
Expand Down

0 comments on commit d43a053

Please sign in to comment.