web3.js/lib/web3/formatters.js

206 lines
5.5 KiB
JavaScript
Raw Normal View History

2015-03-08 18:31:08 +01: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 09:43:07 +01:00
/**
* @file formatters.js
* @author Marek Kotewicz <marek@ethdev.com>
* @author Fabian Vogelsteller <fabian@ethdev.com>
2015-03-08 18:31:08 +01:00
* @date 2015
*/
var utils = require('../utils/utils');
/**
2015-03-23 13:42:36 +01:00
* Should the format output to a big number
2015-03-08 18:31:08 +01:00
*
2015-03-23 13:42:36 +01:00
* @method outputNumberFormatter
2015-03-08 18:31:08 +01:00
* @param {String|Number|BigNumber}
* @returns {BigNumber} object
*/
2015-03-23 13:42:36 +01:00
var outputNumberFormatter = function (number) {
return utils.toBigNumber(number);
};
var inputBlockNumberFormatter = function (blockNumber) {
2015-03-23 14:23:45 +01:00
return blockNumber === undefined ? "pending" : utils.toHex(blockNumber); // instead use default block number here
2015-03-08 18:31:08 +01:00
};
/**
* Formats the input of a transaction and converts all values to HEX
*
* @method inputTransactionFormatter
* @param {Object} transaction options
* @returns object
*/
2015-03-23 13:42:36 +01:00
var inputTransactionFormatter = function (options){
2015-03-08 18:31:08 +01:00
// make code -> data
if (options.code) {
options.data = options.code;
delete options.code;
}
['gasPrice', 'gas', 'value'].forEach(function(key){
2015-03-09 15:09:21 +01:00
options[key] = utils.fromDecimal(options[key]);
2015-03-08 18:31:08 +01:00
});
2015-03-23 13:42:36 +01:00
return options;
2015-03-08 18:31:08 +01:00
};
/**
* Formats the output of a transaction to its proper values
*
* @method outputTransactionFormatter
* @param {Object} transaction
* @returns {Object} transaction
*/
var outputTransactionFormatter = function (tx){
tx.gas = utils.toDecimal(tx.gas);
tx.gasPrice = utils.toBigNumber(tx.gasPrice);
tx.value = utils.toBigNumber(tx.value);
return tx;
};
2015-03-09 20:06:54 +01:00
/**
* Formats the input of a call and converts all values to HEX
*
* @method inputCallFormatter
* @param {Object} transaction options
* @returns object
*/
2015-03-23 13:42:36 +01:00
var inputCallFormatter = function (options){
2015-03-09 20:06:54 +01:00
// make code -> data
if (options.code) {
options.data = options.code;
delete options.code;
}
2015-03-23 13:42:36 +01:00
return options;
2015-03-09 20:06:54 +01:00
};
2015-03-08 18:31:08 +01:00
/**
* Formats the output of a block to its proper values
*
* @method outputBlockFormatter
* @param {Object} block object
* @returns {Object} block object
*/
2015-03-22 09:43:07 +01:00
var outputBlockFormatter = function(block) {
2015-03-08 18:31:08 +01:00
// transform to number
block.gasLimit = utils.toDecimal(block.gasLimit);
block.gasUsed = utils.toDecimal(block.gasUsed);
block.size = utils.toDecimal(block.size);
block.timestamp = utils.toDecimal(block.timestamp);
block.number = utils.toDecimal(block.number);
block.minGasPrice = utils.toBigNumber(block.minGasPrice);
block.difficulty = utils.toBigNumber(block.difficulty);
block.totalDifficulty = utils.toBigNumber(block.totalDifficulty);
2015-03-23 15:08:09 +01:00
if (utils.isArray(block.transactions)) {
2015-03-10 11:03:03 +01:00
block.transactions.forEach(function(item){
if(!utils.isString(item))
return outputTransactionFormatter(item);
});
}
2015-03-08 18:31:08 +01:00
return block;
};
/**
* Formats the output of a log
*
* @method outputLogFormatter
* @param {Object} log object
* @returns {Object} log
*/
var outputLogFormatter = function(log){
2015-03-10 17:07:16 +01:00
log.blockNumber = utils.toDecimal(log.blockNumber);
log.transactionIndex = utils.toDecimal(log.transactionIndex);
log.logIndex = utils.toDecimal(log.logIndex);
2015-03-08 18:31:08 +01:00
return log;
};
2015-03-09 15:09:21 +01:00
2015-03-08 18:31:08 +01:00
/**
* Formats the input of a whisper post and converts all values to HEX
*
* @method inputPostFormatter
* @param {Object} transaction object
* @returns {Object}
*/
2015-03-23 13:42:36 +01:00
var inputPostFormatter = function(post){
2015-03-08 18:31:08 +01:00
2015-03-09 15:09:21 +01:00
post.payload = utils.toHex(post.payload);
post.ttl = utils.fromDecimal(post.ttl);
2015-03-10 17:27:36 +01:00
post.priority = utils.fromDecimal(post.priority);
2015-03-08 18:31:08 +01:00
2015-03-23 15:08:09 +01:00
if(!utils.isArray(post.topics)) {
2015-03-10 16:22:16 +01:00
post.topics = [post.topics];
2015-03-23 15:08:09 +01:00
}
2015-03-08 18:31:08 +01:00
// format the following options
2015-03-10 16:22:16 +01:00
post.topics = post.topics.map(function(topic){
2015-03-08 18:31:08 +01:00
return utils.fromAscii(topic);
});
2015-03-23 13:42:36 +01:00
return post;
2015-03-08 18:31:08 +01:00
};
/**
* Formats the output of a received post message
*
* @method outputPostFormatter
* @param {Object}
* @returns {Object}
*/
var outputPostFormatter = function(post){
post.expiry = utils.toDecimal(post.expiry);
post.sent = utils.toDecimal(post.sent);
post.ttl = utils.toDecimal(post.ttl);
2015-03-10 17:27:36 +01:00
post.workProved = utils.toDecimal(post.workProved);
2015-03-08 18:31:08 +01:00
post.payloadRaw = post.payload;
post.payload = utils.toAscii(post.payload);
2015-03-23 15:08:09 +01:00
if (utils.isJson(post.payload)) {
post.payload = JSON.parse(post.payload);
2015-03-08 18:31:08 +01:00
}
// format the following options
2015-03-10 16:22:16 +01:00
post.topics = post.topics.map(function(topic){
2015-03-08 18:31:08 +01:00
return utils.toAscii(topic);
});
return post;
};
module.exports = {
2015-03-23 13:42:36 +01:00
inputBlockNumberFormatter: inputBlockNumberFormatter,
2015-03-08 18:31:08 +01:00
inputTransactionFormatter: inputTransactionFormatter,
2015-03-09 20:06:54 +01:00
inputCallFormatter: inputCallFormatter,
2015-03-22 09:43:07 +01:00
inputPostFormatter: inputPostFormatter,
2015-03-23 13:42:36 +01:00
outputNumberFormatter: outputNumberFormatter,
2015-03-22 09:43:07 +01:00
outputTransactionFormatter: outputTransactionFormatter,
2015-03-08 18:31:08 +01:00
outputBlockFormatter: outputBlockFormatter,
outputLogFormatter: outputLogFormatter,
outputPostFormatter: outputPostFormatter
};