UNPKG

@colony/colony-js-contract-client

Version:

Method-like interface for Smart Contracts

365 lines (291 loc) 11.9 kB
'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 _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _assert = require('assert'); var _assert2 = _interopRequireDefault(_assert); var _colonyJsUtils = require('@colony/colony-js-utils'); var _ContractClient = require('./ContractClient'); var _ContractClient2 = _interopRequireDefault(_ContractClient); var _ContractMethodSender2 = require('./ContractMethodSender'); var _ContractMethodSender3 = _interopRequireDefault(_ContractMethodSender2); var _MultisigOperation = require('./MultisigOperation'); var _MultisigOperation2 = _interopRequireDefault(_MultisigOperation); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var ContractMethodMultisigSender = function (_ContractMethodSender) { (0, _inherits3.default)(ContractMethodMultisigSender, _ContractMethodSender); /** * {string} functionName - The contract function name to use for * creating the transaction data * nonceFunctionName - The contract function name to use for * getting the transaction nonce value * required in order to send the transaction * getRequiredSignees - Async function that returns the addresses of * the signers which will be required in order to send the transaction * multisigFunctionName - The contract function name to use for * sending the finalized transaction (with multisig support) */ function ContractMethodMultisigSender(_ref) { var client = _ref.client, functionName = _ref.functionName, input = _ref.input, name = _ref.name, getRequiredSignees = _ref.getRequiredSignees, multisigFunctionName = _ref.multisigFunctionName, nonceFunctionName = _ref.nonceFunctionName, nonceInput = _ref.nonceInput, output = _ref.output; (0, _classCallCheck3.default)(this, ContractMethodMultisigSender); var _this = (0, _possibleConstructorReturn3.default)(this, (ContractMethodMultisigSender.__proto__ || Object.getPrototypeOf(ContractMethodMultisigSender)).call(this, { client: client, name: name, output: output, input: input, functionName: functionName })); _this._getRequiredSignees = getRequiredSignees; _this.multisigFunctionName = multisigFunctionName; _this.nonceFunctionName = nonceFunctionName; _this.nonceInput = nonceInput; return _this; } /** * Given a payload and optional signers, start a new MultisigOperation and * refresh it (in order to set the required signees/nonce/etc). */ (0, _createClass3.default)(ContractMethodMultisigSender, [{ key: '_startOperation', value: function () { var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(args) { var op; return _regenerator2.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: // Will throw an error if the payload, signers or nonce are deemed invalid. op = new _MultisigOperation2.default(this, args); _context.next = 3; return op.refresh(); case 3: return _context.abrupt('return', op); case 4: case 'end': return _context.stop(); } } }, _callee, this); })); function _startOperation(_x) { return _ref2.apply(this, arguments); } return _startOperation; }() }, { key: 'getRequiredSignees', value: function () { var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(inputValues) { var signees; return _regenerator2.default.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return this._getRequiredSignees(inputValues); case 2: signees = _context2.sent; (0, _assert2.default)(Array.isArray(signees) && signees.every(_colonyJsUtils.isValidAddress), 'Expected an array of signee addresses'); return _context2.abrupt('return', signees); case 5: case 'end': return _context2.stop(); } } }, _callee2, this); })); function getRequiredSignees(_x2) { return _ref3.apply(this, arguments); } return getRequiredSignees; }() }, { key: 'getNonce', value: function () { var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(inputValues) { var args, response, nonce; return _regenerator2.default.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: args = this.getValidatedArgs(inputValues, this.nonceInput); _context3.next = 3; return this.client.call(this.nonceFunctionName, args); case 3: response = _context3.sent; nonce = (0, _colonyJsUtils.isBigNumber)(response) ? response.toNumber() : response; (0, _assert2.default)(Number(nonce) === nonce && Number.isInteger(nonce), 'Nonce must be an integer'); return _context3.abrupt('return', nonce); case 7: case 'end': return _context3.stop(); } } }, _callee3, this); })); function getNonce(_x3) { return _ref4.apply(this, arguments); } return getNonce; }() // eslint-disable-next-line class-methods-use-this, no-unused-vars }, { key: 'send', value: function () { var _ref5 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4(inputValues, options) { return _regenerator2.default.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: throw new Error('This Sender uses multi-signature transactions; call ' + '`.startOperation()` to start a new `MultisigOperation`'); case 1: case 'end': return _context4.stop(); } } }, _callee4, this); })); function send(_x4, _x5) { return _ref5.apply(this, arguments); } return send; }() /** * Given arguments and options for a multisig function, send a transaction. */ }, { key: 'sendMultisig', value: function () { var _ref6 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5(args, options) { return _regenerator2.default.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: return _context5.abrupt('return', this._send(args, options)); case 1: case 'end': return _context5.stop(); } } }, _callee5, this); })); function sendMultisig(_x6, _x7) { return _ref6.apply(this, arguments); } return sendMultisig; }() /** * Given input values for the target contract method, create transaction data, * and pass the payload into a new MultisigOperation. */ }, { key: 'startOperation', value: function () { var _ref7 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6(inputValues) { var args, data; return _regenerator2.default.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: args = this.getValidatedArgs(inputValues); data = this.client.createTransactionData(this.functionName, args); return _context6.abrupt('return', this._startOperation({ payload: { data: data, inputValues: inputValues, destinationAddress: this.client.contract.address, sourceAddress: this.client.contract.address, value: 0 } })); case 3: case 'end': return _context6.stop(); } } }, _callee6, this); })); function startOperation(_x8) { return _ref7.apply(this, arguments); } return startOperation; }() /** * Given the state of an operation as JSON, restore a MultisigOperation. */ }, { key: 'restoreOperation', value: function () { var _ref8 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee7(json) { var parsed; return _regenerator2.default.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: parsed = {}; _context7.prev = 1; parsed = JSON.parse(json); _context7.next = 8; break; case 5: _context7.prev = 5; _context7.t0 = _context7['catch'](1); throw new Error('Unable to restore operation: could not parse JSON'); case 8: return _context7.abrupt('return', this._startOperation(parsed)); case 9: case 'end': return _context7.stop(); } } }, _callee7, this, [[1, 5]]); })); function restoreOperation(_x9) { return _ref8.apply(this, arguments); } return restoreOperation; }() /** * Override the method from the parent class; use `multisigFunctionName`. */ }, { key: '_sendTransaction', value: function () { var _ref9 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee8(callArgs, transactionOptions) { return _regenerator2.default.wrap(function _callee8$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: return _context8.abrupt('return', this.client.send(this.multisigFunctionName, callArgs, transactionOptions)); case 1: case 'end': return _context8.stop(); } } }, _callee8, this); })); function _sendTransaction(_x10, _x11) { return _ref9.apply(this, arguments); } return _sendTransaction; }() }]); return ContractMethodMultisigSender; }(_ContractMethodSender3.default); /* eslint-disable import/no-cycle */ exports.default = ContractMethodMultisigSender; //# sourceMappingURL=ContractMethodMultisigSender.js.map