Commit 518ff797 authored by Noah Zinsmeister's avatar Noah Zinsmeister

add toExact to TokenAmount

parent 1653d121
import invariant from 'tiny-invariant'
import JSBI from 'jsbi'
import _Big from 'big.js'
import toFormat from 'toformat'
import { BigintIsh, Rounding } from '../../types'
import { TEN, SolidityType } from '../../constants'
......@@ -7,6 +9,8 @@ import { parseBigintIsh, validateSolidityTypeInstance } from '../../utils'
import { Token } from '../token'
import { Fraction } from './fraction'
const Big = toFormat(_Big)
export class TokenAmount extends Fraction {
public readonly token: Token
......@@ -49,4 +53,9 @@ export class TokenAmount extends Fraction {
invariant(decimalPlaces <= this.token.decimals, 'DECIMALS')
return super.toFixed(decimalPlaces, format, rounding)
}
public toExact(format: object = { groupSeparator: '' }): string {
Big.DP = this.token.decimals
return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(format)
}
}
......@@ -173,6 +173,12 @@ describe('entities', () => {
}
})
})
it('TokenAmount', () => {
const amount = new TokenAmount(WETH, '1234567000000000000000')
expect(amount.toExact()).toEqual('1234.567')
expect(amount.toExact({ groupSeparator: ',' })).toEqual('1,234.567')
})
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment