improved contracts interface

This commit is contained in:
Marek Kotewicz
2014-11-14 13:11:47 +01:00
parent 8aaec1d98f
commit ea8db7a4ae
9 changed files with 166 additions and 21 deletions

View File

@ -22,6 +22,8 @@
* @date 2014
*/
var abi = require('./abi');
function flattenPromise (obj) {
if (obj instanceof Promise) {
return Promise.resolve(obj);
@ -292,9 +294,8 @@ var web3 = {
}
};
var eth = web3.eth;
setupMethods(eth, ethMethods());
setupProperties(eth, ethProperties());
setupMethods(web3.eth, ethMethods());
setupProperties(web3.eth, ethProperties());
setupMethods(web3.db, dbMethods());
setupMethods(web3.shh, shhMethods());
@ -454,5 +455,40 @@ function messageHandler(data) {
}
}
web3.contract = function (address, desc) {
var inputParser = abi.inputParser(desc);
var outputParser = abi.outputParser(desc);
var contract = {};
desc.forEach(function (method) {
contract[method.name] = function () {
var params = Array.prototype.slice.call(arguments);
var parsed = inputParser[method.name].apply(null, params);
var onSuccess = function (result) {
return outputParser[method.name](result);
};
return {
call: function (extra) {
extra = extra || {};
extra.to = address;
extra.data = parsed;
return web3.eth.call(extra).then(onSuccess);
},
transact: function (extra) {
extra = extra || {};
extra.to = address;
extra.data = parsed;
return web3.eth.transact(extra).then(onSuccess);
}
};
};
});
return contract;
};
module.exports = web3;