<commit_before>import {__} from 'embark-i18n';
import {AddressUtils, dappPath} from 'embark-utils';
const async = require('async');
import Web3 from 'web3';
const embarkJsUtils = require('embarkjs').Utils;
import checkContractSize from "./checkContractSize";
const {ZERO_ADDRESS} = AddressUtils;
import EthereumAPI from "./api";


class EthereumBlockchainClient {

  constructor(embark) {
    this.embark = embark;
    this.events = embark.events;
    this.logger = embark.logger;
    this.fs = embark.fs;

    this.logFile = dappPath(".embark", "contractEvents.json");

    this.embark.registerActionForEvent("deployment:contract:undeployed", this.addContractJSONToPipeline.bind(this));
    this.embark.registerActionForEvent("deployment:contract:deployed", this.addContractJSONToPipeline.bind(this));
    this.embark.registerActionForEvent('deployment:contract:beforeDeploy', this.determineArguments.bind(this));
    this.embark.registerActionForEvent('deployment:contract:beforeDeploy', this.doLinking.bind(this));
    this.embark.registerActionForEvent('deployment:contract:beforeDeploy', checkContractSize.bind(this));
    this.embark.registerActionForEvent('deployment:contract:beforeDeploy', this.determineAccounts.bind(this));
    this.events.request("blockchain:client:register", "ethereum", this.getClient.bind(this));
    this.events.request("deployment:deployer:register", "ethereum", this.deployer.bind(this));

    this.events.on("blockchain:started", () => {
      this._web3 = null;
    });

    this.registerAPIRequests();
  }

  get web3() {
    return (async () => {
      if (!this._web3) {
        const provider = await this.events.request2("blockchain:client:provider", "ethereum");
        this._web3 = new Web3(provider);
      }
      return this._web3;
    })();
  }

  async registerAPIRequests() {
    const ethereumApi = new EthereumAPI(this.embark, await this.web3, "ethereum");
    ethereumApi.registerRequests();
    ethereumApi.registerAPIs();
  }

  getClient() {
    return {};
  }

  async deployer(contract, done) {
    try {
      const web3 = await this.web3;
      const [account] = await web3.eth.getAccounts();
      const contractObj = new web3.eth.Contract(contract.abiDefinition, contract.address);
      const code = contract.code.substring(0, 2) === '0x' ? contract.code : "0x" + contract.code;
      const contractObject = contractObj.deploy({arguments: (contract.args || []), data: code});
      if (contract.gas === 'auto' || !contract.gas) {
        const gasValue = await contractObject.estimateGas({value: 0, from: account});
        const increase_per = 1 + (Math.random() / 10.0);
        contract.gas = Math.floor(gasValue * increase_per);
      }

      if (!contract.gasPrice) {
        contract.gasPrice = await web3.eth.getGasPrice();
      }

      embarkJsUtils.secureSend(web3, contractObject, {
        from: account, gas: contract.gas
      }, true, (err, receipt) => {
        if (err) {<commit_msg>fix(@embark/deployment): fix undefined in nb arguments in deploy<commit_after>import {__} from 'embark-i18n';
import {AddressUtils, dappPath} from 'embark-utils';
const async = require('async');
import Web3 from 'web3';
const embarkJsUtils = require('embarkjs').Utils;
import checkContractSize from "./checkContractSize";
const {ZERO_ADDRESS} = AddressUtils;
import EthereumAPI from "./api";


class EthereumBlockchainClient {

  constructor(embark) {
    this.embark = embark;
    this.events = embark.events;
    this.logger = embark.logger;
    this.fs = embark.fs;

    this.logFile = dappPath(".embark", "contractEvents.json");

    this.embark.registerActionForEvent("deployment:contract:undeployed", this.addContractJSONToPipeline.bind(this));
    this.embark.registerActionForEvent("deployment:contract:deployed", this.addContractJSONToPipeline.bind(this));
    this.embark.registerActionForEvent('deployment:contract:beforeDeploy', this.determineArguments.bind(this));
    this.embark.registerActionForEvent('deployment:contract:beforeDeploy', this.doLinking.bind(this));
    this.embark.registerActionForEvent('deployment:contract:beforeDeploy', checkContractSize.bind(this));
    this.embark.registerActionForEvent('deployment:contract:beforeDeploy', this.determineAccounts.bind(this));
    this.events.request("blockchain:client:register", "ethereum", this.getClient.bind(this));
    this.events.request("deployment:deployer:register", "ethereum", this.deployer.bind(this));

    this.events.on("blockchain:started", () => {
      this._web3 = null;
    });

    this.registerAPIRequests();
  }

  get web3() {
    return (async () => {
      if (!this._web3) {
        const provider = await this.events.request2("blockchain:client:provider", "ethereum");
        this._web3 = new Web3(provider);
      }
      return this._web3;
    })();
  }

  async registerAPIRequests() {
    const ethereumApi = new EthereumAPI(this.embark, await this.web3, "ethereum");
    ethereumApi.registerRequests();
    ethereumApi.registerAPIs();
  }

  getClient() {
    return {};
  }

  async deployer(contract, done) {
    try {
      const web3 = await this.web3;
      const [account] = await web3.eth.getAccounts();
      const contractObj = new web3.eth.Contract(contract.abiDefinition, contract.address);
      contractObj._jsonInterface.find(obj => {
        if (obj.type === 'constructor') {
          if (!obj.name) {
            // Add constructor as the name of the method, because if there is an error, it prints `undefined` otherwise
            obj.name = 'constructor';
          }
          return true;
        }
        return false;
      });
      const code = contract.code.substring(0, 2) === '0x' ? contract.code : "0x" + contract.code;
      const contractObject = contractObj.deploy({arguments: (contract.args || []), data: code});
      if (contract.gas === 'auto' || !contract.gas) {
        const gasValue = await contractObject.estimateGas({value: 0, from: account});
        const increase_per = 1 + (Math.random() / 10.0);
        contract.gas = Math.floor(gasValue * increase_per);
      }

      if (!contract.gasPrice) {
        contract.gasPrice = await web3.eth.getGasPrice();
      }

      embarkJsUtils.secureSend(web3, contractObject, {
        from: account, gas: contract.gas
      }, true, (err, receipt) => {
        if (err) {
{'fraction_non_alphanumeric': 0.14035364698452796, 'fraction_numerical': 0.008525418377012947, 'mean_word_length': 3.791981845688351, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '5322081', 'n_tokens_mistral': 1963, 'n_tokens_neox': 1900, 'n_words': 357}