From 5c05c5587b04ba21909b9f383b3aafbb04c8cff0 Mon Sep 17 00:00:00 2001 From: Zhi-Qiang Lei Date: Wed, 13 Apr 2022 10:11:57 +0800 Subject: [PATCH] Catch balance query in batch --- src/components/layout.tsx | 48 ++++++++++++++++++--- src/pages/treasuries/[base64CBOR]/index.tsx | 6 ++- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/components/layout.tsx b/src/components/layout.tsx index 5384fd27..baa39ca3 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 { 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<{ + addresses: string[] + treasuries: Treasury[] +}> = ({ addresses, treasuries }) => { + 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 [config, _] = useContext(ConfigContext) const treasuries = useLiveQuery(async () => db.treasuries.toArray()) + const cardano = useCardanoMultiplatformLib() + const addresses = cardano && treasuries && treasuries.map((treasury) => { + const script = cardano.lib.NativeScript.from_bytes(treasury.script) + return cardano.getScriptAddress(script, config.isMainnet).to_bech32() + }) 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