2017-11-12 19:45:52 +00:00
|
|
|
import { Wei } from 'libs/units';
|
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-11-12 19:45:52 +00:00
|
|
|
const toFixed = (num: string, digits: number = 3) => {
|
|
|
|
const [integerPart, fractionPart = ''] = num.split('.');
|
|
|
|
if (fractionPart.length === digits) {
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
if (fractionPart.length < digits) {
|
|
|
|
return `${integerPart}.${fractionPart.padEnd(digits, '0')}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
let decimalPoint = integerPart.length;
|
|
|
|
|
|
|
|
const formattedFraction = fractionPart.slice(0, digits);
|
|
|
|
|
|
|
|
const integerArr = `${integerPart}${formattedFraction}`
|
|
|
|
.split('')
|
|
|
|
.map(str => +str);
|
|
|
|
|
|
|
|
let carryOver = Math.floor((+fractionPart[digits] + 5) / 10);
|
|
|
|
|
|
|
|
// grade school addition / rounding
|
|
|
|
|
|
|
|
for (let i = integerArr.length - 1; i >= 0; i--) {
|
|
|
|
const currVal = integerArr[i] + carryOver;
|
|
|
|
const newVal = currVal % 10;
|
|
|
|
carryOver = Math.floor(currVal / 10);
|
|
|
|
integerArr[i] = newVal;
|
|
|
|
if (i === 0 && carryOver > 0) {
|
|
|
|
integerArr.unshift(0);
|
|
|
|
decimalPoint++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const strArr = integerArr.map(n => n.toString());
|
|
|
|
|
|
|
|
strArr.splice(decimalPoint, 0, '.');
|
|
|
|
|
|
|
|
if (strArr[strArr.length - 1] === '.') {
|
|
|
|
strArr.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
return strArr.join('');
|
|
|
|
};
|
|
|
|
|
2017-07-13 21:02:39 +00:00
|
|
|
// Use in place of angular number filter
|
2017-11-12 19:45:52 +00:00
|
|
|
export function formatNumber(num: string, digits?: number): string {
|
|
|
|
const parts = toFixed(num, 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-11-12 19:45:52 +00:00
|
|
|
export function formatGasLimit(limit: Wei, 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?
|
2017-11-12 19:45:52 +00:00
|
|
|
if (limit.gten(4000000)) {
|
2017-08-08 03:45:08 +00:00
|
|
|
limitStr = '-1';
|
|
|
|
}
|
|
|
|
|
|
|
|
return limitStr;
|
|
|
|
}
|