2014-11-05 17:46:01 +00:00
|
|
|
/*
|
2014-11-05 18:12:21 +00:00
|
|
|
This file is part of ethereum.js.
|
2014-11-05 17:46:01 +00:00
|
|
|
|
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 17:46:01 +00:00
|
|
|
|
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 17:46:01 +00:00
|
|
|
|
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/>.
|
2014-11-05 17:46:01 +00:00
|
|
|
*/
|
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
|
|
|
|
*/
|
2014-11-05 17:46:01 +00:00
|
|
|
|
2015-01-21 20:35:15 +00:00
|
|
|
if (process.env.NODE_ENV !== 'build') {
|
|
|
|
var BigNumber = require('bignumber.js');
|
|
|
|
}
|
|
|
|
|
|
|
|
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 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-05 17:46:01 +00:00
|
|
|
};
|
|
|
|
|
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-05 17:46:01 +00:00
|
|
|
};
|
|
|
|
|
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-05 17:46:01 +00:00
|
|
|
};
|
|
|
|
|
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' },
|
2015-01-25 00:46:46 +00:00
|
|
|
{ name: 'flush', call: 'eth_flush' },
|
2014-11-12 19:39:13 +00:00
|
|
|
{ name: 'lll', call: 'eth_lll' },
|
2014-11-17 14:46:46 +00:00
|
|
|
{ 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-05 17:46:01 +00:00
|
|
|
};
|
|
|
|
|
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;
|
2015-01-21 20:22:05 +00:00
|
|
|
return web3.provider.send({
|
2015-01-21 19:29:02 +00:00
|
|
|
call: call,
|
|
|
|
args: args
|
2014-11-11 10:46:46 +00:00
|
|
|
});
|
2014-11-05 17:46:01 +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 () {
|
2015-01-21 20:22:05 +00:00
|
|
|
return web3.provider.send({
|
2015-01-21 19:29:02 +00:00
|
|
|
call: property.getter
|
2014-11-11 10:46:46 +00:00
|
|
|
});
|
|
|
|
};
|
2015-01-21 20:22:05 +00:00
|
|
|
|
2014-11-11 10:46:46 +00:00
|
|
|
if (property.setter) {
|
|
|
|
proto.set = function (val) {
|
2015-01-21 20:22:05 +00:00
|
|
|
return web3.provider.send({
|
2015-01-21 19:29:02 +00:00
|
|
|
call: property.setter,
|
|
|
|
args: [val]
|
2014-11-05 17:46:01 +00:00
|
|
|
});
|
|
|
|
};
|
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: {},
|
|
|
|
|
2014-12-30 16:59:33 +00:00
|
|
|
toHex: function(str) {
|
|
|
|
var hex = "";
|
|
|
|
for(var i = 0; i < str.length; i++) {
|
|
|
|
var n = str.charCodeAt(i).toString(16);
|
|
|
|
hex += n.length < 2 ? '0' + n : n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hex;
|
|
|
|
},
|
|
|
|
|
2015-01-14 09:50:34 +00:00
|
|
|
/// @returns ascii string representation of hex value prefixed with 0x
|
2014-11-11 10:46:46 +00:00
|
|
|
toAscii: function(hex) {
|
|
|
|
// Find termination
|
|
|
|
var str = "";
|
|
|
|
var i = 0, l = hex.length;
|
|
|
|
if (hex.substring(0, 2) === '0x')
|
|
|
|
i = 2;
|
|
|
|
for(; i < l; i+=2) {
|
2015-01-13 11:49:10 +00:00
|
|
|
var code = parseInt(hex.substr(i, 2), 16);
|
2014-11-11 10:46:46 +00:00
|
|
|
if(code === 0) {
|
|
|
|
break;
|
2014-11-05 17:52:52 +00:00
|
|
|
}
|
|
|
|
|
2015-01-13 11:49:10 +00:00
|
|
|
str += String.fromCharCode(code);
|
2014-11-05 17:46:01 +00:00
|
|
|
}
|
2014-11-05 17:52:52 +00:00
|
|
|
|
2014-11-11 10:46:46 +00:00
|
|
|
return str;
|
|
|
|
},
|
|
|
|
|
2015-01-14 09:50:34 +00:00
|
|
|
/// @returns hex representation (prefixed by 0x) of ascii string
|
2014-11-11 10:46:46 +00:00
|
|
|
fromAscii: function(str, pad) {
|
2015-01-09 16:27:56 +00:00
|
|
|
pad = pad === undefined ? 0 : pad;
|
2014-11-11 10:46:46 +00:00
|
|
|
var hex = this.toHex(str);
|
|
|
|
while(hex.length < pad*2)
|
|
|
|
hex += "00";
|
|
|
|
return "0x" + hex;
|
|
|
|
},
|
|
|
|
|
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) {
|
2015-01-21 20:35:15 +00:00
|
|
|
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-01-21 20:35:15 +00:00
|
|
|
/// TODO: use BigNumber.js to parse int
|
2014-12-22 00:13:49 +00:00
|
|
|
toEth: function(str) {
|
2015-01-06 12:26:51 +00:00
|
|
|
var val = typeof str === "string" ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str) : str;
|
2014-12-22 00:13:49 +00:00
|
|
|
var unit = 0;
|
2015-01-21 20:35:15 +00:00
|
|
|
var units = ETH_UNITS;
|
2014-12-22 00:13:49 +00: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 12:37:02 +00:00
|
|
|
var replaceFunction = function($0, $1, $2) {
|
|
|
|
return $1 + ',' + $2;
|
|
|
|
};
|
2015-01-06 12:26:51 +00:00
|
|
|
|
2014-12-22 00:13:49 +00:00
|
|
|
while (true) {
|
|
|
|
var o = s;
|
2015-01-06 12:26:51 +00:00
|
|
|
s = s.replace(/(\d)(\d\d\d[\.\,])/, replaceFunction);
|
|
|
|
if (o === s)
|
2014-12-22 00:13:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return s + ' ' + units[unit];
|
|
|
|
},
|
|
|
|
|
2015-01-14 09:50:34 +00:00
|
|
|
/// eth object prototype
|
2014-11-11 10:46:46 +00:00
|
|
|
eth: {
|
2015-01-25 21:43:36 +00: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 00:30:19 +00: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 03:09:48 +00:00
|
|
|
if (filter._isEvent) {
|
|
|
|
return filter(indexed);
|
|
|
|
}
|
|
|
|
return new web3.filter(filter, ethWatch);
|
2014-11-05 17:46:01 +00:00
|
|
|
}
|
2014-11-11 10:46:46 +00:00
|
|
|
},
|
2014-11-05 17:46:01 +00:00
|
|
|
|
2015-01-14 09:50:34 +00:00
|
|
|
/// db object prototype
|
|
|
|
db: {},
|
2014-11-05 17:46:01 +00:00
|
|
|
|
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-05 17:46:01 +00:00
|
|
|
}
|
2014-11-11 10:46:46 +00:00
|
|
|
},
|
2014-11-05 17:46:01 +00:00
|
|
|
|
2015-01-14 09:50:34 +00:00
|
|
|
/// @returns true if provider is installed
|
|
|
|
haveProvider: function() {
|
|
|
|
return !!web3.provider.provider;
|
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) {
|
2015-01-21 20:22:05 +00:00
|
|
|
//provider.onmessage = messageHandler; // there will be no async calls, to remove
|
2014-11-11 10:46:46 +00:00
|
|
|
web3.provider.set(provider);
|
|
|
|
};
|
|
|
|
|
2015-01-16 14:49:36 +00:00
|
|
|
module.exports = web3;
|
|
|
|
|