Skip to content

Commit

Permalink
Add missing props to Consensus tx details
Browse files Browse the repository at this point in the history
  • Loading branch information
buberdds committed Jul 15, 2024
1 parent dc3ad4a commit 2539f26
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 26 deletions.
1 change: 1 addition & 0 deletions .changelog/1480.trivial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add missing props to Consensus tx details
2 changes: 1 addition & 1 deletion src/app/components/Delegations/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const Delegations: FC<DelegationsProps> = ({
const tableColumns: TableColProps[] = [
{ key: 'delegator', content: t('common.address') },
{ key: 'amount', content: t('validator.amount'), align: TableCellAlign.Right },
{ key: 'shares', content: t('validator.shares'), align: TableCellAlign.Right },
{ key: 'shares', content: t('common.shares'), align: TableCellAlign.Right },
...(debonding
? [{ key: 'debondingEnd', content: t('validator.debondingEnd'), align: TableCellAlign.Right }]
: []),
Expand Down
83 changes: 62 additions & 21 deletions src/app/pages/ConsensusTransactionDetailPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useParams } from 'react-router-dom'
import { useTranslation } from 'react-i18next'
import { styled } from '@mui/material/styles'
import { Transaction, useGetConsensusTransactionsTxHash } from '../../../oasis-nexus/api'
import { getConsensusTransactionAmount, getConsensusTransactionToAddress } from '../../utils/transaction'
import { StyledDescriptionList } from '../../components/StyledDescriptionList'
import { PageLayout } from '../../components/PageLayout'
import { SubPageCard } from '../../components/SubPageCard'
Expand All @@ -18,6 +19,11 @@ import { ConsensusTransactionMethod } from 'app/components/ConsensusTransactionM
import { useFormattedTimestampStringWithDistance } from 'app/hooks/useFormattedTimestamp'
import { RoundedBalance } from 'app/components/RoundedBalance'
import { AccountLink } from 'app/components/Account/AccountLink'
import { getPreciseNumberFormat } from 'locales/getPreciseNumberFormat'
import { CurrentFiatValue } from '../RuntimeTransactionDetailPage/CurrentFiatValue'
import { AllTokenPrices, useAllTokenPrices } from 'coin-gecko/api'
import { getFiatCurrencyForScope } from '../../../config'
import { transaction } from '@oasisprotocol/client-rt'

const StyledDescriptionDetails = styled('dd')({
'&&': { padding: 0 },
Expand All @@ -28,6 +34,7 @@ export const ConsensusTransactionDetailPage: FC = () => {
const scope = useRequiredScopeParam()
const hash = useParams().hash!
const { isLoading, data } = useGetConsensusTransactionsTxHash(scope.network, hash)
const tokenPrices = useAllTokenPrices(getFiatCurrencyForScope(scope))
const transaction = data?.data
if (!transaction && !isLoading) {
throw AppErrors.NotFoundTxHash
Expand All @@ -36,7 +43,12 @@ export const ConsensusTransactionDetailPage: FC = () => {
return (
<PageLayout>
<SubPageCard featured title={t('transaction.header')}>
<ConsensusTransactionDetailView isLoading={isLoading} transaction={transaction} detailsPage />
<ConsensusTransactionDetailView
detailsPage
isLoading={isLoading}
tokenPrices={tokenPrices}
transaction={transaction}
/>
</SubPageCard>
</PageLayout>
)
Expand All @@ -50,14 +62,19 @@ export const ConsensusTransactionDetailView: FC<{
isLoading?: boolean
transaction: TransactionDetailConsensusBlock | undefined
detailsPage?: boolean
}> = ({ detailsPage, isLoading, transaction }) => {
tokenPrices?: AllTokenPrices
}> = ({ detailsPage, isLoading, transaction, tokenPrices }) => {
const { t } = useTranslation()
const { isMobile } = useScreenSize()
const formattedTimestamp = useFormattedTimestampStringWithDistance(transaction?.timestamp)

if (isLoading) return <TextSkeleton numberOfRows={detailsPage ? 13 : 7} />
if (!transaction) return <></>

const to = getConsensusTransactionToAddress(transaction)
const amount = getConsensusTransactionAmount(transaction)
const tokenPriceInfo = tokenPrices?.[transaction.ticker]

return (
<StyledDescriptionList
titleWidth={isMobile ? '100px' : '200px'}
Expand Down Expand Up @@ -86,33 +103,57 @@ export const ConsensusTransactionDetailView: FC<{
<dt>{t('common.from')}</dt>
<dd>
<AccountLink scope={transaction} address={transaction.sender} />
<CopyToClipboard value={transaction.sender} />
</dd>
<dt>{t('common.to')}</dt>
<dd>
{/* TODO: show recipients address when props is added to API */}
<>-</>
</dd>
<dt>{t('common.value')}</dt>
<dd>
{/* TODO: getPreciseNumberFormat when API returns amount prop */}
<>-</>
</dd>
{detailsPage && (
{to && (
<>
<dt>{t('currentFiatValue.title')}</dt>
<dt>{t('common.to')}</dt>
<dd>
{/* TODO: show CurrentFiatValue when API returns amount prop */}
<>-</>
<AccountLink scope={{ layer: transaction.layer, network: transaction.network }} address={to} />
<CopyToClipboard value={to} />
</dd>
<dt>{t('common.transactionFee')}</dt>
</>
)}
{amount && (
<>
<dt>{t('common.value')}</dt>
<dd>
<RoundedBalance value={transaction.fee} ticker={transaction.ticker} />
{t('common.valueInToken', {
...getPreciseNumberFormat(amount),
ticker: transaction.ticker,
})}
</dd>
<dt>{t('common.gasUsed')}</dt>
</>
)}
{detailsPage && (
<>
{amount &&
!!tokenPriceInfo &&
!tokenPriceInfo.isLoading &&
!tokenPriceInfo.isFree &&
tokenPriceInfo.price !== undefined && (
<>
<dt>{t('currentFiatValue.title')}</dt>
<dd>
<CurrentFiatValue amount={amount} {...tokenPriceInfo} />
</dd>
</>
)}
{transaction.body?.shares && (
<>
<dt>{t('common.shares')}</dt>
<dd>
<RoundedBalance compactLargeNumbers value={transaction.body?.shares} />
</dd>
</>
)}
<dt>{t('common.transactionFee')}</dt>
<dd>
{/* TODO: show when API returns gas_used prop */}
<>-</>
<RoundedBalance value={transaction.fee} ticker={transaction.ticker} />
</dd>

{/* TODO: gasUsed field will be available for Nexus with the next oasis-core release */}

{transaction.gas_limit && (
<>
<dt>{t('common.gasLimit')}</dt>
Expand Down
28 changes: 28 additions & 0 deletions src/app/utils/transaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ConsensusTxMethod, Transaction } from '../../oasis-nexus/api'

export const getConsensusTransactionToAddress = (transaction: Transaction) => {
switch (transaction.method) {
case ConsensusTxMethod.stakingAddEscrow:
return transaction.body?.address
case ConsensusTxMethod.stakingAllow:
return transaction.body?.beneficiary
case ConsensusTxMethod.stakingReclaimEscrow:
return transaction.body?.account
case ConsensusTxMethod.stakingTransfer:
return transaction.body?.to
default:
return undefined
}
}

export const getConsensusTransactionAmount = (transaction: Transaction) => {
switch (transaction.method) {
case ConsensusTxMethod.stakingAllow:
return transaction.body?.amount_change
case ConsensusTxMethod.stakingAddEscrow:
case ConsensusTxMethod.stakingTransfer:
return transaction.body?.amount
default:
return undefined
}
}
2 changes: 1 addition & 1 deletion src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"select": "Select",
"size": "Size",
"sapphire": "Sapphire",
"shares": "Shares",
"show": "Show",
"smartContract": "Smart Contract",
"smartContract_short": "Contract",
Expand Down Expand Up @@ -671,7 +672,6 @@
"signedBlocksDescription": "Last 100 blocks",
"proposedBlocks": "Proposed Blocks",
"snapshot": "Validator Snapshot",
"shares": "Shares",
"sharesDocs": "How shares are calculated",
"staked": "Staked",
"stakingTrend": "Staking Trend",
Expand Down
15 changes: 12 additions & 3 deletions src/oasis-nexus/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,19 @@ export const useGetConsensusTransactionsTxHash: typeof generated.useGetConsensus
...options?.request,
transformResponse: [
...arrayify(axios.defaults.transformResponse),
(data: generated.Transaction, headers, status) => {
if (status !== 200) return data
(transaction: generated.Transaction, headers, status) => {
if (status !== 200) return transaction
return {
...data,
...transaction,
body: {
...transaction.body,
amount: transaction.body?.amount
? fromBaseUnits(transaction.body.amount, consensusDecimals)
: undefined,
amount_change: transaction.body?.amount_change
? fromBaseUnits(transaction.body.amount_change, consensusDecimals)
: undefined,
},
layer: Layer.consensus,
network,
ticker,
Expand Down

0 comments on commit 2539f26

Please sign in to comment.