Metamask returns RPC Error: Error: [ethjs-rpc] rpc error with payload, metamask shows failed [object, object]

:computer: Environment

Truffle, VSC, Ganache, solidity ^0.5.8

:memo:Details

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]"}

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?
:1234: Code to reproduce
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)
    CappedCrowdsale(_cap)
    AllowanceCrowdsale(_wallet)
    
    public
    {
    }
}

deploy_contract.js

module.exports = async function (deployer, network, accounts) {
    await deployer.deploy(A1Token, "A1Token", "A1T", 2,5000000);
    //deployer.deploy(AddNumbers);
    const deployedToken = await A1Token.deployed();
    console.log(deployedToken.address)
    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");
    console.log(cap)
    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);
    console.log(deployedCrowdsale.address);
}

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

1 Like

Hi @DonkyKong123,

Welcome to the community :wave:

Does the error occur when you transfer ether to the crowdsale contract using MetaMask?

Do you get the same error when you call buyTokens using truffle console?

Do you have any tests for your crowdsale contract? OpenZeppelin Contracts tests are a great place to look at ways to test. (https://github.com/OpenZeppelin/openzeppelin-contracts/tree/v2.5.1/test/crowdsale)


As an aside, the npm package name is @openzeppelin/contracts rather than the old name openzeppelin-solidity.

We can import as follows:

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
1 Like

Hi @abcoathup thank you so much! It works! I think it is related the contracts as i was using version 2.1. Anyways thank you once again!

1 Like

A post was split to a new topic: Metamask returns RPC Error

I ran into this error message myself today, but since it’s so cryptic/vague, I wanted to share my solution in case anyone else runs into this. The answer for me was very simple: Just go to Settings --> Advanced in Metamask and then Reset Account (see screenshot below).

The reason you need to do this is because Metamask keeps state internally about the blocknumber of whatever network it’s connected to. Since this was a local network, I had restarted it a couple times, and then Metamask gets confused because the blocknumber you’re trying to submit a transaction for is behind the ones it has in it’s internal state. Re-setting the history is fast/painless, and doesn’t change your balances, and then everything worked fine for me. So just throwing that out there in case anyone else sees this from Google!

2 Likes

thanks, it solve my problem.

1 Like