web3.js/lib/web3/eth.js

255 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,
2015-03-23 08:42:09 -07:00
* params: 2,
2015-03-10 03:03:03 -07:00
* 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
2015-03-23 08:42:09 -07:00
* function(param){ return !!param; } // formats paramter 2
2015-03-10 03:03:03 -07:00
* ]
* },
*
* @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-03-22 10:12:52 -07:00
var Method = require('./method');
var Property = require('./property');
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-22 10:12:52 -07:00
var getBalance = new Method({
2015-03-21 14:57:44 -07:00
name: 'getBalance',
call: 'eth_getBalance',
2015-03-22 10:12:52 -07:00
params: 2,
2015-03-23 08:42:09 -07:00
inputFormatter: [utils.toHex, formatters.inputDefaultBlockNumberFormatter],
outputFormatter: formatters.outputBigNumberFormatter
2015-03-22 10:12:52 -07:00
});
var getStorageAt = new Method({
name: 'getStorageAt',
call: 'eth_getStorageAt',
2015-03-23 07:08:09 -07:00
params: 3,
2015-03-23 08:42:09 -07:00
inputFormatter: [null, utils.toHex, formatters.inputDefaultBlockNumberFormatter]
2015-03-22 10:12:52 -07:00
});
var getCode = new Method({
2015-03-21 14:57:44 -07:00
name: 'getCode',
call: 'eth_getCode',
2015-03-23 07:08:09 -07:00
params: 2,
2015-03-23 08:42:09 -07:00
inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter]
2015-03-22 10:12:52 -07:00
});
var getBlock = new Method({
2015-03-21 14:57:44 -07:00
name: 'getBlock',
call: blockCall,
2015-03-23 07:08:09 -07:00
params: 2,
inputFormatter: [utils.toHex, function (val) { return !!val; }],
outputFormatter: formatters.outputBlockFormatter
2015-03-22 10:12:52 -07:00
});
var getUncle = new Method({
2015-03-21 14:57:44 -07:00
name: 'getUncle',
call: uncleCall,
2015-03-23 07:08:09 -07:00
params: 2,
2015-03-23 08:42:09 -07:00
inputFormatter: [utils.toHex, utils.toHex, function (val) { return !!val; }],
2015-03-21 14:57:44 -07:00
outputFormatter: formatters.outputBlockFormatter,
2015-03-23 07:08:09 -07:00
2015-03-22 10:12:52 -07:00
});
var getCompilers = new Method({
2015-03-21 14:57:44 -07:00
name: 'getCompilers',
2015-03-22 10:12:52 -07:00
call: 'eth_getCompilers',
params: 0
});
var getBlockTransactounCount = new Method({
2015-03-21 14:57:44 -07:00
name: 'getBlockTransactionCount',
call: getBlockTransactionCountCall,
2015-03-22 10:12:52 -07:00
params: 1,
2015-03-23 08:42:09 -07:00
inputFormatter: [utils.toHex],
outputFormatter: utils.toDecimal
2015-03-22 10:12:52 -07:00
});
var getBlockUncleCount = new Method({
2015-03-21 14:57:44 -07:00
name: 'getBlockUncleCount',
call: uncleCountCall,
2015-03-22 10:12:52 -07:00
params: 1,
2015-03-23 08:42:09 -07:00
inputFormatter: [utils.toHex],
outputFormatter: utils.toDecimal
2015-03-22 10:12:52 -07:00
});
var getTransaction = new Method({
2015-03-21 14:57:44 -07:00
name: 'getTransaction',
call: 'eth_getTransactionByHash',
2015-03-22 10:12:52 -07:00
params: 1,
2015-03-21 14:57:44 -07:00
outputFormatter: formatters.outputTransactionFormatter
2015-03-22 10:12:52 -07:00
});
var getTransactionFromBlock = new Method({
2015-03-21 14:57:44 -07:00
name: 'getTransactionFromBlock',
call: transactionFromBlockCall,
2015-03-22 10:12:52 -07:00
params: 2,
2015-03-23 08:42:09 -07:00
inputFormatter: [utils.toHex, utils.toHex],
outputFormatter: formatters.outputTransactionFormatter
2015-03-22 10:12:52 -07:00
});
var getTransactionCount = new Method({
2015-03-21 14:57:44 -07:00
name: 'getTransactionCount',
call: 'eth_getTransactionCount',
2015-03-22 10:12:52 -07:00
params: 2,
2015-03-23 08:42:09 -07:00
inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter],
2015-03-21 14:57:44 -07:00
outputFormatter: utils.toDecimal
2015-03-22 10:12:52 -07:00
});
var sendTransaction = new Method({
2015-03-21 14:57:44 -07:00
name: 'sendTransaction',
call: 'eth_sendTransaction',
2015-03-22 10:12:52 -07:00
params: 1,
2015-03-23 08:42:09 -07:00
inputFormatter: [formatters.inputTransactionFormatter]
2015-03-22 10:12:52 -07:00
});
var call = new Method({
2015-03-21 14:57:44 -07:00
name: 'call',
call: 'eth_call',
2015-03-22 10:12:52 -07:00
params: 2,
2015-03-23 08:42:09 -07:00
inputFormatter: [formatters.inputCallFormatter, formatters.inputDefaultBlockNumberFormatter]
2015-03-22 10:12:52 -07:00
});
var compileSolidity = new Method({
2015-03-21 14:57:44 -07:00
name: 'compile.solidity',
2015-03-22 10:12:52 -07:00
call: 'eth_compileSolidity',
params: 1
});
var compileLLL = new Method({
2015-03-21 14:57:44 -07:00
name: 'compile.lll',
call: 'eth_compileLLL',
2015-03-23 08:50:13 -07:00
params: 1
2015-03-22 10:12:52 -07:00
});
var compileSerpent = new Method({
2015-03-21 14:57:44 -07:00
name: 'compile.serpent',
call: 'eth_compileSerpent',
2015-03-23 08:50:13 -07:00
params: 1
2015-03-22 10:12:52 -07:00
});
var flush = new Method({
2015-03-21 14:57:44 -07:00
name: 'flush',
2015-03-22 10:12:52 -07:00
call: 'eth_flush',
params: 0
});
var methods = [
getBalance,
getStorageAt,
getCode,
getBlock,
getUncle,
getCompilers,
getBlockTransactounCount,
getBlockUncleCount,
getTransaction,
getTransactionFromBlock,
getTransactionCount,
call,
sendTransaction,
compileSolidity,
compileLLL,
compileSerpent,
flush
];
2015-02-05 14:37:30 -08:00
/// @returns an array of objects describing web3.eth api properties
var properties = [
new Property({
name: 'coinbase',
getter: 'eth_coinbase'
}),
new Property({
name: 'mining',
getter: 'eth_mining'
}),
new Property({
name: 'gasPrice',
getter: 'eth_gasPrice',
outputFormatter: formatters.inputNumberFormatter
}),
new Property({
name: 'accounts',
getter: 'eth_accounts'
}),
new Property({
name: 'blockNumber',
getter: 'eth_blockNumber',
outputFormatter: utils.toDecimal
})
];
2015-02-05 14:37:30 -08:00
module.exports = {
methods: methods,
properties: properties
};