MyCrypto/common/utils/formatters.js

54 lines
1.6 KiB
JavaScript
Raw Normal View History

// @flow
import Big from 'bignumber.js';
export function toFixedIfLarger(number: number, fixedSize: number = 6): string {
2017-06-24 12:53:53 -07:00
return parseFloat(number.toFixed(fixedSize)).toString();
}
export function combineAndUpper(...args: string[]) {
return args.reduce((acc, item) => acc.concat(item.toUpperCase()), '');
}
// Use in place of angular number filter
export function formatNumber(number: Big, digits: number = 3): string {
let parts = number.toFixed(digits).split('.');
// Remove trailing zeroes on decimal (If there is a decimal)
if (parts[1]) {
parts[1] = parts[1].replace(/0+$/, '');
// If there's nothing left, remove decimal altogether
if (!parts[1]) {
parts.pop();
}
}
// Commafy the whole numbers
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
return parts.join('.');
}
// TODO: Comment up this function to make it clear what's happening here.
export function formatGasLimit(limit: Big, transactionUnit: string = 'ether') {
let limitStr = limit.toString();
// I'm guessing this is some known off-by-one-error from the node?
// 21k is only the limit for ethereum though, so make sure they're
// sending ether if we're going to fix it for them.
if (limitStr === '21001' && transactionUnit === 'ether') {
limitStr = '21000';
}
// If they've exceeded the gas limit per block, make it -1
// TODO: Explain why not cap at limit?
// TODO: Make this dynamic, potentially. Would require promisifying this fn.
// TODO: Figure out if this is only true for ether. Do other currencies have
// this limit?
if (limit.gte(4000000)) {
limitStr = '-1';
}
return limitStr;
}