2018-09-14 14:54:27 -07:00
|
|
|
// From https://github.com/MyCryptoHQ/MyCrypto/blob/develop/common/libs/units.ts
|
|
|
|
import BN from 'bn.js';
|
|
|
|
import { stripHexPrefix } from 'utils/formatters';
|
|
|
|
|
2018-12-27 09:41:26 -08:00
|
|
|
export const ZCASH_DECIMAL = 8;
|
2018-09-14 14:54:27 -07:00
|
|
|
export const Units = {
|
2018-12-27 09:41:26 -08:00
|
|
|
zat: '1',
|
|
|
|
zcash: '100000000',
|
2018-09-14 14:54:27 -07:00
|
|
|
};
|
2018-12-27 09:41:26 -08:00
|
|
|
|
|
|
|
export type Zat = BN;
|
2019-12-03 16:02:39 -08:00
|
|
|
export type Usd = BN;
|
2018-12-27 09:41:26 -08:00
|
|
|
export type UnitKey = keyof typeof Units;
|
|
|
|
|
|
|
|
export const handleValues = (input: string | BN) => {
|
2018-09-14 14:54:27 -07:00
|
|
|
if (typeof input === 'string') {
|
|
|
|
return input.startsWith('0x') ? new BN(stripHexPrefix(input), 16) : new BN(input);
|
|
|
|
}
|
|
|
|
if (typeof input === 'number') {
|
|
|
|
return new BN(input);
|
|
|
|
}
|
|
|
|
if (BN.isBN(input)) {
|
|
|
|
return input;
|
|
|
|
} else {
|
|
|
|
throw Error('unsupported value conversion');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-27 09:41:26 -08:00
|
|
|
export const Zat = (input: string | BN): Zat => handleValues(input);
|
2018-09-14 14:54:27 -07:00
|
|
|
|
|
|
|
const stripRightZeros = (str: string) => {
|
|
|
|
const strippedStr = str.replace(/0+$/, '');
|
|
|
|
return strippedStr === '' ? null : strippedStr;
|
|
|
|
};
|
|
|
|
|
2018-12-27 09:41:26 -08:00
|
|
|
export const baseToConvertedUnit = (value: string, decimal: number) => {
|
2018-09-14 14:54:27 -07:00
|
|
|
if (decimal === 0) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
const paddedValue = value.padStart(decimal + 1, '0'); // 0.1 ==>
|
|
|
|
const integerPart = paddedValue.slice(0, -decimal);
|
|
|
|
const fractionPart = stripRightZeros(paddedValue.slice(-decimal));
|
|
|
|
return fractionPart ? `${integerPart}.${fractionPart}` : `${integerPart}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
const convertedToBaseUnit = (value: string, decimal: number) => {
|
|
|
|
if (decimal === 0) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
const [integerPart, fractionPart = ''] = value.split('.');
|
|
|
|
const paddedFraction = fractionPart.padEnd(decimal, '0');
|
|
|
|
return `${integerPart}${paddedFraction}`;
|
|
|
|
};
|
|
|
|
|
2018-12-27 09:41:26 -08:00
|
|
|
export const fromZat = (zat: Zat) => {
|
|
|
|
return baseToConvertedUnit(zat.toString(), ZCASH_DECIMAL);
|
2018-09-14 14:54:27 -07:00
|
|
|
};
|
|
|
|
|
2018-12-27 09:41:26 -08:00
|
|
|
export const toZat = (value: string | number): Zat => {
|
2018-09-20 11:58:47 -07:00
|
|
|
value = value.toString();
|
2018-12-27 09:41:26 -08:00
|
|
|
const zat = convertedToBaseUnit(value, ZCASH_DECIMAL);
|
|
|
|
return Zat(zat);
|
2018-09-14 14:54:27 -07:00
|
|
|
};
|
|
|
|
|
2019-12-03 16:02:39 -08:00
|
|
|
export const toUsd = (value: string | number): Usd => {
|
|
|
|
value = value.toString();
|
|
|
|
const hasDecimal = value.indexOf('.') !== -1;
|
|
|
|
|
|
|
|
// decimals aren't allowed for proposal targets,
|
|
|
|
// but remove decimal if it exists
|
|
|
|
value = hasDecimal ? value.split('.')[0] : value;
|
|
|
|
return new BN(value, 10);
|
|
|
|
};
|
|
|
|
|
2018-12-27 09:41:26 -08:00
|
|
|
export const getDecimalFromUnitKey = (key: UnitKey) => Units[key].length - 1;
|