|
1 | 1 | <script lang="ts"> |
2 | 2 | import { wallet } from "$lib/stores/wallet"; |
3 | 3 | import { walletMock } from "$lib/stores/walletMock"; |
4 | | - import { score } from "$lib/stores/score"; |
| 4 | + import { score, scoreActions } from "$lib/stores/score"; |
5 | 5 | import { airdrop } from "$lib/stores/airdrop"; |
6 | 6 | import type { AirdropState } from "$lib/stores/airdrop"; |
7 | 7 | import ClaimCard from "$lib/components/ClaimCard.svelte"; |
8 | 8 | import ChecklistItem from "$lib/components/ChecklistItem.svelte"; |
9 | | - import { onMount } from "svelte"; |
| 9 | + import { onDestroy, onMount } from "svelte"; |
| 10 | + import { getScore } from "$lib/api/client"; |
10 | 11 | import type { PageData } from "./$types"; |
| 12 | + import { browser } from "$app/environment"; |
| 13 | + import { formatUnits } from "viem"; |
11 | 14 |
|
12 | 15 | export let data: PageData; |
13 | 16 |
|
| 17 | + let lastLoadedAddress: `0x${string}` | null = null; |
| 18 | + let currentLoadAddress: `0x${string}` | null = null; |
| 19 | + let unsubscribeWallet: () => void; |
| 20 | +
|
| 21 | + async function loadScore(address: `0x${string}`) { |
| 22 | + if (!browser) return; |
| 23 | + if (currentLoadAddress === address || lastLoadedAddress === address) return; |
| 24 | +
|
| 25 | + currentLoadAddress = address; |
| 26 | + scoreActions.setLoading(true); |
| 27 | +
|
| 28 | + try { |
| 29 | + const scoreData = await getScore(address); |
| 30 | + if (!scoreData) { |
| 31 | + throw new Error("No score data returned"); |
| 32 | + } |
| 33 | +
|
| 34 | + scoreActions.setValue(scoreData.score1e6 ?? 0); |
| 35 | + score.update((s) => ({ |
| 36 | + ...s, |
| 37 | + loading: false, |
| 38 | + lastUpdated: scoreData.updatedAt, |
| 39 | + error: undefined, |
| 40 | + })); |
| 41 | +
|
| 42 | + lastLoadedAddress = address; |
| 43 | + } catch (error) { |
| 44 | + console.error("Failed to load score for claim page:", error); |
| 45 | + scoreActions.setError("Failed to load reputation score."); |
| 46 | + } finally { |
| 47 | + if (currentLoadAddress === address) { |
| 48 | + currentLoadAddress = null; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +
|
14 | 53 | onMount(() => { |
15 | 54 | if (data.scoreData) { |
16 | 55 | score.set({ |
|
21 | 60 | } else if (data.error) { |
22 | 61 | score.set({ loading: false, error: data.error }); |
23 | 62 | } |
| 63 | +
|
| 64 | + unsubscribeWallet = wallet.subscribe(($wallet) => { |
| 65 | + if (!browser) return; |
| 66 | +
|
| 67 | + if (!$walletMock.enabled && $wallet.connected && $wallet.address) { |
| 68 | + void loadScore($wallet.address); |
| 69 | + } else if (!$walletMock.enabled && (!$wallet.connected || !$wallet.address)) { |
| 70 | + lastLoadedAddress = null; |
| 71 | + scoreActions.reset(); |
| 72 | + } |
| 73 | + }); |
| 74 | + }); |
| 75 | +
|
| 76 | + onDestroy(() => { |
| 77 | + if (unsubscribeWallet) unsubscribeWallet(); |
24 | 78 | }); |
25 | 79 |
|
26 | 80 | // Determine connection status considering both real and mock states |
|
39 | 93 | $: expectedPayout = calculatePayout($score.value || 0, $airdrop); |
40 | 94 |
|
41 | 95 | function calculatePayout(scoreValue: number, airdropConfig: Partial<AirdropState>): number { |
42 | | - if (!scoreValue || !airdropConfig.floor || !airdropConfig.cap) return 0; |
| 96 | + const { floor, cap, minPayout, maxPayout, decimals } = airdropConfig; |
| 97 | + if ( |
| 98 | + !scoreValue || |
| 99 | + floor === undefined || |
| 100 | + cap === undefined || |
| 101 | + minPayout === undefined || |
| 102 | + maxPayout === undefined || |
| 103 | + decimals === undefined |
| 104 | + ) { |
| 105 | + return 0; |
| 106 | + } |
43 | 107 |
|
44 | | - if (scoreValue < airdropConfig.floor) return 0; |
| 108 | + if (scoreValue < floor) return 0; |
45 | 109 |
|
46 | | - const clampedScore = Math.min(scoreValue, airdropConfig.cap); |
47 | | - const range = airdropConfig.cap - airdropConfig.floor; |
| 110 | + const clampedScore = Math.min(scoreValue, cap); |
| 111 | + const range = cap - floor; |
48 | 112 | if (range <= 0) { |
49 | | - return Number(airdropConfig.maxPayout ?? 0n); |
| 113 | + return Number(formatUnits(maxPayout, decimals)); |
50 | 114 | } |
51 | 115 |
|
52 | | - const normalizedScore = (clampedScore - airdropConfig.floor) / range; |
53 | | -
|
54 | | - const minPayout = Number(airdropConfig.minPayout ?? 0n); |
55 | | - const maxPayout = Number(airdropConfig.maxPayout ?? minPayout); |
56 | | - const payoutRange = Math.max(0, maxPayout - minPayout); |
| 116 | + const normalizedScore = (clampedScore - floor) / range; |
| 117 | + const minTokens = Number(formatUnits(minPayout, decimals)); |
| 118 | + const maxTokens = Number(formatUnits(maxPayout, decimals)); |
| 119 | + const payoutRange = Math.max(0, maxTokens - minTokens); |
57 | 120 |
|
58 | | - return minPayout + normalizedScore * payoutRange; |
| 121 | + return minTokens + normalizedScore * payoutRange; |
59 | 122 | } |
60 | 123 | </script> |
61 | 124 |
|
|
0 commit comments