Skip to content

Commit dba2d9e

Browse files
authored
Merge pull request #1845 from oasisprotocol/lw/format-event-values
Format token amounts in events
2 parents ae2996b + 245b628 commit dba2d9e

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

.changelog/1845.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Format token amounts in events

src/app/components/RuntimeEvents/RuntimeEventDetails.tsx

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { EvmAbiParam, RuntimeEvent, RuntimeEventType } from '../../../oasis-nexus/api'
22
import { FC } from 'react'
33
import { TFunction } from 'i18next'
4-
import { useTranslation } from 'react-i18next'
4+
import { Trans, useTranslation } from 'react-i18next'
55
import { StyledDescriptionList } from '../StyledDescriptionList'
66
import { useScreenSize } from '../../hooks/useScreensize'
77
import Table from '@mui/material/Table'
@@ -31,6 +31,9 @@ import LanIcon from '@mui/icons-material/Lan'
3131
import LanOutlinedIcon from '@mui/icons-material/LanOutlined'
3232
import { MethodIcon } from '../ConsensusTransactionMethod'
3333
import { TransactionLink } from '../Transactions/TransactionLink'
34+
import Tooltip from '@mui/material/Tooltip'
35+
import { tooltipDelay } from '../../../styles/theme'
36+
import { PlaceholderLabel } from '../../utils/PlaceholderLabel'
3437

3538
const getRuntimeEventMethodLabel = (t: TFunction, method: string | undefined) => {
3639
switch (method) {
@@ -116,6 +119,7 @@ const EvmEventParamData: FC<{
116119
address?: string
117120
alwaysTrimOnTable?: boolean
118121
}> = ({ scope, param, address, alwaysTrimOnTable }) => {
122+
const { t } = useTranslation()
119123
/**
120124
* According to the API docs:
121125
*
@@ -131,8 +135,43 @@ const EvmEventParamData: FC<{
131135
<AccountLink address={address} scope={scope} alwaysTrimOnTablet={alwaysTrimOnTable} />
132136
) : null
133137
case 'uint256':
134-
// TODO: format with BigNumber
135-
return <span>{param.value as string}</span>
138+
if (param.evm_token?.type === 'ERC20') {
139+
return (
140+
<Tooltip
141+
arrow
142+
placement="top"
143+
title={param.value_raw}
144+
enterDelay={tooltipDelay}
145+
enterNextDelay={tooltipDelay}
146+
>
147+
<span>
148+
{t('common.valueInToken', {
149+
...getPreciseNumberFormat(param.value as string),
150+
ticker: param.evm_token.symbol,
151+
})}
152+
</span>
153+
</Tooltip>
154+
)
155+
}
156+
if (param.evm_token?.type === 'ERC721') {
157+
return (
158+
<Trans
159+
t={t}
160+
i18nKey="common.tokenInstance"
161+
components={{
162+
InstanceLink: <PlaceholderLabel label={param.value as string} />,
163+
TickerLink: <PlaceholderLabel label={param.evm_token.symbol ?? t('common.missing')} />,
164+
}}
165+
/>
166+
)
167+
}
168+
return (
169+
<span>
170+
{t('common.valueLong', {
171+
...getPreciseNumberFormat(param.value as string),
172+
})}
173+
</span>
174+
)
136175
default:
137176
return <span>{JSON.stringify(param.value, null, ' ')}</span>
138177
}

src/app/components/Tokens/TokenTransfers.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { TokenLink } from './TokenLink'
1313
import { PlaceholderLabel } from '../../utils/PlaceholderLabel'
1414
import { TokenTypeTag } from './TokenList'
1515
import { parseEvmEvent } from '../../utils/parseEvmEvent'
16-
import { fromBaseUnits } from '../../utils/number-utils'
1716
import { TransferIcon } from '../TransferIcon'
1817
import { TableCellAge } from '../TableCellAge'
1918
import { TableHeaderAge } from '../TableHeaderAge'
@@ -33,14 +32,10 @@ export const EventBalance: FC<{
3332

3433
const tokenEthAddress = event.body.address
3534
const tokenType = event.evm_token?.type
36-
const tokenDecimals = event.evm_token?.decimals
3735
const ticker = event.evm_token?.symbol
3836

3937
if (tokenType === EvmTokenType.ERC20) {
40-
// We are calling it 'raw' since it's not yet normalized according to decimals.
41-
const rawValue = event.evm_log_params?.find(param => param.name === 'value')?.value as string | undefined
42-
const value = rawValue === undefined ? undefined : fromBaseUnits(rawValue, tokenDecimals || 0)
43-
38+
const value = event.evm_log_params?.find(param => param.name === 'value')?.value as string | undefined
4439
return (
4540
<RoundedBalance
4641
value={value}

src/oasis-nexus/api.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ declare module './generated/api' {
9393
layer: Layer
9494
}
9595

96+
export interface EvmAbiParam {
97+
/** Added to fields that are likely an amount in tokens */
98+
evm_token?: EvmEventToken
99+
value_raw?: string
100+
}
101+
96102
export interface ConsensusEvent {
97103
network: Network
98104
}
@@ -893,6 +899,28 @@ const fixChecksumAddressInEvmEventParam = (param: generated.EvmAbiParam): genera
893899
}
894900
: param
895901

902+
const addTokenToParams = (event: generated.RuntimeEvent) => {
903+
if (event.evm_token?.type === 'ERC20') {
904+
if (event.evm_log_name === 'Transfer' || event.evm_log_name === 'Approval') {
905+
const valueParam = event.evm_log_params?.[2]
906+
if (valueParam?.evm_type === 'uint256' && typeof valueParam.value === 'string') {
907+
valueParam.evm_token = event.evm_token
908+
valueParam.value_raw = valueParam.value
909+
valueParam.value = fromBaseUnits(valueParam.value, event.evm_token.decimals ?? 0)
910+
}
911+
}
912+
}
913+
if (event.evm_token?.type === 'ERC721') {
914+
if (event.evm_log_name === 'Transfer' || event.evm_log_name === 'Approval') {
915+
const tokenParam = event.evm_log_params?.[2]
916+
if (tokenParam?.evm_type === 'uint256' && typeof tokenParam.value === 'string') {
917+
tokenParam.evm_token = event.evm_token
918+
}
919+
}
920+
}
921+
return event
922+
}
923+
896924
export const useGetRuntimeEvents: typeof generated.useGetRuntimeEvents = (
897925
network,
898926
runtime,
@@ -929,6 +957,9 @@ export const useGetRuntimeEvents: typeof generated.useGetRuntimeEvents = (
929957
network,
930958
}
931959
})
960+
.map(event => {
961+
return addTokenToParams(event)
962+
})
932963
.map((event): generated.RuntimeEvent => {
933964
if (
934965
event.type === RuntimeEventType.accountstransfer ||

0 commit comments

Comments
 (0)