I have a simple React Project with [email protected] installed and I want to call a contract's method like this:

await maincontract.methods.CurrentMethod("arguments").send({ from: accounts[0]/*my account should go here*/, gas: 3000000 });

This code works well in localhost Ganache but instead of accounts[0] from localhost I want to use my own account that I have in Ropsten network and I guess that to use that account, I need an infura project that can connect me to the Ropsten network and I also need the mnemonic phrase or private key of that account in order to use that account to call contract methods.

So basically how can I use my account located in Ropsten network to call a contract located in the same, Ropsten network? Do I need to pass in mnemonic phrase? Do I need to pass private key? what do I need to do?

I have tried using truffle-wallet-provider but it doesn't install, don't even ask about the errors. There is another truffle-wallet-provider.

This is what I am trying locally on Ganache but it doesn't seem to work, the contract code is working great, tested using Ethereum Remix.

const web3 = new Web3("HTTP://"); //Ganache Local Host
    const maincontract = new web3.eth.Contract(ABI, CONTRACT_ADDRESS); //already deployed the contract using Ethereum Remix and copied the Contract address and ABI
    const privateKey = "c1937e53c13e500644f45d77e075ca0059dcbae204ef06aa08494a129d871fdc"; //took the account right below the first account and copy pasted its private key
    const account = web3.eth.accounts.privateKeyToAccount(privateKey); //creating account
    const transaction = maincontract.methods.CurrentMethod("arguments");
    const options = { //TransactionConfig I guess
      to: transaction._parent._address,
      data: transaction.encodeABI(),
      gas: await transaction.estimateGas({ from: account.address }),
      gasPrice: await web3.eth.getGasPrice()
    await web3.eth.accounts.signTransaction(options, privateKey).then(tx => {
      //signed = tx.rawTransaction; 
      console.log(tx.rawTransaction.toString()) //this is showing random Hex but I see no transaction log in Ganache

1 Answer 1


Your scheme relies on your account being unlocked on the node that you're connected to.

While this scheme is reasonable for testing your system on your local host via Ganache, it is not recommended for an operational system, because anyone hacking your node could exploit your (unlocked) account at will.

Instead of unlocking your account on the node that you're connected to, you may want to consider signing each transaction before sending it to the node, thus minimizing the number of "breach points" in your sysetm.

For example:

const account = web3.eth.accounts.privateKeyToAccount(privateKey);
const transaction = maincontract.methods.CurrentMethod("arguments");
const options = {
    to      : transaction._parent._address,
    data    : transaction.encodeABI(),
    gas     : await transaction.estimateGas({from: account.address}),
    gasPrice: await web3.eth.getGasPrice() // or use some predefined value
const signed  = await web3.eth.accounts.signTransaction(options, privateKey);
const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
  • this sounds great, let me try this ASAP Commented Dec 22, 2019 at 11:12
  • what is transaction._parent._address in the options? I am already calling the contract address when creating the contract const like this const maincontract = new web3.eth.Contract(ABI, CONTRACT_ADDRESS); so what is the to in here? Commented Dec 22, 2019 at 15:27
  • 1
    @AKC: It's the contract address. Here, you can simply use maincontract._address or maincontract.options.address. More generally, if you put the code that I gave you inside a function and pass transaction to that function as an input parameter, then you can use transaction._parent._address (as I wrote in my answer). This shall allow you to use that function for different transactions (and not only for the specific one in your question). Commented Dec 22, 2019 at 15:34
  • hey the code isn't working, I tested it in Ganache, pretty basic stuff, I think I am making a mistake somewhere, accessing Ganache is ok with Ethereum Remix but its not calling the contract from web3, have you tested this code? I am using the exact same code but no luck. I am uploading the main code in the question at the end, hopefully it will help Commented Dec 22, 2019 at 16:15
  • @AKC: Yes I have tested it using web3.js v1.2.1. I'm not quite sure how you are using it in your script because you haven't posted it anywhere, and I'm not quite sure what you mean by "the code isn't working" because you haven't explained what the problem is and at which line of code. So I cannot really help you any further. Commented Dec 22, 2019 at 16:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.