good morning!!!!

Skip to content
Snippets Groups Projects
Commit c8ee08c2 authored by Marek Kotewicz's avatar Marek Kotewicz
Browse files

contract.js simplified

parent 842b8cf3
Branches
Tags
No related merge requests found
...@@ -428,7 +428,7 @@ module.exports = { ...@@ -428,7 +428,7 @@ module.exports = {
}; };
},{"./web3":7}],2:[function(require,module,exports){ },{"./web3":8}],2:[function(require,module,exports){
/* /*
This file is part of ethereum.js. This file is part of ethereum.js.
...@@ -453,78 +453,35 @@ module.exports = { ...@@ -453,78 +453,35 @@ module.exports = {
var web3 = require('./web3'); var web3 = require('./web3');
var abi = require('./abi'); var abi = require('./abi');
var eventImplementation = require('./event');
/** var addFunctionRelatedPropertiesToContract = function (contract) {
* 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) { contract.call = function (options) {
// workaround for invalid assumption that method.name is the full anonymous prototype of the method. contract._isTransact = false;
// it's not. it's just the name. the rest of the code assumes it's actually the anonymous contract._options = options;
// prototype, so we make it so as a workaround. return contract;
// TODO: we may not want to modify input params, maybe use copy instead?
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 = {
address: address,
};
Object.defineProperty(result, 'topics', {
get: function() {
return abi.filterEvents(desc).map(function (event) {
return abi.methodSignature(event.name);
});
}
});
result.call = function (options) {
result._isTransact = false;
result._options = options;
return result;
}; };
result.transact = function (options) { contract.transact = function (options) {
result._isTransact = true; contract._isTransact = true;
result._options = options; contract._options = options;
return result; return contract;
}; };
result._options = {}; contract._options = {};
['gas', 'gasPrice', 'value', 'from'].forEach(function(p) { ['gas', 'gasPrice', 'value', 'from'].forEach(function(p) {
result[p] = function (v) { contract[p] = function (v) {
result._options[p] = v; contract._options[p] = v;
return result; return contract;
}; };
}); });
};
var addFunctionsToContract = function (contract, desc, address) {
var inputParser = abi.inputParser(desc);
var outputParser = abi.outputParser(desc);
// create contract functions // create contract functions
abi.filterFunctions(desc).forEach(function (method) { abi.filterFunctions(desc).forEach(function (method) {
...@@ -537,16 +494,16 @@ var contract = function (address, desc) { ...@@ -537,16 +494,16 @@ var contract = function (address, desc) {
var signature = abi.methodSignature(method.name); var signature = abi.methodSignature(method.name);
var parsed = inputParser[displayName][typeName].apply(null, params); var parsed = inputParser[displayName][typeName].apply(null, params);
var options = result._options || {}; var options = contract._options || {};
options.to = address; options.to = address;
options.data = signature + parsed; 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; var collapse = options.collapse !== false;
// reset // reset
result._options = {}; contract._options = {};
result._isTransact = null; contract._isTransact = null;
if (isTransact) { if (isTransact) {
// it's used byt natspec.js // it's used byt natspec.js
...@@ -573,48 +530,122 @@ var contract = function (address, desc) { ...@@ -573,48 +530,122 @@ var contract = function (address, desc) {
return ret; return ret;
}; };
if (result[displayName] === undefined) { if (contract[displayName] === undefined) {
result[displayName] = impl; contract[displayName] = impl;
} }
result[displayName][typeName] = 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 // create contract events
abi.filterEvents(desc).forEach(function (event) { abi.filterEvents(desc).forEach(function (e) {
var impl = function () {
var params = Array.prototype.slice.call(arguments);
var signature = abi.methodSignature(e.name);
var eventImpl = eventImplementation(address, signature);
var o = eventImpl.apply(null, params);
return web3.eth.watch(o);
};
// TODO: rename these methods, cause they are used not only for methods // TODO: rename these methods, cause they are used not only for methods
var displayName = abi.methodDisplayName(event.name); var displayName = abi.methodDisplayName(e.name);
var typeName = abi.methodTypeName(event.name); var typeName = abi.methodTypeName(e.name);
if (contract[displayName] === undefined) {
contract[displayName] = impl;
}
var impl = function (options) { contract[displayName][typeName] = impl;
var signature = abi.methodSignature(event.name);
var o = options || {};
o.address = o.address || address;
o.topics = o.topics || [];
o.topics.push(signature);
return web3.eth.watch(o); });
}; };
if (result[displayName] === undefined) {
result[displayName] = impl;
}
result[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) {
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.
// TODO: we may not want to modify input params, maybe use copy instead?
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; return result;
}; };
module.exports = contract; module.exports = contract;
},{"./abi":1,"./web3":7}],3:[function(require,module,exports){ },{"./abi":1,"./event":3,"./web3":8}],3:[function(require,module,exports){
var abi = require('./abi');
var implementationOfEvent = function (address, signature) {
return function (options) {
var o = options || {};
o.address = o.address || address;
o.topics = o.topics || [];
o.topics.push(signature);
return o;
};
};
module.exports = implementationOfEvent;
},{"./abi":1}],4:[function(require,module,exports){
/* /*
This file is part of ethereum.js. This file is part of ethereum.js.
...@@ -689,7 +720,7 @@ Filter.prototype.logs = function () { ...@@ -689,7 +720,7 @@ Filter.prototype.logs = function () {
module.exports = Filter; module.exports = Filter;
},{"./web3":7}],4:[function(require,module,exports){ },{"./web3":8}],5:[function(require,module,exports){
/* /*
This file is part of ethereum.js. This file is part of ethereum.js.
...@@ -761,7 +792,7 @@ HttpSyncProvider.prototype.send = function (payload) { ...@@ -761,7 +792,7 @@ HttpSyncProvider.prototype.send = function (payload) {
module.exports = HttpSyncProvider; module.exports = HttpSyncProvider;
},{}],5:[function(require,module,exports){ },{}],6:[function(require,module,exports){
/* /*
This file is part of ethereum.js. This file is part of ethereum.js.
...@@ -873,7 +904,7 @@ ProviderManager.prototype.stopPolling = function (pollId) { ...@@ -873,7 +904,7 @@ ProviderManager.prototype.stopPolling = function (pollId) {
module.exports = ProviderManager; module.exports = ProviderManager;
},{"./web3":7}],6:[function(require,module,exports){ },{"./web3":8}],7:[function(require,module,exports){
/* /*
This file is part of ethereum.js. This file is part of ethereum.js.
...@@ -907,7 +938,7 @@ QtSyncProvider.prototype.send = function (payload) { ...@@ -907,7 +938,7 @@ QtSyncProvider.prototype.send = function (payload) {
module.exports = QtSyncProvider; module.exports = QtSyncProvider;
},{}],7:[function(require,module,exports){ },{}],8:[function(require,module,exports){
/* /*
This file is part of ethereum.js. This file is part of ethereum.js.
...@@ -1249,7 +1280,7 @@ web3.abi = require('./lib/abi'); ...@@ -1249,7 +1280,7 @@ web3.abi = require('./lib/abi');
module.exports = web3; module.exports = web3;
},{"./lib/abi":1,"./lib/contract":2,"./lib/filter":3,"./lib/httpsync":4,"./lib/providermanager":5,"./lib/qtsync":6,"./lib/web3":7}]},{},["web3"]) },{"./lib/abi":1,"./lib/contract":2,"./lib/filter":4,"./lib/httpsync":5,"./lib/providermanager":6,"./lib/qtsync":7,"./lib/web3":8}]},{},["web3"])
//# sourceMappingURL=ethereum.js.map //# sourceMappingURL=ethereum.js.map
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
...@@ -22,78 +22,35 @@ ...@@ -22,78 +22,35 @@
var web3 = require('./web3'); var web3 = require('./web3');
var abi = require('./abi'); var abi = require('./abi');
var eventImplementation = require('./event');
/** var addFunctionRelatedPropertiesToContract = function (contract) {
* 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.
// TODO: we may not want to modify input params, maybe use copy instead?
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 = {
address: address,
};
Object.defineProperty(result, 'topics', { contract.call = function (options) {
get: function() { contract._isTransact = false;
return abi.filterEvents(desc).map(function (event) { contract._options = options;
return abi.methodSignature(event.name); return contract;
});
}
});
result.call = function (options) {
result._isTransact = false;
result._options = options;
return result;
}; };
result.transact = function (options) { contract.transact = function (options) {
result._isTransact = true; contract._isTransact = true;
result._options = options; contract._options = options;
return result; return contract;
}; };
result._options = {}; contract._options = {};
['gas', 'gasPrice', 'value', 'from'].forEach(function(p) { ['gas', 'gasPrice', 'value', 'from'].forEach(function(p) {
result[p] = function (v) { contract[p] = function (v) {
result._options[p] = v; contract._options[p] = v;
return result; return contract;
}; };
}); });
};
var addFunctionsToContract = function (contract, desc, address) {
var inputParser = abi.inputParser(desc);
var outputParser = abi.outputParser(desc);
// create contract functions // create contract functions
abi.filterFunctions(desc).forEach(function (method) { abi.filterFunctions(desc).forEach(function (method) {
...@@ -106,16 +63,16 @@ var contract = function (address, desc) { ...@@ -106,16 +63,16 @@ var contract = function (address, desc) {
var signature = abi.methodSignature(method.name); var signature = abi.methodSignature(method.name);
var parsed = inputParser[displayName][typeName].apply(null, params); var parsed = inputParser[displayName][typeName].apply(null, params);
var options = result._options || {}; var options = contract._options || {};
options.to = address; options.to = address;
options.data = signature + parsed; 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; var collapse = options.collapse !== false;
// reset // reset
result._options = {}; contract._options = {};
result._isTransact = null; contract._isTransact = null;
if (isTransact) { if (isTransact) {
// it's used byt natspec.js // it's used byt natspec.js
...@@ -142,41 +99,97 @@ var contract = function (address, desc) { ...@@ -142,41 +99,97 @@ var contract = function (address, desc) {
return ret; return ret;
}; };
if (result[displayName] === undefined) { if (contract[displayName] === undefined) {
result[displayName] = impl; contract[displayName] = impl;
} }
result[displayName][typeName] = 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 // create contract events
abi.filterEvents(desc).forEach(function (event) { abi.filterEvents(desc).forEach(function (e) {
var impl = function () {
var params = Array.prototype.slice.call(arguments);
var signature = abi.methodSignature(e.name);
var eventImpl = eventImplementation(address, signature);
var o = eventImpl.apply(null, params);
return web3.eth.watch(o);
};
// TODO: rename these methods, cause they are used not only for methods // TODO: rename these methods, cause they are used not only for methods
var displayName = abi.methodDisplayName(event.name); var displayName = abi.methodDisplayName(e.name);
var typeName = abi.methodTypeName(event.name); var typeName = abi.methodTypeName(e.name);
if (contract[displayName] === undefined) {
contract[displayName] = impl;
}
var impl = function (options) { contract[displayName][typeName] = impl;
var signature = abi.methodSignature(event.name);
var o = options || {};
o.address = o.address || address;
o.topics = o.topics || [];
o.topics.push(signature);
return web3.eth.watch(o); });
}; };
if (result[displayName] === undefined) {
result[displayName] = impl;
}
result[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) {
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.
// TODO: we may not want to modify input params, maybe use copy instead?
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; return result;
}; };
......
var abi = require('./abi'); var abi = require('./abi');
var implementationOfEvent = function (event, address, signature) { var implementationOfEvent = function (address, signature) {
return function (options) { return function (options) {
var o = options || {}; var o = options || {};
......
...@@ -7,13 +7,9 @@ describe('event', function () { ...@@ -7,13 +7,9 @@ describe('event', function () {
// given // given
var address = '0x012345'; var address = '0x012345';
var signature = '0x987654'; var signature = '0x987654';
var e = {
name: 'test',
type: 'event',
};
// when // when
var impl = event(e, address, signature); var impl = event(address, signature);
var result = impl(); var result = impl();
// then // then
...@@ -23,3 +19,4 @@ describe('event', function () { ...@@ -23,3 +19,4 @@ describe('event', function () {
}); });
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment