2014-11-05 19:46:01 +02:00
|
|
|
/*
|
2014-11-05 20:12:21 +02:00
|
|
|
This file is part of ethereum.js.
|
2014-11-05 19:46:01 +02:00
|
|
|
|
2014-11-05 20:12:21 +02: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 19:46:01 +02:00
|
|
|
|
2014-11-05 20:12:21 +02: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 19:46:01 +02:00
|
|
|
|
2014-11-05 20:12:21 +02: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/>.
|
2014-11-05 19:46:01 +02:00
|
|
|
*/
|
2015-01-13 19:28:49 +02:00
|
|
|
/** @file web3.js
|
2014-11-11 11:30:21 +02:00
|
|
|
* @authors:
|
|
|
|
* Jeffrey Wilcke <jeff@ethdev.com>
|
|
|
|
* Marek Kotewicz <marek@ethdev.com>
|
|
|
|
* Marian Oancea <marian@ethdev.com>
|
2014-12-22 02:13:49 +02:00
|
|
|
* Gav Wood <g@ethdev.com>
|
2014-11-11 11:30:21 +02:00
|
|
|
* @date 2014
|
|
|
|
*/
|
2014-11-05 19:46:01 +02:00
|
|
|
|
2015-01-21 22:35:15 +02:00
|
|
|
if (process.env.NODE_ENV !== 'build') {
|
|
|
|
var BigNumber = require('bignumber.js');
|
|
|
|
}
|
|
|
|
|
2015-01-31 14:54:39 +02:00
|
|
|
var utils = require('./utils');
|
|
|
|
|
2015-01-21 22:35:15 +02:00
|
|
|
var ETH_UNITS = [
|
|
|
|
'wei',
|
|
|
|
'Kwei',
|
|
|
|
'Mwei',
|
|
|
|
'Gwei',
|
|
|
|
'szabo',
|
|
|
|
'finney',
|
|
|
|
'ether',
|
|
|
|
'grand',
|
|
|
|
'Mether',
|
|
|
|
'Gether',
|
|
|
|
'Tether',
|
|
|
|
'Pether',
|
|
|
|
'Eether',
|
|
|
|
'Zether',
|
|
|
|
'Yether',
|
|
|
|
'Nether',
|
|
|
|
'Dether',
|
|
|
|
'Vether',
|
|
|
|
'Uether'
|
|
|
|
];
|
|
|
|
|
2015-01-13 18:05:05 +02:00
|
|
|
/// @returns an array of objects describing web3 api methods
|
2014-12-22 02:13:49 +02:00
|
|
|
var web3Methods = function () {
|
|
|
|
return [
|
|
|
|
{ name: 'sha3', call: 'web3_sha3' }
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2015-01-13 18:05:05 +02:00
|
|
|
/// @returns an array of objects describing web3.eth api methods
|
2014-11-11 12:46:46 +02:00
|
|
|
var ethMethods = function () {
|
|
|
|
var blockCall = function (args) {
|
|
|
|
return typeof args[0] === "string" ? "eth_blockByHash" : "eth_blockByNumber";
|
2014-11-05 19:46:01 +02:00
|
|
|
};
|
|
|
|
|
2014-11-11 12:46:46 +02:00
|
|
|
var transactionCall = function (args) {
|
2014-12-22 02:13:49 +02:00
|
|
|
return typeof args[0] === "string" ? 'eth_transactionByHash' : 'eth_transactionByNumber';
|
2014-11-05 19:46:01 +02:00
|
|
|
};
|
|
|
|
|
2014-11-11 12:46:46 +02:00
|
|
|
var uncleCall = function (args) {
|
2014-12-22 02:13:49 +02:00
|
|
|
return typeof args[0] === "string" ? 'eth_uncleByHash' : 'eth_uncleByNumber';
|
2014-11-05 19:46:01 +02:00
|
|
|
};
|
|
|
|
|
2014-11-11 12:46:46 +02:00
|
|
|
var methods = [
|
|
|
|
{ name: 'balanceAt', call: 'eth_balanceAt' },
|
|
|
|
{ name: 'stateAt', call: 'eth_stateAt' },
|
2014-11-13 19:29:31 +02:00
|
|
|
{ name: 'storageAt', call: 'eth_storageAt' },
|
2014-11-11 12:46:46 +02: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 21:39:13 +02:00
|
|
|
{ name: 'compilers', call: 'eth_compilers' },
|
2015-01-25 02:46:46 +02:00
|
|
|
{ name: 'flush', call: 'eth_flush' },
|
2014-11-12 21:39:13 +02:00
|
|
|
{ name: 'lll', call: 'eth_lll' },
|
2014-11-17 16:46:46 +02:00
|
|
|
{ name: 'solidity', call: 'eth_solidity' },
|
2014-11-18 14:59:17 +02:00
|
|
|
{ name: 'serpent', call: 'eth_serpent' },
|
|
|
|
{ name: 'logs', call: 'eth_logs' }
|
2014-11-11 12:46:46 +02:00
|
|
|
];
|
|
|
|
return methods;
|
|
|
|
};
|
|
|
|
|
2015-01-13 18:05:05 +02:00
|
|
|
/// @returns an array of objects describing web3.eth api properties
|
2014-11-11 12:46:46 +02: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 18:05:05 +02:00
|
|
|
/// @returns an array of objects describing web3.db api methods
|
2014-11-11 12:46:46 +02: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 18:05:05 +02:00
|
|
|
/// @returns an array of objects describing web3.shh api methods
|
2014-11-11 12:46:46 +02: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 18:05:05 +02:00
|
|
|
/// @returns an array of objects describing web3.eth.watch api methods
|
2014-11-11 12:46:46 +02:00
|
|
|
var ethWatchMethods = function () {
|
|
|
|
var newFilter = function (args) {
|
|
|
|
return typeof args[0] === 'string' ? 'eth_newFilterString' : 'eth_newFilter';
|
2014-11-05 19:46:01 +02:00
|
|
|
};
|
|
|
|
|
2014-11-11 12:46:46 +02:00
|
|
|
return [
|
|
|
|
{ name: 'newFilter', call: newFilter },
|
|
|
|
{ name: 'uninstallFilter', call: 'eth_uninstallFilter' },
|
2014-11-18 14:59:17 +02:00
|
|
|
{ name: 'getMessages', call: 'eth_filterLogs' }
|
2014-11-11 12:46:46 +02:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2015-01-13 18:05:05 +02:00
|
|
|
/// @returns an array of objects describing web3.shh.watch api methods
|
2014-11-11 12:46:46 +02:00
|
|
|
var shhWatchMethods = function () {
|
|
|
|
return [
|
|
|
|
{ name: 'newFilter', call: 'shh_newFilter' },
|
|
|
|
{ name: 'uninstallFilter', call: 'shh_uninstallFilter' },
|
2015-01-12 19:34:37 +02:00
|
|
|
{ name: 'getMessages', call: 'shh_getMessages' }
|
2014-11-11 12:46:46 +02:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2015-01-13 18:05:05 +02:00
|
|
|
/// creates methods in a given object based on method description on input
|
|
|
|
/// setups api calls for these methods
|
2014-11-11 12:46:46 +02:00
|
|
|
var setupMethods = function (obj, methods) {
|
|
|
|
methods.forEach(function (method) {
|
|
|
|
obj[method.name] = function () {
|
2015-01-21 21:29:02 +02:00
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
var call = typeof method.call === 'function' ? method.call(args) : method.call;
|
2015-01-21 22:22:05 +02:00
|
|
|
return web3.provider.send({
|
2015-01-21 21:29:02 +02:00
|
|
|
call: call,
|
|
|
|
args: args
|
2014-11-11 12:46:46 +02:00
|
|
|
});
|
2014-11-05 19:46:01 +02:00
|
|
|
};
|
2014-11-11 12:46:46 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-01-13 18:05:05 +02:00
|
|
|
/// creates properties in a given object based on properties description on input
|
|
|
|
/// setups api calls for these properties
|
2014-11-11 12:46:46 +02:00
|
|
|
var setupProperties = function (obj, properties) {
|
|
|
|
properties.forEach(function (property) {
|
|
|
|
var proto = {};
|
|
|
|
proto.get = function () {
|
2015-01-21 22:22:05 +02:00
|
|
|
return web3.provider.send({
|
2015-01-21 21:29:02 +02:00
|
|
|
call: property.getter
|
2014-11-11 12:46:46 +02:00
|
|
|
});
|
|
|
|
};
|
2015-01-21 22:22:05 +02:00
|
|
|
|
2014-11-11 12:46:46 +02:00
|
|
|
if (property.setter) {
|
|
|
|
proto.set = function (val) {
|
2015-01-21 22:22:05 +02:00
|
|
|
return web3.provider.send({
|
2015-01-21 21:29:02 +02:00
|
|
|
call: property.setter,
|
|
|
|
args: [val]
|
2014-11-05 19:46:01 +02:00
|
|
|
});
|
|
|
|
};
|
2014-11-11 12:46:46 +02:00
|
|
|
}
|
|
|
|
Object.defineProperty(obj, property.name, proto);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-01-13 18:05:05 +02:00
|
|
|
/// setups web3 object, and it's in-browser executed methods
|
2014-11-11 12:46:46 +02:00
|
|
|
var web3 = {
|
|
|
|
_callbacks: {},
|
|
|
|
_events: {},
|
|
|
|
providers: {},
|
|
|
|
|
2015-01-14 11:50:34 +02:00
|
|
|
/// @returns ascii string representation of hex value prefixed with 0x
|
2015-01-31 14:54:39 +02:00
|
|
|
toAscii: utils.toAscii,
|
2014-11-11 12:46:46 +02:00
|
|
|
|
2015-01-14 11:50:34 +02:00
|
|
|
/// @returns hex representation (prefixed by 0x) of ascii string
|
2015-01-31 14:54:39 +02:00
|
|
|
fromAscii: utils.fromAscii,
|
2014-11-11 12:46:46 +02:00
|
|
|
|
2015-01-14 11:50:34 +02:00
|
|
|
/// @returns decimal representaton of hex value prefixed by 0x
|
2014-12-22 02:13:49 +02:00
|
|
|
toDecimal: function (val) {
|
2015-01-27 16:20:22 +02: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 02:13:49 +02:00
|
|
|
},
|
|
|
|
|
2015-01-14 11:50:34 +02:00
|
|
|
/// @returns hex representation (prefixed by 0x) of decimal value
|
2014-12-22 02:13:49 +02:00
|
|
|
fromDecimal: function (val) {
|
2015-01-21 22:35:15 +02:00
|
|
|
return "0x" + (new BigNumber(val).toString(16));
|
2014-12-22 02:13:49 +02:00
|
|
|
},
|
|
|
|
|
2015-01-14 11:50:34 +02:00
|
|
|
/// used to transform value/string to eth string
|
2015-01-21 22:35:15 +02:00
|
|
|
/// TODO: use BigNumber.js to parse int
|
2014-12-22 02:13:49 +02:00
|
|
|
toEth: function(str) {
|
2015-01-06 14:26:51 +02:00
|
|
|
var val = typeof str === "string" ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str) : str;
|
2014-12-22 02:13:49 +02:00
|
|
|
var unit = 0;
|
2015-01-21 22:35:15 +02:00
|
|
|
var units = ETH_UNITS;
|
2014-12-22 02:13:49 +02:00
|
|
|
while (val > 3000 && unit < units.length - 1)
|
|
|
|
{
|
|
|
|
val /= 1000;
|
|
|
|
unit++;
|
|
|
|
}
|
|
|
|
var s = val.toString().length < val.toFixed(2).length ? val.toString() : val.toFixed(2);
|
2015-01-06 14:37:02 +02:00
|
|
|
var replaceFunction = function($0, $1, $2) {
|
|
|
|
return $1 + ',' + $2;
|
|
|
|
};
|
2015-01-06 14:26:51 +02:00
|
|
|
|
2014-12-22 02:13:49 +02:00
|
|
|
while (true) {
|
|
|
|
var o = s;
|
2015-01-06 14:26:51 +02:00
|
|
|
s = s.replace(/(\d)(\d\d\d[\.\,])/, replaceFunction);
|
|
|
|
if (o === s)
|
2014-12-22 02:13:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return s + ' ' + units[unit];
|
|
|
|
},
|
|
|
|
|
2015-01-14 11:50:34 +02:00
|
|
|
/// eth object prototype
|
2014-11-11 12:46:46 +02:00
|
|
|
eth: {
|
2015-01-25 23:43:36 +02:00
|
|
|
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 02:30:19 +02:00
|
|
|
|
|
|
|
/// @param filter may be a string, object or event
|
|
|
|
/// @param indexed is optional, this may be an object with optional event indexed params
|
|
|
|
watch: function (filter, indexed) {
|
2015-01-31 05:09:48 +02:00
|
|
|
if (filter._isEvent) {
|
|
|
|
return filter(indexed);
|
|
|
|
}
|
|
|
|
return new web3.filter(filter, ethWatch);
|
2014-11-05 19:46:01 +02:00
|
|
|
}
|
2014-11-11 12:46:46 +02:00
|
|
|
},
|
2014-11-05 19:46:01 +02:00
|
|
|
|
2015-01-14 11:50:34 +02:00
|
|
|
/// db object prototype
|
|
|
|
db: {},
|
2014-11-05 19:46:01 +02:00
|
|
|
|
2015-01-14 11:50:34 +02:00
|
|
|
/// shh object prototype
|
2014-11-11 12:46:46 +02:00
|
|
|
shh: {
|
2015-01-31 02:30:19 +02:00
|
|
|
|
|
|
|
/// @param filter may be a string, object or event
|
|
|
|
watch: function (filter, indexed) {
|
2015-01-31 05:09:48 +02:00
|
|
|
return new web3.filter(filter, shhWatch);
|
2014-11-05 19:46:01 +02:00
|
|
|
}
|
2014-11-11 12:46:46 +02:00
|
|
|
},
|
2014-11-05 19:46:01 +02:00
|
|
|
|
2015-01-14 11:50:34 +02:00
|
|
|
/// @returns true if provider is installed
|
|
|
|
haveProvider: function() {
|
|
|
|
return !!web3.provider.provider;
|
2014-11-11 12:46:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-13 18:05:05 +02:00
|
|
|
/// setups all api methods
|
2014-12-22 02:13:49 +02:00
|
|
|
setupMethods(web3, web3Methods());
|
2014-11-14 14:11:47 +02:00
|
|
|
setupMethods(web3.eth, ethMethods());
|
|
|
|
setupProperties(web3.eth, ethProperties());
|
2014-11-11 12:46:46 +02:00
|
|
|
setupMethods(web3.db, dbMethods());
|
|
|
|
setupMethods(web3.shh, shhMethods());
|
|
|
|
|
|
|
|
var ethWatch = {
|
|
|
|
changed: 'eth_changed'
|
|
|
|
};
|
2015-01-13 18:05:05 +02:00
|
|
|
|
2014-11-11 12:46:46 +02:00
|
|
|
setupMethods(ethWatch, ethWatchMethods());
|
2015-01-13 18:05:05 +02:00
|
|
|
|
2014-11-11 12:46:46 +02:00
|
|
|
var shhWatch = {
|
|
|
|
changed: 'shh_changed'
|
|
|
|
};
|
2015-01-13 18:05:05 +02:00
|
|
|
|
2014-11-11 12:46:46 +02:00
|
|
|
setupMethods(shhWatch, shhWatchMethods());
|
|
|
|
|
|
|
|
web3.setProvider = function(provider) {
|
2015-01-21 22:22:05 +02:00
|
|
|
//provider.onmessage = messageHandler; // there will be no async calls, to remove
|
2014-11-11 12:46:46 +02:00
|
|
|
web3.provider.set(provider);
|
|
|
|
};
|
|
|
|
|
2015-01-16 16:49:36 +02:00
|
|
|
module.exports = web3;
|
|
|
|
|