import { TokenInfo } from "@solana/spl-token-registry"; import { TokenAccounts } from "@stream-swap/ui"; import round from "lodash/round"; import { PublicKey } from "@solana/web3.js"; import { useSolBalance } from "@stream-swap/ui"; import { useWallet, useConnection } from "@solana/wallet-adapter-react"; import { NATIVE_MINT } from "@solana/spl-token"; export const Balance = ({ token, tokenAccounts, setInput, }: { token: TokenInfo | null | undefined; tokenAccounts: TokenAccounts | undefined; setInput?: (value: React.SetStateAction) => void; }) => { const { connection } = useConnection(); const { publicKey } = useWallet(); const { data: solBalance } = useSolBalance(connection, publicKey); const wSol = token?.address === NATIVE_MINT.toBase58(); const tokenAccount = token ? tokenAccounts?.getByMint(new PublicKey(token?.address)) : null; if (!token) { return null; } const balance = tokenAccount && tokenAccount.decimals ? Number(tokenAccount.account.amount) / Math.pow(10, tokenAccount.decimals) : 0; return (
Balance: {" "} {round(wSol ? solBalance?.uiAmount || 0 : balance, 2)} {setInput && !!balance && ( <>
setInput((balance / 2).toString())} className="border-[2px] border-solid rounded-[20px] border-sky-500 w-[50px] text-center text-xs cursor-pointer uppercase font-bold mr-1" > Half
setInput(balance.toString())} className="border-[2px] border-solid rounded-[20px] border-sky-500 w-[50px] text-center text-xs cursor-pointer uppercase font-bold" > Max
)}
); };