MyCrypto/common/libs/units.ts
HenryNguyen5 5d4b36d453 Migrate to Typescript (#224)
* Refactor babel/types

* Refactor entry point

* Refactor actions

* Refactor api

* Full project refactor -- Broad type fixing sweep

* - completely fix merge conflicts
- handle various type errors

* Add tslint to package.json

* Dependency cleanup

* Fix module resolution

* Work on type definitions for untyped libs

* progress commit

* Add more definition typing

* various type additions

* Add unit types

* Fix sagaiterator  + unit types

* various types added

* additional type additions

* Fix typing on Sagas

* remove flowfixmes; swap translate for translateRaw

* Get rid of contracts - awaiting Henry's contract PR

* Remove contracts from routing

* Fix most of actions/reducers

* refactor actions directory structure

* fix reducer action type imports

* Fix most of type errors pre-actions refactor

* fix action creator imports in containers

* Refactor more

* Refactor index of actions

* fix action imports; use module level index export

* package-lock.json updated

* Use action types in props

* Type up action creators

* Fix most of connect errors

* Typefixing progress

* More types

* Fix run-time errors

* Caching improvements for webpack

* Remove path resolve from webpack

* Update non-breaking packages to latest version

* Fix token typing

* Remove unused color code

* Fix wallet decrypt dispatch

* Set redux-form related props/functions to ANY, since we're stripping it out later on

* Revert BigNumber.js package changes

* Extend window to custom object for Perf

* Format Navigation

* Typecase keystore errors as any (since we shouldnt touch this)

* Push wallet context fix

* - find/replace value->payload in swap
- properly type swap state properties
- extract inline reducer into reducer function

* - type local storage retrieved items as generic

* - bind all RPCClient methods with fat arrow

* - reformat

* Change to enums for constants

* Change state into any

* Fix swap errors

* ensure that seconds are passed into state as integers

* Fix rest of errors

* use parseInt explicitly instead of type coercion

* Fix derivation-checker, remove flow command, add tslint command, add tslint-react, tell travis to use tslint instead of flow.

* Whoops, remove those tests.

* Remove unsupported (yet) config option.

* Fix precommit to target ts and tsx files.

* Fix some errors, ignore some silly rules.

* Revert jest to v19, use ts-jest and make all tests typescript. Fixes all but one.

* Get rid of saga tests

* Fix tslint errors
2017-09-24 19:06:28 -07:00

106 lines
2.4 KiB
TypeScript

import Big, { BigNumber } from 'bignumber.js';
import { Token } from 'config/data';
const Units = {
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'
};
export type TUnit = typeof Units;
export type UnitKey = keyof TUnit;
class Unit {
public unit: UnitKey;
public amount: BigNumber;
constructor(amount: BigNumber, unit: UnitKey) {
this.unit = unit;
this.amount = amount;
}
public toString(base?: number) {
return this.amount.toString(base);
}
public toPrecision(precision?: number) {
return this.amount.toPrecision(precision);
}
public toWei(): Wei {
return new Wei(toWei(this.amount, this.unit));
}
public toGWei(): GWei {
return new GWei(toUnit(this.amount, this.unit, 'gwei'));
}
public toEther(): Ether {
return new Ether(toUnit(this.amount, this.unit, 'ether'));
}
}
// tslint:disable:max-classes-per-file
export class Ether extends Unit {
constructor(amount: BigNumber | number | string) {
super(new Big(amount), 'ether');
}
}
export class Wei extends Unit {
constructor(amount: BigNumber | number | string) {
super(new Big(amount), 'wei');
}
}
export class GWei extends Unit {
constructor(amount: BigNumber | number | string) {
super(new Big(amount), 'gwei');
}
}
function getValueOfUnit(unit: UnitKey) {
return new Big(Units[unit]);
}
export function toWei(num: BigNumber, unit: UnitKey): BigNumber {
return num.times(getValueOfUnit(unit));
}
export function toUnit(
num: BigNumber,
fromUnit: UnitKey,
convertToUnit: UnitKey
): BigNumber {
return toWei(num, fromUnit).div(getValueOfUnit(convertToUnit));
}
export function toTokenUnit(num: BigNumber, token: Token): BigNumber {
return num.times(new Big(10).pow(token.decimal));
}
export function toTokenDisplay(num: BigNumber, token: Token): BigNumber {
return num.times(new Big(10).pow(-token.decimal));
}