2017-11-12 19:45:52 +00:00
|
|
|
import BN from 'bn.js';
|
|
|
|
import { stripHexPrefix } from 'libs/values';
|
|
|
|
|
|
|
|
type UnitKey = keyof typeof Units;
|
|
|
|
type Wei = BN;
|
|
|
|
type TokenValue = BN;
|
2017-07-13 21:02:39 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
const Units = {
|
2017-07-13 21:02:39 +00:00
|
|
|
wei: '1',
|
|
|
|
kwei: '1000',
|
|
|
|
ada: '1000',
|
|
|
|
femtoether: '1000',
|
|
|
|
mwei: '1000000',
|
|
|
|
babbage: '1000000',
|
|
|
|
picoether: '1000000',
|
|
|
|
gwei: '1000000000',
|
|
|
|
shannon: '1000000000',
|
|
|
|
nanoether: '1000000000',
|
|
|
|
nano: '1000000000',
|
|
|
|
szabo: '1000000000000',
|
|
|
|
microether: '1000000000000',
|
|
|
|
micro: '1000000000000',
|
|
|
|
finney: '1000000000000000',
|
|
|
|
milliether: '1000000000000000',
|
|
|
|
milli: '1000000000000000',
|
|
|
|
ether: '1000000000000000000',
|
|
|
|
kether: '1000000000000000000000',
|
|
|
|
grand: '1000000000000000000000',
|
|
|
|
einstein: '1000000000000000000000',
|
|
|
|
mether: '1000000000000000000000000',
|
|
|
|
gether: '1000000000000000000000000000',
|
|
|
|
tether: '1000000000000000000000000000000'
|
|
|
|
};
|
2017-11-12 19:45:52 +00:00
|
|
|
const handleValues = (input: string | BN) => {
|
|
|
|
if (typeof input === 'string') {
|
|
|
|
return input.startsWith('0x')
|
|
|
|
? new BN(stripHexPrefix(input), 16)
|
|
|
|
: new BN(input);
|
2017-09-08 19:01:31 +00:00
|
|
|
}
|
2017-11-12 19:45:52 +00:00
|
|
|
if (typeof input === 'number') {
|
|
|
|
return new BN(input);
|
2017-09-08 19:01:31 +00:00
|
|
|
}
|
2017-11-12 19:45:52 +00:00
|
|
|
if (BN.isBN(input)) {
|
|
|
|
return input;
|
2017-09-12 01:29:07 +00:00
|
|
|
}
|
2017-11-12 19:45:52 +00:00
|
|
|
throw Error('unsupported value conversion');
|
|
|
|
};
|
2017-09-12 01:29:07 +00:00
|
|
|
|
2017-11-12 19:45:52 +00:00
|
|
|
const Wei = (input: string | BN): Wei => handleValues(input);
|
2017-09-08 19:01:31 +00:00
|
|
|
|
2017-11-12 19:45:52 +00:00
|
|
|
const TokenValue = (input: string | BN) => handleValues(input);
|
2017-09-08 19:01:31 +00:00
|
|
|
|
2017-11-12 19:45:52 +00:00
|
|
|
const getDecimal = (key: UnitKey) => Units[key].length - 1;
|
2017-09-08 19:01:31 +00:00
|
|
|
|
2017-11-12 19:45:52 +00:00
|
|
|
const stripRightZeros = (str: string) => {
|
|
|
|
const strippedStr = str.replace(/0+$/, '');
|
|
|
|
return strippedStr === '' ? null : strippedStr;
|
|
|
|
};
|
2017-09-08 19:01:31 +00:00
|
|
|
|
2017-11-12 19:45:52 +00:00
|
|
|
const baseToConvertedUnit = (value: string, decimal: number) => {
|
|
|
|
if (decimal === 0) {
|
|
|
|
return value;
|
2017-09-08 19:01:31 +00:00
|
|
|
}
|
2017-11-12 19:45:52 +00:00
|
|
|
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}`;
|
|
|
|
};
|
2017-09-08 19:01:31 +00:00
|
|
|
|
2017-11-12 19:45:52 +00:00
|
|
|
const convertedToBaseUnit = (value: string, decimal: number) => {
|
|
|
|
if (decimal === 0) {
|
|
|
|
return value;
|
2017-09-08 19:01:31 +00:00
|
|
|
}
|
2017-11-12 19:45:52 +00:00
|
|
|
const [integerPart, fractionPart = ''] = value.split('.');
|
|
|
|
const paddedFraction = fractionPart.padEnd(decimal, '0');
|
|
|
|
return `${integerPart}${paddedFraction}`;
|
|
|
|
};
|
2017-08-23 06:57:18 +00:00
|
|
|
|
2017-11-12 19:45:52 +00:00
|
|
|
const fromWei = (wei: Wei, unit: UnitKey) => {
|
|
|
|
const decimal = getDecimal(unit);
|
|
|
|
return baseToConvertedUnit(wei.toString(), decimal);
|
|
|
|
};
|
2017-08-23 06:57:18 +00:00
|
|
|
|
2017-11-12 19:45:52 +00:00
|
|
|
const toWei = (value: string, decimal: number): Wei => {
|
|
|
|
const wei = convertedToBaseUnit(value, decimal);
|
|
|
|
return Wei(wei);
|
|
|
|
};
|
2017-08-23 06:57:18 +00:00
|
|
|
|
2017-11-12 19:45:52 +00:00
|
|
|
const fromTokenBase = (value: TokenValue, decimal: number) =>
|
|
|
|
baseToConvertedUnit(value.toString(), decimal);
|
|
|
|
|
|
|
|
const toTokenBase = (value: string, decimal: number) =>
|
|
|
|
TokenValue(convertedToBaseUnit(value, decimal));
|
|
|
|
|
|
|
|
export {
|
|
|
|
TokenValue,
|
|
|
|
fromWei,
|
|
|
|
toWei,
|
|
|
|
toTokenBase,
|
|
|
|
fromTokenBase,
|
|
|
|
Wei,
|
|
|
|
getDecimal,
|
|
|
|
UnitKey
|
|
|
|
};
|