Skip to content
Open
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
14 changes: 9 additions & 5 deletions frontend/src/lib/components/forms/createMarket.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import { Input } from '$lib/components/ui/input';
import { Textarea } from '$lib/components/ui/textarea';
import { Checkbox } from '$lib/components/ui/checkbox';
import { roundToTenth } from '$lib/components/marketDataUtils';
import { roundToTenth, roundToHundredth } from '$lib/components/marketDataUtils';
import { websocket_api } from 'schema-js';
import { protoSuperForm } from './protoSuperForm';
import type { Snippet } from 'svelte';
import X from '@lucide/svelte/icons/x';
import Plus from '@lucide/svelte/icons/plus';

const isSudo = $derived(serverState.isAdmin && serverState.sudoEnabled);
const settlementStep = $derived(isSudo ? '0.01' : '0.1');
const roundSettlement = $derived(isSudo ? roundToHundredth : roundToTenth);

interface Props {
children: Snippet;
onclick?: () => void;
Expand Down Expand Up @@ -221,10 +225,10 @@
{...props}
type="number"
max="1000000000000"
step="0.1"
step={settlementStep}
bind:value={$formData.minSettlement}
onblur={() => {
$formData.minSettlement = roundToTenth(
$formData.minSettlement = roundSettlement(
$formData.minSettlement as unknown as number
);
}}
Expand All @@ -241,10 +245,10 @@
{...props}
type="number"
max="1000000000000"
step="0.1"
step={settlementStep}
bind:value={$formData.maxSettlement}
onblur={() => {
$formData.maxSettlement = roundToTenth(
$formData.maxSettlement = roundSettlement(
$formData.maxSettlement as unknown as number
);
}}
Expand Down
16 changes: 10 additions & 6 deletions frontend/src/lib/components/forms/makeTransfer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
import { Input } from '$lib/components/ui/input';
import * as Popover from '$lib/components/ui/popover';
import * as Tooltip from '$lib/components/ui/tooltip';
import { roundToTenth } from '$lib/components/marketDataUtils';
import { roundToTenth, roundToTenThousandth } from '$lib/components/marketDataUtils';
import { cn, formatNumber } from '$lib/utils';
import ChevronsUpDown from '@lucide/svelte/icons/chevrons-up-down';
import { websocket_api } from 'schema-js';
import { tick } from 'svelte';
import type { Snippet } from 'svelte';
import { protoSuperForm } from './protoSuperForm';

const isSudo = $derived(serverState.isAdmin && serverState.sudoEnabled);
const transferStep = $derived(isSudo ? '0.0001' : '0.1');
const roundTransfer = $derived(isSudo ? roundToTenThousandth : roundToTenth);

interface Props {
children?: Snippet;
onclick?: () => void;
Expand Down Expand Up @@ -379,13 +383,13 @@
<Input
{...props}
type="number"
min="0.1"
min={isSudo ? '0.0001' : '0.1'}
max={maxAmount}
step="0.1"
step={transferStep}
class="w-32"
bind:value={$formData.amount}
onblur={() => {
$formData.amount = roundToTenth($formData.amount as unknown as number);
$formData.amount = roundTransfer($formData.amount as unknown as number);
}}
/>
{#if selectedCount > 1}
Expand All @@ -394,7 +398,7 @@
</div>
<p class="text-sm text-muted-foreground">
Total: <span class={exceedsBalance ? 'text-destructive line-through' : ''}
>📎 {formatNumber(roundToTenth(totalAmount))}</span
>📎 {formatNumber(roundTransfer(totalAmount))}</span
>
</p>
{#if exceedsBalance}
Expand Down Expand Up @@ -438,7 +442,7 @@
<li>{dest.name}</li>
{/each}
</ul>
Total: 📎 {formatNumber(roundToTenth(confirmAmount * confirmDestinations.length))}
Total: 📎 {formatNumber(roundTransfer(confirmAmount * confirmDestinations.length))}
{/if}
</AlertDialog.Description>
</AlertDialog.Header>
Expand Down
16 changes: 10 additions & 6 deletions frontend/src/lib/components/forms/settleMarket.svelte
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
<script lang="ts">
import { sendClientMessage } from '$lib/api.svelte';
import { sendClientMessage, serverState } from '$lib/api.svelte';
import * as AlertDialog from '$lib/components/ui/alert-dialog';
import * as Dialog from '$lib/components/ui/dialog';
import * as Form from '$lib/components/ui/form';
import * as Popover from '$lib/components/ui/popover';
import { Input } from '$lib/components/ui/input';
import { Button } from '$lib/components/ui/button';
import { roundToTenth } from '$lib/components/marketDataUtils';
import { roundToTenth, roundToHundredth } from '$lib/components/marketDataUtils';
import ChevronRight from '@lucide/svelte/icons/chevron-right';
import ChevronDown from '@lucide/svelte/icons/chevron-down';
import { websocket_api } from 'schema-js';
import { protoSuperForm } from './protoSuperForm';
import { formatMarketName } from '$lib/utils';

const isSudo = $derived(serverState.isAdmin && serverState.sudoEnabled);
const settleStep = $derived(isSudo ? '0.01' : '0.1');
const roundSettle = $derived(isSudo ? roundToHundredth : roundToTenth);

interface Props {
id: number | null | undefined;
name: string | null | undefined;
Expand Down Expand Up @@ -92,13 +96,13 @@
type="number"
min={minSettlement}
max={maxSettlement}
step="0.1"
step={settleStep}
placeholder="Settle Price"
aria-label="Settle Price"
class="h-10 w-32"
bind:value={$formData.settlePrice}
onblur={() => {
$formData.settlePrice = roundToTenth($formData.settlePrice as unknown as number);
$formData.settlePrice = roundSettle($formData.settlePrice as unknown as number);
}}
/>
{/snippet}
Expand Down Expand Up @@ -133,13 +137,13 @@
type="number"
min={minSettlement}
max={maxSettlement}
step="0.1"
step={settleStep}
placeholder="Settle Price"
aria-label="Settle Price"
class="h-10"
bind:value={$formData.settlePrice}
onblur={() => {
$formData.settlePrice = roundToTenth($formData.settlePrice as unknown as number);
$formData.settlePrice = roundSettle($formData.settlePrice as unknown as number);
}}
/>
{/snippet}
Expand Down
28 changes: 28 additions & 0 deletions frontend/src/lib/components/marketDataUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,34 @@ export function roundToTenth<T extends number | string | null | undefined>(value
return Number(tenthFormatter.format(numeric));
}

const hundredthFormatter = new Intl.NumberFormat(undefined, {
maximumFractionDigits: 2,
useGrouping: false
});

export function roundToHundredth<T extends number | string | null | undefined>(
value: T
): T | number {
if (value === '' || value === null || value === undefined) return value;
const numeric = typeof value === 'number' ? value : Number(value);
if (!Number.isFinite(numeric)) return value;
return Number(hundredthFormatter.format(numeric));
}

const tenThousandthFormatter = new Intl.NumberFormat(undefined, {
maximumFractionDigits: 4,
useGrouping: false
});

export function roundToTenThousandth<T extends number | string | null | undefined>(
value: T
): T | number {
if (value === '' || value === null || value === undefined) return value;
const numeric = typeof value === 'number' ? value : Number(value);
if (!Number.isFinite(numeric)) return value;
return Number(tenThousandthFormatter.format(numeric));
}

export function roundToWhole<T extends number | string | null | undefined>(value: T): T | number {
if (value === '' || value === null || value === undefined) return value;
const numeric = typeof value === 'number' ? value : Number(value);
Expand Down