const ACCOUNT = '0x6B0c56d1Ad5144b4d37fa6e27DC9afd5C2435c3B';
const Web3 = require('web3');

function getFreshConnection(){
  return new Web3("ws://localhost:8546");
}

const web3 = getFreshConnection();

async function waitForConnection(after) {
  await getFreshConnection().eth.net.isListening()
    .then(() => {
      console.log('connected!');
      after();
    })
    .catch(e => setTimeout(() => {
      console.log('wait for connection');
      waitForConnection(after)
    }, 500));
}

async function waitForConnectionAsync() {
  await new Promise(resolve => waitForConnection(resolve));
}

async function timeout(ms){
  return await new Promise(resolve => setTimeout(resolve, ms));
}

module.exports = {web3, getFreshConnection, waitForConnectionAsync, ACCOUNT, timeout};
