-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from xch-dev/show-usd-balance
Show USD balance for XCH and CATs
- Loading branch information
Showing
8 changed files
with
321 additions
and
53 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,30 @@ | ||
import { writeText } from '@tauri-apps/plugin-clipboard-manager'; | ||
import { CopyCheckIcon, CopyIcon } from 'lucide-react'; | ||
import { useState } from 'react'; | ||
import { Button } from './ui/button'; | ||
|
||
export function CopyButton(props: { value: string; className?: string }) { | ||
const [copied, setCopied] = useState(false); | ||
|
||
const copyAddress = () => { | ||
writeText(props.value); | ||
|
||
setCopied(true); | ||
setTimeout(() => setCopied(false), 2000); | ||
}; | ||
|
||
return ( | ||
<Button | ||
size='icon' | ||
variant='ghost' | ||
onClick={copyAddress} | ||
className={props.className} | ||
> | ||
{copied ? ( | ||
<CopyCheckIcon className='h-5 w-5 text-emerald-500' /> | ||
) : ( | ||
<CopyIcon className='h-5 w-5 text-muted-foreground' /> | ||
)} | ||
</Button> | ||
); | ||
} |
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,92 @@ | ||
import { useWalletState } from '@/state'; | ||
import { | ||
createContext, | ||
ReactNode, | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from 'react'; | ||
|
||
interface PriceContextType { | ||
getBalanceInUsd: (assetId: string, balance: string) => string; | ||
} | ||
|
||
const PriceContext = createContext<PriceContextType | undefined>(undefined); | ||
|
||
export function PriceProvider({ children }: { children: ReactNode }) { | ||
const walletState = useWalletState(); | ||
|
||
const [xchUsdPrice, setChiaPrice] = useState<number>(0); | ||
const [catPrices, setCatPrices] = useState<Record<string, number>>({}); | ||
|
||
useEffect(() => { | ||
const fetchCatPrices = () => | ||
fetch('https://api.dexie.space/v2/prices/tickers') | ||
.then((res) => res.json()) | ||
.then((data) => { | ||
const tickers = data.tickers.reduce( | ||
(acc: Record<string, string>, ticker: any) => { | ||
acc[ticker.base_id] = ticker.last_price || 0; | ||
return acc; | ||
}, | ||
{}, | ||
); | ||
setCatPrices(tickers); | ||
}) | ||
.catch(() => { | ||
setCatPrices({}); | ||
}); | ||
|
||
const fetchChiaPrice = () => | ||
fetch( | ||
'https://api.coingecko.com/api/v3/simple/price?ids=chia&vs_currencies=usd', | ||
) | ||
.then((res) => res.json()) | ||
.then((data) => { | ||
setChiaPrice(data.chia.usd || 0); | ||
}) | ||
.catch(() => { | ||
setChiaPrice(0); | ||
}); | ||
|
||
const fetchPrices = () => Promise.all([fetchCatPrices(), fetchChiaPrice()]); | ||
|
||
if (walletState.sync.unit.ticker === 'XCH') { | ||
fetchPrices(); | ||
const interval = setInterval(fetchPrices, 60000); | ||
return () => clearInterval(interval); | ||
} else { | ||
setChiaPrice(0); | ||
setCatPrices({}); | ||
} | ||
}, [walletState.sync.unit.ticker]); | ||
|
||
const getBalanceInUsd = useCallback( | ||
(assetId: string, balance: string) => { | ||
if (assetId === 'xch') { | ||
return (Number(balance) * xchUsdPrice).toFixed(2); | ||
} | ||
return ( | ||
Number(balance) * | ||
(catPrices[assetId] || 0) * | ||
xchUsdPrice | ||
).toFixed(2); | ||
}, | ||
[xchUsdPrice, catPrices], | ||
); | ||
|
||
return ( | ||
<PriceContext.Provider value={{ getBalanceInUsd }}> | ||
{children} | ||
</PriceContext.Provider> | ||
); | ||
} | ||
|
||
export function usePrices() { | ||
const context = useContext(PriceContext); | ||
if (context === undefined) { | ||
throw new Error('usePeers must be used within a PeerProvider'); | ||
} | ||
return context; | ||
} |
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,52 @@ | ||
import { useSearchParams } from 'react-router-dom'; | ||
|
||
export interface TokenParams { | ||
view: TokenView; | ||
showHidden: boolean; | ||
} | ||
|
||
export enum TokenView { | ||
Name = 'name', | ||
Balance = 'balance', | ||
} | ||
|
||
export function parseView(view: string): TokenView { | ||
switch (view) { | ||
case 'name': | ||
return TokenView.Name; | ||
case 'balance': | ||
return TokenView.Balance; | ||
default: | ||
return TokenView.Name; | ||
} | ||
} | ||
|
||
export type SetTokenParams = (params: Partial<TokenParams>) => void; | ||
|
||
export function useTokenParams(): [TokenParams, SetTokenParams] { | ||
const [params, setParams] = useSearchParams(); | ||
|
||
const view = parseView(params.get('view') ?? 'name'); | ||
const showHidden = (params.get('showHidden') ?? 'false') === 'true'; | ||
|
||
const updateParams = ({ view, showHidden }: Partial<TokenParams>) => { | ||
setParams( | ||
(prev) => { | ||
const next = new URLSearchParams(prev); | ||
|
||
if (view !== undefined) { | ||
next.set('view', view); | ||
} | ||
|
||
if (showHidden !== undefined) { | ||
next.set('showHidden', showHidden.toString()); | ||
} | ||
|
||
return next; | ||
}, | ||
{ replace: true }, | ||
); | ||
}; | ||
|
||
return [{ view, showHidden }, updateParams]; | ||
} |
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.