Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Oct 23, 2024
1 parent 31f54d7 commit 54d21b3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions tauri-app/src/lib/components/tables/OrderAPY.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { subgraphUrl } from '$lib/stores/settings';
import { TableBodyCell, TableHeadCell } from 'flowbite-svelte';
import ApyTimeFilters from '../charts/APYTimeFilters.svelte';
import { bigintString18ToPercentage } from '$lib/utils/number';
import { bigintStringToPercentage } from '$lib/utils/number';
export let id: string;
Expand All @@ -33,7 +33,7 @@
<svelte:fragment slot="bodyRow" let:item>
<TableBodyCell tdClass="break-all px-4 py-2" data-testid="apy-field">
{item.denominatedApy
? bigintString18ToPercentage(item.denominatedApy.apy, 5) +
? bigintStringToPercentage(item.denominatedApy.apy, 18, 5) +
'% in ' +
(item.denominatedApy.token.symbol ??
item.denominatedApy.token.name ??
Expand Down
4 changes: 2 additions & 2 deletions tauri-app/src/lib/components/tables/OrderAPY.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { mockIPC } from '@tauri-apps/api/mocks';
import type { OrderAPY } from '$lib/typeshare/subgraphTypes';
import { QueryClient } from '@tanstack/svelte-query';
import OrderApy from './OrderAPY.svelte';
import { bigintString18ToPercentage } from '$lib/utils/number';
import { bigintStringToPercentage } from '$lib/utils/number';

vi.mock('$lib/stores/settings', async (importOriginal) => {
const { writable } = await import('svelte/store');
Expand Down Expand Up @@ -73,7 +73,7 @@ test('renders table with correct data', async () => {

// checking
for (let i = 0; i < mockOrderApy.length; i++) {
const display = bigintString18ToPercentage(mockOrderApy[i].denominatedApy!.apy, 5);
const display = bigintStringToPercentage(mockOrderApy[i].denominatedApy!.apy, 18, 5);
expect(rows[i]).toHaveTextContent(display);
}
});
Expand Down
16 changes: 11 additions & 5 deletions tauri-app/src/lib/utils/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ export function bigintToFloat(value: bigint, decimals: number) {
* Converts a bigint string 18point decimals value to a float string, optionally
* keeping the given number of decimals digits after "."
* @param value - The bigint string value
* @param decimalPoint - (optional) the number of digits to keep after "."
* @param valueDecimals - The bigint string value decimals point
* @param decimalPoint - (optional) The number of digits to keep after "." in final result, defaults to valueDecimals
*/
export function bigintString18ToPercentage(value: string, decimalPoint?: number): string {
let valueString = formatUnits(BigInt(value) * 100n, 18);
export function bigintStringToPercentage(
value: string,
valueDecimals: number,
finalDecimalsDigits?: number,
): string {
const finalDecimals = finalDecimalsDigits !== undefined ? finalDecimalsDigits : valueDecimals;
let valueString = formatUnits(BigInt(value) * 100n, valueDecimals);
const index = valueString.indexOf('.');
if (decimalPoint !== undefined && index > -1) {
valueString = valueString.substring(0, decimalPoint === 0 ? index : index + decimalPoint);
if (index > -1) {
valueString = valueString.substring(0, finalDecimals === 0 ? index : index + finalDecimals + 1);
}
return valueString;
}

0 comments on commit 54d21b3

Please sign in to comment.