-
Notifications
You must be signed in to change notification settings - Fork 114
Transaction Explorer: Token Summary #1731
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fe94dfa
UI done with mock data
quietbits dea134e
Fix tests
quietbits 82afaf8
Update address style
quietbits 7659145
Use real data
quietbits a26ab89
Merge branch 'main' of https://github.com/stellar/laboratory into tx-…
quietbits 1e644bd
Resolved merge conflicts
quietbits dbb2793
Tests added
quietbits 0767f3d
Merge branch 'main' of https://github.com/stellar/laboratory into tx-…
quietbits 10933f4
Handle no asset case
quietbits a80173c
Merge branch 'main' of https://github.com/stellar/laboratory into tx-…
quietbits 1258eeb
Fix StellarExpert link
quietbits 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 hidden or 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 hidden or 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
216 changes: 216 additions & 0 deletions
216
src/app/(sidebar)/transaction-dashboard/components/TokenSummary.tsx
This file contains hidden or 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,216 @@ | ||
| import { Fragment } from "react"; | ||
| import { CopyText, Icon, IconButton, Link, Text } from "@stellar/design-system"; | ||
| import { BigNumber } from "bignumber.js"; | ||
|
|
||
| import { Box } from "@/components/layout/Box"; | ||
| import { DataTable } from "@/components/DataTable"; | ||
| import { TransactionTabEmptyMessage } from "@/components/TransactionTabEmptyMessage"; | ||
|
|
||
| import { formatAmount } from "@/helpers/formatAmount"; | ||
| import { shortenStellarAddress } from "@/helpers/shortenStellarAddress"; | ||
| import { getStellarExpertNetwork } from "@/helpers/getStellarExpertNetwork"; | ||
|
|
||
| import { STELLAR_EXPERT } from "@/constants/settings"; | ||
| import { useStore } from "@/store/useStore"; | ||
|
|
||
| import { RpcTxJsonResponse } from "@/types/types"; | ||
|
|
||
| export const TokenSummary = ({ | ||
| txDetails, | ||
| }: { | ||
| txDetails: RpcTxJsonResponse | null | undefined; | ||
| }) => { | ||
| type TokenItem = { | ||
| addressFrom: string; | ||
| addressTo: string; | ||
| amount: string; | ||
| assetCode: string; | ||
| assetIssuer: string; | ||
| }; | ||
|
|
||
| type EventGroupsType = { | ||
| native: { | ||
| id: string; | ||
| title: string; | ||
| items: TokenItem[]; | ||
| }; | ||
| asset: { | ||
| id: string; | ||
| title: string; | ||
| items: TokenItem[]; | ||
| }; | ||
| }; | ||
|
|
||
| const getGroupedTransferEvents = () => { | ||
| const formattedEvents = getTransferEvents(txDetails); | ||
|
|
||
| const groups: EventGroupsType = { | ||
| asset: { | ||
| id: "asset-ev", | ||
| title: "Contract token transferred", | ||
| items: [], | ||
| }, | ||
| native: { | ||
| id: "native-ev", | ||
| title: "Native token (XLM) transferred", | ||
| items: [], | ||
| }, | ||
| }; | ||
|
|
||
| formattedEvents.forEach((event) => { | ||
| if (event.assetCode === "XLM" && event.assetIssuer === "native") { | ||
| groups.native.items.push(event); | ||
| } else { | ||
| groups.asset.items.push(event); | ||
| } | ||
| }); | ||
|
|
||
| // Return only groups with items | ||
| return Object.values(groups).filter((group) => group.items.length); | ||
| }; | ||
|
|
||
| const { network } = useStore(); | ||
|
|
||
| const seNetwork = getStellarExpertNetwork(network.id); | ||
quietbits marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const formatAssetAddress = (address: string) => { | ||
| const isContractAddress = address.startsWith("C"); | ||
|
|
||
| return ( | ||
| <Box | ||
| gap="sm" | ||
| direction="row" | ||
| align="center" | ||
| addlClassName="TransactionTokenSummary__address" | ||
| > | ||
| <Link | ||
| href={`${STELLAR_EXPERT}/${seNetwork}/${isContractAddress ? "contract" : "account"}/${address}`} | ||
| > | ||
| {shortenStellarAddress(address)} | ||
| </Link> | ||
|
|
||
| <CopyText textToCopy={address} tooltipPlacement="top"> | ||
| <IconButton | ||
| icon={<Icon.Copy01 />} | ||
| altText="Copy address" | ||
| customSize="1rem" | ||
| /> | ||
| </CopyText> | ||
| </Box> | ||
| ); | ||
| }; | ||
|
|
||
| const formatAssetAmount = (amount: string) => { | ||
| return ( | ||
| <span className="TransactionTokenSummary__amount"> | ||
| {formatAmount( | ||
| new BigNumber(amount).dividedBy(10_000_000).toNumber(), | ||
quietbits marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 2, | ||
| )} | ||
| </span> | ||
| ); | ||
| }; | ||
|
|
||
| const formatAsset = (code: string, issuer: string) => { | ||
| if (!code || !issuer) { | ||
| return null; | ||
| } | ||
|
|
||
| const assetString = | ||
| code === "XLM" && issuer === "native" ? code : `${code}-${issuer}`; | ||
|
|
||
| return ( | ||
| <Link | ||
| href={`${STELLAR_EXPERT}/${seNetwork}/asset/${assetString}`} | ||
| icon={<Icon.LinkExternal01 />} | ||
| iconPosition="right" | ||
| > | ||
| {code} | ||
| </Link> | ||
| ); | ||
| }; | ||
|
|
||
| const groupedTransferEvents = getGroupedTransferEvents(); | ||
|
|
||
| if (!groupedTransferEvents.length) { | ||
| return ( | ||
| <TransactionTabEmptyMessage> | ||
| There are no transfer events in this transaction | ||
| </TransactionTabEmptyMessage> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <Box gap="lg" addlClassName="TransactionTokenSummary"> | ||
| {groupedTransferEvents.map((ev) => ( | ||
| <Fragment key={ev.id}> | ||
| <Box gap="md" data-testid={ev.id}> | ||
| <Text as="div" size="sm" weight="medium"> | ||
| {ev.title} | ||
| </Text> | ||
|
|
||
| <DataTable | ||
| tableId="tx-dash-token-summary-assets" | ||
| tableData={ev.items} | ||
| tableHeaders={[ | ||
| { id: "address-from", value: "From" }, | ||
| { id: "address-to", value: "To" }, | ||
| { id: "amount", value: "Amount" }, | ||
| { id: "asset", value: "Asset" }, | ||
| ]} | ||
| formatDataRow={(ti: TokenItem) => [ | ||
| { value: formatAssetAddress(ti.addressFrom), isOverflow: true }, | ||
| { value: formatAssetAddress(ti.addressTo), isOverflow: true }, | ||
| { value: formatAssetAmount(ti.amount) }, | ||
| { value: formatAsset(ti.assetCode, ti.assetIssuer) }, | ||
| ]} | ||
| cssGridTemplateColumns="minmax(210px, 1fr) minmax(210px, 1fr) minmax(160px, 1fr) minmax(110px, 1fr)" | ||
| hidePagination={true} | ||
| /> | ||
| </Box> | ||
| </Fragment> | ||
| ))} | ||
| </Box> | ||
| ); | ||
| }; | ||
|
|
||
| const getTransferEvents = (data: RpcTxJsonResponse | null | undefined) => { | ||
| const events = data?.events?.contractEventsJson; | ||
|
|
||
| if (!events) { | ||
| return []; | ||
| } | ||
|
|
||
| const contractEvents = Array.isArray(events[0]) | ||
| ? events.flatMap((group: any[]) => group) | ||
| : events; | ||
|
|
||
| return contractEvents | ||
| .filter((event: any) => event?.body?.v0?.topics?.[0]?.symbol === "transfer") | ||
| .map((ev: any) => { | ||
| const addressFrom = ev.body.v0.topics[1].address; | ||
| const addressTo = ev.body.v0.topics[2].address; | ||
|
|
||
| const token = ev.body.v0.topics?.[3]?.string; | ||
|
|
||
| let assetCode = ""; | ||
| let assetIssuer = ""; | ||
|
|
||
| if (token) { | ||
| [assetCode, assetIssuer] = token.split(":"); | ||
| } | ||
jeesunikim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return { | ||
| addressFrom, | ||
| addressTo, | ||
| // Transfer event data type https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0041.md#transfer-event | ||
| amount: | ||
| ev.body.v0.data?.map?.[0]?.val?.i128 || | ||
| ev.body.v0.data?.i128 || | ||
| ev.body.v0.data?.amount || | ||
| "0", | ||
| assetCode: assetCode === "native" ? "XLM" : assetCode, | ||
| assetIssuer: assetCode === "native" ? "native" : assetIssuer, | ||
| }; | ||
| }); | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
| import { Text } from "@stellar/design-system"; | ||
| import { Box } from "@/components/layout/Box"; | ||
|
|
||
| export const TransactionTabEmptyMessage = ({ | ||
| children, | ||
| }: { | ||
| children: React.ReactNode; | ||
| }) => ( | ||
| <Box | ||
| gap="sm" | ||
| align="center" | ||
| justify="center" | ||
| addlClassName="TransactionTab__emptyMessage" | ||
| > | ||
| <Text as="div" size="sm" weight="medium"> | ||
| {children} | ||
| </Text> | ||
| </Box> | ||
| ); |
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.