-
Notifications
You must be signed in to change notification settings - Fork 3
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
Inspect Block Page #378
Open
JasonMHasperhoven
wants to merge
15
commits into
main
Choose a base branch
from
inspect-block
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+325
−11
Open
Inspect Block Page #378
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
aeb25d3
Add inspect block page
JasonMHasperhoven a0b6456
Fetch transactions by height
JasonMHasperhoven 3eb8166
Merge branch 'main' into inspect-block
JasonMHasperhoven d1af3c0
Setup block summary nextjs api route
JasonMHasperhoven 3323d3c
Improve display type for batchSwaps
JasonMHasperhoven c7606aa
Parse batchSwaps from pindexer
JasonMHasperhoven deb152b
Fix lint issues
JasonMHasperhoven 16f0476
Parse from jsonb
JasonMHasperhoven 27c5b0f
Update batch swaps type
JasonMHasperhoven 682c142
Merge branch 'main' into inspect-block
JasonMHasperhoven 3d6fe0d
Merge branch main into inspect-block
JasonMHasperhoven 73a77c4
Replace Table with TableCell
JasonMHasperhoven e8b45e0
Address PR comments
JasonMHasperhoven 86596dd
Merge branch 'main' into inspect-block
JasonMHasperhoven f349f14
Use serialization functions
JasonMHasperhoven File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { GET } from '@/shared/api/server/block'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { BlockSummaryApiResponse } from '@/shared/api/server/block/types'; | ||
import { apiFetch } from '@/shared/utils/api-fetch'; | ||
|
||
export const useBlockSummary = (height: string) => { | ||
return useQuery({ | ||
queryKey: ['block', height], | ||
retry: 1, | ||
queryFn: async (): Promise<BlockSummaryApiResponse> => { | ||
if (!height) { | ||
throw new Error('Invalid block height'); | ||
} | ||
return apiFetch<BlockSummaryApiResponse>(`/api/block/${height}`); | ||
}, | ||
}); | ||
}; |
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 |
---|---|---|
@@ -1,9 +1 @@ | ||
import { Text } from '@penumbra-zone/ui/Text'; | ||
|
||
export function InspectBlock() { | ||
return ( | ||
<div className='flex flex-col items-center justify-center h-[400px]'> | ||
<Text color='text.muted'>Coming soon...</Text> | ||
</div> | ||
); | ||
} | ||
export { InspectBlock } from './ui'; |
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,97 @@ | ||
import { InfoCard } from '@/pages/explore/ui/info-card'; | ||
import { Text } from '@penumbra-zone/ui/Text'; | ||
import { TableCell } from '@penumbra-zone/ui/TableCell'; | ||
import { BlockSummaryApiResponse } from '@/shared/api/server/block/types'; | ||
import { ValueViewComponent } from '@penumbra-zone/ui/ValueView'; | ||
import { pnum } from '@penumbra-zone/types/pnum'; | ||
|
||
export function BlockSummary({ blockSummary }: { blockSummary: BlockSummaryApiResponse }) { | ||
if ('error' in blockSummary) { | ||
return <div>Error: {blockSummary.error}</div>; | ||
} | ||
|
||
return ( | ||
<div> | ||
<div className='grid grid-cols-1 tablet:grid-cols-2 desktop:grid-cols-3 gap-2 mb-8'> | ||
<InfoCard title='Total Transactions'> | ||
<Text large color='text.primary'> | ||
{blockSummary.numTxs} | ||
</Text> | ||
</InfoCard> | ||
<InfoCard title='Total Swaps'> | ||
<Text large color='text.primary'> | ||
{blockSummary.numSwaps} | ||
</Text> | ||
</InfoCard> | ||
<InfoCard title='Total Swap Claims'> | ||
<Text large color='text.primary'> | ||
{blockSummary.numSwapClaims} | ||
</Text> | ||
</InfoCard> | ||
<InfoCard title='Total Open LPs'> | ||
<Text large color='text.primary'> | ||
{blockSummary.numOpenLps} | ||
</Text> | ||
</InfoCard> | ||
<InfoCard title='Total Closed LPs'> | ||
<Text large color='text.primary'> | ||
{blockSummary.numClosedLps} | ||
</Text> | ||
</InfoCard> | ||
<InfoCard title='Total Withdrawn LPs'> | ||
<Text large color='text.primary'> | ||
{blockSummary.numWithdrawnLps} | ||
</Text> | ||
</InfoCard> | ||
</div> | ||
<div> | ||
<div className='mb-4'> | ||
<Text large color='text.primary'> | ||
Swaps | ||
</Text> | ||
</div> | ||
<div className='grid grid-cols-4'> | ||
<div className='grid grid-cols-subgrid col-span-4'> | ||
<TableCell heading>From</TableCell> | ||
<TableCell heading>To</TableCell> | ||
<TableCell heading>Price</TableCell> | ||
<TableCell heading>Number of Hops</TableCell> | ||
</div> | ||
{blockSummary.batchSwaps.length ? ( | ||
blockSummary.batchSwaps.map(swap => ( | ||
<div className='grid grid-cols-subgrid col-span-4' key={JSON.stringify(swap)}> | ||
<TableCell> | ||
<ValueViewComponent | ||
valueView={pnum(swap.startInput).toValueView(swap.startAsset)} | ||
trailingZeros={false} | ||
/> | ||
</TableCell> | ||
<TableCell> | ||
<ValueViewComponent | ||
valueView={pnum(swap.endOutput).toValueView(swap.endAsset)} | ||
trailingZeros={false} | ||
/> | ||
</TableCell> | ||
<TableCell> | ||
<Text color='text.primary'> | ||
{swap.endPrice} {swap.endAsset.symbol} | ||
</Text> | ||
</TableCell> | ||
<TableCell> | ||
<Text color='text.primary'>{swap.numSwaps}</Text> | ||
</TableCell> | ||
</div> | ||
)) | ||
) : ( | ||
<div className='grid grid-cols-subgrid col-span-4'> | ||
<TableCell>--</TableCell> | ||
<TableCell>--</TableCell> | ||
<TableCell>--</TableCell> | ||
<TableCell>--</TableCell> | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
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,47 @@ | ||
'use client'; | ||
|
||
import { useParams } from 'next/navigation'; | ||
import { useBlockSummary } from '../api/block'; | ||
import { Card } from '@penumbra-zone/ui/Card'; | ||
import { Skeleton } from '@/shared/ui/skeleton'; | ||
import { BlockSummary } from './block-summary'; | ||
|
||
export function InspectBlock() { | ||
const params = useParams<{ height: string }>(); | ||
const blockheight = params?.height; | ||
const { data: blockSummary, isError } = useBlockSummary(blockheight ?? ''); | ||
|
||
return ( | ||
<div className='flex flex-col items-center justify-center'> | ||
<div className='mb-4'> | ||
{isError ? ( | ||
<Card title={`Couldn’t fetch transaction.`}> | ||
<div className='w-[840px] min-h-[300px] text-white p-2'> | ||
Something went wrong while fetching the transaction. | ||
</div> | ||
</Card> | ||
) : ( | ||
<Card title={`Block #${blockheight}`}> | ||
<div className='w-[840px] min-h-[300px] text-white p-2'> | ||
{blockSummary ? ( | ||
<BlockSummary blockSummary={blockSummary} /> | ||
) : ( | ||
<div> | ||
<div className='w-[822px] h-8 mb-2'> | ||
<Skeleton /> | ||
</div> | ||
<div className='w-[722px] h-6 mb-2'> | ||
<Skeleton /> | ||
</div> | ||
<div className='w-[422px] h-4'> | ||
<Skeleton /> | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
</Card> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
} |
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,103 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { pindexer } from '@/shared/database'; | ||
import { AssetId } from '@penumbra-zone/protobuf/penumbra/core/asset/v1/asset_pb'; | ||
import { ChainRegistryClient, Registry } from '@penumbra-labs/registry'; | ||
import { getDisplayDenomExponent } from '@penumbra-zone/getters/metadata'; | ||
import { pnum } from '@penumbra-zone/types/pnum'; | ||
import { hexToUint8Array, base64ToHex } from '@penumbra-zone/types/hex'; | ||
import { joinLoHi, LoHi } from '@penumbra-zone/types/lo-hi'; | ||
import { serialize, Serialized } from '@/shared/utils/serializer'; | ||
import { BatchSwapSummary as PindexerBatchSwapSummary } from '@/shared/database/schema'; | ||
import { BatchSwapSummary, BlockSummaryApiResponse } from './types'; | ||
|
||
function isLoHi(value: unknown): value is LoHi { | ||
return typeof value === 'object' && value !== null && 'lo' in value; | ||
} | ||
|
||
interface AssetWithInner { | ||
inner: string; | ||
} | ||
|
||
function isAsset(value: unknown): value is AssetWithInner { | ||
return ( | ||
typeof value === 'object' && | ||
value !== null && | ||
'inner' in value && | ||
value.inner !== null && | ||
typeof (value as AssetWithInner).inner === 'string' | ||
); | ||
} | ||
|
||
export const getBatchSwapDisplayData = | ||
(registry: Registry) => | ||
(batchSwapSummary: PindexerBatchSwapSummary): BatchSwapSummary => { | ||
if (!isLoHi(batchSwapSummary.input) || !isLoHi(batchSwapSummary.output)) { | ||
throw new Error('Invalid input or output: expected LoHi type'); | ||
} | ||
|
||
if (!isAsset(batchSwapSummary.asset_start) || !isAsset(batchSwapSummary.asset_end)) { | ||
throw new Error('Invalid asset_start or asset_end'); | ||
} | ||
|
||
const startAssetId = new AssetId({ | ||
inner: hexToUint8Array(base64ToHex(batchSwapSummary.asset_start.inner)), | ||
}); | ||
const startMetadata = registry.getMetadata(startAssetId); | ||
const startExponent = getDisplayDenomExponent.optional(startMetadata) ?? 0; | ||
|
||
const endAssetId = new AssetId({ | ||
inner: hexToUint8Array(base64ToHex(batchSwapSummary.asset_end.inner)), | ||
}); | ||
const endMetadata = registry.getMetadata(endAssetId); | ||
const endExponent = getDisplayDenomExponent.optional(endMetadata) ?? 0; | ||
|
||
const inputBigInt = joinLoHi(batchSwapSummary.input.lo, batchSwapSummary.input.hi); | ||
const outputBigInt = joinLoHi(batchSwapSummary.output.lo, batchSwapSummary.output.hi); | ||
|
||
return { | ||
startAsset: startMetadata, | ||
endAsset: endMetadata, | ||
JasonMHasperhoven marked this conversation as resolved.
Show resolved
Hide resolved
|
||
startInput: pnum(inputBigInt, startExponent).toString(), | ||
endOutput: pnum(outputBigInt, endExponent).toString(), | ||
endPrice: pnum(outputBigInt / inputBigInt, endExponent).toFormattedString(), | ||
numSwaps: batchSwapSummary.num_swaps, | ||
}; | ||
}; | ||
|
||
export async function GET( | ||
_req: NextRequest, | ||
{ params }: { params: { height: string } }, | ||
): Promise<NextResponse<Serialized<BlockSummaryApiResponse>>> { | ||
const chainId = process.env['PENUMBRA_CHAIN_ID']; | ||
if (!chainId) { | ||
return NextResponse.json({ error: 'PENUMBRA_CHAIN_ID is not set' }, { status: 500 }); | ||
} | ||
|
||
const height = params.height; | ||
if (!height) { | ||
return NextResponse.json({ error: 'height is required' }, { status: 400 }); | ||
} | ||
|
||
const registryClient = new ChainRegistryClient(); | ||
const registry = await registryClient.remote.get(chainId); | ||
|
||
const blockSummary = await pindexer.getBlockSummary(Number(height)); | ||
|
||
if (!blockSummary) { | ||
return NextResponse.json({ error: 'Block summary not found' }, { status: 404 }); | ||
} | ||
|
||
return NextResponse.json( | ||
serialize({ | ||
height: blockSummary.height, | ||
time: blockSummary.time, | ||
batchSwaps: blockSummary.batch_swaps.map(getBatchSwapDisplayData(registry)), | ||
numOpenLps: blockSummary.num_open_lps, | ||
numClosedLps: blockSummary.num_closed_lps, | ||
numWithdrawnLps: blockSummary.num_withdrawn_lps, | ||
numSwaps: blockSummary.num_swaps, | ||
numSwapClaims: blockSummary.num_swap_claims, | ||
numTxs: blockSummary.num_txs, | ||
}), | ||
); | ||
} |
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,24 @@ | ||
import { Metadata } from '@penumbra-zone/protobuf/penumbra/core/asset/v1/asset_pb'; | ||
|
||
export interface BatchSwapSummary { | ||
startAsset: Metadata; | ||
endAsset: Metadata; | ||
startInput: string; | ||
endOutput: string; | ||
endPrice: string; | ||
numSwaps: number; | ||
} | ||
|
||
export type BlockSummaryApiResponse = | ||
| { | ||
height: number; | ||
time: Date; | ||
batchSwaps: BatchSwapSummary[]; | ||
numOpenLps: number; | ||
numClosedLps: number; | ||
numWithdrawnLps: number; | ||
numSwaps: number; | ||
numSwapClaims: number; | ||
numTxs: number; | ||
} | ||
| { error: string }; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: how ok is the experience with this component after using it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't try all the props, but it was a straight fwd drop-n-replace 👍