web3.js/lib/web3/formatters.js

299 lines
8.5 KiB
JavaScript
Raw Normal View History

2015-03-08 18:31:08 +01:00
/*
2015-10-08 15:34:07 +08:00
This file is part of web3.js.
2015-03-08 18:31:08 +01:00
2015-10-08 15:34:07 +08:00
web3.js is free software: you can redistribute it and/or modify
2015-03-08 18:31:08 +01:00
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.
2015-10-08 15:34:07 +08:00
web3.js is distributed in the hope that it will be useful,
2015-03-08 18:31:08 +01:00
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
2015-10-08 15:34:07 +08:00
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
2015-03-08 18:31:08 +01:00
*/
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 17:11:19 +01:00
var config = require('../utils/config');
2015-08-08 20:34:12 +02:00
var Iban = require('./iban');
2015-03-08 18:31:08 +01:00
/**
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 16:42:09 +01:00
* @method outputBigNumberFormatter
2015-03-08 18:31:08 +01:00
* @param {String|Number|BigNumber}
* @returns {BigNumber} object
*/
2015-03-23 16:42:09 +01:00
var outputBigNumberFormatter = function (number) {
2015-03-23 13:42:36 +01:00
return utils.toBigNumber(number);
};
2015-03-30 09:24:00 +02:00
var isPredefinedBlockNumber = function (blockNumber) {
return blockNumber === 'latest' || blockNumber === 'pending' || blockNumber === 'earliest';
};
2015-03-23 16:42:09 +01:00
var inputDefaultBlockNumberFormatter = function (blockNumber) {
if (blockNumber === undefined) {
return config.defaultBlock;
}
return inputBlockNumberFormatter(blockNumber);
};
var inputBlockNumberFormatter = function (blockNumber) {
if (blockNumber === undefined) {
return undefined;
2015-03-30 09:24:00 +02:00
} else if (isPredefinedBlockNumber(blockNumber)) {
return blockNumber;
}
return utils.toHex(blockNumber);
2015-03-08 18:31:08 +01:00
};
2015-08-08 21:22:01 +02:00
/**
* Formats the input of a transaction and converts all values to HEX
*
* @method inputCallFormatter
* @param {Object} transaction options
* @returns object
*/
var inputCallFormatter = function (options){
options.from = options.from || config.defaultAccount;
if (options.from) {
options.from = inputAddressFormatter(options.from);
}
if (options.to) { // it might be contract creation
options.to = inputAddressFormatter(options.to);
}
['gasPrice', 'gas', 'value', 'nonce'].filter(function (key) {
return options[key] !== undefined;
}).forEach(function(key){
options[key] = utils.fromDecimal(options[key]);
});
return options;
};
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
options.from = options.from || config.defaultAccount;
2015-08-08 21:22:01 +02:00
options.from = inputAddressFormatter(options.from);
2015-08-08 21:22:01 +02:00
if (options.to) { // it might be contract creation
options.to = inputAddressFormatter(options.to);
2015-03-08 18:31:08 +01:00
}
['gasPrice', 'gas', 'value', 'nonce'].filter(function (key) {
2015-03-31 09:34:06 +02:00
return options[key] !== undefined;
}).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
2015-07-06 10:19:42 +02:00
* @param {Object} tx
* @returns {Object}
2015-03-08 18:31:08 +01:00
*/
var outputTransactionFormatter = function (tx){
2015-06-08 17:06:20 +02:00
if(tx.blockNumber !== null)
tx.blockNumber = utils.toDecimal(tx.blockNumber);
if(tx.transactionIndex !== null)
tx.transactionIndex = utils.toDecimal(tx.transactionIndex);
2015-04-20 16:31:40 +02:00
tx.nonce = utils.toDecimal(tx.nonce);
2015-03-08 18:31:08 +01:00
tx.gas = utils.toDecimal(tx.gas);
tx.gasPrice = utils.toBigNumber(tx.gasPrice);
tx.value = utils.toBigNumber(tx.value);
return tx;
};
2015-07-06 10:19:42 +02:00
/**
* Formats the output of a transaction receipt to its proper values
*
* @method outputTransactionReceiptFormatter
* @param {Object} receipt
* @returns {Object}
*/
var outputTransactionReceiptFormatter = function (receipt){
if(receipt.blockNumber !== null)
receipt.blockNumber = utils.toDecimal(receipt.blockNumber);
if(receipt.transactionIndex !== null)
receipt.transactionIndex = utils.toDecimal(receipt.transactionIndex);
receipt.cumulativeGasUsed = utils.toDecimal(receipt.cumulativeGasUsed);
receipt.gasUsed = utils.toDecimal(receipt.gasUsed);
if(utils.isArray(receipt.logs)) {
receipt.logs = receipt.logs.map(function(log){
return outputLogFormatter(log);
});
}
return receipt;
};
2015-03-08 18:31:08 +01:00
/**
* Formats the output of a block to its proper values
*
* @method outputBlockFormatter
2015-07-06 10:19:42 +02:00
* @param {Object} block
* @returns {Object}
2015-03-08 18:31:08 +01:00
*/
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);
if(block.number !== null)
block.number = utils.toDecimal(block.number);
2015-03-08 18:31:08 +01:00
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
*/
2015-03-28 15:15:26 +01:00
var outputLogFormatter = function(log) {
if(log.blockNumber !== null)
log.blockNumber = utils.toDecimal(log.blockNumber);
if(log.transactionIndex !== null)
log.transactionIndex = utils.toDecimal(log.transactionIndex);
if(log.logIndex !== null)
log.logIndex = utils.toDecimal(log.logIndex);
2015-03-10 17:07:16 +01:00
2015-03-08 18:31:08 +01:00
return log;
};
/**
* Formats the input of a whisper post and converts all values to HEX
*
* @method inputPostFormatter
* @param {Object} transaction object
* @returns {Object}
*/
2015-03-28 15:15:26 +01:00
var inputPostFormatter = function(post) {
2015-03-08 18:31:08 +01:00
// post.payload = utils.toHex(post.payload);
2015-03-09 15:09:21 +01:00
post.ttl = utils.fromDecimal(post.ttl);
2015-04-16 12:22:06 +02:00
post.workToProve = utils.fromDecimal(post.workToProve);
2015-03-10 17:27:36 +01:00
post.priority = utils.fromDecimal(post.priority);
2015-03-08 18:31:08 +01:00
2015-04-16 12:22:06 +02:00
// fallback
if (!utils.isArray(post.topics)) {
post.topics = post.topics ? [post.topics] : [];
2015-03-23 15:08:09 +01:00
}
2015-03-08 18:31:08 +01:00
// format the following options
// post.topics = post.topics.map(function(topic){
// return utils.fromUtf8(topic);
// });
2015-03-08 18:31:08 +01:00
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);
// post.payloadRaw = post.payload;
// post.payload = utils.toAscii(post.payload);
2015-03-08 18:31:08 +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
if (!post.topics) {
post.topics = [];
}
// post.topics = post.topics.map(function(topic){
// return utils.toAscii(topic);
// });
2015-03-08 18:31:08 +01:00
return post;
};
2015-08-08 20:34:12 +02:00
var inputAddressFormatter = function (address) {
var iban = new Iban(address);
if (iban.isValid() && iban.isDirect()) {
return '0x' + iban.address();
} else if (utils.isStrictAddress(address)) {
return address;
} else if (utils.isAddress(address)) {
return '0x' + address;
}
throw 'invalid address';
};
var outputSyncingFormatter = function(result) {
result.startingBlock = utils.toDecimal(result.startingBlock);
result.currentBlock = utils.toDecimal(result.currentBlock);
result.highestBlock = utils.toDecimal(result.highestBlock);
return result;
};
2015-03-08 18:31:08 +01:00
module.exports = {
2015-03-23 16:42:09 +01:00
inputDefaultBlockNumberFormatter: inputDefaultBlockNumberFormatter,
inputBlockNumberFormatter: inputBlockNumberFormatter,
2015-08-08 21:22:01 +02:00
inputCallFormatter: inputCallFormatter,
2015-03-08 18:31:08 +01:00
inputTransactionFormatter: inputTransactionFormatter,
2015-08-08 20:34:12 +02:00
inputAddressFormatter: inputAddressFormatter,
2015-03-22 09:43:07 +01:00
inputPostFormatter: inputPostFormatter,
2015-03-23 16:42:09 +01:00
outputBigNumberFormatter: outputBigNumberFormatter,
2015-03-22 09:43:07 +01:00
outputTransactionFormatter: outputTransactionFormatter,
2015-07-06 10:19:42 +02:00
outputTransactionReceiptFormatter: outputTransactionReceiptFormatter,
2015-03-08 18:31:08 +01:00
outputBlockFormatter: outputBlockFormatter,
outputLogFormatter: outputLogFormatter,
outputPostFormatter: outputPostFormatter,
outputSyncingFormatter: outputSyncingFormatter
2015-03-08 18:31:08 +01:00
};