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

/*
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/>.
*/
/** @file web3.js
* @authors:
* Jeffrey Wilcke <jeff@ethdev.com>
* Marek Kotewicz <marek@ethdev.com>
* Marian Oancea <marian@ethdev.com>
* Gav Wood <g@ethdev.com>
* @date 2014
*/
if (process.env.NODE_ENV !== 'build') {
var BigNumber = require('bignumber.js');
}
var eth = require('./eth');
var db = require('./db');
var shh = require('./shh');
var watches = require('./watches');
var filter = require('./filter');
var utils = require('./utils');
var requestManager = require('./requestmanager');
/// @returns an array of objects describing web3 api methods
var web3Methods = function () {
return [
{ name: 'sha3', call: 'web3_sha3' }
];
};
/// creates methods in a given object based on method description on input
/// setups api calls for these methods
var setupMethods = function (obj, methods) {
methods.forEach(function (method) {
obj[method.name] = function () {
var args = Array.prototype.slice.call(arguments);
var call = typeof method.call === 'function' ? method.call(args) : method.call;
return web3.manager.send({
method: call,
params: args
});
};
});
};
/// creates properties in a given object based on properties description on input
/// setups api calls for these properties
var setupProperties = function (obj, properties) {
properties.forEach(function (property) {
var proto = {};
proto.get = function () {
return web3.manager.send({
method: property.getter
});
};
if (property.setter) {
proto.set = function (val) {
return web3.manager.send({
method: property.setter,
params: [val]
});
};
}
Object.defineProperty(obj, property.name, proto);
});
};
/*jshint maxparams:4 */
var startPolling = function (method, id, callback, uninstall) {
web3.manager.startPolling({
method: method,
params: [id]
}, id, callback, uninstall);
};
/*jshint maxparams:3 */
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
};
/// setups web3 object, and it's in-browser executed methods
var web3 = {
manager: requestManager(),
providers: {},
/// @returns ascii string representation of hex value prefixed with 0x
toAscii: utils.toAscii,
/// @returns hex representation (prefixed by 0x) of ascii string
fromAscii: utils.fromAscii,
/// @returns decimal representaton of hex value prefixed by 0x
toDecimal: function (val) {
// remove 0x and place 0, if it's required
val = val.length > 2 ? val.substring(2) : "0";
return (new BigNumber(val, 16).toString(10));
},
/// @returns hex representation (prefixed by 0x) of decimal value
fromDecimal: function (val) {
return "0x" + (new BigNumber(val).toString(16));
},
/// used to transform value/string to eth string
toEth: utils.toEth,
/// eth object prototype
eth: {
Squashed &#39;libjsqrc/ethereumjs/&#39; changes from 81bbe8c..d223ac0 d223ac0 Additional work on the JS stuff to make it more similar to Solidity. 113a380 Fix string outputs. Auto collapse for &lt; 2 returns. fb34c6c Auto select call or transact depending on constness. 7869294 Fixes to ethereum.js and standard.js. 61a0158 Vanity addresses in AZ. Fixes to ethereum.js eth.flush() c01f0ae Merge commit &#39;b1971f46cce7d21be5f6277c9f8c406a0d195f78&#39; into natspec 29fa57e natspec.js updated to use sync api 13f39af Merge commit &#39;93fa6855afad7a7eb86e49efe384372a6060da35&#39; into natspec 6c4432d Merge commit &#39;b32dedc5b54f853cf63bb2663c8bca6aa4272b0b&#39; into natspec 375ca54 Merge commit &#39;8e3ba3a4285cc7e902a018196b3849df56944dd0&#39; into natspec d6a92b1 fixed natspec example 5e623f1 Merge commit &#39;cdd7af2a6e87363d0ff7d2528f9d9b521bf2ef20&#39; into natspec ad8e92e Merge commit &#39;eb4984c0d036c1420e782ca136810e851e33fb37&#39; into natspec 81ff253 Merge commit &#39;c4638ba3edbd14677da5441d61f7845668df2b22&#39; into natspec e68f8e7 natspec changes 689630d Merge commit &#39;53b4fda16d0b191be8ab986379a328aa38aaf916&#39; into natspec 5c79ee4 fixed natspec_contract.html example 83505e6 Merge commit &#39;2b4d38b9bf059014596e1ab00c99dc2ad4ab3761&#39; into ethereumjs 2ce109e Merge commit &#39;6a383d6ed3fb37cf6739f6ac441652adfdb70463&#39; into ethereumjs 1c4d8f3 Merge commit &#39;29333fc213b62b27ef826616cf77430947fb6eab&#39; into ethereumjs de4ea8e Merge commit &#39;d876522bc0614fea3180a54be57bcb61784b352e&#39; into ethereumjs 6d02c0d Merge commit &#39;1a6dbeff6e86d65cae6d7db366cbaa4182eaff7f&#39; into ethereumjs 508f116 Merge pull request #798 from LefterisJP/natspec_OnContractCreation a4049fb Cleaned up some unused functions dac93ed Natspechandler: Get function hash from transaction data f54c90a A first version of Natspec warning popup b6c0e53 Auto-generated commits, too. e95bb34 Fix for JS API formatting. git-subtree-dir: libjsqrc/ethereumjs git-subtree-split: d223ac0379c4c1f8c209e777ec9ed63384590157
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;
};
},
/// @param filter may be a string, object or event
/// @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'...)
/// TODO: fix it, 4 params? no way
/*jshint maxparams:4 */
watch: function (fil, indexed, options, formatter) {
if (fil._isEvent) {
return fil(indexed, options);
}
return filter(fil, ethWatch, formatter);
}
/*jshint maxparams:3 */
},
/// db object prototype
db: {},
/// shh object prototype
shh: {
/// @param filter may be a string, object or event
watch: function (fil) {
return filter(fil, shhWatch);
}
},
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();
}
};
/// setups all api methods
setupMethods(web3, web3Methods());
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());
module.exports = web3;