You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
Trying to start Ganache programmatically with a given list of accounts doesn't work.
constganache=require("ganache");constoptions={wallet: {accounts: ["0xMyPrivateKey, 10000000000000000000"],}};constserver=ganache.server(options);constPORT=8545;server.listen(PORT,err=>{if(err)throwerr;console.log(`ganache listening on port ${PORT}...`);constprovider=server.provider;provider.request({method: "eth_accounts",params: []}).then(accounts=>console.log(accounts));});
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();constganache=require("ganache");constWeb3=require('web3');constoptions={wallet: {accounts: [{secretKey: `${process.env.PRIVATE_KEY}`,balance: Web3.utils.toHex(`${process.env.INITIAL_BALANCE}`)}],}};constport=process.env.PORT||8545;constserver=ganache.server(options);server.listen(port,err=>{if(err)throwerr;console.log(`ganache listening on port ${port}...`);constprovider=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"
Trying to start Ganache programmatically with a given list of accounts doesn't work.
It does work using the CLI:
ganache-cli --account="0xMyPrivateKey, 10000000000000000000"
I've tried with different options but none works programmatically in JS:
Anyone knows what's wrong with my options?
Or if there's another glitch I do not see?
The text was updated successfully, but these errors were encountered: