op-geth/lib/web3.js

257 lines
8.0 KiB
JavaScript
Raw Normal View History

/*
2014-11-05 18:12:21 +00:00
This file is part of ethereum.js.
2014-11-05 18:12:21 +00:00
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.
2014-11-05 18:12:21 +00:00
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.
2014-11-05 18:12:21 +00:00
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/>.
*/
2015-01-13 17:28:49 +00:00
/** @file web3.js
2014-11-11 09:30:21 +00:00
* @authors:
* Jeffrey Wilcke <jeff@ethdev.com>
* Marek Kotewicz <marek@ethdev.com>
* Marian Oancea <marian@ethdev.com>
2014-12-22 00:13:49 +00:00
* Gav Wood <g@ethdev.com>
2014-11-11 09:30:21 +00:00
* @date 2014
*/
if (process.env.NODE_ENV !== 'build') {
var BigNumber = require('bignumber.js');
}
2015-01-31 12:54:39 +00:00
var utils = require('./utils');
2015-01-13 16:05:05 +00:00
/// @returns an array of objects describing web3 api methods
2014-12-22 00:13:49 +00:00
var web3Methods = function () {
return [
{ name: 'sha3', call: 'web3_sha3' }
];
};
2015-01-13 16:05:05 +00:00
/// @returns an array of objects describing web3.eth api methods
2014-11-11 10:46:46 +00:00
var ethMethods = function () {
var blockCall = function (args) {
return typeof args[0] === "string" ? "eth_blockByHash" : "eth_blockByNumber";
};
2014-11-11 10:46:46 +00:00
var transactionCall = function (args) {
2014-12-22 00:13:49 +00:00
return typeof args[0] === "string" ? 'eth_transactionByHash' : 'eth_transactionByNumber';
};
2014-11-11 10:46:46 +00:00
var uncleCall = function (args) {
2014-12-22 00:13:49 +00:00
return typeof args[0] === "string" ? 'eth_uncleByHash' : 'eth_uncleByNumber';
};
2014-11-11 10:46:46 +00:00
var methods = [
{ name: 'balanceAt', call: 'eth_balanceAt' },
{ name: 'stateAt', call: 'eth_stateAt' },
2014-11-13 17:29:31 +00:00
{ name: 'storageAt', call: 'eth_storageAt' },
2014-11-11 10:46:46 +00:00
{ name: 'countAt', call: 'eth_countAt'},
{ name: 'codeAt', call: 'eth_codeAt' },
{ name: 'transact', call: 'eth_transact' },
{ name: 'call', call: 'eth_call' },
{ name: 'block', call: blockCall },
{ name: 'transaction', call: transactionCall },
{ name: 'uncle', call: uncleCall },
2014-11-12 19:39:13 +00:00
{ name: 'compilers', call: 'eth_compilers' },
{ name: 'flush', call: 'eth_flush' },
2014-11-12 19:39:13 +00:00
{ name: 'lll', call: 'eth_lll' },
{ name: 'solidity', call: 'eth_solidity' },
2014-11-18 12:59:17 +00:00
{ name: 'serpent', call: 'eth_serpent' },
{ name: 'logs', call: 'eth_logs' }
2014-11-11 10:46:46 +00:00
];
return methods;
};
2015-01-13 16:05:05 +00:00
/// @returns an array of objects describing web3.eth api properties
2014-11-11 10:46:46 +00:00
var ethProperties = function () {
return [
{ name: 'coinbase', getter: 'eth_coinbase', setter: 'eth_setCoinbase' },
{ name: 'listening', getter: 'eth_listening', setter: 'eth_setListening' },
{ name: 'mining', getter: 'eth_mining', setter: 'eth_setMining' },
{ name: 'gasPrice', getter: 'eth_gasPrice' },
{ name: 'accounts', getter: 'eth_accounts' },
{ name: 'peerCount', getter: 'eth_peerCount' },
{ name: 'defaultBlock', getter: 'eth_defaultBlock', setter: 'eth_setDefaultBlock' },
{ name: 'number', getter: 'eth_number'}
];
};
2015-01-13 16:05:05 +00:00
/// @returns an array of objects describing web3.db api methods
2014-11-11 10:46:46 +00:00
var dbMethods = function () {
return [
{ name: 'put', call: 'db_put' },
{ name: 'get', call: 'db_get' },
{ name: 'putString', call: 'db_putString' },
{ name: 'getString', call: 'db_getString' }
];
};
2015-01-13 16:05:05 +00:00
/// @returns an array of objects describing web3.shh api methods
2014-11-11 10:46:46 +00:00
var shhMethods = function () {
return [
{ name: 'post', call: 'shh_post' },
{ name: 'newIdentity', call: 'shh_newIdentity' },
{ name: 'haveIdentity', call: 'shh_haveIdentity' },
{ name: 'newGroup', call: 'shh_newGroup' },
{ name: 'addToGroup', call: 'shh_addToGroup' }
];
};
2015-01-13 16:05:05 +00:00
/// @returns an array of objects describing web3.eth.watch api methods
2014-11-11 10:46:46 +00:00
var ethWatchMethods = function () {
var newFilter = function (args) {
return typeof args[0] === 'string' ? 'eth_newFilterString' : 'eth_newFilter';
};
2014-11-11 10:46:46 +00:00
return [
{ name: 'newFilter', call: newFilter },
{ name: 'uninstallFilter', call: 'eth_uninstallFilter' },
2014-11-18 12:59:17 +00:00
{ name: 'getMessages', call: 'eth_filterLogs' }
2014-11-11 10:46:46 +00:00
];
};
2015-01-13 16:05:05 +00:00
/// @returns an array of objects describing web3.shh.watch api methods
2014-11-11 10:46:46 +00:00
var shhWatchMethods = function () {
return [
{ name: 'newFilter', call: 'shh_newFilter' },
{ name: 'uninstallFilter', call: 'shh_uninstallFilter' },
2015-01-12 17:34:37 +00:00
{ name: 'getMessages', call: 'shh_getMessages' }
2014-11-11 10:46:46 +00:00
];
};
2015-01-13 16:05:05 +00:00
/// creates methods in a given object based on method description on input
/// setups api calls for these methods
2014-11-11 10:46:46 +00:00
var setupMethods = function (obj, methods) {
methods.forEach(function (method) {
obj[method.name] = function () {
2015-01-21 19:29:02 +00:00
var args = Array.prototype.slice.call(arguments);
var call = typeof method.call === 'function' ? method.call(args) : method.call;
return web3.provider.send({
method: call,
params: args
2014-11-11 10:46:46 +00:00
});
};
2014-11-11 10:46:46 +00:00
});
};
2015-01-13 16:05:05 +00:00
/// creates properties in a given object based on properties description on input
/// setups api calls for these properties
2014-11-11 10:46:46 +00:00
var setupProperties = function (obj, properties) {
properties.forEach(function (property) {
var proto = {};
proto.get = function () {
return web3.provider.send({
method: property.getter
2014-11-11 10:46:46 +00:00
});
};
2014-11-11 10:46:46 +00:00
if (property.setter) {
proto.set = function (val) {
return web3.provider.send({
method: property.setter,
params: [val]
});
};
2014-11-11 10:46:46 +00:00
}
Object.defineProperty(obj, property.name, proto);
});
};
2015-01-13 16:05:05 +00:00
/// setups web3 object, and it's in-browser executed methods
2014-11-11 10:46:46 +00:00
var web3 = {
_callbacks: {},
_events: {},
providers: {},
2015-01-14 09:50:34 +00:00
/// @returns ascii string representation of hex value prefixed with 0x
2015-01-31 12:54:39 +00:00
toAscii: utils.toAscii,
2014-11-11 10:46:46 +00:00
2015-01-14 09:50:34 +00:00
/// @returns hex representation (prefixed by 0x) of ascii string
2015-01-31 12:54:39 +00:00
fromAscii: utils.fromAscii,
2014-11-11 10:46:46 +00:00
2015-01-14 09:50:34 +00:00
/// @returns decimal representaton of hex value prefixed by 0x
2014-12-22 00:13:49 +00:00
toDecimal: function (val) {
2015-01-27 14:20:22 +00:00
// remove 0x and place 0, if it's required
val = val.length > 2 ? val.substring(2) : "0";
return (new BigNumber(val, 16).toString(10));
2014-12-22 00:13:49 +00:00
},
2015-01-14 09:50:34 +00:00
/// @returns hex representation (prefixed by 0x) of decimal value
2014-12-22 00:13:49 +00:00
fromDecimal: function (val) {
return "0x" + (new BigNumber(val).toString(16));
2014-12-22 00:13:49 +00:00
},
2015-01-14 09:50:34 +00:00
/// used to transform value/string to eth string
2015-02-02 16:40:05 +00:00
toEth: utils.toEth,
2014-12-22 00:13:49 +00:00
2015-01-14 09:50:34 +00:00
/// eth object prototype
2014-11-11 10:46:46 +00:00
eth: {
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;
};
},
2015-01-31 00:30:19 +00:00
/// @param filter may be a string, object or event
2015-01-31 15:01:41 +00:00
/// @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'...)
watch: function (filter, indexed, options) {
2015-01-31 03:09:48 +00:00
if (filter._isEvent) {
2015-01-31 15:01:41 +00:00
return filter(indexed, options);
2015-01-31 03:09:48 +00:00
}
return new web3.filter(filter, ethWatch);
}
2014-11-11 10:46:46 +00:00
},
2015-01-14 09:50:34 +00:00
/// db object prototype
db: {},
2015-01-14 09:50:34 +00:00
/// shh object prototype
2014-11-11 10:46:46 +00:00
shh: {
2015-01-31 00:30:19 +00:00
/// @param filter may be a string, object or event
watch: function (filter, indexed) {
2015-01-31 03:09:48 +00:00
return new web3.filter(filter, shhWatch);
}
2014-11-11 10:46:46 +00:00
},
};
2015-01-13 16:05:05 +00:00
/// setups all api methods
2014-12-22 00:13:49 +00:00
setupMethods(web3, web3Methods());
2014-11-14 12:11:47 +00:00
setupMethods(web3.eth, ethMethods());
setupProperties(web3.eth, ethProperties());
2014-11-11 10:46:46 +00:00
setupMethods(web3.db, dbMethods());
setupMethods(web3.shh, shhMethods());
var ethWatch = {
changed: 'eth_changed'
};
2015-01-13 16:05:05 +00:00
2014-11-11 10:46:46 +00:00
setupMethods(ethWatch, ethWatchMethods());
2015-01-13 16:05:05 +00:00
2014-11-11 10:46:46 +00:00
var shhWatch = {
changed: 'shh_changed'
};
2015-01-13 16:05:05 +00:00
2014-11-11 10:46:46 +00:00
setupMethods(shhWatch, shhWatchMethods());
web3.setProvider = function(provider) {
web3.provider.set(provider);
};
module.exports = web3;