-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1da3e2a
commit 3aafc0d
Showing
17 changed files
with
312 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/features/applications/components/application-live-transactions.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { ApplicationId } from '../data/types' | ||
import { useCallback } from 'react' | ||
import { LiveTransactionsTable } from '@/features/transactions/components/live-transactions-table' | ||
import { TransactionResult } from '@algorandfoundation/algokit-utils/types/indexer' | ||
import { flattenTransactionResult } from '@/features/transactions/utils/flatten-transaction-result' | ||
import { TransactionType as AlgoSdkTransactionType } from 'algosdk' | ||
import { applicationTransactionsTableColumns } from '../utils/application-transactions-table-columns' | ||
import { Transaction, InnerTransaction } from '@/features/transactions/models' | ||
import { getApplicationTransactionsTableSubRows } from '../utils/get-application-transactions-table-sub-rows' | ||
|
||
type Props = { | ||
applicationId: ApplicationId | ||
} | ||
|
||
export function ApplicationLiveTransactions({ applicationId }: Props) { | ||
const filter = useCallback( | ||
(transactionResult: TransactionResult) => { | ||
const flattenedTransactionResults = flattenTransactionResult(transactionResult) | ||
return flattenedTransactionResults.some( | ||
(txn) => txn['tx-type'] === AlgoSdkTransactionType.appl && txn['application-transaction']?.['application-id'] === applicationId | ||
) | ||
}, | ||
[applicationId] | ||
) | ||
|
||
const getSubRows = useCallback( | ||
(row: Transaction | InnerTransaction) => getApplicationTransactionsTableSubRows(applicationId, row), | ||
[applicationId] | ||
) | ||
|
||
return <LiveTransactionsTable filter={filter} getSubRows={getSubRows} columns={applicationTransactionsTableColumns} /> | ||
} |
26 changes: 26 additions & 0 deletions
26
src/features/applications/components/application-transaction-history.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { LazyLoadDataTable } from '@/features/common/components/lazy-load-data-table' | ||
import { ApplicationId } from '../data/types' | ||
import { useFetchNextApplicationTransactionsPage } from '../data/application-transaction-history' | ||
import { applicationTransactionsTableColumns } from '../utils/application-transactions-table-columns' | ||
import { InnerTransaction, Transaction } from '@/features/transactions/models' | ||
import { useCallback } from 'react' | ||
import { getApplicationTransactionsTableSubRows } from '../utils/get-application-transactions-table-sub-rows' | ||
|
||
type Props = { | ||
applicationId: ApplicationId | ||
} | ||
|
||
export function ApplicationTransactionHistory({ applicationId }: Props) { | ||
// TODO: for the future | ||
// How we handle getSubRows isn't the best practice. Ideally, we should create a new view model, for example, TransactionForApplication | ||
// and then fetchNextPage should return a list of TransactionForApplication | ||
// TransactionForApplication should be similar to Transaction, but the InnerTransactions should be only transactions that are related to the application | ||
// This way, getSubRows simply return the innerTransactions | ||
const fetchNextPage = useFetchNextApplicationTransactionsPage(applicationId) | ||
const getSubRows = useCallback( | ||
(row: Transaction | InnerTransaction) => getApplicationTransactionsTableSubRows(applicationId, row), | ||
[applicationId] | ||
) | ||
|
||
return <LazyLoadDataTable columns={applicationTransactionsTableColumns} getSubRows={getSubRows} fetchNextPage={fetchNextPage} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/features/applications/data/application-transaction-history.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { ApplicationId } from './types' | ||
import { indexer } from '@/features/common/data' | ||
import { TransactionResult, TransactionSearchResults } from '@algorandfoundation/algokit-utils/types/indexer' | ||
import { useMemo } from 'react' | ||
import { JotaiStore } from '@/features/common/data/types' | ||
import { createTransactionsAtom, transactionResultsAtom } from '@/features/transactions/data' | ||
import { atomEffect } from 'jotai-effect' | ||
import { atom, useStore } from 'jotai' | ||
|
||
const fetchApplicationTransactionResults = async (applicationID: ApplicationId, pageSize: number, nextPageToken?: string) => { | ||
const results = (await indexer | ||
.searchForTransactions() | ||
.applicationID(applicationID) | ||
.nextToken(nextPageToken ?? '') | ||
.limit(pageSize) | ||
.do()) as TransactionSearchResults | ||
return { | ||
transactionResults: results.transactions, | ||
nextPageToken: results['next-token'], | ||
} as const | ||
} | ||
|
||
const createSyncEffect = (transactionResults: TransactionResult[]) => { | ||
return atomEffect((_, set) => { | ||
;(async () => { | ||
try { | ||
set(transactionResultsAtom, (prev) => { | ||
const next = new Map(prev) | ||
transactionResults.forEach((transactionResult) => { | ||
if (!next.has(transactionResult.id)) { | ||
next.set(transactionResult.id, atom(transactionResult)) | ||
} | ||
}) | ||
return next | ||
}) | ||
} catch (e) { | ||
// Ignore any errors as there is nothing to sync | ||
} | ||
})() | ||
}) | ||
} | ||
|
||
const createApplicationTransactionsAtom = (store: JotaiStore, applicationID: ApplicationId, pageSize: number, nextPageToken?: string) => { | ||
return atom(async (get) => { | ||
const { transactionResults, nextPageToken: newNextPageToken } = await fetchApplicationTransactionResults( | ||
applicationID, | ||
pageSize, | ||
nextPageToken | ||
) | ||
|
||
get(createSyncEffect(transactionResults)) | ||
|
||
const transactions = await get(createTransactionsAtom(store, transactionResults)) | ||
|
||
return { | ||
rows: transactions, | ||
nextPageToken: newNextPageToken, | ||
} | ||
}) | ||
} | ||
|
||
export const useFetchNextApplicationTransactionsPage = (applicationID: ApplicationId) => { | ||
const store = useStore() | ||
|
||
return useMemo(() => { | ||
return (pageSize: number, nextPageToken?: string) => createApplicationTransactionsAtom(store, applicationID, pageSize, nextPageToken) | ||
}, [store, applicationID]) | ||
} |
55 changes: 55 additions & 0 deletions
55
src/features/applications/utils/application-transactions-table-columns.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { InnerTransaction, Transaction, TransactionType } from '@/features/transactions/models' | ||
import { cn } from '@/features/common/utils' | ||
import { ellipseAddress } from '@/utils/ellipse-address' | ||
import { ColumnDef } from '@tanstack/react-table' | ||
import { DisplayAlgo } from '@/features/common/components/display-algo' | ||
import { TransactionLink } from '@/features/transactions/components/transaction-link' | ||
import { asTo } from '@/features/common/mappers/to' | ||
import { InnerTransactionLink } from '@/features/transactions/components/inner-transaction-link' | ||
|
||
const indentationWidth = 20 | ||
|
||
export const applicationTransactionsTableColumns: ColumnDef<Transaction | InnerTransaction>[] = [ | ||
{ | ||
header: 'Transaction Id', | ||
accessorFn: (transaction) => transaction, | ||
cell: ({ row, getValue }) => { | ||
const transaction = getValue<Transaction | InnerTransaction>() | ||
return ( | ||
<div | ||
style={{ | ||
marginLeft: `${indentationWidth * row.depth}px`, | ||
}} | ||
> | ||
{'innerId' in transaction ? ( | ||
<InnerTransactionLink transactionId={transaction.networkTransactionId} innerTransactionId={transaction.innerId} /> | ||
) : ( | ||
<TransactionLink transactionId={transaction.id} short={true} /> | ||
)} | ||
</div> | ||
) | ||
}, | ||
}, | ||
{ | ||
header: 'Round', | ||
accessorKey: 'confirmedRound', | ||
}, | ||
{ | ||
accessorKey: 'sender', | ||
header: 'From', | ||
cell: (c) => ellipseAddress(c.getValue<string>()), | ||
}, | ||
{ | ||
header: 'To', | ||
accessorFn: asTo, | ||
}, | ||
{ | ||
header: 'Fee', | ||
accessorFn: (transaction) => transaction, | ||
cell: (c) => { | ||
const transaction = c.getValue<Transaction>() | ||
if (transaction.type === TransactionType.ApplicationCall) | ||
return <DisplayAlgo className={cn('justify-center')} amount={transaction.fee} /> | ||
}, | ||
}, | ||
] |
13 changes: 13 additions & 0 deletions
13
src/features/applications/utils/get-application-transactions-table-sub-rows.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Transaction, InnerTransaction, TransactionType } from '@/features/transactions/models' | ||
import { flattenInnerTransactions } from '@/utils/flatten-inner-transactions' | ||
|
||
export const getApplicationTransactionsTableSubRows = (applicationId: number, transaction: Transaction | InnerTransaction) => { | ||
if (transaction.type !== TransactionType.ApplicationCall || transaction.innerTransactions.length === 0) { | ||
return [] | ||
} | ||
|
||
return transaction.innerTransactions.filter((innerTransaction) => { | ||
const txns = flattenInnerTransactions(innerTransaction) | ||
return txns.some(({ transaction: txn }) => txn.type === TransactionType.ApplicationCall && txn.applicationId === applicationId) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.