/* 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 . */ /** * @file formatters.js * @author Marek Kotewicz * @author Fabian Vogelsteller * @date 2015 */ var utils = require('../utils/utils'); var config = require('../utils/config'); /** * Should the format output to a big number * * @method outputBigNumberFormatter * @param {String|Number|BigNumber} * @returns {BigNumber} object */ var outputBigNumberFormatter = function (number) { return utils.toBigNumber(number); }; var isPredefinedBlockNumber = function (blockNumber) { return blockNumber === 'latest' || blockNumber === 'pending' || blockNumber === 'earliest'; }; var inputDefaultBlockNumberFormatter = function (blockNumber) { if (blockNumber === undefined) { return config.defaultBlock; } return inputBlockNumberFormatter(blockNumber); }; var inputBlockNumberFormatter = function (blockNumber) { if (blockNumber === undefined) { return undefined; } else if (isPredefinedBlockNumber(blockNumber)) { return blockNumber; } return utils.toHex(blockNumber); }; /** * Formats the input of a transaction and converts all values to HEX * * @method inputTransactionFormatter * @param {Object} transaction options * @returns object */ var inputTransactionFormatter = function (options){ options.from = options.from || config.defaultAccount; // make code -> data if (options.code) { options.data = options.code; delete options.code; } ['gasPrice', 'gas', 'value'].filter(function (key) { return options[key] !== undefined; }).forEach(function(key){ options[key] = utils.fromDecimal(options[key]); }); return options; }; /** * Formats the output of a transaction to its proper values * * @method outputTransactionFormatter * @param {Object} transaction * @returns {Object} transaction */ var outputTransactionFormatter = function (tx){ tx.blockNumber = utils.toDecimal(tx.blockNumber); tx.transactionIndex = utils.toDecimal(tx.transactionIndex); tx.nonce = utils.toDecimal(tx.nonce); tx.gas = utils.toDecimal(tx.gas); tx.gasPrice = utils.toBigNumber(tx.gasPrice); tx.value = utils.toBigNumber(tx.value); return tx; }; /** * Formats the output of a block to its proper values * * @method outputBlockFormatter * @param {Object} block object * @returns {Object} block object */ var outputBlockFormatter = function(block) { // 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.difficulty = utils.toBigNumber(block.difficulty); block.totalDifficulty = utils.toBigNumber(block.totalDifficulty); if (utils.isArray(block.transactions)) { block.transactions.forEach(function(item){ if(!utils.isString(item)) return outputTransactionFormatter(item); }); } return block; }; /** * Formats the output of a log * * @method outputLogFormatter * @param {Object} log object * @returns {Object} log */ var outputLogFormatter = function(log) { if (log === null) { // 'pending' && 'latest' filters are nulls return null; } log.blockNumber = utils.toDecimal(log.blockNumber); log.transactionIndex = utils.toDecimal(log.transactionIndex); log.logIndex = utils.toDecimal(log.logIndex); return log; }; /** * Formats the input of a whisper post and converts all values to HEX * * @method inputPostFormatter * @param {Object} transaction object * @returns {Object} */ var inputPostFormatter = function(post) { post.payload = utils.toHex(post.payload); post.ttl = utils.fromDecimal(post.ttl); post.workToProve = utils.fromDecimal(post.workToProve); post.priority = utils.fromDecimal(post.priority); // fallback if (!utils.isArray(post.topics)) { post.topics = post.topics ? [post.topics] : []; } // format the following options post.topics = post.topics.map(function(topic){ return utils.fromAscii(topic); }); return post; }; /** * 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); post.workProved = utils.toDecimal(post.workProved); post.payloadRaw = post.payload; post.payload = utils.toAscii(post.payload); if (utils.isJson(post.payload)) { post.payload = JSON.parse(post.payload); } // format the following options if (!post.topics) { post.topics = []; } post.topics = post.topics.map(function(topic){ return utils.toAscii(topic); }); return post; }; module.exports = { inputDefaultBlockNumberFormatter: inputDefaultBlockNumberFormatter, inputBlockNumberFormatter: inputBlockNumberFormatter, inputTransactionFormatter: inputTransactionFormatter, inputPostFormatter: inputPostFormatter, outputBigNumberFormatter: outputBigNumberFormatter, outputTransactionFormatter: outputTransactionFormatter, outputBlockFormatter: outputBlockFormatter, outputLogFormatter: outputLogFormatter, outputPostFormatter: outputPostFormatter };