(Fix) Error when using up to the max amount of decimals for token transfer (#1576)
This commit is contained in:
parent
0c67c48648
commit
325adda00d
|
@ -203,4 +203,32 @@ describe('isERC721Contract', () => {
|
|||
// then
|
||||
expect(txValue).toEqual(expectedResult)
|
||||
})
|
||||
|
||||
it('It should return the right conversion from token to unit with exceeding decimals', () => {
|
||||
// given
|
||||
const decimals = Number(18)
|
||||
|
||||
const expectedResult = '333333333333333398'
|
||||
const VALUE = '0.33333333333333339878798333'
|
||||
|
||||
// when
|
||||
const txValue = toTokenUnit(VALUE, decimals)
|
||||
|
||||
// then
|
||||
expect(txValue).toEqual(expectedResult)
|
||||
})
|
||||
|
||||
it('It should return the right conversion from token to unit with exact decimals', () => {
|
||||
// given
|
||||
const decimals = Number(18)
|
||||
|
||||
const expectedResult = '333333333333333399'
|
||||
const VALUE = '0.333333333333333399'
|
||||
|
||||
// when
|
||||
const txValue = toTokenUnit(VALUE, decimals)
|
||||
|
||||
// then
|
||||
expect(txValue).toEqual(expectedResult)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -7,13 +7,5 @@ export const humanReadableValue = (value: number | string, decimals = 18): strin
|
|||
export const fromTokenUnit = (amount: number | string, decimals: string | number): string =>
|
||||
new BigNumber(amount).times(`1e-${decimals}`).toFixed()
|
||||
|
||||
export const toTokenUnit = (amount: number | string, decimals: string | number): string => {
|
||||
const amountBN = new BigNumber(amount).times(`1e${decimals}`)
|
||||
const [, amountDecimalPlaces] = amount.toString().split('.')
|
||||
|
||||
if (amountDecimalPlaces?.length >= +decimals) {
|
||||
return amountBN.toFixed(+decimals, BigNumber.ROUND_DOWN)
|
||||
}
|
||||
|
||||
return amountBN.toFixed()
|
||||
}
|
||||
export const toTokenUnit = (amount: number | string, decimals: string | number): string =>
|
||||
new BigNumber(amount).times(`1e${decimals}`).toFixed(0, BigNumber.ROUND_DOWN)
|
||||
|
|
Loading…
Reference in New Issue