Skip to content

Commit

Permalink
feat: EIP-5792 (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom authored May 7, 2024
1 parent b8c1828 commit 7011e24
Show file tree
Hide file tree
Showing 17 changed files with 1,434 additions and 348 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"remeda": "^1.14.0",
"sonner": "^0.7.2",
"use-sync-external-store": "^1.2.0",
"viem": "^2.9.13",
"viem": "^2.10.1",
"zustand": "^4.3.8"
},
"devDependencies": {
Expand Down
59 changes: 37 additions & 22 deletions src/components/abi/DecodedCalldata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ export function DecodedCalldata({
address,
data,
labelRight,
}: { address?: Address | null; data: Hex; labelRight?: ReactNode }) {
showRawData,
}: {
address?: Address | null
data: Hex
labelRight?: ReactNode
showRawData?: boolean
}) {
const selector = slice(data, 0, 4)

// Try extract ABI from whatsabi autoloading (etherscan, 4byte dbs, etc)
Expand Down Expand Up @@ -107,7 +113,7 @@ export function DecodedCalldata({
</Text>
</Box>
)}
<LabelledContent label="Function">
<LabelledContent label="Signature">
<Box
backgroundColor="surface/primary"
paddingHorizontal="8px"
Expand All @@ -128,32 +134,41 @@ export function DecodedCalldata({
)}
</>
)}
{abiItem && <Separator />}
<LabelledContent label="Raw Data" labelRight={labelRight}>
<Box
backgroundColor="surface/primary"
paddingHorizontal="8px"
paddingVertical="12px"
>
{selector && rawArgs ? (
<Text family="mono" size="11px">
<Text color="text/tertiary">{selector}</Text>
{rawArgs.replace('0x', '')}
</Text>
) : (
<Text family="mono" size="11px">
{data}
</Text>
)}
</Box>
</LabelledContent>
{showRawData && abiItem && <Separator />}
{showRawData && (
<LabelledContent label="Raw Data" labelRight={labelRight}>
<Box
backgroundColor="surface/primary"
paddingHorizontal="8px"
paddingVertical="12px"
>
{selector && rawArgs ? (
<Text family="mono" size="11px">
<Text color="text/tertiary">{selector}</Text>
{rawArgs.replace('0x', '')}
</Text>
) : (
<Text family="mono" size="11px">
{data}
</Text>
)}
</Box>
</LabelledContent>
)}
</Stack>
)
}

function getAbiItem({ abi, selector }: { abi: Abi; selector: Hex }) {
const abiItem =
(getAbiItem_viem({ abi, name: selector }) as AbiItem) ||
(getAbiItem_viem({
abi: abi.map((x) => ({
...x,
inputs: (x as any).inputs || [],
outputs: (x as any).outputs || [],
})),
name: selector,
}) as AbiItem) ||
abi.find((x: any) => x.name === selector) ||
abi.find((x: any) => x.selector === selector)
if (!abiItem) return
Expand Down
37 changes: 37 additions & 0 deletions src/components/abi/FormattedAbiFunctionName.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useMemo } from 'react'
import { type Hex, parseAbiItem, slice } from 'viem'

import { Text } from '~/design-system'
import { useLookupSignature } from '../../hooks/useLookupSignature'

import { FormattedAbiItem } from './FormattedAbiItem'

export function FormattedAbiFunctionName({ data }: { data: Hex }) {
const selector = slice(data, 0, 4)
const { data: signature } = useLookupSignature({
selector,
})

const abiItem = useMemo(() => {
if (!signature) return
const abiItem = parseAbiItem(`function ${signature}`)
if (abiItem.type !== 'function') return
return abiItem
}, [signature])

return (
<Text family="mono" size="11px" wrap={false}>
{abiItem ? (
<FormattedAbiItem
abiItem={abiItem}
compact
showIndexed={false}
showParameters={false}
showType={false}
/>
) : (
selector
)}
</Text>
)
}
4 changes: 3 additions & 1 deletion src/components/abi/FormattedAbiItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function FormattedAbiItem({
compact,
showIndexed = true,
showParameterNames = true,
showParameters = true,
showStateMutability = true,
showReturns = true,
showType = true,
Expand All @@ -19,6 +20,7 @@ export function FormattedAbiItem({
compact?: boolean
showIndexed?: boolean
showParameterNames?: boolean
showParameters?: boolean
showStateMutability?: boolean
showReturns?: boolean
showType?: boolean
Expand All @@ -30,7 +32,7 @@ export function FormattedAbiItem({
<>
{showType && <Text color="text/tertiary">function </Text>}
{abiItem.name && <Text>{abiItem.name}</Text>}(
{abiItem.inputs && (
{showParameters && abiItem.inputs && (
<FormattedAbiParameters
params={abiItem.inputs}
showNames={showParameterNames}
Expand Down
Loading

0 comments on commit 7011e24

Please sign in to comment.