aboutsummaryrefslogtreecommitdiffstats
path: root/lib/contract.js
diff options
context:
space:
mode:
authorMarek Kotewicz <marek.kotewicz@gmail.com>2015-01-29 22:22:55 +0800
committerMarek Kotewicz <marek.kotewicz@gmail.com>2015-01-29 22:22:55 +0800
commit94e0e5ab7d8ec9adcd03fedc3abe5cf6444a5123 (patch)
treedf0b2e41f5dc9e50b99fa45d597199fd71201678 /lib/contract.js
parente1c0862658ca80d2a4a85bd15eab38b0482fee1b (diff)
parent8613382869503c6123c3f47772bfdb192f6d3c76 (diff)
downloaddexon-94e0e5ab7d8ec9adcd03fedc3abe5cf6444a5123.tar
dexon-94e0e5ab7d8ec9adcd03fedc3abe5cf6444a5123.tar.gz
dexon-94e0e5ab7d8ec9adcd03fedc3abe5cf6444a5123.tar.bz2
dexon-94e0e5ab7d8ec9adcd03fedc3abe5cf6444a5123.tar.lz
dexon-94e0e5ab7d8ec9adcd03fedc3abe5cf6444a5123.tar.xz
dexon-94e0e5ab7d8ec9adcd03fedc3abe5cf6444a5123.tar.zst
dexon-94e0e5ab7d8ec9adcd03fedc3abe5cf6444a5123.zip
Merge branch 'cpp' into cpp2
Conflicts: example/balance.html
Diffstat (limited to 'lib/contract.js')
-rw-r--r--lib/contract.js182
1 files changed, 120 insertions, 62 deletions
diff --git a/lib/contract.js b/lib/contract.js
index e71734d0b..0bf3ee471 100644
--- a/lib/contract.js
+++ b/lib/contract.js
@@ -20,71 +20,40 @@
* @date 2014
*/
-var web3 = require('./web3'); // jshint ignore:line
+var web3 = require('./web3');
var abi = require('./abi');
-
-/**
- * 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 eventImpl = require('./event');
+
+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);
+
+ // create contract functions
+ abi.filterFunctions(desc).forEach(function (method) {
var displayName = abi.methodDisplayName(method.name);
var typeName = abi.methodTypeName(method.name);
@@ -94,16 +63,16 @@ var contract = function (address, desc) {
var signature = abi.methodSignature(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
@@ -130,14 +99,103 @@ 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;
+
+ Object.defineProperty(contract, 'topics', {
+ get: function() {
+ return abi.filterEvents(desc).map(function (e) {
+ return abi.methodSignature(e.name);
+ });
+ }
+ });
+
+};
+
+var addEventsToContract = function (contract, desc, address) {
+ // create contract events
+ abi.filterEvents(desc).forEach(function (e) {
+
+ var impl = function () {
+ var params = Array.prototype.slice.call(arguments);
+ var signature = abi.methodSignature(e.name);
+ var event = eventImpl(address, signature);
+ var o = event.apply(null, params);
+ return web3.eth.watch(o);
+ };
+
+ impl.address = address;
+
+ Object.defineProperty(impl, 'topics', {
+ get: function() {
+ return [abi.methodSignature(e.name)];
+ }
+ });
+
+ // TODO: rename these methods, cause they are used not only for methods
+ var displayName = abi.methodDisplayName(e.name);
+ var typeName = abi.methodTypeName(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;
};