-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:
formatDecimals
precision (#912)
- Loading branch information
Showing
13 changed files
with
311 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@coinbase/onchainkit": patch | ||
--- | ||
|
||
**fix**: formatDecimals precision by @0xAlec #912 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
/** | ||
* @vitest-environment node | ||
*/ | ||
import { fromReadableAmount } from './fromReadableAmount'; | ||
|
||
describe('fromReadableAmount', () => { | ||
it('converts whole numbers correctly', () => { | ||
expect(fromReadableAmount('100', 18)).toBe('100000000000000000000'); | ||
expect(fromReadableAmount('1', 6)).toBe('1000000'); | ||
}); | ||
|
||
it('handles decimals correctly', () => { | ||
expect(fromReadableAmount('1.5', 18)).toBe('1500000000000000000'); | ||
expect(fromReadableAmount('0.1', 6)).toBe('100000'); | ||
}); | ||
|
||
it('handles very small numbers', () => { | ||
expect(fromReadableAmount('0.000000000000000001', 18)).toBe('1'); | ||
expect(fromReadableAmount('0.000001', 6)).toBe('1'); | ||
}); | ||
|
||
it('handles numbers with fewer digits than decimals', () => { | ||
expect(fromReadableAmount('0.1', 18)).toBe('100000000000000000'); | ||
expect(fromReadableAmount('0.000001', 18)).toBe('1000000000000'); | ||
}); | ||
|
||
it('handles zero correctly', () => { | ||
expect(fromReadableAmount('0', 18)).toBe('0'); | ||
expect(fromReadableAmount('0.0', 18)).toBe('0'); | ||
}); | ||
|
||
it('handles large numbers correctly', () => { | ||
expect(fromReadableAmount('1000000000000000000', 18)).toBe( | ||
'1000000000000000000000000000000000000', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function fromReadableAmount(amount: string, decimals: number): string { | ||
const [wholePart, fractionalPart = ''] = amount.split('.'); | ||
const paddedFractionalPart = fractionalPart.padEnd(decimals, '0'); | ||
const trimmedFractionalPart = paddedFractionalPart.slice(0, decimals); | ||
return ( | ||
BigInt(wholePart + trimmedFractionalPart) * | ||
10n ** BigInt(decimals - trimmedFractionalPart.length) | ||
).toString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.