Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Cannot use specific accounts programmatically #2571

Closed
MatthieuScarset opened this issue Mar 10, 2022 · 3 comments
Closed

Cannot use specific accounts programmatically #2571

MatthieuScarset opened this issue Mar 10, 2022 · 3 comments

Comments

@MatthieuScarset
Copy link
Contributor

MatthieuScarset commented Mar 10, 2022

Trying to start Ganache programmatically with a given list of accounts doesn't work.

const ganache = require("ganache");
const options = {
  wallet: {
    accounts: ["0xMyPrivateKey, 10000000000000000000"],
  }
};
const server = ganache.server(options);
const PORT = 8545;
server.listen(PORT, err => {
  if (err) throw err;
  console.log(`ganache listening on port ${PORT}...`);
  const provider = server.provider;
  provider.request({
    method: "eth_accounts",
    params: []
  }).then(accounts => console.log(accounts));
});

It does work using the CLI:

ganache-cli --account="0xMyPrivateKey, 10000000000000000000"

I've tried with different options but none works programmatically in JS:

const options = { account: "0xMyPrivateKey, 10000000000000000000" };
const options = { account: ["0xMyPrivateKey, 10000000000000000000"] };
const options = { wallet: { account: ["0xMyPrivateKey, 10000000000000000000"] } };

Anyone knows what's wrong with my options?
Or if there's another glitch I do not see?

@MicaiahReid
Copy link
Contributor

I was able to reproduce this and fixed it with:

const options = {
  wallet: {
    accounts: [{balance:"8AC7230489E80000", secretKey:"0xMyPrivateKey"}],
  }
};

This definitely isn't documented well, so I'll mark this as a bug. Let me know if the above fix doesn't work for you.
//cc @davidmurdoch

@MatthieuScarset
Copy link
Contributor Author

MatthieuScarset commented Mar 10, 2022

Thank you for your answer @MicaiahReid

It works 👍 but the balance seems to be malformatted in your example.

It needs to be "hex encoded and prefixed by 0x", that's what Ganache told me in my terminal 🤷‍♂️

I have Web3 dependency installed so I've used it to format the number to an Hex value.

So here's my final working JS code to start a Ganache chain with custom account options:

require("dotenv").config();
const ganache = require("ganache");
const Web3 = require('web3');

const options = {
  wallet: {
    accounts: [
      {
        secretKey: `${process.env.PRIVATE_KEY}`,
        balance: Web3.utils.toHex(`${process.env.INITIAL_BALANCE}`)
      }
    ],
  }
};

const port = process.env.PORT || 8545;
const server = ganache.server(options);
server.listen(port, err => {
  if (err) throw err;
  console.log(`ganache listening on port ${port}...`);
  const provider = server.provider;

  provider.request({
    method: "eth_accounts",
    params: []
  }).then(accounts => console.log(accounts));

});

This code assumes you have a .env file with these values:

# ===========================================================================
# NOT SAFE FOR PRODUCTION!!!
# ===========================================================================
PRIVATE_KEY="0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"
INITIAL_BALANCE="50000000000000000000"
PORT="8545"

Hope it helps others.

Thanks again

@MicaiahReid
Copy link
Contributor

@MatthieuScarset Oops, yes, good catch on the balance, I meant to type "0x8AC7230489E80000".

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants