aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/mist/assets/ext/ethereum.js/lib/contract.js
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-02-05 04:52:17 +0800
committerobscuren <geffobscura@gmail.com>2015-02-05 04:52:34 +0800
commit7c278d6ac20e7f34816f02d6db7f8aeb5d8ebc94 (patch)
treeb3e541941c0a6b8f8c9772034bd77cece339b355 /cmd/mist/assets/ext/ethereum.js/lib/contract.js
parent3f03197daebace568a61bf69c06c97e30080a749 (diff)
parent859a1999cb204d2c6fcb08d6569c738c5af5cd86 (diff)
downloaddexon-7c278d6ac20e7f34816f02d6db7f8aeb5d8ebc94.tar.gz
dexon-7c278d6ac20e7f34816f02d6db7f8aeb5d8ebc94.tar.zst
dexon-7c278d6ac20e7f34816f02d6db7f8aeb5d8ebc94.zip
updated tests
Diffstat (limited to 'cmd/mist/assets/ext/ethereum.js/lib/contract.js')
-rw-r--r--cmd/mist/assets/ext/ethereum.js/lib/contract.js212
1 files changed, 142 insertions, 70 deletions
diff --git a/cmd/mist/assets/ext/ethereum.js/lib/contract.js b/cmd/mist/assets/ext/ethereum.js/lib/contract.js
index e71734d0b..a0525bd9d 100644
--- a/cmd/mist/assets/ext/ethereum.js/lib/contract.js
+++ b/cmd/mist/assets/ext/ethereum.js/lib/contract.js
@@ -20,98 +20,78 @@
* @date 2014
*/
-var web3 = require('./web3'); // jshint ignore:line
+var web3 = require('./web3');
var abi = require('./abi');
+var utils = require('./utils');
+var eventImpl = require('./event');
+
+var exportNatspecGlobals = function (vars) {
+ // it's used byt natspec.js
+ // TODO: figure out better way to solve this
+ web3._currentContractAbi = vars.abi;
+ web3._currentContractAddress = vars.address;
+ web3._currentContractMethodName = vars.method;
+ web3._currentContractMethodParams = vars.params;
+};
-/**
- * This method should be called when we want to call / transact some solidity method from javascript
- * it returns an object which has same methods available as solidity contract description
- * usage example:
- *
- * var abi = [{
- * name: 'myMethod',
- * inputs: [{ name: 'a', type: 'string' }],
- * outputs: [{name: 'd', type: 'string' }]
- * }]; // contract abi
- *
- * var myContract = web3.eth.contract('0x0123123121', abi); // creation of contract object
- *
- * myContract.myMethod('this is test string param for call'); // myMethod call (implicit, default)
- * myContract.call().myMethod('this is test string param for call'); // myMethod call (explicit)
- * myContract.transact().myMethod('this is test string param for transact'); // myMethod transact
- *
- * @param address - address of the contract, which should be called
- * @param desc - abi json description of the contract, which is being created
- * @returns contract object
- */
-
-var contract = function (address, desc) {
-
- desc.forEach(function (method) {
- // workaround for invalid assumption that method.name is the full anonymous prototype of the method.
- // it's not. it's just the name. the rest of the code assumes it's actually the anonymous
- // prototype, so we make it so as a workaround.
- if (method.name.indexOf('(') === -1) {
- var displayName = method.name;
- var typeName = method.inputs.map(function(i){return i.type; }).join();
- method.name = displayName + '(' + typeName + ')';
- }
- });
-
- var inputParser = abi.inputParser(desc);
- var outputParser = abi.outputParser(desc);
-
- var result = {};
-
- result.call = function (options) {
- result._isTransact = false;
- result._options = options;
- return result;
+var addFunctionRelatedPropertiesToContract = function (contract) {
+
+ contract.call = function (options) {
+ contract._isTransact = false;
+ contract._options = options;
+ return contract;
};
- result.transact = function (options) {
- result._isTransact = true;
- result._options = options;
- return result;
+ contract.transact = function (options) {
+ contract._isTransact = true;
+ contract._options = options;
+ return contract;
};
- result._options = {};
+ contract._options = {};
['gas', 'gasPrice', 'value', 'from'].forEach(function(p) {
- result[p] = function (v) {
- result._options[p] = v;
- return result;
+ contract[p] = function (v) {
+ contract._options[p] = v;
+ return contract;
};
});
+};
- desc.forEach(function (method) {
+var addFunctionsToContract = function (contract, desc, address) {
+ var inputParser = abi.inputParser(desc);
+ var outputParser = abi.outputParser(desc);
- var displayName = abi.methodDisplayName(method.name);
- var typeName = abi.methodTypeName(method.name);
+ // create contract functions
+ utils.filterFunctions(desc).forEach(function (method) {
+
+ var displayName = utils.extractDisplayName(method.name);
+ var typeName = utils.extractTypeName(method.name);
var impl = function () {
var params = Array.prototype.slice.call(arguments);
- var signature = abi.methodSignature(method.name);
+ var signature = abi.signatureFromAscii(method.name);
var parsed = inputParser[displayName][typeName].apply(null, params);
- var options = result._options || {};
+ var options = contract._options || {};
options.to = address;
options.data = signature + parsed;
- var isTransact = result._isTransact === true || (result._isTransact !== false && !method.constant);
+ var isTransact = contract._isTransact === true || (contract._isTransact !== false && !method.constant);
var collapse = options.collapse !== false;
// reset
- result._options = {};
- result._isTransact = null;
+ contract._options = {};
+ contract._isTransact = null;
if (isTransact) {
- // it's used byt natspec.js
- // TODO: figure out better way to solve this
- web3._currentContractAbi = desc;
- web3._currentContractAddress = address;
- web3._currentContractMethodName = method.name;
- web3._currentContractMethodParams = params;
+
+ exportNatspecGlobals({
+ abi: desc,
+ address: address,
+ method: method.name,
+ params: params
+ });
// transactions do not have any output, cause we do not know, when they will be processed
web3.eth.transact(options);
@@ -130,14 +110,106 @@ var contract = function (address, desc) {
return ret;
};
- if (result[displayName] === undefined) {
- result[displayName] = impl;
+ if (contract[displayName] === undefined) {
+ contract[displayName] = impl;
+ }
+
+ contract[displayName][typeName] = impl;
+ });
+};
+
+var addEventRelatedPropertiesToContract = function (contract, desc, address) {
+ contract.address = address;
+ contract._onWatchEventResult = function (data) {
+ var matchingEvent = event.getMatchingEvent(utils.filterEvents(desc));
+ var parser = eventImpl.outputParser(matchingEvent);
+ return parser(data);
+ };
+
+ Object.defineProperty(contract, 'topic', {
+ get: function() {
+ return utils.filterEvents(desc).map(function (e) {
+ return abi.eventSignatureFromAscii(e.name);
+ });
+ }
+ });
+
+};
+
+var addEventsToContract = function (contract, desc, address) {
+ // create contract events
+ utils.filterEvents(desc).forEach(function (e) {
+
+ var impl = function () {
+ var params = Array.prototype.slice.call(arguments);
+ var signature = abi.eventSignatureFromAscii(e.name);
+ var event = eventImpl.inputParser(address, signature, e);
+ var o = event.apply(null, params);
+ o._onWatchEventResult = function (data) {
+ var parser = eventImpl.outputParser(e);
+ return parser(data);
+ };
+ return web3.eth.watch(o);
+ };
+
+ // this property should be used by eth.filter to check if object is an event
+ impl._isEvent = true;
+
+ var displayName = utils.extractDisplayName(e.name);
+ var typeName = utils.extractTypeName(e.name);
+
+ if (contract[displayName] === undefined) {
+ contract[displayName] = impl;
}
- result[displayName][typeName] = impl;
+ contract[displayName][typeName] = impl;
+
+ });
+};
+
+
+/**
+ * This method should be called when we want to call / transact some solidity method from javascript
+ * it returns an object which has same methods available as solidity contract description
+ * usage example:
+ *
+ * var abi = [{
+ * name: 'myMethod',
+ * inputs: [{ name: 'a', type: 'string' }],
+ * outputs: [{name: 'd', type: 'string' }]
+ * }]; // contract abi
+ *
+ * var myContract = web3.eth.contract('0x0123123121', abi); // creation of contract object
+ *
+ * myContract.myMethod('this is test string param for call'); // myMethod call (implicit, default)
+ * myContract.call().myMethod('this is test string param for call'); // myMethod call (explicit)
+ * myContract.transact().myMethod('this is test string param for transact'); // myMethod transact
+ *
+ * @param address - address of the contract, which should be called
+ * @param desc - abi json description of the contract, which is being created
+ * @returns contract object
+ */
+
+var contract = function (address, desc) {
+ // workaround for invalid assumption that method.name is the full anonymous prototype of the method.
+ // it's not. it's just the name. the rest of the code assumes it's actually the anonymous
+ // prototype, so we make it so as a workaround.
+ // TODO: we may not want to modify input params, maybe use copy instead?
+ desc.forEach(function (method) {
+ if (method.name.indexOf('(') === -1) {
+ var displayName = method.name;
+ var typeName = method.inputs.map(function(i){return i.type; }).join();
+ method.name = displayName + '(' + typeName + ')';
+ }
});
+ var result = {};
+ addFunctionRelatedPropertiesToContract(result);
+ addFunctionsToContract(result, desc, address);
+ addEventRelatedPropertiesToContract(result, desc, address);
+ addEventsToContract(result, desc, address);
+
return result;
};