-
Notifications
You must be signed in to change notification settings - Fork 619
wallet txns activity #6090
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wallet txns activity #6090
Changes from all commits
b17c331
724c7e7
6b78ad6
3f08e8d
f9822c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { DASHBOARD_THIRDWEB_CLIENT_ID } from "@/constants/env"; | ||
|
|
||
| interface Transaction { | ||
| chain_id: number; | ||
| hash: string; | ||
| nonce: number; | ||
| block_hash: string; | ||
| block_number: number; | ||
| block_timestamp: number; | ||
| transaction_index: number; | ||
| from_address: string; | ||
| to_address: string | null; | ||
| value: number; | ||
| gas: number; | ||
| gas_price: number | null; | ||
| data: string | null; | ||
| function_selector: string | null; | ||
| max_fee_per_gas: number | null; | ||
| max_priority_fee_per_gas: number | null; | ||
| transaction_type: number | null; | ||
| r: string | null; | ||
| s: string | null; | ||
| v: number | null; | ||
| access_list_json: string | null; | ||
| contract_address: string | null; | ||
| gas_used: number | null; | ||
| cumulative_gas_used: number | null; | ||
| effective_gas_price: number | null; | ||
| blob_gas_used: number | null; | ||
| blob_gas_price: number | null; | ||
| logs_bloom: string | null; | ||
| status: boolean | null; // true for success, false for failure | ||
| } | ||
|
|
||
| interface InsightsResponse { | ||
| meta: { | ||
| address: string; | ||
| signature: string; | ||
| page: number; | ||
| total_items: number; | ||
| total_pages: number; | ||
| limit_per_chain: number; | ||
| chain_ids: number[]; | ||
| }; | ||
| data: Transaction[]; | ||
| } | ||
|
|
||
| export async function fetchTxActivity(args: { | ||
| chainId: number; | ||
| address: string; | ||
| limit_per_type?: number; | ||
| page?: number; | ||
| }): Promise<Transaction[]> { | ||
| let { chainId, address, limit_per_type, page } = args; | ||
| if (!limit_per_type) limit_per_type = 100; | ||
| if (!page) page = 0; | ||
|
|
||
| const outgoingTxsResponse = await fetch( | ||
| `https://insight.thirdweb-dev.com/v1/transactions?chain=${chainId}&filter_from_address=${address}&page=${page}&limit=${limit_per_type}&sort_by=block_number&sort_order=desc`, | ||
| { | ||
| headers: { | ||
| "x-client-id": DASHBOARD_THIRDWEB_CLIENT_ID, | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| const incomingTxsResponse = await fetch( | ||
| `https://insight.thirdweb-dev.com/v1/transactions?chain=${chainId}&filter_to_address=${address}&page=${page}&limit=${limit_per_type}&sort_by=block_number&sort_order=desc`, | ||
| { | ||
| headers: { | ||
| "x-client-id": DASHBOARD_THIRDWEB_CLIENT_ID, | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| if (!outgoingTxsResponse.ok || !incomingTxsResponse.ok) { | ||
| throw new Error("Failed to fetch transaction history"); | ||
| } | ||
|
|
||
| const outgoingTxsData: InsightsResponse = await outgoingTxsResponse.json(); | ||
catalyst17 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const incomingTxsData: InsightsResponse = await incomingTxsResponse.json(); | ||
|
|
||
| return [...outgoingTxsData.data, ...incomingTxsData.data].sort( | ||
| (a, b) => b.block_number - a.block_number, | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { useEffect, useState } from "react"; | ||
| import { fetchTxActivity } from "../actions/fetchTxActivity"; | ||
|
|
||
| interface TxActivityItem { | ||
| id: string; | ||
| // all txs we retrieve for now are outgoing | ||
| // TODO: add incoming | ||
| type: "out" | "in"; | ||
| amount: string; | ||
| to?: string; | ||
| from?: string; | ||
| method?: string; | ||
| date: string; | ||
| } | ||
|
|
||
| export function useGetTxActivity(chainId: number, address: string) { | ||
| const [txActivity, setTxActivity] = useState<TxActivityItem[]>([]); | ||
| const [isLoading, setIsLoading] = useState(true); | ||
|
|
||
| useEffect(() => { | ||
| (async () => { | ||
| const response = await fetchTxActivity({ chainId, address }); | ||
| const activity = response.map((tx): TxActivityItem => { | ||
| const type = | ||
| tx.to_address?.toLowerCase() === address.toLowerCase() ? "in" : "out"; | ||
| return { | ||
| id: tx.hash, | ||
| type, | ||
| amount: `${tx.value / 10 ** 18} ETH`, | ||
| to: tx.to_address || undefined, | ||
| from: tx.from_address, | ||
| method: tx.function_selector || undefined, | ||
| date: new Date(tx.block_timestamp * 1000).toLocaleString(), | ||
| }; | ||
| }); | ||
| setTxActivity(activity); | ||
| setIsLoading(false); | ||
| })(); | ||
|
Comment on lines
+18
to
+38
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace useEffect-based data fetching with useQuery from React Query. The implementation should look like: useQuery(['txActivity', chainId, address], () => fetchTxActivity({chainId, address}), { select: (data) => data.map(tx => ({ ... })) }) Spotted by Graphite Reviewer (based on CI logs) |
||
| }, [address, chainId]); | ||
|
|
||
| return { txActivity, isLoading }; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.