diff --git a/src/App.tsx b/src/App.tsx index 78d06961..97481851 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -9,6 +9,7 @@ import EscapeBanner from './components/common/EscapeBanner'; import OfflineBanner from './components/common/OfflineBanner'; import GlobalPauseBanner from './components/common/GlobalPauseBanner'; import PendingTransactionBanner from './components/common/PendingTransactionBanner'; +import PendingFeeChangeBanner from './components/fees/PendingFeeChangeBanner'; import SessionExpiryWatcher from './components/common/SessionExpiryWatcher'; import BadgeAchievementListener from './components/common/BadgeAchievementListener'; import { useContractPausedStore } from './hooks/useContractPausedStore'; @@ -67,6 +68,7 @@ function App() { + = ({ @@ -31,8 +36,10 @@ const SwapPanel: React.FC = ({ onAmountChange, defaultSide = 'buy', creatorId, + oracleAsset = 'XLM', }) => { const [side, setSide] = useState(defaultSide); + const oracle = useOracleStatus(oracleAsset); const handleSideChange = useCallback((value: string) => { setSide(value as SwapSide); @@ -54,6 +61,45 @@ const SwapPanel: React.FC = ({ className="space-y-4 rounded-xl border border-white/10 bg-white/[0.02] p-4" data-testid="swap-panel" > +
+ Trade Execution + +
+ + {/* Fallback Banner - shows warning but does NOT block actions */} + {oracle.isFallback && ( +
+ +
+ + Oracle Fallback Active + + The price feed is currently running on its fallback source ({oracle.sourceName}). Swaps remain active but may experience price deviation. +
+
+ )} + + {/* Stale Price Banner - warns and explains disabled actions */} + {oracle.isStale && ( +
+ +
+ + Oracle Feed Stale + + {oracle.staleReason || 'Price feed is stale. Trading is paused until fresh prices are reported.'} +
+
+ )} + = ({ {creatorId && ( - +
+ + {oracle.isStale && ( +

+ Action disabled: Price is stale. Please wait for an oracle update. +

+ )} +
)} ); diff --git a/src/components/__tests__/SwapPanel.oracle.test.tsx b/src/components/__tests__/SwapPanel.oracle.test.tsx new file mode 100644 index 00000000..13d73805 --- /dev/null +++ b/src/components/__tests__/SwapPanel.oracle.test.tsx @@ -0,0 +1,83 @@ +import { render, screen, waitFor } from '@testing-library/react'; +import { describe, expect, it, beforeEach, vi } from 'vitest'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import SwapPanel from '../SwapPanel'; +import { oracleService } from '@/services/oracle.service'; + +function renderWithQuery(ui: React.ReactElement) { + const queryClient = new QueryClient({ + defaultOptions: { queries: { retry: false } }, + }); + return render({ui}); +} + +describe('SwapPanel - Oracle Integration', () => { + beforeEach(() => { + oracleService.reset(); + }); + + it('shows fallback banner when oracle source is fallback but does not block swap action', async () => { + oracleService.setMockStatus('XLM', { + status: 'fallback', + source: 'fallback', + sourceName: 'Secondary Feed', + updatedAt: Date.now() - 5_000, + }); + + renderWithQuery( + + ); + + await waitFor(() => { + expect(screen.getByTestId('oracle-fallback-banner')).toBeInTheDocument(); + }); + + // Fallback warning displayed + expect(screen.getByTestId('oracle-fallback-banner')).toHaveTextContent(/Oracle Fallback Active/i); + + // Action button remains enabled! + const submitButton = screen.getByTestId('swap-submit'); + expect(submitButton).not.toBeDisabled(); + }); + + it('shows stale banner and disables swap action with explanation when price is stale', async () => { + oracleService.setMockStatus('XLM', { + status: 'stale', + source: 'primary', + updatedAt: Date.now() - 600_000, // 10 minutes ago + }); + + renderWithQuery( + + ); + + await waitFor(() => { + expect(screen.getByTestId('oracle-stale-banner')).toBeInTheDocument(); + }); + + expect(screen.getByTestId('oracle-stale-banner')).toHaveTextContent(/Oracle Feed Stale/i); + + // Action is blocked and disabled + const submitButton = screen.getByTestId('swap-submit'); + expect(submitButton).toBeDisabled(); + + // Explanation reason is rendered + expect(screen.getByTestId('swap-stale-disabled-reason')).toHaveTextContent( + /Action disabled: Price is stale/i + ); + }); +}); diff --git a/src/components/admin/FeeChangeAdminPanel.tsx b/src/components/admin/FeeChangeAdminPanel.tsx new file mode 100644 index 00000000..9b41b5e4 --- /dev/null +++ b/src/components/admin/FeeChangeAdminPanel.tsx @@ -0,0 +1,123 @@ +import { useState } from 'react'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Label } from '@/components/ui/label'; +import { useFeeChangeStore } from '@/hooks/useFeeChange'; +import { FEE_BOUNDS } from '@/constants/fees'; +import { bpsToPercent } from '@/utils/numberFormat.utils'; + +function defaultEta(): string { + const date = new Date(Date.now() + 24 * 3_600_000); + const pad = (value: number) => String(value).padStart(2, '0'); + return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad( + date.getDate() + )}T${pad(date.getHours())}:${pad(date.getMinutes())}`; +} + +export default function FeeChangeAdminPanel() { + const state = useFeeChangeStore(store => store.state); + const queue = useFeeChangeStore(store => store.queue); + const cancel = useFeeChangeStore(store => store.cancel); + const execute = useFeeChangeStore(store => store.execute); + const [newFeeBps, setNewFeeBps] = useState( + String(FEE_BOUNDS.DEFAULT_FEE_BPS) + ); + const [eta, setEta] = useState(defaultEta); + const [error, setError] = useState(null); + + const handleQueue = () => { + try { + queue({ + queuedFeeBps: Number(newFeeBps), + eta: new Date(eta).toISOString(), + }); + setError(null); + } catch (err) { + setError( + err instanceof Error ? err.message : 'Could not queue fee change.' + ); + } + }; + + return ( +
+

Fee changes

+

+ Current fee: {bpsToPercent(state.currentFeeBps)} · Status:{' '} + {state.status} +

+ + {state.status === 'pending' && ( +
+

+ Queued {bpsToPercent(state.queuedFeeBps ?? 0)} for{' '} + {state.eta + ? new Date(state.eta).toLocaleString() + : 'an unset time'} + . +

+
+ + +
+
+ )} + + {state.status !== 'pending' && ( +
+
+ + setNewFeeBps(event.target.value)} + data-testid="fee-change-bps" + /> +
+
+ + setEta(event.target.value)} + data-testid="fee-change-eta-input" + /> +
+
+ +
+
+ )} + + {error && ( +

+ {error} +

+ )} +
+ ); +} diff --git a/src/components/bounties/BountyCard.tsx b/src/components/bounties/BountyCard.tsx new file mode 100644 index 00000000..bd3c0174 --- /dev/null +++ b/src/components/bounties/BountyCard.tsx @@ -0,0 +1,193 @@ +import { Link } from 'react-router'; +import { + CalendarClock, + LockKeyhole, + Trophy, + Users, + Wallet, +} from 'lucide-react'; +import { Button } from '@/components/ui/button'; +import { cn } from '@/lib/utils'; +import type { Bounty } from '@/services/bounty.service'; +import { formatDisplayKeyPrice } from '@/utils/keyPriceDisplay.utils'; + +const CARD = + 'rounded-2xl border border-white/10 bg-white/[0.02] shadow-xl backdrop-blur-md'; + +function formatCountdown(deadline: string, nowMs: number): string { + const remaining = new Date(deadline).getTime() - nowMs; + if (remaining <= 0) return 'Expired'; + + const days = Math.floor(remaining / 86_400_000); + const hours = Math.floor((remaining % 86_400_000) / 3_600_000); + const minutes = Math.floor((remaining % 3_600_000) / 60_000); + + if (days > 0) return `${days}d ${hours}h left`; + if (hours > 0) return `${hours}h ${minutes}m left`; + return `${minutes}m left`; +} + +export interface BountyCardProps { + bounty: Bounty; + nowMs: number; + remainingWinners: number; + awardedCount: number; + isCreator: boolean; + canSubmit: boolean; + eligibilityReason?: string | null; + onSubmit?: (bounty: Bounty) => void; + onReclaim?: (bounty: Bounty) => void; + isReclaiming?: boolean; +} + +export default function BountyCard({ + bounty, + nowMs, + remainingWinners, + awardedCount, + isCreator, + canSubmit, + eligibilityReason, + onSubmit, + onReclaim, + isReclaiming = false, +}: BountyCardProps) { + const expired = new Date(bounty.deadline).getTime() <= nowMs; + const claimable = isCreator && expired && bounty.status === 'open'; + + return ( +
+
+
+ + {bounty.keyName} + +

+ {bounty.title} +

+
+ + {bounty.status} + +
+ +

+ {bounty.description} +

+ +
+
+
+
+
+ {formatDisplayKeyPrice(bounty.rewardStroops)} +
+
+
+
+
+
+ {formatCountdown(bounty.deadline, nowMs)} +
+
+
+
+
+
+ + {remainingWinners} + {' '} + / {bounty.winnersLimit} + + ({awardedCount} awarded) + +
+
+
+
+
+
+ {bounty.holderGated ? ( + + + ) : ( + 'Open to all' + )} +
+
+
+ + {eligibilityReason && ( +

+ {eligibilityReason} +

+ )} + +
+ + Manage submissions + +
+ {claimable && ( + + )} + {onSubmit && bounty.status === 'open' && !expired && ( + + )} +
+
+
+ ); +} diff --git a/src/components/bounties/PostBountyForm.tsx b/src/components/bounties/PostBountyForm.tsx new file mode 100644 index 00000000..2127ab76 --- /dev/null +++ b/src/components/bounties/PostBountyForm.tsx @@ -0,0 +1,209 @@ +import { useState } from 'react'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Label } from '@/components/ui/label'; +import { Textarea } from '@/components/ui/textarea'; +import { Checkbox } from '@/components/ui/checkbox'; +import { STROOPS_PER_XLM } from '@/constants/stellar'; +import type { CreateBountyInput } from '@/services/bounty.service'; + +export interface PostBountyFormProps { + keys: Array<{ id: string; name: string }>; + onSubmit: (input: CreateBountyInput) => void; + isSubmitting?: boolean; +} + +function toDateTimeLocal(daysFromNow: number): string { + const date = new Date(Date.now() + daysFromNow * 86_400_000); + return new Date(date.getTime() - date.getTimezoneOffset() * 60_000) + .toISOString() + .slice(0, 16); +} + +export default function PostBountyForm({ + keys, + onSubmit, + isSubmitting = false, +}: PostBountyFormProps) { + const [keyId, setKeyId] = useState(keys[0]?.id ?? ''); + const [title, setTitle] = useState(''); + const [description, setDescription] = useState(''); + const [rewardXlm, setRewardXlm] = useState(''); + const [deadline, setDeadline] = useState(() => toDateTimeLocal(7)); + const [winnersLimit, setWinnersLimit] = useState('1'); + const [holderGated, setHolderGated] = useState(true); + const [errors, setErrors] = useState>({}); + + const handleSubmit = (event: React.FormEvent) => { + event.preventDefault(); + const nextErrors: Record = {}; + const reward = Number(rewardXlm); + const winners = Number(winnersLimit); + const deadlineMs = new Date(deadline).getTime(); + + if (!title.trim()) nextErrors.title = 'Give the bounty a title.'; + if (!Number.isFinite(reward) || reward <= 0) + nextErrors.reward = 'Reward must be greater than zero.'; + if (!Number.isFinite(deadlineMs) || deadlineMs <= Date.now()) + nextErrors.deadline = 'Deadline must be in the future.'; + if (!Number.isInteger(winners) || winners < 1) + nextErrors.winners = 'Winner count must be at least one.'; + + setErrors(nextErrors); + if (Object.keys(nextErrors).length > 0) return; + + onSubmit({ + keyId, + keyName: keys.find(key => key.id === keyId)?.name ?? keyId, + title, + description, + rewardStroops: Math.round(reward * STROOPS_PER_XLM), + deadline: new Date(deadlineMs).toISOString(), + winnersLimit: winners, + holderGated, + }); + + setTitle(''); + setDescription(''); + setRewardXlm(''); + }; + + return ( +
+

Post a bounty

+

+ Rewards are escrowed until you award winners or reclaim after the + deadline. +

+ +
+
+ + +
+
+ + setRewardXlm(event.target.value)} + data-testid="post-bounty-reward" + aria-invalid={Boolean(errors.reward)} + /> + {errors.reward && ( +

+ {errors.reward} +

+ )} +
+
+ +
+ + setTitle(event.target.value)} + placeholder="Design a launch banner" + data-testid="post-bounty-title" + aria-invalid={Boolean(errors.title)} + /> + {errors.title && ( +

+ {errors.title} +

+ )} +
+ +
+ +