2017-09-25 02:06:28 +00:00
|
|
|
import Big, { BigNumber } from 'bignumber.js';
|
|
|
|
import { Token } from 'config/data';
|
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-09-25 02:06:28 +00:00
|
|
|
export type TUnit = typeof Units;
|
|
|
|
export type UnitKey = keyof TUnit;
|
2017-07-13 21:02:39 +00:00
|
|
|
|
2017-09-08 19:01:31 +00:00
|
|
|
class Unit {
|
2017-09-25 02:06:28 +00:00
|
|
|
public unit: UnitKey;
|
|
|
|
public amount: BigNumber;
|
2017-09-08 19:01:31 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
constructor(amount: BigNumber, unit: UnitKey) {
|
2017-09-08 19:01:31 +00:00
|
|
|
this.unit = unit;
|
|
|
|
this.amount = amount;
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public toString(base?: number) {
|
2017-09-08 19:01:31 +00:00
|
|
|
return this.amount.toString(base);
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public toPrecision(precision?: number) {
|
2017-09-12 01:29:07 +00:00
|
|
|
return this.amount.toPrecision(precision);
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public toWei(): Wei {
|
2017-09-08 19:01:31 +00:00
|
|
|
return new Wei(toWei(this.amount, this.unit));
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public toGWei(): GWei {
|
2017-09-08 19:01:31 +00:00
|
|
|
return new GWei(toUnit(this.amount, this.unit, 'gwei'));
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public toEther(): Ether {
|
2017-09-08 19:01:31 +00:00
|
|
|
return new Ether(toUnit(this.amount, this.unit, 'ether'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
// tslint:disable:max-classes-per-file
|
2017-09-08 19:01:31 +00:00
|
|
|
export class Ether extends Unit {
|
2017-09-25 02:06:28 +00:00
|
|
|
constructor(amount: BigNumber | number | string) {
|
2017-09-08 19:01:31 +00:00
|
|
|
super(new Big(amount), 'ether');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Wei extends Unit {
|
2017-09-25 02:06:28 +00:00
|
|
|
constructor(amount: BigNumber | number | string) {
|
2017-09-08 19:01:31 +00:00
|
|
|
super(new Big(amount), 'wei');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class GWei extends Unit {
|
2017-09-25 02:06:28 +00:00
|
|
|
constructor(amount: BigNumber | number | string) {
|
2017-09-08 19:01:31 +00:00
|
|
|
super(new Big(amount), 'gwei');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
function getValueOfUnit(unit: UnitKey) {
|
|
|
|
return new Big(Units[unit]);
|
2017-07-13 21:02:39 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export function toWei(num: BigNumber, unit: UnitKey): BigNumber {
|
|
|
|
return num.times(getValueOfUnit(unit));
|
2017-07-13 21:02:39 +00:00
|
|
|
}
|
2017-08-23 06:57:18 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export function toUnit(
|
|
|
|
num: BigNumber,
|
|
|
|
fromUnit: UnitKey,
|
|
|
|
convertToUnit: UnitKey
|
|
|
|
): BigNumber {
|
|
|
|
return toWei(num, fromUnit).div(getValueOfUnit(convertToUnit));
|
2017-08-23 06:57:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export function toTokenUnit(num: BigNumber, token: Token): BigNumber {
|
|
|
|
return num.times(new Big(10).pow(token.decimal));
|
2017-08-23 06:57:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export function toTokenDisplay(num: BigNumber, token: Token): BigNumber {
|
|
|
|
return num.times(new Big(10).pow(-token.decimal));
|
2017-08-23 06:57:18 +00:00
|
|
|
}
|