Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

Commit

Permalink
Merge 378b918 into c4b8f0d
Browse files Browse the repository at this point in the history
  • Loading branch information
Velenir committed Sep 25, 2020
2 parents c4b8f0d + 378b918 commit eb9420a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gnosis.pm/dex-js",
"version": "0.7.0",
"version": "0.7.1-rc.0",
"description": "Gnosis Protocol JS integration: utils, contracts and other goodies",
"license": "MIT",
"bugs": {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,12 @@ export function parseAmount(amountFmt: string, amountPrecision: number): BN | nu
return null
}
const adjustedAmount = adjustPrecision(amountFmt, amountPrecision)
const groups = /^(\d+)(?:\.(\d+))?$/.exec(adjustedAmount)
const groups = /^(\d*)(?:\.(\d+))?$/.exec(adjustedAmount)
if (groups) {
const [, integerPart, decimalPart = ''] = groups
const decimalBN = new BN(decimalPart.padEnd(amountPrecision, '0'))
const factor = TEN.pow(new BN(amountPrecision))
return new BN(integerPart).mul(factor).add(decimalBN)
return new BN(integerPart || 0).mul(factor).add(decimalBN)
} else {
return null
}
Expand Down
12 changes: 12 additions & 0 deletions test/utils/format/adjustPrecision.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ describe('over precision', () => {
})
})

describe('.<decimal> numbers', () => {
test('with decimals', () => {
expect(adjustPrecision('.1', 3)).toBe('.1')
})
test('truncating', () => {
expect(adjustPrecision('.2345', 3)).toBe('.234')
})
test('zero padding', () => {
expect(adjustPrecision('.00000000', 2)).toBe('.00')
})
})

describe('null values', () => {
test('empty string', () => {
expect(adjustPrecision('', 2)).toBe('')
Expand Down
14 changes: 14 additions & 0 deletions test/utils/format/parseAmount.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ describe('Exact decimal amounts', () => {
})
})

describe('.<decimal> amounts', () => {
test('.5 Ether', async () => {
expect(parseAmount('.5', DEFAULT_PRECISION)).toEqual(new BN(toWei(new BN('500'), 'milliether')))
})

test('.234 Ether', async () => {
expect(parseAmount('.234', DEFAULT_PRECISION)).toEqual(new BN(toWei(new BN('234'), 'milliether')))
})

test('1.2345 Ether', async () => {
expect(parseAmount('.2345', DEFAULT_PRECISION)).toEqual(new BN(toWei(new BN('234500'), 'microether')))
})
})

describe('Tokens with precision 6', () => {
test('1 unit', async () => {
expect(parseAmount('1', 6)).toEqual(new BN('1000000'))
Expand Down

0 comments on commit eb9420a

Please sign in to comment.