-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TOK-525: number formatter #446
Open
jurajpiar
wants to merge
8
commits into
RootstockCollective:develop
Choose a base branch
from
jurajpiar:TOK-525/number_formatter
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+642
−115
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
030fe66
feat(cr): number formatter
jurajpiar 97498eb
feat(cr): use new number formatter
jurajpiar 70e418a
allows iterative execution
jurajpiar fbfc30c
adds further tests and improvements
jurajpiar 8d8b37b
formats tables as per req
jurajpiar 736ceae
fix: change rif decimals to 2
jurajpiar 990d23a
fix: format metrics use symbol
franciscotobar 851d4eb
test(formatMetrics): unit tests
franciscotobar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
85 changes: 38 additions & 47 deletions
85
src/app/collective-rewards/rewards/utils/formatMetrics.test.ts
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 |
---|---|---|
@@ -1,64 +1,55 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { formatMetrics, formatOnchainFraction } from './formatMetrics' | ||
import { describe, it, expect, test } from 'vitest' | ||
import { formatMetrics } from './formatMetrics' | ||
import { formatUnits, parseEther } from 'viem' | ||
|
||
describe('formatOnchainFraction', () => { | ||
it('should format onchain fraction correctly with default decimals', () => { | ||
const amount = BigInt('1000000000000000000') | ||
const result = formatOnchainFraction(amount) | ||
expect(result).toBe('1') | ||
}) | ||
const formatFiatAmount = (amount: number, currency: string) => `= ${currency} ${amount}` | ||
|
||
it('should format onchain fraction correctly with custom display decimals', () => { | ||
const amount = BigInt('1234567890000000000') | ||
const result = formatOnchainFraction(amount, 3) | ||
expect(result).toBe('1.234') | ||
}) | ||
const formatAmount = (amount: number, symbol: string) => `${amount} ${symbol}` | ||
|
||
it('should round to the lower number', () => { | ||
const amount = BigInt('1237567890000000000') | ||
const result = formatOnchainFraction(amount, 2) | ||
expect(result).toBe('1.23') | ||
}) | ||
describe('formatMetrics', () => { | ||
it('should return `fiatAmount` equals to 0 if `amount` is zero', () => { | ||
const result = formatMetrics(0, 10, 'RIF', 'USD') | ||
|
||
it('should format onchain fraction correctly with custom decimals', () => { | ||
const amount = BigInt('123456789') | ||
const result = formatOnchainFraction(amount, 2, 9) | ||
expect(result).toBe('0.12') | ||
expect(result.fiatAmount).toBe(formatFiatAmount(0, 'USD')) | ||
}) | ||
it('should return `fiatAmount` equals to 0 if `price` is zero', () => { | ||
const result = formatMetrics(10, 0, 'RIF', 'USD') | ||
|
||
it('should handle zero amount correctly', () => { | ||
const amount = BigInt('0') | ||
const result = formatOnchainFraction(amount) | ||
expect(result).toBe('0') | ||
expect(result.fiatAmount).toBe(formatFiatAmount(0, 'USD')) | ||
}) | ||
it('should return `amount` equals to 0 if `amount` is zero ', () => { | ||
const result = formatMetrics(0, 10, 'RIF', 'USD') | ||
|
||
it('should handle numbers without decimals', () => { | ||
const amount = BigInt('9000000000000000000') | ||
const result = formatOnchainFraction(amount) | ||
expect(result).toBe('9') | ||
expect(result.amount).toBe(formatAmount(0, 'RIF')) | ||
}) | ||
it('should accept value as different data types(amount)', () => { | ||
const valueAsNumber = 1.23456789 | ||
const valueAsBigInt = 1234567890000000000n | ||
|
||
it('should handle numbers without decimals and more than 1 character', () => { | ||
const amount = BigInt('90000000000000000000') | ||
const result = formatOnchainFraction(amount) | ||
expect(result).toBe('90') | ||
}) | ||
const resultBigInt = formatMetrics(valueAsBigInt, 10, 'RIF', 'USD') | ||
const resultNumber = formatMetrics(valueAsNumber, 10, 'RIF', 'USD') | ||
|
||
it('should handle numbers with decimals and 3 characters', () => { | ||
const amount = BigInt('900561000000000000000') | ||
const result = formatOnchainFraction(amount) | ||
expect(result).toBe('900.56') | ||
expect(resultBigInt.amount).toBe(formatAmount(1.23, 'RIF')) | ||
expect(resultBigInt.fiatAmount).toBe(formatFiatAmount(12.346, 'USD')) | ||
expect(resultNumber.amount).toBe(formatAmount(1.23, 'RIF')) | ||
expect(resultNumber.fiatAmount).toBe(formatFiatAmount(12.346, 'USD')) | ||
}) | ||
it('should accept value as different data types(price)', () => { | ||
const resultBigInt = formatMetrics(1.23456789, 10n, 'RIF', 'USD') | ||
const resultNumber = formatMetrics(1.23456789, 10, 'RIF', 'USD') | ||
|
||
it('should handle numbers with decimals and 3 characters', () => { | ||
const amount = BigInt('9000127000000000000000') | ||
const result = formatOnchainFraction(amount) | ||
expect(result).toBe('9000.12') | ||
expect(resultBigInt.amount).toBe(formatAmount(1.23, 'RIF')) | ||
expect(resultBigInt.fiatAmount).toBe(formatFiatAmount(12.346, 'USD')) | ||
expect(resultNumber.amount).toBe(formatAmount(1.23, 'RIF')) | ||
expect(resultNumber.fiatAmount).toBe(formatFiatAmount(12.346, 'USD')) | ||
}) | ||
it.only('should format RIF properly', () => { | ||
const result = formatMetrics(parseEther('10'), 10, 'RIF', 'USD') | ||
|
||
it('should handle very big numbers', () => { | ||
const amount = BigInt('987654321123456789012300000000000000') | ||
const result = formatOnchainFraction(amount) | ||
expect(result).toBe('987654321123456789.01') | ||
expect(result.amount).toBe(formatAmount(10, 'RIF')) | ||
expect(result.fiatAmount).toBe(formatFiatAmount(100, 'USD')) | ||
}) | ||
it('should format RBTC properly', () => {}) | ||
it('should apply format properly', () => {}) | ||
it('should format currency properly', () => {}) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started with the creation of the tests, but I found out the following challenge that we may need to change or not.
If I do
formatCurrency(10,USD)
it returns 0.If I want to represent a 10 I need to do
formatCurrency(10.000000000000000000,USD)
orformatCurrency(10000000000000000000,USD)
.Im not sure if this is the way that it should work but its confusing.