Skip to content

Instantly share code, notes, and snippets.

@riordant
Created January 21, 2021 09:06
Show Gist options
  • Save riordant/45b5e7e89c677064a4e5af7750ae7005 to your computer and use it in GitHub Desktop.
Save riordant/45b5e7e89c677064a4e5af7750ae7005 to your computer and use it in GitHub Desktop.
Miner for ganache-cli. run ganache-cli -d and this will mine blocks at an average of 5 seconds.
var ethers = require('ethers');
// ganache deterministic mnemonic
const mnemonic = 'myth like bonus scare over problem client lizard pioneer submit female collect';
// use last funded account
const account = "m/44'/60'/0'/0/9";
// default ganache provider
const provider = new ethers.providers.JsonRpcProvider("http://127.0.0.1:8545");
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
function timeout(secs) {
return new Promise(resolve => setTimeout(resolve, secs * 1000));
}
async function mine() {
wallet = new ethers.Wallet.fromMnemonic(mnemonic, account);
wallet = wallet.connect(provider);
amount = "1";
nonce = 0;
while(true){
console.log('mining tx..');
const transaction = {
nonce: nonce++,
to: wallet.address,
value: ethers.utils.parseEther(amount),
};
await wallet.sendTransaction(transaction);
console.log('done.');
await timeout(getRandomInt(2,8)); // average 5 second block time
}
}
mine();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment