web3.js/lib/web3/eth.js

194 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/>.
*/
2015-03-22 01:43:07 -07:00
/**
* @file eth.js
* @author Marek Kotewicz <marek@ethdev.com>
* @author 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-21 14:57:44 -07:00
"use strict";
2015-03-10 03:03:03 -07:00
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
2015-03-21 14:57:44 -07:00
var methods = [{
name: 'getBalance',
call: 'eth_getBalance',
addDefaultblock: 2,
2015-03-22 01:43:07 -07:00
outputFormatter: formatters.inputNumberFormatter
2015-03-21 14:57:44 -07:00
}, {
name: 'getStorage',
call: 'eth_getStorage',
addDefaultblock: 2
}, {
name: 'getStorageAt',
call: 'eth_getStorageAt',
addDefaultblock: 3,
inputFormatter: utils.toHex
}, {
name: 'getCode',
call: 'eth_getCode',
addDefaultblock: 2
}, {
name: 'getBlock',
call: blockCall,
outputFormatter: formatters.outputBlockFormatter,
2015-03-22 01:43:07 -07:00
inputFormatter: formatters.inputBlockFormatter
2015-03-21 14:57:44 -07:00
}, {
name: 'getUncle',
call: uncleCall,
outputFormatter: formatters.outputBlockFormatter,
2015-03-22 01:43:07 -07:00
inputFormatter: formatters.inputUncleFormatter
2015-03-21 14:57:44 -07:00
}, {
name: 'getCompilers',
call: 'eth_getCompilers'
}, {
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
}, {
name: 'compile.solidity',
call: 'eth_compileSolidity'
}, {
name: 'compile.lll',
call: 'eth_compileLLL',
inputFormatter: utils.toHex
}, {
name: 'compile.serpent',
call: 'eth_compileSerpent',
inputFormatter: utils.toHex
}, {
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' },
2015-03-16 01:58:10 -07:00
{ name: 'codeAt', call: 'eth_codeAt', newMethod: 'eth.getCode' },
2015-03-02 07:29:23 -08:00
{ 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-03-22 01:43:07 -07:00
{ name: 'gasPrice', getter: 'eth_gasPrice', outputFormatter: formatters.inputNumberFormatter},
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
};