diff --git a/src/components/currency.tsx b/src/components/currency.tsx index c14bde07..dbb4190f 100644 --- a/src/components/currency.tsx +++ b/src/components/currency.tsx @@ -51,18 +51,12 @@ const AssetAmount: NextPage<{ decimals: number symbol: string className?: string -}> = ({ quantity, decimals, symbol, className }) => ( - -) +}> = ({ quantity, decimals, symbol, className }) => { + const value = toDecimal(quantity, decimals).replace(/([0-9]+(\.[0-9]+[1-9])?)(\.?0+$)/, "$1") + return ( + {`${value} ${symbol}`} + ) +} const ADAAmount: NextPage<{ lovelace: bigint diff --git a/src/components/layout.tsx b/src/components/layout.tsx index 5384fd27..a7ee6ec6 100644 --- a/src/components/layout.tsx +++ b/src/components/layout.tsx @@ -9,7 +9,10 @@ import { db, Treasury } from '../db' import { useRouter } from 'next/router' import Image from 'next/image' import { getTreasuriesPath } from '../route' -import { encodeCardanoData } from '../cardano/multiplatform-lib' +import { Cardano, encodeCardanoData, useCardanoMultiplatformLib } from '../cardano/multiplatform-lib' +import { getBalanceByPaymentAddresses, usePaymentAddressesQuery } from '../cardano/query-api' +import type { Value } from '../cardano/query-api' +import { ADAAmount } from './currency' const Toggle: NextPage<{ isOn: boolean @@ -143,25 +146,58 @@ const PrimaryBar: NextPage = () => { const TreasuryListing: NextPage<{ treasury: Treasury -}> = ({ treasury }) => { + balance?: Value +}> = ({ treasury, balance }) => { const { name, script } = treasury const base64CBOR = encodeCardanoData(script, 'base64') + const lovelace = balance?.lovelace return ( - {name} +
{name}
+ {lovelace &&
}
) } +const TreasuryList: NextPage<{ + cardano: Cardano + treasuries: Treasury[] +}> = ({ cardano, treasuries }) => { + const [config, _] = useContext(ConfigContext) + const addresses = cardano && treasuries && treasuries.map((treasury) => { + const script = cardano.lib.NativeScript.from_bytes(treasury.script) + return cardano.getScriptAddress(script, config.isMainnet).to_bech32() + }) + const { data } = usePaymentAddressesQuery({ + variables: { addresses }, + fetchPolicy: 'cache-first', + pollInterval: 10000 + }) + const balanceMap = new Map() + data?.paymentAddresses.forEach((paymentAddress) => { + const address = paymentAddress.address + const balance = getBalanceByPaymentAddresses([paymentAddress]) + balanceMap.set(address, balance) + }) + const balances = (addresses ?? []).map((address) => balanceMap.get(address)) + + return ( + + ) +} + const SecondaryBar: NextPage = () => { const treasuries = useLiveQuery(async () => db.treasuries.toArray()) + const cardano = useCardanoMultiplatformLib() return ( ) } diff --git a/src/pages/treasuries/[base64CBOR]/index.tsx b/src/pages/treasuries/[base64CBOR]/index.tsx index 026a2ecc..91b61e93 100644 --- a/src/pages/treasuries/[base64CBOR]/index.tsx +++ b/src/pages/treasuries/[base64CBOR]/index.tsx @@ -20,7 +20,11 @@ const ShowBalance: NextPage<{ }> = ({ cardano, script, className }) => { const [config, _] = useContext(ConfigContext) const address = cardano.getScriptAddress(script, config.isMainnet).to_bech32() - const { loading, error, data } = usePaymentAddressesQuery({ variables: { addresses: [address] }, fetchPolicy: 'network-only' }) + const { loading, error, data } = usePaymentAddressesQuery({ + variables: { addresses: [address] }, + fetchPolicy: 'cache-first', + pollInterval: 2000 + }) if (loading) return null if (error) return null