-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b7ebdb
commit 18f006c
Showing
18 changed files
with
170 additions
and
38 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<script lang="ts"> | ||
export let size = 20 | ||
</script> | ||
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width={size} height={size}> | ||
<path | ||
d="M30,8h-4.1c-0.5-2.3-2.5-4-4.9-4s-4.4,1.7-4.9,4H2v2h14.1c0.5,2.3,2.5,4,4.9,4s4.4-1.7,4.9-4H30V8z M21,12c-1.7,0-3-1.3-3-3 | ||
s1.3-3,3-3s3,1.3,3,3S22.7,12,21,12z" | ||
/> | ||
<path | ||
d="M2,24h4.1c0.5,2.3,2.5,4,4.9,4s4.4-1.7,4.9-4H30v-2H15.9c-0.5-2.3-2.5-4-4.9-4s-4.4,1.7-4.9,4H2V24z M11,20c1.7,0,3,1.3,3,3 | ||
s-1.3,3-3,3s-3-1.3-3-3S9.3,20,11,20z" | ||
/> | ||
</svg> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { getFromLocalStorage, saveToLocalStorage } from '$lib/adapters/utils' | ||
import { writable, type Writable } from 'svelte/store' | ||
import { z } from 'zod' | ||
|
||
export const fiatSymbolList = ['DAI', 'EUR', 'USD', 'CZK'] as const | ||
const fiatSymbolSchema = z.enum(fiatSymbolList) | ||
export type FiatSymbol = z.infer<typeof fiatSymbolSchema> | ||
|
||
interface Preferences { | ||
fiatSymbol: FiatSymbol | ||
} | ||
|
||
interface PreferenceStore extends Writable<Preferences> { | ||
setFiatSymbol: (newFiatSymbol: FiatSymbol) => void | ||
} | ||
|
||
function createPreferenceStore(): PreferenceStore { | ||
let fiatSymbol: FiatSymbol = 'DAI' | ||
try { | ||
fiatSymbol = getFromLocalStorage<FiatSymbol>('fiat', fiatSymbolSchema) ?? fiatSymbol | ||
} catch (error) { | ||
// this is fine | ||
} | ||
const store = writable<Preferences>({ fiatSymbol }) | ||
|
||
return { | ||
...store, | ||
setFiatSymbol: (newFiatSymbol: FiatSymbol) => { | ||
saveToLocalStorage('fiat', newFiatSymbol) | ||
store.update((theme) => ({ ...theme, fiatSymbol: newFiatSymbol })) | ||
}, | ||
} | ||
} | ||
|
||
export const preferences = createPreferenceStore() |
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,41 +1,57 @@ | ||
import type { ExchangeRateRecord } from '$lib/stores/exchangeRates' | ||
import { isValidNumber } from '$lib/utils' | ||
|
||
interface FiatAmount { | ||
symbol?: string | ||
amount?: number | ||
symbol: string | ||
amount: number | ||
error?: Error | ||
refreshing: boolean | ||
} | ||
|
||
function calculateFiatAmount( | ||
fiatRates: Map<string, ExchangeRateRecord>, | ||
fiatSymbol?: string, | ||
tokenAmount?: string, | ||
tokenSymbol?: string, | ||
fiatSymbol: string, | ||
tokenAmount: number, | ||
tokenSymbol: string, | ||
): FiatAmount { | ||
if (!fiatSymbol || !tokenAmount || !tokenSymbol) return { symbol: fiatSymbol, refreshing: false } | ||
const exchangeRate = fiatRates.get(tokenSymbol) | ||
const rate = exchangeRate?.rates[fiatSymbol] | ||
let amount: number | undefined = undefined | ||
|
||
if (rate !== undefined) amount = Number(tokenAmount) * rate | ||
if (rate === undefined) | ||
return { | ||
symbol: fiatSymbol, | ||
amount: 0, | ||
refreshing: Boolean(exchangeRate?.refreshing), | ||
error: new Error(`No exchange rate for ${tokenSymbol} to ${fiatSymbol}`), | ||
} | ||
|
||
return { | ||
symbol: fiatSymbol, | ||
amount, | ||
amount: tokenAmount * rate, | ||
refreshing: Boolean(exchangeRate?.refreshing), | ||
error: exchangeRate?.error, | ||
} | ||
} | ||
|
||
export function getFiatAmountText( | ||
fiatRates: Map<string, ExchangeRateRecord>, | ||
fiatSymbol?: string, | ||
tokenAmount?: string, | ||
tokenSymbol?: string, | ||
fiatSymbol: string, | ||
tokenAmount: string, | ||
tokenSymbol: string, | ||
): string { | ||
const fiatAmount = calculateFiatAmount(fiatRates, fiatSymbol, tokenAmount, tokenSymbol) | ||
if (tokenAmount === '') { | ||
const fiatAmount = calculateFiatAmount(fiatRates, fiatSymbol, 1, tokenSymbol) | ||
if (fiatAmount.amount !== undefined) | ||
return `1 ${tokenSymbol} ≈ ${fiatAmount.amount.toFixed(2)} ${fiatSymbol}` | ||
if (fiatAmount.refreshing) return `Getting exchange rate` | ||
return `Failed to load ${fiatSymbol} amount` | ||
} | ||
|
||
if (!isValidNumber(tokenAmount)) return 'Please enter a valid number' | ||
const amount = Number(tokenAmount) | ||
if (isNaN(amount)) return 'Please enter a valid number' | ||
const fiatAmount = calculateFiatAmount(fiatRates, fiatSymbol, amount, tokenSymbol) | ||
if (fiatAmount.amount !== undefined) return `≈${fiatAmount.amount.toFixed(2)} ${fiatSymbol}` | ||
else if (fiatAmount.refreshing) return `Getting exchange rate` | ||
if (fiatAmount.refreshing) return `Getting exchange rate` | ||
return `Failed to load ${fiatSymbol} amount` | ||
} |
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
Oops, something went wrong.