<commit_before>const _ = require('lodash');
const Promise = require("bluebird");
const path = require("path");
const chalk = require('chalk');
const util = require('./util.js');
const Web3 = new require('web3');
const SolidityEvent = require('web3/lib/web3/event.js');
const EthWallet = require('ethereumjs-wallet');
const ethUtil = require('ethereumjs-util');
const fs = require('fs');
const yamljs = require('yamljs');
Promise.config({
  longStackTraces: false
});

module.exports = exports = function(file, extra, walletFile, showTransactions, showEvents, genPreload) {
  var ALL_STEPS, VARS, CONTRACTS, STEP_INDEX, CONTRACT_ADDRESS, TX2CB, TEST_RESULTS = [], ALIAS_TO_NAME = {}, DEFAULT_KEY;

  util.readFile(file).then(function(data) {
    var cf;
    try {
      cf = JSON.parse(data);
    } catch(e) {
      cf = yamljs.parse(data);

      if (!cf) {
        console.log(chalk.red("Failed parsing config file: " + e));
        process.exit();
      }
    }
    DEFAULT_KEY = _.get(cf, 'default_key', null);
    VARS = _.extend(_.get(cf, 'vars', {}), extra);
    ALL_STEPS = _.get(cf, 'steps', []);
    CONTRACTS = {};
    CONTRACT_ADDRESS = _.get(cf, 'addresses', {});
    TX2CB = {};
    STEP_INDEX = 0;

    var walletPromise;
    if (walletFile) {
      walletPromise = extractPrivateKeyFromWallet(walletFile);
    } else {
      walletPromise = Promise.resolve();
    }

    var scriptDir = path.dirname(file);

    return walletPromise.then(function() {
      return util.step('Compiling contracts', Promise.map(cf.contracts || [], function(file) {
        return util.getContract(path.resolve(scriptDir, file));
      }).then(function(res) {
        res.forEach(function(c) {
          CONTRACTS[c.name] = c;
        });
      })).then(execStep);
    }).then(() => {
      if (genPreload) {
        let preload = [];
        _.each(CONTRACTS, (contract, name) => {
          preload.push(`${name}At = function(address) { return eth.contract(${JSON.stringify(contract.abi)}).at(address); }\n`);
        });

        _.each(CONTRACT_ADDRESS, (addr, alias) => {
          var name = CONTRACTS[ALIAS_TO_NAME[alias]].name;
          preload.push(`${alias} = ${name}At(${JSON.stringify(addr)});\n`);
        });


        return new Promise((resolve, reject) => {
          fs.writeFile(genPreload, preload.join('\n'), function(err) {
            if (err) {
              reject(`Failed writing preload file at ${genPreload}`);
            } else {
              console.log(chalk.green(`Preload file writen in ${genPreload}`));
              resolve();
            }
          });
        });
      }
    });
  }).catch(util.printException).finally(function() {
    if (TEST_RESULTS.length>0) {
      console.log();
      console.log();
      console.log('Test results:');
      TEST_RESULTS.forEach(function(line) {
        console.log(line);
      });
    }
    process.exit();
  });

  var resolveVar = function(name) {<commit_msg>Preload, set default account, receipt watching function<commit_after>const _ = require('lodash');
const Promise = require("bluebird");
const path = require("path");
const chalk = require('chalk');
const util = require('./util.js');
const Web3 = new require('web3');
const SolidityEvent = require('web3/lib/web3/event.js');
const EthWallet = require('ethereumjs-wallet');
const ethUtil = require('ethereumjs-util');
const fs = require('fs');
const yamljs = require('yamljs');
Promise.config({
  longStackTraces: false
});

module.exports = exports = function(file, extra, walletFile, showTransactions, showEvents, genPreload) {
  var ALL_STEPS, VARS, CONTRACTS, STEP_INDEX, CONTRACT_ADDRESS, TX2CB, TEST_RESULTS = [], ALIAS_TO_NAME = {}, DEFAULT_KEY;

  util.readFile(file).then(function(data) {
    var cf;
    try {
      cf = JSON.parse(data);
    } catch(e) {
      cf = yamljs.parse(data);

      if (!cf) {
        console.log(chalk.red("Failed parsing config file: " + e));
        process.exit();
      }
    }
    DEFAULT_KEY = _.get(cf, 'default_key', null);
    VARS = _.extend(_.get(cf, 'vars', {}), extra);
    ALL_STEPS = _.get(cf, 'steps', []);
    CONTRACTS = {};
    CONTRACT_ADDRESS = _.get(cf, 'addresses', {});
    TX2CB = {};
    STEP_INDEX = 0;

    var walletPromise;
    if (walletFile) {
      walletPromise = extractPrivateKeyFromWallet(walletFile);
    } else {
      walletPromise = Promise.resolve();
    }

    var scriptDir = path.dirname(file);

    return walletPromise.then(function() {
      return util.step('Compiling contracts', Promise.map(cf.contracts || [], function(file) {
        return util.getContract(path.resolve(scriptDir, file));
      }).then(function(res) {
        res.forEach(function(c) {
          CONTRACTS[c.name] = c;
        });
      })).then(execStep);
    }).then(() => {
      if (genPreload) {
        let preload = [];
        _.each(CONTRACTS, (contract, name) => {
          preload.push(`${name}At = function(address) { return eth.contract(${JSON.stringify(contract.abi)}).at(address); }\n`);
        });

        _.each(CONTRACT_ADDRESS, (addr, alias) => {
          var name = CONTRACTS[ALIAS_TO_NAME[alias]].name;
          preload.push(`${alias} = ${name}At(${JSON.stringify(addr)});\n`);
        });

        preload.push(`
eth.defaultAccount = eth.accounts[0];
function receipt(txId) {
  var filter = eth.filter('latest');
  filter.watch(function() {
    var ret = eth.getTransactionReceipt(txId);
    if (ret) {
      inspect(ret);
      filter.stopWatching();
    }
  });
}        
`);

        return new Promise((resolve, reject) => {
          fs.writeFile(genPreload, preload.join('\n'), function(err) {
            if (err) {
              reject(`Failed writing preload file at ${genPreload}`);
            } else {
              console.log(chalk.green(`Preload file writen in ${genPreload}`));
              resolve();
            }
          });
        });
      }
    });
  }).catch(util.printException).finally(function() {
    if (TEST_RESULTS.length>0) {
      console.log();
      console.log();
      console.log('Test results:');
      TEST_RESULTS.forEach(function(line) {
        console.log(line);
      });
    }
    process.exit();
  });

  var resolveVar = function(name) {
{'fraction_non_alphanumeric': 0.1694560669456067, 'fraction_numerical': 0.0027357579658834888, 'mean_word_length': 2.803549571603427, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4066177', 'n_tokens_mistral': 1978, 'n_tokens_neox': 1960, 'n_words': 366}