2015-02-05 22:37:30 +00: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 08:43:07 +00:00
|
|
|
/**
|
|
|
|
* @file eth.js
|
|
|
|
* @author Marek Kotewicz <marek@ethdev.com>
|
|
|
|
* @author Fabian Vogelsteller <fabian@ethdev.com>
|
2015-02-05 22:37:30 +00:00
|
|
|
* @date 2015
|
|
|
|
*/
|
|
|
|
|
2015-03-10 10:03:03 +00:00
|
|
|
/**
|
|
|
|
* Web3
|
2015-04-28 21:24:33 +00:00
|
|
|
*
|
2015-03-10 10:03:03 +00:00
|
|
|
* @module web3
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Eth methods and properties
|
|
|
|
*
|
|
|
|
* An example method object can look as follows:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* name: 'getBlock',
|
|
|
|
* call: blockCall,
|
2015-03-23 15:42:09 +00:00
|
|
|
* params: 2,
|
2015-03-10 10:03:03 +00: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 15:42:09 +00:00
|
|
|
* function(param){ return !!param; } // formats paramter 2
|
2015-03-10 10:03:03 +00:00
|
|
|
* ]
|
|
|
|
* },
|
|
|
|
*
|
|
|
|
* @class [web3] eth
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
|
2015-03-21 21:57:44 +00:00
|
|
|
"use strict";
|
2015-03-10 10:03:03 +00:00
|
|
|
|
2015-03-08 17:31:08 +00:00
|
|
|
var formatters = require('./formatters');
|
2015-03-08 17:18:52 +00:00
|
|
|
var utils = require('../utils/utils');
|
2015-03-22 17:12:52 +00:00
|
|
|
var Method = require('./method');
|
2015-03-23 17:26:48 +00:00
|
|
|
var Property = require('./property');
|
2015-02-23 16:05:53 +00:00
|
|
|
|
2015-02-19 17:37:09 +00:00
|
|
|
var blockCall = function (args) {
|
2015-03-09 19:06:54 +00:00
|
|
|
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? "eth_getBlockByHash" : "eth_getBlockByNumber";
|
2015-02-19 17:37:09 +00:00
|
|
|
};
|
2015-02-05 22:37:30 +00:00
|
|
|
|
2015-03-09 19:06:54 +00:00
|
|
|
var transactionFromBlockCall = function (args) {
|
|
|
|
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getTransactionByBlockHashAndIndex' : 'eth_getTransactionByBlockNumberAndIndex';
|
2015-02-19 17:37:09 +00:00
|
|
|
};
|
2015-02-05 22:37:30 +00:00
|
|
|
|
2015-02-19 17:37:09 +00:00
|
|
|
var uncleCall = function (args) {
|
2015-03-09 19:06:54 +00:00
|
|
|
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleByBlockHashAndIndex' : 'eth_getUncleByBlockNumberAndIndex';
|
2015-02-19 17:37:09 +00:00
|
|
|
};
|
2015-02-05 22:37:30 +00:00
|
|
|
|
2015-03-09 19:06:54 +00:00
|
|
|
var getBlockTransactionCountCall = function (args) {
|
|
|
|
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getBlockTransactionCountByHash' : 'eth_getBlockTransactionCountByNumber';
|
2015-02-19 17:37:09 +00:00
|
|
|
};
|
2015-02-17 18:55:02 +00:00
|
|
|
|
2015-02-19 17:37:09 +00:00
|
|
|
var uncleCountCall = function (args) {
|
2015-03-09 19:06:54 +00:00
|
|
|
return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleCountByBlockHash' : 'eth_getUncleCountByBlockNumber';
|
2015-02-19 17:37:09 +00:00
|
|
|
};
|
2015-02-17 18:55:02 +00:00
|
|
|
|
2015-02-19 17:37:09 +00:00
|
|
|
/// @returns an array of objects describing web3.eth api methods
|
2015-03-22 17:12:52 +00:00
|
|
|
|
|
|
|
var getBalance = new Method({
|
2015-04-28 21:24:33 +00:00
|
|
|
name: 'getBalance',
|
|
|
|
call: 'eth_getBalance',
|
2015-03-22 17:12:52 +00:00
|
|
|
params: 2,
|
2015-04-02 11:08:40 +00:00
|
|
|
inputFormatter: [utils.toAddress, formatters.inputDefaultBlockNumberFormatter],
|
2015-03-23 15:42:09 +00:00
|
|
|
outputFormatter: formatters.outputBigNumberFormatter
|
2015-03-22 17:12:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var getStorageAt = new Method({
|
2015-04-28 21:24:33 +00:00
|
|
|
name: 'getStorageAt',
|
|
|
|
call: 'eth_getStorageAt',
|
2015-03-23 14:08:09 +00:00
|
|
|
params: 3,
|
2015-03-23 15:42:09 +00:00
|
|
|
inputFormatter: [null, utils.toHex, formatters.inputDefaultBlockNumberFormatter]
|
2015-03-22 17:12:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var getCode = new Method({
|
2015-03-21 21:57:44 +00:00
|
|
|
name: 'getCode',
|
|
|
|
call: 'eth_getCode',
|
2015-03-23 14:08:09 +00:00
|
|
|
params: 2,
|
2015-04-02 11:08:40 +00:00
|
|
|
inputFormatter: [utils.toAddress, formatters.inputDefaultBlockNumberFormatter]
|
2015-03-22 17:12:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var getBlock = new Method({
|
2015-04-28 21:24:33 +00:00
|
|
|
name: 'getBlock',
|
2015-03-21 21:57:44 +00:00
|
|
|
call: blockCall,
|
2015-03-23 14:08:09 +00:00
|
|
|
params: 2,
|
2015-04-22 16:16:40 +00:00
|
|
|
inputFormatter: [formatters.inputBlockNumberFormatter, function (val) { return !!val; }],
|
2015-03-23 14:08:09 +00:00
|
|
|
outputFormatter: formatters.outputBlockFormatter
|
2015-03-22 17:12:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var getUncle = new Method({
|
2015-03-21 21:57:44 +00:00
|
|
|
name: 'getUncle',
|
|
|
|
call: uncleCall,
|
2015-04-02 15:25:27 +00:00
|
|
|
params: 2,
|
2015-04-22 16:16:40 +00:00
|
|
|
inputFormatter: [formatters.inputBlockNumberFormatter, utils.toHex],
|
2015-03-21 21:57:44 +00:00
|
|
|
outputFormatter: formatters.outputBlockFormatter,
|
2015-03-23 14:08:09 +00:00
|
|
|
|
2015-03-22 17:12:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var getCompilers = new Method({
|
2015-03-21 21:57:44 +00:00
|
|
|
name: 'getCompilers',
|
2015-03-22 17:12:52 +00:00
|
|
|
call: 'eth_getCompilers',
|
|
|
|
params: 0
|
|
|
|
});
|
|
|
|
|
2015-03-24 08:42:09 +00:00
|
|
|
var getBlockTransactionCount = new Method({
|
2015-03-21 21:57:44 +00:00
|
|
|
name: 'getBlockTransactionCount',
|
|
|
|
call: getBlockTransactionCountCall,
|
2015-03-22 17:12:52 +00:00
|
|
|
params: 1,
|
2015-04-02 14:30:40 +00:00
|
|
|
inputFormatter: [formatters.inputBlockNumberFormatter],
|
2015-03-23 15:42:09 +00:00
|
|
|
outputFormatter: utils.toDecimal
|
2015-03-22 17:12:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var getBlockUncleCount = new Method({
|
2015-03-21 21:57:44 +00:00
|
|
|
name: 'getBlockUncleCount',
|
|
|
|
call: uncleCountCall,
|
2015-03-22 17:12:52 +00:00
|
|
|
params: 1,
|
2015-04-02 14:30:40 +00:00
|
|
|
inputFormatter: [formatters.inputBlockNumberFormatter],
|
2015-03-23 15:42:09 +00:00
|
|
|
outputFormatter: utils.toDecimal
|
2015-03-22 17:12:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var getTransaction = new Method({
|
2015-03-21 21:57:44 +00:00
|
|
|
name: 'getTransaction',
|
|
|
|
call: 'eth_getTransactionByHash',
|
2015-03-22 17:12:52 +00:00
|
|
|
params: 1,
|
2015-03-21 21:57:44 +00:00
|
|
|
outputFormatter: formatters.outputTransactionFormatter
|
2015-03-22 17:12:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var getTransactionFromBlock = new Method({
|
2015-03-21 21:57:44 +00:00
|
|
|
name: 'getTransactionFromBlock',
|
|
|
|
call: transactionFromBlockCall,
|
2015-03-22 17:12:52 +00:00
|
|
|
params: 2,
|
2015-04-22 16:16:40 +00:00
|
|
|
inputFormatter: [formatters.inputBlockNumberFormatter, utils.toHex],
|
2015-03-23 15:42:09 +00:00
|
|
|
outputFormatter: formatters.outputTransactionFormatter
|
2015-03-22 17:12:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var getTransactionCount = new Method({
|
2015-03-21 21:57:44 +00:00
|
|
|
name: 'getTransactionCount',
|
|
|
|
call: 'eth_getTransactionCount',
|
2015-03-22 17:12:52 +00:00
|
|
|
params: 2,
|
2015-03-23 15:42:09 +00:00
|
|
|
inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter],
|
2015-03-21 21:57:44 +00:00
|
|
|
outputFormatter: utils.toDecimal
|
2015-03-22 17:12:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var sendTransaction = new Method({
|
2015-03-21 21:57:44 +00:00
|
|
|
name: 'sendTransaction',
|
|
|
|
call: 'eth_sendTransaction',
|
2015-03-22 17:12:52 +00:00
|
|
|
params: 1,
|
2015-03-23 15:42:09 +00:00
|
|
|
inputFormatter: [formatters.inputTransactionFormatter]
|
2015-03-22 17:12:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var call = new Method({
|
2015-03-21 21:57:44 +00:00
|
|
|
name: 'call',
|
|
|
|
call: 'eth_call',
|
2015-03-22 17:12:52 +00:00
|
|
|
params: 2,
|
2015-03-27 13:53:47 +00:00
|
|
|
inputFormatter: [formatters.inputTransactionFormatter, formatters.inputDefaultBlockNumberFormatter]
|
2015-03-22 17:12:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var compileSolidity = new Method({
|
2015-03-21 21:57:44 +00:00
|
|
|
name: 'compile.solidity',
|
2015-03-22 17:12:52 +00:00
|
|
|
call: 'eth_compileSolidity',
|
|
|
|
params: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
var compileLLL = new Method({
|
2015-03-21 21:57:44 +00:00
|
|
|
name: 'compile.lll',
|
|
|
|
call: 'eth_compileLLL',
|
2015-03-23 15:50:13 +00:00
|
|
|
params: 1
|
2015-03-22 17:12:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var compileSerpent = new Method({
|
2015-03-21 21:57:44 +00:00
|
|
|
name: 'compile.serpent',
|
|
|
|
call: 'eth_compileSerpent',
|
2015-03-23 15:50:13 +00:00
|
|
|
params: 1
|
2015-03-22 17:12:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var methods = [
|
|
|
|
getBalance,
|
|
|
|
getStorageAt,
|
|
|
|
getCode,
|
|
|
|
getBlock,
|
|
|
|
getUncle,
|
|
|
|
getCompilers,
|
2015-03-24 08:42:09 +00:00
|
|
|
getBlockTransactionCount,
|
2015-03-22 17:12:52 +00:00
|
|
|
getBlockUncleCount,
|
|
|
|
getTransaction,
|
|
|
|
getTransactionFromBlock,
|
|
|
|
getTransactionCount,
|
|
|
|
call,
|
|
|
|
sendTransaction,
|
|
|
|
compileSolidity,
|
|
|
|
compileLLL,
|
|
|
|
compileSerpent,
|
2015-02-19 17:37:09 +00:00
|
|
|
];
|
2015-02-05 22:37:30 +00:00
|
|
|
|
|
|
|
/// @returns an array of objects describing web3.eth api properties
|
2015-03-23 17:26:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-02-19 17:37:09 +00:00
|
|
|
var properties = [
|
2015-03-23 17:26:48 +00:00
|
|
|
new Property({
|
|
|
|
name: 'coinbase',
|
|
|
|
getter: 'eth_coinbase'
|
|
|
|
}),
|
|
|
|
new Property({
|
|
|
|
name: 'mining',
|
|
|
|
getter: 'eth_mining'
|
|
|
|
}),
|
2015-04-28 21:24:33 +00:00
|
|
|
new Property({
|
|
|
|
name: 'hashrate',
|
|
|
|
getter: 'eth_hashrate',
|
|
|
|
outputFormatter: utils.toDecimal
|
|
|
|
}),
|
2015-03-23 17:26:48 +00:00
|
|
|
new Property({
|
|
|
|
name: 'gasPrice',
|
|
|
|
getter: 'eth_gasPrice',
|
2015-04-08 13:40:52 +00:00
|
|
|
outputFormatter: formatters.outputBigNumberFormatter
|
2015-03-23 17:26:48 +00:00
|
|
|
}),
|
|
|
|
new Property({
|
|
|
|
name: 'accounts',
|
|
|
|
getter: 'eth_accounts'
|
|
|
|
}),
|
|
|
|
new Property({
|
|
|
|
name: 'blockNumber',
|
|
|
|
getter: 'eth_blockNumber',
|
|
|
|
outputFormatter: utils.toDecimal
|
|
|
|
})
|
2015-02-19 17:37:09 +00:00
|
|
|
];
|
|
|
|
|
2015-02-05 22:37:30 +00:00
|
|
|
module.exports = {
|
|
|
|
methods: methods,
|
|
|
|
properties: properties
|
|
|
|
};
|
|
|
|
|