Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/wallet/backend/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const urlToPaymentId = (url: string) =>
url.split('/incoming-payments/')[1]

export const transformBalance = (value: number, scale: number): bigint => {
return BigInt(Math.floor(value * 10 ** scale))
return BigInt(Math.floor(Number((value * 10 ** scale).toPrecision(15))))
}

export const applyScale = (
Expand Down
14 changes: 13 additions & 1 deletion packages/wallet/backend/tests/utils/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { applyScale } from '@/utils/helpers'
import { applyScale, transformBalance } from '@/utils/helpers'

describe('Utils Helper Functions', (): void => {
describe('applyScale', (): void => {
Expand All @@ -19,4 +19,16 @@ describe('Utils Helper Functions', (): void => {
expect(applyScale(1000)).toBe(10)
})
})

describe('transformBalance', (): void => {
it('should not lose a cent to floating point error', (): void => {
expect(transformBalance(19.99, 2)).toBe(1999n)
expect(transformBalance(1.13, 2)).toBe(113n)
expect(transformBalance(0.29, 2)).toBe(29n)
})

it('should floor values below the asset scale', (): void => {
expect(transformBalance(19.999, 2)).toBe(1999n)
})
})
})
Loading