mine.js 917 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. This script advances Ganache network state. It runs as a sidecar pod alongside the devnet and
  3. ensures that manual token transfers triggered through the web UI will be able to be confirmed.
  4. */
  5. advanceBlock = () => {
  6. return new Promise((resolve, reject) => {
  7. web3.currentProvider.send(
  8. {
  9. jsonrpc: "2.0",
  10. method: "evm_mine",
  11. id: new Date().getTime(),
  12. },
  13. (err, result) => {
  14. if (err) {
  15. return reject(err);
  16. }
  17. const newBlockHash = web3.eth.getBlock("latest").hash;
  18. return resolve(newBlockHash);
  19. },
  20. );
  21. });
  22. };
  23. function sleep(ms) {
  24. return new Promise((resolve) => setTimeout(resolve, ms));
  25. }
  26. module.exports = function (callback) {
  27. const fn = async () => {
  28. while (true) {
  29. console.log(await advanceBlock());
  30. await sleep(1000);
  31. }
  32. };
  33. fn().catch((reason) => console.error(reason));
  34. };