@colony/colony-js-contract-client
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -31,2 +31,6 @@ 'use strict'; | ||
var _ContractEvent = require('./ContractEvent'); | ||
var _ContractEvent2 = _interopRequireDefault(_ContractEvent); | ||
var _ContractMethod = require('./ContractMethod'); | ||
@@ -59,3 +63,5 @@ | ||
// The contract interface (as provided by the adapter) | ||
// The contract loading query the class was constructed with | ||
// The adapter used to communicate with the blockchain | ||
get: function get() { | ||
@@ -65,6 +71,7 @@ return _ContractMethodCaller2.default; | ||
// The contract loading query the class was constructed with | ||
// The contract event subscription methods | ||
// The adapter used to communicate with the blockchain | ||
// The contract interface (as provided by the adapter) | ||
}, { | ||
@@ -81,2 +88,7 @@ key: 'Sender', | ||
}, { | ||
key: 'Event', | ||
get: function get() { | ||
return _ContractEvent2.default; | ||
} | ||
}, { | ||
key: 'defaultQuery', | ||
@@ -94,2 +106,3 @@ get: function get() { | ||
(0, _classCallCheck3.default)(this, ContractClient); | ||
this.events = {}; | ||
@@ -291,3 +304,22 @@ this.adapter = adapter; | ||
} | ||
/** | ||
* Add event subscription functionality for a particular event of this | ||
* contract to the given ContractClient instance. | ||
*/ | ||
}, { | ||
key: 'addEvent', | ||
value: function addEvent(eventName, argsDef) { | ||
if (Reflect.has(this.events, eventName)) { | ||
throw new Error('An event named "' + eventName + '" already exists'); | ||
} | ||
Object.assign(this.events, (0, _defineProperty3.default)({}, eventName, new _ContractEvent2.default({ | ||
eventName: eventName, | ||
client: this, | ||
argsDef: argsDef | ||
}))); | ||
} | ||
}, { | ||
key: 'contract', | ||
@@ -294,0 +326,0 @@ get: function get() { |
@@ -33,5 +33,5 @@ 'use strict'; | ||
var _paramTypes = require('../modules/paramTypes'); | ||
var _paramConversion = require('../modules/paramConversion'); | ||
var _inputValidation = require('../modules/inputValidation'); | ||
var _paramValidation = require('../modules/paramValidation'); | ||
@@ -63,60 +63,10 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
* Given the result of a contract method call, and the input values used to | ||
* make the call, transform these with the expected output parameters in | ||
* order to get named output values as the method's `OutputValues` | ||
* The inputValues are included for function overloading. | ||
* Given named input values, transform these with the expected parameters | ||
* in order to get an array of arguments expected by the contract function. | ||
*/ | ||
// eslint-disable-next-line no-unused-vars | ||
(0, _createClass3.default)(ContractMethod, [{ | ||
key: '_getOutputValues', | ||
value: function _getOutputValues(callResult, inputValues) { | ||
// It may be a single value or an array; treat it as an array | ||
var values = [].concat(callResult); | ||
// Clean values according to their type and coalesce into an object | ||
return this.output && this.output.length ? // $FlowFixMe | ||
this.output.map(function (_ref2, index) { | ||
var _ref3 = (0, _slicedToArray3.default)(_ref2, 2), | ||
name = _ref3[0], | ||
type = _ref3[1]; | ||
return [name, (0, _paramTypes.convertOutputValue)(values[index], type)]; | ||
}).reduce(function (acc, _ref4) { | ||
var _ref5 = (0, _slicedToArray3.default)(_ref4, 2), | ||
name = _ref5[0], | ||
value = _ref5[1]; | ||
return Object.assign(acc, (0, _defineProperty3.default)({}, name, value)); | ||
}, {}) : callResult; | ||
} | ||
/** | ||
* Given input values, map them against the method's expected parameters, | ||
* with the appropriate conversion for each type. | ||
* Fall back to default values for each parameter. | ||
*/ | ||
}, { | ||
key: '_parseInputValues', | ||
value: function _parseInputValues(inputValues) { | ||
return this.input.map(function (_ref6) { | ||
var _ref7 = (0, _slicedToArray3.default)(_ref6, 3), | ||
paramName = _ref7[0], | ||
paramType = _ref7[1], | ||
defaultValue = _ref7[2]; | ||
return (0, _paramTypes.convertInputValue)(Object.hasOwnProperty.call(inputValues, paramName) ? inputValues[paramName] : defaultValue, paramType); | ||
}); | ||
} | ||
/** | ||
* Given named input values, transform these with the expected parameters | ||
* in order to get an array of arguments expected by the contract function. | ||
*/ | ||
}, { | ||
key: '_getMethodArgs', | ||
value: function _getMethodArgs(inputValues) { | ||
value: function _getMethodArgs(inputValues, params) { | ||
var args = []; | ||
@@ -126,4 +76,4 @@ | ||
if (this.input && this.input.length) { | ||
args = this._parseInputValues(inputValues); | ||
if (params && params.length) { | ||
args = this.convertInputValues(inputValues, params); | ||
} else if ((0, _lodash2.default)(inputValues) && Object.getOwnPropertyNames(inputValues).length) { | ||
@@ -148,72 +98,61 @@ // eslint-disable-next-line no-console | ||
/** | ||
* Given input values, validate them and return parsed method args. | ||
* Given input values, validate them against the expected params | ||
*/ | ||
}, { | ||
key: 'getValidatedArgs', | ||
value: function getValidatedArgs(inputValues) { | ||
this.validate(inputValues); | ||
return this._getMethodArgs(inputValues); | ||
key: 'validate', | ||
value: function validate(inputValues) { | ||
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.input; | ||
return (0, _paramValidation.validateParams)(inputValues, params, this.assertValid); | ||
} | ||
/** | ||
* Given parameters (as an object with named parameters, but potentially any | ||
* kind of invalid input), validate each parameter against the expected type | ||
* for this method, throwing vaidation errors or returning true. | ||
* Given input values, map them against the expected parameters, | ||
* with the appropriate conversion for each type. | ||
* Fall back to default values for each parameter. | ||
*/ | ||
}, { | ||
key: 'validate', | ||
value: function validate(input) { | ||
var _this = this; | ||
key: 'convertInputValues', | ||
value: function convertInputValues(inputValues) { | ||
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.input; | ||
if ((0, _inputValidation.areParamPairsEmpty)(this.input) && (0, _inputValidation.isInputEmpty)(input)) return true; | ||
return (0, _paramConversion.convertInputValues)(inputValues, params); | ||
} | ||
this.assertValid((0, _lodash2.default)(input), 'Expected parameters as an object'); | ||
/** | ||
* Given the result of a contract method call, transform these with the | ||
* expected output parameters in order to get named output values as the | ||
* method's `OutputValues`. | ||
*/ | ||
var inputValues = Object.assign({}, input); | ||
}, { | ||
key: 'convertOutputValues', | ||
value: function convertOutputValues(callResult, | ||
// eslint-disable-next-line no-unused-vars | ||
inputValues) { | ||
var values = [].concat(callResult); | ||
var paramNames = this.input.map(function (_ref8) { | ||
var _ref9 = (0, _slicedToArray3.default)(_ref8, 1), | ||
name = _ref9[0]; | ||
var parsedResult = this.output.reduce(function (acc, _ref2, index) { | ||
var _ref3 = (0, _slicedToArray3.default)(_ref2, 1), | ||
name = _ref3[0]; | ||
return name; | ||
}); | ||
var extraParams = Object.keys(inputValues).filter(function (name) { | ||
return !paramNames.includes(name); | ||
}); | ||
this.assertValid(extraParams.length === 0, 'Unexpected parameters: "' + extraParams.join(', ') + '"'); | ||
return Object.assign(acc, (0, _defineProperty3.default)({}, name, values[index])); | ||
}, {}); | ||
// Either the parameter name should exist in the inputValues, | ||
// or the parameter should have a default value. | ||
var missingParams = this.input.filter(function (param) { | ||
return !(Object.hasOwnProperty.call(inputValues, param[0]) || param.length === 3); | ||
}); | ||
this.assertValid(missingParams.length === 0, 'Missing parameters: "' + missingParams.map(function (_ref10) { | ||
var _ref11 = (0, _slicedToArray3.default)(_ref10, 1), | ||
name = _ref11[0]; | ||
return (0, _paramConversion.convertOutputValues)(parsedResult, this.output); | ||
} | ||
return name; | ||
}).join(', ') + '"'); | ||
/** | ||
* Given input values, validate them and return parsed method args. | ||
*/ | ||
return this.input.every(function (param) { | ||
return _this._validateValue(inputValues[param[0]], param); | ||
}); | ||
} | ||
}, { | ||
key: '_validateValue', | ||
value: function _validateValue(value, _ref12) { | ||
var _ref13 = (0, _slicedToArray3.default)(_ref12, 3), | ||
name = _ref13[0], | ||
type = _ref13[1], | ||
defaultValue = _ref13[2]; | ||
key: 'getValidatedArgs', | ||
value: function getValidatedArgs(inputValues) { | ||
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.input; | ||
var reason = void 0; | ||
var isValid = false; | ||
try { | ||
isValid = (0, _paramTypes.validateValue)(typeof value !== 'undefined' ? value : defaultValue, type); | ||
} catch (error) { | ||
reason = error.message || error.toString(); | ||
} | ||
return this.assertValid(Boolean(isValid), 'Parameter "' + name + '" expected a value of type "' + type + '"' + (reason ? ' (' + reason + ')' : '')); | ||
this.validate(inputValues, params); | ||
return this._getMethodArgs(inputValues, params); | ||
} | ||
@@ -220,0 +159,0 @@ }]); |
@@ -140,3 +140,3 @@ 'use strict'; | ||
var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(inputValues) { | ||
var args, response, outputValues; | ||
var args, callResult, outputValues; | ||
return _regenerator2.default.wrap(function _callee2$(_context2) { | ||
@@ -151,4 +151,4 @@ while (1) { | ||
case 3: | ||
response = _context2.sent; | ||
outputValues = this._getOutputValues(response, inputValues); | ||
callResult = _context2.sent; | ||
outputValues = this.convertOutputValues(callResult, inputValues); | ||
@@ -155,0 +155,0 @@ if (!this.constructor.containsNullValues(outputValues)) { |
@@ -59,4 +59,5 @@ 'use strict'; | ||
* getting the transaction nonce value | ||
* required in order to send the transaction | ||
* getRequiredSignees - Async function that returns the addresses of | ||
* the signers (needed to send the transaction) | ||
* the signers which will be required in order to send the transaction | ||
* multisigFunctionName - The contract function name to use for | ||
@@ -74,2 +75,3 @@ * sending the finalized transaction (with multisig support) | ||
nonceFunctionName = _ref.nonceFunctionName, | ||
nonceInput = _ref.nonceInput, | ||
output = _ref.output; | ||
@@ -83,2 +85,3 @@ (0, _classCallCheck3.default)(this, ContractMethodMultisigSender); | ||
_this.nonceFunctionName = nonceFunctionName; | ||
_this.nonceInput = nonceInput; | ||
return _this; | ||
@@ -156,11 +159,7 @@ } | ||
}() | ||
// XXX After https://github.com/JoinColony/colonyNetwork/issues/192 is | ||
// fixed, the inputValues should be accepted by this method. | ||
}, { | ||
key: 'getNonce', | ||
value: function () { | ||
var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3() { | ||
var response, nonce; | ||
var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(inputValues) { | ||
var args, response, nonce; | ||
return _regenerator2.default.wrap(function _callee3$(_context3) { | ||
@@ -170,6 +169,7 @@ while (1) { | ||
case 0: | ||
_context3.next = 2; | ||
return this.client.call(this.nonceFunctionName, []); | ||
args = this.getValidatedArgs(inputValues, this.nonceInput); | ||
_context3.next = 3; | ||
return this.client.call(this.nonceFunctionName, args); | ||
case 2: | ||
case 3: | ||
response = _context3.sent; | ||
@@ -182,3 +182,3 @@ nonce = (0, _colonyJsUtils.isBigNumber)(response) ? response.toNumber() : response; | ||
case 6: | ||
case 7: | ||
case 'end': | ||
@@ -191,3 +191,3 @@ return _context3.stop(); | ||
function getNonce() { | ||
function getNonce(_x3) { | ||
return _ref4.apply(this, arguments); | ||
@@ -218,3 +218,3 @@ } | ||
function send(_x3, _x4) { | ||
function send(_x4, _x5) { | ||
return _ref5.apply(this, arguments); | ||
@@ -248,3 +248,3 @@ } | ||
function sendMultisig(_x5, _x6) { | ||
function sendMultisig(_x6, _x7) { | ||
return _ref6.apply(this, arguments); | ||
@@ -290,3 +290,3 @@ } | ||
function startOperation(_x7) { | ||
function startOperation(_x8) { | ||
return _ref7.apply(this, arguments); | ||
@@ -334,3 +334,3 @@ } | ||
function restoreOperation(_x8) { | ||
function restoreOperation(_x9) { | ||
return _ref8.apply(this, arguments); | ||
@@ -364,3 +364,3 @@ } | ||
function _sendTransaction(_x9, _x10) { | ||
function _sendTransaction(_x10, _x11) { | ||
return _ref9.apply(this, arguments); | ||
@@ -367,0 +367,0 @@ } |
@@ -415,3 +415,3 @@ 'use strict'; | ||
_context4.next = 3; | ||
return this.sender.getNonce(); | ||
return this.sender.getNonce(this.payload.inputValues); | ||
@@ -425,3 +425,3 @@ case 3: | ||
_context4.next = 8; | ||
return this.sender.getNonce(); | ||
return this.sender.getNonce(this.payload.inputValues); | ||
@@ -516,3 +516,2 @@ case 8: | ||
// $FlowFixMe https://github.com/facebook/flow/issues/6151 | ||
return this.requiredSignees.filter(function (address) { | ||
@@ -519,0 +518,0 @@ return !_this4._signers[address]; |
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.convertInputValue = exports.convertOutputValue = exports.validateValue = undefined; | ||
exports.convertInputValue = exports.convertOutputValue = exports.validateValueType = undefined; | ||
@@ -27,3 +27,3 @@ var _defineProperty2 = require('babel-runtime/helpers/defineProperty'); | ||
var _inputValidation = require('./inputValidation'); | ||
var _paramValidation = require('./paramValidation'); | ||
@@ -66,5 +66,5 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
boolean: { | ||
validate: _inputValidation.isBoolean, | ||
validate: _paramValidation.isBoolean, | ||
convertOutput: function convertOutput(value) { | ||
return (0, _inputValidation.isBoolean)(value) ? value : null; | ||
return (0, _paramValidation.isBoolean)(value) ? value : null; | ||
}, | ||
@@ -148,3 +148,3 @@ | ||
*/ | ||
var validateValue = exports.validateValue = typeMapFn.bind(undefined, 'validate'); | ||
var validateValueType = exports.validateValueType = typeMapFn.bind(undefined, 'validate'); | ||
@@ -151,0 +151,0 @@ /** |
{ | ||
"name": "@colony/colony-js-contract-client", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Method-like interface for Smart Contracts", | ||
@@ -44,4 +44,4 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@colony/colony-js-adapter": "^1.3.0", | ||
"@colony/colony-js-contract-loader": "^1.3.0", | ||
"@colony/colony-js-adapter": "^1.4.0", | ||
"@colony/colony-js-contract-loader": "^1.4.0", | ||
"@colony/colony-js-utils": "^1.3.0", | ||
@@ -48,0 +48,0 @@ "assert": "^1.4.1", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1154042
51
6086