From 99c1a4351ab112993ff1b52b8fd9e362cafe6977 Mon Sep 17 00:00:00 2001 From: "yongen.loong" Date: Fri, 15 Nov 2024 14:01:01 +0800 Subject: [PATCH] feat: aelf scan indexer --- .env | 7 +- app/deployments/page.tsx | 55 ++--------- components/providers/apollo-wrapper.tsx | 36 +++---- data/graphql.ts | 119 +++++++++++++++++------- 4 files changed, 118 insertions(+), 99 deletions(-) diff --git a/.env b/.env index 94e9624..82aa027 100644 --- a/.env +++ b/.env @@ -3,6 +3,7 @@ SOLANG_BUILD_SERVER_BASE_URL=https://playground-next.test.aelf.dev SOLIDITY_ENABLED=false GA_TAG= GITHUB_API_KEY= -NEXT_PUBLIC_FAUCET_API_URL = -NEXT_PUBLIC_GOOGLE_CAPTCHA_SITEKEY = -NEXT_PUBLIC_INDEXER_GRAPHQL_ENDPOINT = \ No newline at end of file +NEXT_PUBLIC_FAUCET_API_URL= +NEXT_PUBLIC_GOOGLE_CAPTCHA_SITEKEY= +NEXT_PUBLIC_AELFSCAN_GRAPHQL_ENDPOINT= +NEXT_PUBLIC_TMRWDAO_GRAPHQL_ENDPOINT= \ No newline at end of file diff --git a/app/deployments/page.tsx b/app/deployments/page.tsx index 444ba32..86426e9 100644 --- a/app/deployments/page.tsx +++ b/app/deployments/page.tsx @@ -11,12 +11,13 @@ import { } from "@/components/ui/table"; import { format } from "date-fns"; import Link from "next/link"; -import { useLogs, useProposalsInfo, useTransactions } from "@/data/client"; +import { useContractList } from "@/data/graphql"; export default function Page() { const wallet = useWallet(); - const { data } = useTransactions(wallet?.wallet.address); - const contractTransactions = data?.filter(i => i.method === "DeployUserSmartContract"); + const { data, loading } = useContractList(wallet?.wallet.address); + + if (loading) return
Loading...
; return (
@@ -26,20 +27,16 @@ export default function Page() { Date and time Contract Address - Proposal - {contractTransactions ? ( - contractTransactions + {data ? ( + data.contractList.items .map((i) => ( - - {format(i.time, "PPPppp")} - - - + + {format(i.metadata.block.blockTime, "PPPppp")} - + )) @@ -59,26 +56,6 @@ export default function Page() { ); } -function Proposal({ id }: { id: string }) { - const { data, isLoading, error } = useLogs(id); - - if (isLoading) return Loading...; - if (error || !data || !data.proposalId) return -; - - return ; -} - -function ContractAddress({ id }: { id: string }) { - const { data: logs } = useLogs(id); - const { data: info } = useProposalsInfo(logs?.proposalId ? [logs.proposalId] : []); - const { data, isLoading, error } = useLogs(info?.getNetworkDaoProposalReleasedIndex.data?.[0]?.transactionInfo.transactionId); - - if (isLoading) return Loading...; - if (error || !data) return -; - - return ; -} - function ViewAddressOnExplorer({ address }: { address: string }) { return ( ); } - -function ViewProposalOnExplorer({ id }: { id: string }) { - return ( - - {id} - - ); -} \ No newline at end of file diff --git a/components/providers/apollo-wrapper.tsx b/components/providers/apollo-wrapper.tsx index 03df204..737f5d0 100644 --- a/components/providers/apollo-wrapper.tsx +++ b/components/providers/apollo-wrapper.tsx @@ -2,33 +2,37 @@ // ^ this file needs the "use client" pragma // https://github.com/apollographql/apollo-client-nextjs/tree/main?tab=readme-ov-file#in-client-components-and-streaming-ssr -import { HttpLink } from "@apollo/client"; +import { HttpLink, ApolloLink } from "@apollo/client"; import { ApolloNextAppProvider, ApolloClient, InMemoryCache, } from "@apollo/experimental-nextjs-app-support"; -import { env } from 'next-runtime-env'; +import { env } from "next-runtime-env"; + +const tmrwdaoLink = new HttpLink({ + uri: env("NEXT_PUBLIC_TMRWDAO_GRAPHQL_ENDPOINT"), + fetchOptions: { cache: "no-store" }, +}); + +const aelfscanLink = new HttpLink({ + uri: env("NEXT_PUBLIC_AELFSCAN_GRAPHQL_ENDPOINT"), + fetchOptions: { cache: "no-store" }, +}); + +export const AELFSCAN_CLIENT_NAME = "aelfscanLink"; // have a function to create a client for you function makeClient() { - const httpLink = new HttpLink({ - // this needs to be an absolute url, as relative urls cannot be used in SSR - uri: env("NEXT_PUBLIC_INDEXER_GRAPHQL_ENDPOINT"), - // you can disable result caching here if you want to - // (this does not work if you are rendering your page with `export const dynamic = "force-static"`) - fetchOptions: { cache: "no-store" }, - // you can override the default `fetchOptions` on a per query basis - // via the `context` property on the options passed as a second argument - // to an Apollo Client data fetching hook, e.g.: - // const { data } = useSuspenseQuery(MY_QUERY, { context: { fetchOptions: { cache: "force-cache" }}}); - }); - // use the `ApolloClient` from "@apollo/experimental-nextjs-app-support" return new ApolloClient({ // use the `InMemoryCache` from "@apollo/experimental-nextjs-app-support" cache: new InMemoryCache(), - link: httpLink, + link: ApolloLink.split( + (operation) => operation.getContext().clientName === AELFSCAN_CLIENT_NAME, + aelfscanLink, //if above + tmrwdaoLink + ), }); } @@ -39,4 +43,4 @@ export function ApolloWrapper({ children }: React.PropsWithChildren) { {children} ); -} \ No newline at end of file +} diff --git a/data/graphql.ts b/data/graphql.ts index ca40f4d..03d00c0 100644 --- a/data/graphql.ts +++ b/data/graphql.ts @@ -1,43 +1,94 @@ -import { useQuery, gql } from '@apollo/client'; +import { AELFSCAN_CLIENT_NAME } from "@/components/providers/apollo-wrapper"; +import { useQuery, gql } from "@apollo/client"; interface GetNetworkDaoProposalReleasedIndexResponse { - getNetworkDaoProposalReleasedIndex: { - data: Array<{ - proposalId: string - orgType: string - transactionInfo: { - transactionId: string + getNetworkDaoProposalReleasedIndex: { + data: Array<{ + proposalId: string; + orgType: string; + transactionInfo: { + transactionId: string; + }; + }>; + }; +} + +export const useProposalReleaseInfo = ( + proposalIds: string[] = [], + pollInterval: number = 0 +) => { + return useQuery( + gql` + query GetNetworkDaoProposalReleasedIndex( + $input: GetNetworkDaoProposalReleasedIndexInput! + ) { + getNetworkDaoProposalReleasedIndex(input: $input) { + data { + proposalId + orgType + transactionInfo { + transactionId } - }> + } + } + } + `, + { + variables: { + input: { + startBlockHeight: 1, + endBlockHeight: 4000000000, + orgType: "PARLIAMENT", + skipCount: 0, + maxResultCount: 1, + proposalIds: proposalIds, + }, + }, + pollInterval, + skip: proposalIds.length === 0, } -} + ); +}; -export const useProposalReleaseInfo = (proposalIds: string[] = [], pollInterval: number = 0) => { +interface GetContractListResponse { + contractList: { + items: Array<{ + address: string; + metadata: { + block: { + blockTime: string; + }; + }; + }>; + }; +} - return useQuery(gql` - query GetNetworkDaoProposalReleasedIndex($input: GetNetworkDaoProposalReleasedIndexInput!) { - getNetworkDaoProposalReleasedIndex(input: $input) { - data { - proposalId - orgType - transactionInfo { - transactionId - } - } +export const useContractList = (author?: string) => { + return useQuery( + gql` + query ContractList($input: GetContractInfoDto) { + contractList(input: $input) { + items { + address + metadata { + block { + blockTime + } } + } } - `, { - variables: { - "input": { - "startBlockHeight": 1, - "endBlockHeight": 4000000000, - "orgType": "PARLIAMENT", - "skipCount": 0, - "maxResultCount": 1, - "proposalIds": proposalIds - }, + } + `, + { + variables: { + input: { + author: author, + maxResultCount: 1000, + skipCount: 0, }, - pollInterval, - skip: proposalIds.length === 0 - }) -} \ No newline at end of file + }, + skip: !author, + context: {clientName: AELFSCAN_CLIENT_NAME} + } + ); +};