My metamask shows ALERT: Transaction Error. Exception thrown in contract code. When I am about to transfer using truffle, open zeppelin and ganache on localhost:7545

after confirming the transaction it shows

inpage.js:1 MetaMask - RPC Error: Error: [ethjs-rpc] rpc error with payload {"id":6736485951990,"jsonrpc":"2.0","params":["0xf86f048504a817c800836170d494ef9b511c0ddaea1f38ed82becb523a414dc6d7cc8806f05b59d3b2000080822d46a0fa7b64f06fe5520527e3ea35a445d3c461ca97ac25d73062caa9d64025fde7aaa02a5127f46ea982b6b71fd88ebe2a22679b45de0ebaef32137a65171f562caa03"],"method":"eth_sendRawTransaction"} [object Object] {code: -32603, message: "Error: [ethjs-rpc] rpc error with payload {"id":67…method":"eth_sendRawTransaction"} [object Object]", stack: "Error: Error: [ethjs-rpc] rpc error with payload {…method":"eth_sendRawTransaction"} [object Object]"}

My Crowdsale code

pragma solidity ^0.5.8;
import "../node_modules/openzeppelin-solidity/contracts/crowdsale/Crowdsale.sol";
import "../node_modules/openzeppelin-solidity/contracts/crowdsale/validation/TimedCrowdsale.sol";
import "../node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
import "../node_modules/openzeppelin-solidity/contracts/crowdsale/validation/CappedCrowdsale.sol";
import "../node_modules/openzeppelin-solidity/contracts/crowdsale/emission/AllowanceCrowdsale.sol";

contract MyCrowdsale is Crowdsale, TimedCrowdsale, CappedCrowdsale, AllowanceCrowdsale {
    constructor (
        uint256 _rate,
        address payable _wallet,
        ERC20 _token,
        uint256 _openingTime,
        uint256 _closingTime,
        uint256 _cap,
        address _tokenAdd
    Crowdsale(_rate, _wallet, _token)
    TimedCrowdsale(_openingTime, _closingTime)



module.exports = async function (deployer, network, accounts) {
    await deployer.deploy(A1Token, "A1Token", "A1T", 2,5000000);
    const deployedToken = await A1Token.deployed();
    const rate = 1000;
    const wallet = accounts[0];
    const timeNow = Math.floor(Date.now() / 1000);
    const openingTime = timeNow + duration.seconds(30);
    const closingTime = timeNow + duration.years(1);
    const cap = web3.utils.toWei("100");
    await deployer.deploy(MyCrowdsale,
         rate, wallet, deployedToken.address, openingTime, closingTime, cap, wallet
    const deployedCrowdsale = await MyCrowdsale.deployed();
    //await deployedToken.transfer(deployedCrowdsale.address, await deployedToken.totalSupply());
    deployedToken.approve(deployedCrowdsale.address, 20000);

And this is linked to a ERC20 token with an initial supply.

I have tried reinstalling metamask and clearing cache, i have enabled ethereum.enable();, tried using truffle develop account, ganache CLI and GUI, changing gas limit and gas. How do i fix this?

  • Are you using the same mnemonic in both ganache and truffle? Did you restart ganache after resetting metamask?
    – Ismael
    Jun 9, 2020 at 13:01
  • Hi, im sorry but i do not understand whether im using the same mnemonic, but the contracts are being carried out using the first address in ganache. I tried restarting and it did not work. I have just found out that the ganache log says not enough gas but when i try to increase it, it says Runtime Error: revert. Jun 9, 2020 at 13:25
  • Everytime you restart ganache it will change network id, accounts, and balances. That confuses metamask who is not aware of those changes. Either use ganache with a persistent database (ethereum.stackexchange.com/questions/40153/…) or reset metamask after every time you restart ganache.
    – Ismael
    Jun 9, 2020 at 13:59
  • Oh i see, i am using a ganache workspace therefore, the accounts and balances are always there Jun 9, 2020 at 14:34


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.