From 12aa9a29e4367ab79d42cc00f97955bc1c099d24 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 25 May 2017 13:19:46 +0200 Subject: Add toUnitAmount and toBaseUnitAmount static helpers and tests --- package.json | 1 + src/ts/0x.js.ts | 26 ++++++++++++++++++++++++++ src/ts/globals.d.ts | 13 +++++++++++++ test/0x.js.ts | 25 ++++++++++++++++++++++++- 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 950ad0d38..63ad8d61d 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "awesome-typescript-loader": "^3.1.3", "bignumber.js": "^4.0.2", "chai": "^3.5.0", + "chai-bignumber": "^2.0.0", "mocha": "^3.4.1", "npm-run-all": "^4.0.2", "nyc": "^10.3.2", diff --git a/src/ts/0x.js.ts b/src/ts/0x.js.ts index 1cc2d8b44..b71c691d5 100644 --- a/src/ts/0x.js.ts +++ b/src/ts/0x.js.ts @@ -50,4 +50,30 @@ export class ZeroEx { const salt = randomNumber.times(factor).round(); return salt; } + /* + * A unit amount is defined as the amount of a token above the specified decimal places (integer part). + * E.g: If a currency has 18 decimal places, 1e18 or one quintillion of the currency is equivalent + * to 1 unit. + */ + public static toUnitAmount(amount: BigNumber.BigNumber, decimals: number): BigNumber.BigNumber { + assert.isBigNumber('amount', amount); + assert.isNumber('decimals', decimals); + + const aUnit = new BigNumber(10).pow(decimals); + const unit = amount.div(aUnit); + return unit; + } + /* + * A baseUnit is defined as the smallest denomination of a token. An amount expressed in baseUnits + * is the amount expressed in the smallest denomination. + * E.g: 1 unit of a token with 18 decimal places is expressed in baseUnits as 1000000000000000000 + */ + public static toBaseUnitAmount(amount: BigNumber.BigNumber, decimals: number): BigNumber.BigNumber { + assert.isBigNumber('amount', amount); + assert.isNumber('decimals', decimals); + + const unit = new BigNumber(10).pow(decimals); + const baseUnitAmount = amount.times(unit); + return baseUnitAmount; + } } diff --git a/src/ts/globals.d.ts b/src/ts/globals.d.ts index 659638a1e..974b4b26d 100644 --- a/src/ts/globals.d.ts +++ b/src/ts/globals.d.ts @@ -1,3 +1,5 @@ +declare module 'chai-bignumber'; + declare type ETHPublicKey = string; declare type ETHAddressHex = string; declare type ETHAddressBuff = Buffer; @@ -6,6 +8,17 @@ declare interface Schema { id: string; } +// HACK: In order to merge the bignumber declaration added by chai-bignumber to the chai Assertion +// interface we must use `namespace` as the Chai definitelyTyped definition does. Since we otherwise +// disallow `namespace`, we disable tslint for the following. +/* tslint:disable */ +declare namespace Chai { + interface Assertion { + bignumber: Assertion; + } +} +/* tslint:enable */ + declare module 'ethereumjs-util' { const toBuffer: (data: string) => Buffer; const hashPersonalMessage: (msg: Buffer) => Buffer; diff --git a/test/0x.js.ts b/test/0x.js.ts index 09266018b..46e5c4da5 100644 --- a/test/0x.js.ts +++ b/test/0x.js.ts @@ -1,7 +1,12 @@ import {ZeroEx} from '../src/ts/0x.js'; -import {expect} from 'chai'; +import * as chai from 'chai'; import 'mocha'; import * as BigNumber from 'bignumber.js'; +import ChaiBigNumber = require('chai-bignumber'); + +// Use BigNumber chai add-on +chai.use(ChaiBigNumber()); +const expect = chai.expect; describe('ZeroEx library', () => { describe('#isValidSignature', () => { @@ -82,4 +87,22 @@ describe('ZeroEx library', () => { expect(salt.lessThan(twoPow256)).to.be.true; }); }); + describe('#toUnitAmount', () => { + it('Should return the expected unit amount for the decimals passed in', () => { + const baseUnitAmount = new BigNumber(1000000000); + const decimals = 6; + const unitAmount = ZeroEx.toUnitAmount(baseUnitAmount, decimals); + const expectedUnitAmount = new BigNumber(1000); + expect(unitAmount).to.be.bignumber.equal(expectedUnitAmount); + }); + }); + describe('#toBaseUnitAmount', () => { + it('Should return the expected base unit amount for the decimals passed in', () => { + const unitAmount = new BigNumber(1000); + const decimals = 6; + const baseUnitAmount = ZeroEx.toBaseUnitAmount(unitAmount, decimals); + const expectedUnitAmount = new BigNumber(1000000000); + expect(baseUnitAmount).to.be.bignumber.equal(expectedUnitAmount); + }); + }); }); -- cgit v1.2.3