'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _truffleContract = require('truffle-contract');

var _truffleContract2 = _interopRequireDefault(_truffleContract);

var _eth = require('../constants/eth');

var ethConstants = _interopRequireWildcard(_eth);

var _error = require('../constants/error');

var _isRequired = require('./isRequired');

var _isRequired2 = _interopRequireDefault(_isRequired);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * Deploy a contract on the Ethereum network using the contract artifact.
 * @param {string} account - The account to deploy it under.
 * @param {number} value - The value to send.
 * @param {object} artifact - JSON artifact of the contract.
 * @param {object} web3Provider - Web3 Provider object (NOTE NOT Kleros Web3Wrapper)
 * @param {...any} args - Extra arguments.
 * @returns {object} - truffle-contract Object | err The contract object or an error
 */
var deployContractAsync = function () {
  var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
    for (var _len = arguments.length, args = Array(_len > 4 ? _len - 4 : 0), _key = 4; _key < _len; _key++) {
      args[_key - 4] = arguments[_key];
    }

    var account = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _isRequired2.default)('account');
    var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : (0, _isRequired2.default)('value');
    var artifact = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : (0, _isRequired2.default)('artifact');
    var web3Provider = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : (0, _isRequired2.default)('web3Provider');
    var MyContract;
    return _regenerator2.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.prev = 0;
            MyContract = (0, _truffleContract2.default)({
              abi: artifact.abi,
              unlinked_binary: artifact.bytecode ? artifact.bytecode : artifact.unlinked_binary
            });

            MyContract.setProvider(web3Provider);

            return _context.abrupt('return', MyContract.new.apply(MyContract, args.concat([{
              from: account,
              value: value,
              gas: ethConstants.TRANSACTION.GAS
            }])));

          case 6:
            _context.prev = 6;
            _context.t0 = _context['catch'](0);

            console.error(_context.t0);
            throw new Error(_error.UNABLE_TO_DEPLOY_CONTRACT);

          case 10:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, undefined, [[0, 6]]);
  }));

  return function deployContractAsync() {
    return _ref.apply(this, arguments);
  };
}();

exports.default = deployContractAsync;
//# sourceMappingURL=deployContractAsync.js.map