You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
5.7 KiB
190 lines
5.7 KiB
10 years ago
|
/*
|
||
|
This file is part of ethereum.js.
|
||
|
|
||
|
ethereum.js is free software: you can redistribute it and/or modify
|
||
|
it under the terms of the GNU Lesser General Public License as published by
|
||
|
the Free Software Foundation, either version 3 of the License, or
|
||
|
(at your option) any later version.
|
||
|
|
||
|
ethereum.js is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
GNU Lesser General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Lesser General Public License
|
||
|
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
10 years ago
|
/** @file web3.js
|
||
10 years ago
|
* @authors:
|
||
|
* Jeffrey Wilcke <jeff@ethdev.com>
|
||
|
* Marek Kotewicz <marek@ethdev.com>
|
||
|
* Marian Oancea <marian@ethdev.com>
|
||
|
* Gav Wood <g@ethdev.com>
|
||
|
* @date 2014
|
||
|
*/
|
||
|
|
||
10 years ago
|
if (process.env.NODE_ENV !== 'build') {
|
||
|
var BigNumber = require('bignumber.js');
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
var eth = require('./eth');
|
||
|
var db = require('./db');
|
||
|
var shh = require('./shh');
|
||
|
var watches = require('./watches');
|
||
|
var filter = require('./filter');
|
||
10 years ago
|
var utils = require('./utils');
|
||
10 years ago
|
var requestManager = require('./requestmanager');
|
||
10 years ago
|
|
||
10 years ago
|
/// @returns an array of objects describing web3 api methods
|
||
10 years ago
|
var web3Methods = function () {
|
||
|
return [
|
||
|
{ name: 'sha3', call: 'web3_sha3' }
|
||
|
];
|
||
|
};
|
||
|
|
||
10 years ago
|
/// creates methods in a given object based on method description on input
|
||
|
/// setups api calls for these methods
|
||
10 years ago
|
var setupMethods = function (obj, methods) {
|
||
|
methods.forEach(function (method) {
|
||
|
obj[method.name] = function () {
|
||
10 years ago
|
var args = Array.prototype.slice.call(arguments);
|
||
|
var call = typeof method.call === 'function' ? method.call(args) : method.call;
|
||
10 years ago
|
return web3.manager.send({
|
||
10 years ago
|
method: call,
|
||
|
params: args
|
||
10 years ago
|
});
|
||
|
};
|
||
|
});
|
||
|
};
|
||
|
|
||
10 years ago
|
/// creates properties in a given object based on properties description on input
|
||
|
/// setups api calls for these properties
|
||
10 years ago
|
var setupProperties = function (obj, properties) {
|
||
|
properties.forEach(function (property) {
|
||
|
var proto = {};
|
||
|
proto.get = function () {
|
||
10 years ago
|
return web3.manager.send({
|
||
10 years ago
|
method: property.getter
|
||
10 years ago
|
});
|
||
|
};
|
||
10 years ago
|
|
||
10 years ago
|
if (property.setter) {
|
||
|
proto.set = function (val) {
|
||
10 years ago
|
return web3.manager.send({
|
||
10 years ago
|
method: property.setter,
|
||
|
params: [val]
|
||
10 years ago
|
});
|
||
|
};
|
||
|
}
|
||
|
Object.defineProperty(obj, property.name, proto);
|
||
|
});
|
||
|
};
|
||
|
|
||
10 years ago
|
/*jshint maxparams:4 */
|
||
10 years ago
|
var startPolling = function (method, id, callback, uninstall) {
|
||
|
web3.manager.startPolling({
|
||
|
method: method,
|
||
|
params: [id]
|
||
|
}, id, callback, uninstall);
|
||
|
};
|
||
10 years ago
|
/*jshint maxparams:3 */
|
||
10 years ago
|
|
||
|
var stopPolling = function (id) {
|
||
|
web3.manager.stopPolling(id);
|
||
|
};
|
||
|
|
||
|
var ethWatch = {
|
||
|
startPolling: startPolling.bind(null, 'eth_changed'),
|
||
|
stopPolling: stopPolling
|
||
|
};
|
||
|
|
||
|
var shhWatch = {
|
||
|
startPolling: startPolling.bind(null, 'shh_changed'),
|
||
|
stopPolling: stopPolling
|
||
|
};
|
||
|
|
||
10 years ago
|
/// setups web3 object, and it's in-browser executed methods
|
||
10 years ago
|
var web3 = {
|
||
10 years ago
|
manager: requestManager(),
|
||
10 years ago
|
providers: {},
|
||
|
|
||
10 years ago
|
/// @returns ascii string representation of hex value prefixed with 0x
|
||
10 years ago
|
toAscii: utils.toAscii,
|
||
10 years ago
|
|
||
10 years ago
|
/// @returns hex representation (prefixed by 0x) of ascii string
|
||
10 years ago
|
fromAscii: utils.fromAscii,
|
||
10 years ago
|
|
||
10 years ago
|
/// @returns decimal representaton of hex value prefixed by 0x
|
||
10 years ago
|
toDecimal: function (val) {
|
||
10 years ago
|
// remove 0x and place 0, if it's required
|
||
|
val = val.length > 2 ? val.substring(2) : "0";
|
||
|
return (new BigNumber(val, 16).toString(10));
|
||
10 years ago
|
},
|
||
|
|
||
10 years ago
|
/// @returns hex representation (prefixed by 0x) of decimal value
|
||
10 years ago
|
fromDecimal: function (val) {
|
||
10 years ago
|
return "0x" + (new BigNumber(val).toString(16));
|
||
10 years ago
|
},
|
||
|
|
||
10 years ago
|
/// used to transform value/string to eth string
|
||
10 years ago
|
toEth: utils.toEth,
|
||
10 years ago
|
|
||
10 years ago
|
/// eth object prototype
|
||
10 years ago
|
eth: {
|
||
10 years ago
|
contractFromAbi: function (abi) {
|
||
|
return function(addr) {
|
||
|
// Default to address of Config. TODO: rremove prior to genesis.
|
||
|
addr = addr || '0xc6d9d2cd449a754c494264e1809c50e34d64562b';
|
||
|
var ret = web3.eth.contract(addr, abi);
|
||
|
ret.address = addr;
|
||
|
return ret;
|
||
|
};
|
||
|
},
|
||
10 years ago
|
|
||
|
/// @param filter may be a string, object or event
|
||
10 years ago
|
/// @param indexed is optional, this is an object with optional event indexed params
|
||
|
/// @param options is optional, this is an object with optional event options ('max'...)
|
||
10 years ago
|
/// TODO: fix it, 4 params? no way
|
||
10 years ago
|
/*jshint maxparams:4 */
|
||
10 years ago
|
watch: function (fil, indexed, options, formatter) {
|
||
|
if (fil._isEvent) {
|
||
|
return fil(indexed, options);
|
||
10 years ago
|
}
|
||
10 years ago
|
return filter(fil, ethWatch, formatter);
|
||
10 years ago
|
}
|
||
10 years ago
|
/*jshint maxparams:3 */
|
||
10 years ago
|
},
|
||
|
|
||
10 years ago
|
/// db object prototype
|
||
|
db: {},
|
||
10 years ago
|
|
||
10 years ago
|
/// shh object prototype
|
||
10 years ago
|
shh: {
|
||
10 years ago
|
/// @param filter may be a string, object or event
|
||
10 years ago
|
watch: function (fil) {
|
||
|
return filter(fil, shhWatch);
|
||
10 years ago
|
}
|
||
|
},
|
||
10 years ago
|
setProvider: function (provider) {
|
||
|
web3.manager.setProvider(provider);
|
||
|
},
|
||
|
|
||
|
/// Should be called to reset state of web3 object
|
||
|
/// Resets everything except manager
|
||
|
reset: function () {
|
||
|
web3.manager.reset();
|
||
|
}
|
||
10 years ago
|
};
|
||
|
|
||
10 years ago
|
/// setups all api methods
|
||
10 years ago
|
setupMethods(web3, web3Methods());
|
||
10 years ago
|
setupMethods(web3.eth, eth.methods());
|
||
|
setupProperties(web3.eth, eth.properties());
|
||
|
setupMethods(web3.db, db.methods());
|
||
|
setupMethods(web3.shh, shh.methods());
|
||
|
setupMethods(ethWatch, watches.eth());
|
||
|
setupMethods(shhWatch, watches.shh());
|
||
10 years ago
|
|
||
10 years ago
|
module.exports = web3;
|
||
|
|