Skip to content

Commit

Permalink
Merge pull request #85 from 00labs/fix-upscale
Browse files Browse the repository at this point in the history
Fix upscale to handle overflow
  • Loading branch information
mliu authored Nov 21, 2023
2 parents a6fe4a6 + 8485c51 commit 89c34c5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/huma-shared/src/utils/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const upScale = <T = string>(
return num as T
}
const result = BigNumber.isBigNumber(num)
? num.mul(10 ** decimals!)
? num.mul(BigNumber.from(10).pow(decimals!))
: Number(num) * 10 ** decimals!
if (typeof num === 'string') {
return String(result) as T
Expand Down
2 changes: 2 additions & 0 deletions packages/huma-shared/src/utils/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ export const PoolContractMap: PoolContractMapType = {
abi: HDT_ABI,
},
extra: {
borrower: '0xea57a8a51377752ffddaa3db4d13ce8f97677f2d',
rwrUploader: '0xea57a8a51377752ffddaa3db4d13ce8f97677f2d',
disableBorrow: true,
detailsPage: true,
},
Expand Down
8 changes: 8 additions & 0 deletions packages/huma-shared/tests/utils/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ describe('upScale', () => {
expect(upScale('123.45', 2)).toBe('12345')
expect(upScale(123.45, 2)).toBe(12345)
})

it('returns the upscaled number if the input is a valid number and decimals would cause overflow', () => {
expect(
upScale<BigNumber>(BigNumber.from(10), 18).eq(
BigNumber.from('10000000000000000000'),
),
).toBe(true)
})
})

describe('toBigNumber', () => {
Expand Down

0 comments on commit 89c34c5

Please sign in to comment.