Need help running test script for ERC20PresetMinterPauser.sol

Hello, community. I am looking for help running unit tests for a contract that inherits from Ownable.sol and ERC20PresetMinterPauser.sol

I am using https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v4.3/test/token/ERC20/presets/ERC20PresetMinterPauser.test.js as a boilerplate.

Hitting a wall here and would love some help or direction if possible, thank you all so much!

:1234: Code to reproduce

// contracts/MasaToken.sol
// SPDX-License-Identifier: MIT
//Using OpenZepplin 3.0 and Truffle
//BP: next we need to add required funtions

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol"; //@dev added OpenZepplin 3.0 Ownable
import "@openzeppelin/contracts/token/ERC20/presets/ERC20PresetMinterPauser.sol"; //@dev added OpenZepplin 3.0 Minter and Pauser

contract MasaToken is ERC20PresetMinterPauser, Ownable {
    
    string public _name = "Masa"; //token name
	string public _symbol = "CORN"; //token symbol
	uint8 public _decimals = 18; 
	uint256 private _initialSupply = 1192258185 * (10 ** uint256(decimals())); // mint supply of 1,192,258,185;

    constructor() ERC20PresetMinterPauser(_name, _symbol) {
        _mint(msg.sender, _initialSupply);
    }

}

Testing script

//test script is based on the boilerplate from: https://github.com/OpenZeppelin/openzeppelin-contracts/edit/master/test/token/ERC20/presets/ERC20PresetMinterPauser.test.js


//@dec import utilities and test helpers
const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { ZERO_ADDRESS } = constants;

const { expect } = require('chai');


//@dev load compiled artifacts

const MasaToken = artifacts.require('MasaToken'); //@dev needs https://hardhat.org/plugins/nomiclabs-hardhat-truffle4.html; otherwise test throw's 'TypeError: artifacts.require is not a function'

//@dev start test block

contract('MasaToken', function (accounts) {
  const [ deployer, other ] = accounts;

  const name = 'Masa'; //@dev the name for the Masa Token smart contract
  const symbol = 'CORN'; //@dev the symbol for the Masa Token

  const amount = new BN('1192258185'); //

  const DEFAULT_ADMIN_ROLE = '0x0000000000000000000000000000000000000000000000000000000000000000';
  const MINTER_ROLE = web3.utils.soliditySha3('MINTER_ROLE');
  const PAUSER_ROLE = web3.utils.soliditySha3('PAUSER_ROLE');

  beforeEach(async function () {
    this.token = await MasaToken.new(name, symbol, { from: deployer });
  });

  it('deployer has the default admin role', async function () {
    expect(await this.token.getRoleMemberCount(DEFAULT_ADMIN_ROLE)).to.be.bignumber.equal('1');
    expect(await this.token.getRoleMember(DEFAULT_ADMIN_ROLE, 0)).to.equal(deployer);
  });

  it('deployer has the minter role', async function () {
    expect(await this.token.getRoleMemberCount(MINTER_ROLE)).to.be.bignumber.equal('1');
    expect(await this.token.getRoleMember(MINTER_ROLE, 0)).to.equal(deployer);
  });

  it('deployer has the pauser role', async function () {
    expect(await this.token.getRoleMemberCount(PAUSER_ROLE)).to.be.bignumber.equal('1');
    expect(await this.token.getRoleMember(PAUSER_ROLE, 0)).to.equal(deployer);
  });

  it('minter and pauser role admin is the default admin', async function () {
    expect(await this.token.getRoleAdmin(MINTER_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
    expect(await this.token.getRoleAdmin(PAUSER_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
  });

  describe('minting', function () {
    it('deployer can mint tokens', async function () {
      const receipt = await this.token.mint(other, amount, { from: deployer });
      expectEvent(receipt, 'Transfer', { from: ZERO_ADDRESS, to: other, value: amount });

      expect(await this.token.balanceOf(other)).to.be.bignumber.equal(amount);
    });

    it('other accounts cannot mint tokens', async function () {
      await expectRevert(
        this.token.mint(other, amount, { from: other }),
        'MasaToken: must have minter role to mint',
      );
    });
  });

  describe('pausing', function () {
    it('deployer can pause', async function () {
      const receipt = await this.token.pause({ from: deployer });
      expectEvent(receipt, 'Paused', { account: deployer });

      expect(await this.token.paused()).to.equal(true);
    });

    it('deployer can unpause', async function () {
      await this.token.pause({ from: deployer });

      const receipt = await this.token.unpause({ from: deployer });
      expectEvent(receipt, 'Unpaused', { account: deployer });

      expect(await this.token.paused()).to.equal(false);
    });

    it('cannot mint while paused', async function () {
      await this.token.pause({ from: deployer });

      await expectRevert(
        this.token.mint(other, amount, { from: deployer }),
        'ERC20Pausable: token transfer while paused',
      );
    });

    it('other accounts cannot pause', async function () {
      await expectRevert(
        this.token.pause({ from: other }),
        'MasaToken: must have pauser role to pause',
      );
    });

    it('other accounts cannot unpause', async function () {
      await this.token.pause({ from: deployer });

      await expectRevert(
        this.token.unpause({ from: other }),
        'MasaToken: must have pauser role to unpause',
      );
    });
  });

  describe('burning', function () {
    it('holders can burn their tokens', async function () {
      await this.token.mint(other, amount, { from: deployer });

      const receipt = await this.token.burn(amount.subn(1), { from: other });
      expectEvent(receipt, 'Transfer', { from: other, to: ZERO_ADDRESS, value: amount.subn(1) });

      expect(await this.token.balanceOf(other)).to.be.bignumber.equal('1');
    });
  });
});

Throws the following error in truffle test

Contract: MasaToken
    1) "before each" hook for "deployer has the default admin role"


  0 passing (501ms)
  1 failing

  1) Contract: MasaToken
       "before each" hook for "deployer has the default admin role":
     Error: Invalid number of parameters for "undefined". Got 3 expected 0!
      at Context.<anonymous> (test/MasaToken.js:29:37)
      at processTicksAndRejections (internal/process/task_queues.js:95:5)

Throws the following error in hardhat

TypeError: Cannot read property 'soliditySha3' of undefined

:computer: Environment

hardhat configs

require("@nomiclabs/hardhat-waffle");
//require("hardhat-erc1820");
/**
 * @type import('hardhat/config').HardhatUserConfig
 */
require("@nomiclabs/hardhat-etherscan");
require("@nomiclabs/hardhat-truffle4");
require("@nomiclabs/hardhat-web3");

Using:

  1. Truffle
  2. Hardhat
  3. Chai

It looks like your constructor is currently written to take no arguments, that's why the error message is saying "Invalid number of parameters". You are passing both the name and symbol in the beforeEach function of your unit tests.

1 Like

You have to choose one of Truffle or Hardhat, not both. Note that if you use Hardhat with the Truffle-like setup, you still have to run hardhat test. See Hardhat - Testing with Web3.js & Truffle