web3.js/lib/web3/eth.js

150 lines
6.5 KiB
JavaScript
Raw Normal View History

2015-02-05 14:37:30 -08:00
/*
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 eth.js
* @authors:
* Marek Kotewicz <marek@ethdev.com>
2015-03-12 08:33:19 -07:00
* Fabian Vogelsteller <fabian@ethdev.com>
2015-02-05 14:37:30 -08:00
* @date 2015
*/
2015-03-10 03:03:03 -07:00
/**
* Web3
*
* @module web3
*/
/**
* Eth methods and properties
*
* An example method object can look as follows:
*
* {
* name: 'getBlock',
* call: blockCall,
* outputFormatter: formatters.outputBlockFormatter,
* inputFormatter: [ // can be a formatter funciton or an array of functions. Where each item in the array will be used for one parameter
* utils.toHex, // formats paramter 1
* function(param){ if(!param) return false; } // formats paramter 2
* ]
* },
*
* @class [web3] eth
* @constructor
*/
2015-03-08 10:31:08 -07:00
var formatters = require('./formatters');
2015-03-08 10:18:52 -07:00
var utils = require('../utils/utils');
2015-02-23 08:05:53 -08:00
var blockCall = function (args) {
2015-03-09 12:06:54 -07:00
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? "eth_getBlockByHash" : "eth_getBlockByNumber";
};
2015-02-05 14:37:30 -08:00
2015-03-09 12:06:54 -07:00
var transactionFromBlockCall = function (args) {
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getTransactionByBlockHashAndIndex' : 'eth_getTransactionByBlockNumberAndIndex';
};
2015-02-05 14:37:30 -08:00
var uncleCall = function (args) {
2015-03-09 12:06:54 -07:00
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleByBlockHashAndIndex' : 'eth_getUncleByBlockNumberAndIndex';
};
2015-02-05 14:37:30 -08:00
2015-03-09 12:06:54 -07:00
var getBlockTransactionCountCall = function (args) {
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getBlockTransactionCountByHash' : 'eth_getBlockTransactionCountByNumber';
};
var uncleCountCall = function (args) {
2015-03-09 12:06:54 -07:00
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleCountByBlockHash' : 'eth_getUncleCountByBlockNumber';
};
/// @returns an array of objects describing web3.eth api methods
var methods = [
2015-03-10 03:03:03 -07:00
{ name: 'getBalance', call: 'eth_getBalance', addDefaultblock: 2,
outputFormatter: formatters.convertToBigNumber},
2015-03-07 10:32:05 -08:00
{ name: 'getStorage', call: 'eth_getStorage', addDefaultblock: 2},
2015-03-10 03:03:03 -07:00
{ name: 'getStorageAt', call: 'eth_getStorageAt', addDefaultblock: 3,
inputFormatter: utils.toHex},
2015-03-07 10:32:05 -08:00
{ name: 'getData', call: 'eth_getData', addDefaultblock: 2},
2015-03-10 03:03:03 -07:00
{ name: 'getBlock', call: blockCall,
outputFormatter: formatters.outputBlockFormatter,
inputFormatter: [utils.toHex, function(param){ return (!param) ? false : true; }]},
{ name: 'getUncle', call: uncleCall,
outputFormatter: formatters.outputBlockFormatter,
2015-03-10 06:49:23 -07:00
inputFormatter: [utils.toHex, utils.toHex, function(param){ return (!param) ? false : true; }]},
2015-03-07 10:32:05 -08:00
{ name: 'getCompilers', call: 'eth_getCompilers' },
2015-03-10 03:03:03 -07:00
{ name: 'getBlockTransactionCount', call: getBlockTransactionCountCall,
outputFormatter: utils.toDecimal,
inputFormatter: utils.toHex },
{ name: 'getBlockUncleCount', call: uncleCountCall,
outputFormatter: utils.toDecimal,
inputFormatter: utils.toHex },
{ name: 'getTransaction', call: 'eth_getTransactionByHash',
outputFormatter: formatters.outputTransactionFormatter },
{ name: 'getTransactionFromBlock', call: transactionFromBlockCall,
outputFormatter: formatters.outputTransactionFormatter,
inputFormatter: utils.toHex },
{ name: 'getTransactionCount', call: 'eth_getTransactionCount', addDefaultblock: 2,
outputFormatter: utils.toDecimal},
{ name: 'sendTransaction', call: 'eth_sendTransaction',
inputFormatter: formatters.inputTransactionFormatter },
{ name: 'call', call: 'eth_call', addDefaultblock: 2,
inputFormatter: formatters.inputCallFormatter },
2015-03-12 08:33:19 -07:00
{ name: 'compile.solidity', call: 'eth_compileSolidity' },
2015-03-10 06:49:23 -07:00
{ name: 'compile.lll', call: 'eth_compileLLL', inputFormatter: utils.toHex },
{ name: 'compile.serpent', call: 'eth_compileSerpent', inputFormatter: utils.toHex },
2015-02-25 07:30:23 -08:00
{ name: 'flush', call: 'eth_flush' },
// deprecated methods
2015-03-02 07:29:23 -08:00
{ name: 'balanceAt', call: 'eth_balanceAt', newMethod: 'eth.getBalance' },
{ name: 'stateAt', call: 'eth_stateAt', newMethod: 'eth.getStorageAt' },
{ name: 'storageAt', call: 'eth_storageAt', newMethod: 'eth.getStorage' },
{ name: 'countAt', call: 'eth_countAt', newMethod: 'eth.getTransactionCount' },
{ name: 'codeAt', call: 'eth_codeAt', newMethod: 'eth.getData' },
{ name: 'transact', call: 'eth_transact', newMethod: 'eth.sendTransaction' },
{ name: 'block', call: blockCall, newMethod: 'eth.getBlock' },
2015-03-09 12:06:54 -07:00
{ name: 'transaction', call: transactionFromBlockCall, newMethod: 'eth.getTransaction' },
2015-03-02 07:29:23 -08:00
{ name: 'uncle', call: uncleCall, newMethod: 'eth.getUncle' },
{ name: 'compilers', call: 'eth_compilers', newMethod: 'eth.getCompilers' },
{ name: 'solidity', call: 'eth_solidity', newMethod: 'eth.compile.solidity' },
{ name: 'lll', call: 'eth_lll', newMethod: 'eth.compile.lll' },
{ name: 'serpent', call: 'eth_serpent', newMethod: 'eth.compile.serpent' },
2015-03-09 12:06:54 -07:00
{ name: 'transactionCount', call: getBlockTransactionCountCall, newMethod: 'eth.getBlockTransactionCount' },
2015-03-02 07:29:23 -08:00
{ name: 'uncleCount', call: uncleCountCall, newMethod: 'eth.getBlockUncleCount' },
{ name: 'logs', call: 'eth_logs' }
];
2015-02-05 14:37:30 -08:00
/// @returns an array of objects describing web3.eth api properties
var properties = [
2015-03-03 02:11:52 -08:00
{ name: 'coinbase', getter: 'eth_coinbase'},
{ name: 'mining', getter: 'eth_mining'},
2015-02-23 08:05:53 -08:00
{ name: 'gasPrice', getter: 'eth_gasPrice', outputFormatter: formatters.convertToBigNumber},
2015-02-05 14:37:30 -08:00
{ name: 'accounts', getter: 'eth_accounts' },
2015-03-09 07:09:21 -07:00
{ name: 'blockNumber', getter: 'eth_blockNumber', outputFormatter: utils.toDecimal},
// deprecated properties
2015-03-02 07:29:23 -08:00
{ name: 'listening', getter: 'net_listening', setter: 'eth_setListening', newProperty: 'net.listening'},
{ name: 'peerCount', getter: 'net_peerCount', newProperty: 'net.peerCount'},
{ name: 'number', getter: 'eth_number', newProperty: 'eth.blockNumber'}
];
2015-02-05 14:37:30 -08:00
module.exports = {
methods: methods,
properties: properties
};