2017-09-25 02:06:28 +00:00
|
|
|
import Big, { BigNumber } from 'bignumber.js';
|
2017-06-24 18:17:09 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export function toFixedIfLarger(num: number, fixedSize: number = 6): string {
|
|
|
|
return parseFloat(num.toFixed(fixedSize)).toString();
|
2017-06-24 18:17:09 +00:00
|
|
|
}
|
2017-07-13 21:02:39 +00:00
|
|
|
|
2017-07-22 21:24:03 +00:00
|
|
|
export function combineAndUpper(...args: string[]) {
|
|
|
|
return args.reduce((acc, item) => acc.concat(item.toUpperCase()), '');
|
|
|
|
}
|
|
|
|
|
2017-07-13 21:02:39 +00:00
|
|
|
// Use in place of angular number filter
|
2017-09-25 02:06:28 +00:00
|
|
|
export function formatNumber(num: BigNumber, digits: number = 3): string {
|
|
|
|
const parts = num.toFixed(digits).split('.');
|
2017-07-27 23:21:50 +00:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
2017-07-13 21:02:39 +00:00
|
|
|
}
|
2017-07-27 23:21:50 +00:00
|
|
|
|
|
|
|
// Commafy the whole numbers
|
2017-07-13 21:02:39 +00:00
|
|
|
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
|
|
|
|
|
|
return parts.join('.');
|
|
|
|
}
|
2017-08-08 03:45:08 +00:00
|
|
|
|
|
|
|
// TODO: Comment up this function to make it clear what's happening here.
|
2017-09-25 02:06:28 +00:00
|
|
|
export function formatGasLimit(
|
|
|
|
limit: BigNumber,
|
|
|
|
transactionUnit: string = 'ether'
|
|
|
|
) {
|
2017-08-08 03:45:08 +00:00
|
|
|
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;
|
|
|
|
}
|