How to write a test with "from"-argument to send a transaction from a contract?

In case I have a ERC20Mintable token, and a MintableCrowdsale-contract becomes the only MinterRole on that token, how can I write proper tests for this specific role?

Lets say, I have deployed a token and instantiate the token sale as follows:

beforeEach(async function () {
    tokenSale = await MintingTokenSale.new(openingTime, closingTime, token.address, {from: owner});

    await token.addMinter(tokenSale.address, { from: owner });
    await token.renounceMinter({ from: owner });
});

With latest 2 lines I made the TokenSale-contract to the only minter.

What I tried out now was to mint a token from this specific role.

        await token.mint(buyer, 5, {from: tokenSale.address});

But this raises an error in “truffle test”:

Error: Returned error: sender account not recognized

2 Likes

Hi @oxuw4

It isn’t possible to send a transaction in testing as if it were from a smart contract. So the following (as you found) can’t work:

await token.mint(buyer, 5, {from: tokenSale.address});

You can test the public/external functions of the smart contract using Externally Owned Accounts (EOAs), so to test the behavior of minting where the only minter is the crowdsale contract, you need to purchase via the crowdsale and can use accounts provided by ganache.

I suggest you have a look at the existing OpenZeppelin tests, including the MintedCrowdsale test.

Hi @oxuw4 did you have any more questions on this?
If this solved your question, you can mark it the reply as the solution.