Skip to content

Commit

Permalink
fix: sum all amount/value from messages in cosmos tx
Browse files Browse the repository at this point in the history
  • Loading branch information
Tien Nam Dao committed Nov 8, 2022
1 parent 23df7c9 commit 28a3aa3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 33 deletions.
5 changes: 3 additions & 2 deletions pages/blocks/[block].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import Empty from 'components/Typography/Empty'
import Head from 'next/head'
import React from 'react'
import { getStakingValidatorByHex } from 'utils/address'
import { caculateCosmosAmount, getTransactionType } from 'utils/cosmos'
import { getTransactionType } from 'utils/cosmos'
import { CardInfoLabels } from 'utils/enum'
import { LinkMaker, sortArrayFollowValue } from 'utils/helper'
import TransactionRow from 'views/transactions/TransactionRow'
import { caculateAmount } from 'views/transactions/utils'
import Layout from '../../components/Layout'

type Props = {
Expand Down Expand Up @@ -124,7 +125,7 @@ const BlockDetailPage: React.FC<Props> = ({ errorMessage, blockDetail, blockHeig
) : (
<>
{transactions?.map((item, index) => {
const fee = caculateCosmosAmount(item.fee)
const fee = caculateAmount(item.fee)
return (
<TransactionRow
key={item.hash}
Expand Down
7 changes: 6 additions & 1 deletion types/transactions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ interface TokenAmount {
interface TransactionMessage {
type: TransacionTypeEnum
evmType?: string
content: MsgCreateValidatorContent | MsgUnjailContent | MsgEthereumTxContent | MsgBeginRedelegateContent
content:
| MsgCreateValidatorContent
| MsgUnjailContent
| MsgEthereumTxContent
| MsgBeginRedelegateContent
| MsgSendContent
}

interface TransactionItem {
Expand Down
23 changes: 2 additions & 21 deletions utils/cosmos.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { astraToEth } from '@astradefi/address-converter'
import { BigNumber } from 'ethers'
import { formatEther } from 'ethers/lib/utils'
import { TransactionRowProps } from 'views/transactions/TransactionRow'
import { caculateAmount } from 'views/transactions/utils'
import { TransacionTypeEnum } from './enum'

/**
Expand All @@ -13,25 +13,6 @@ export const getTransactionType = (type: TransacionTypeEnum) => type?.split('.')
export const getTransactionEvmType = (messages: TransactionMessage[]) =>
messages.length > 0 ? messages[0].evmType : ''

/**
* return fee with format
* @param fees
* @returns TokenAmount
*/
export const caculateCosmosAmount = (amounts: TokenAmount[]): TokenAmount => {
if (!amounts || amounts.length == 0) {
return { amount: '0', denom: 'aastra' }
}
let totalAmount = BigNumber.from('0')
for (let amount of amounts) {
totalAmount = totalAmount.add(BigNumber.from(amount.amount))
}
return {
amount: totalAmount.toBigInt().toString(),
denom: amounts[0].denom
}
}

export const convertMessageToTransfer = (
messages: TransactionMessage[],
blockTime: string,
Expand All @@ -53,7 +34,7 @@ export const convertMessageToTransfer = (
}

const _parseCosmosMsgSend = (content: MsgSendContent, blockTime: string, status: boolean): TransactionRowProps => {
const amountItem = caculateCosmosAmount(content?.amount)
const amountItem = caculateAmount(content?.amount)
return {
style: 'inject',
blockNumber: content.height,
Expand Down
5 changes: 3 additions & 2 deletions views/transactions/hook/useTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import usePagination from 'hooks/usePagination'
import { differenceWith, isEmpty } from 'lodash'
import { useCallback, useEffect, useState } from 'react'
import useSWR from 'swr'
import { caculateCosmosAmount, getTransactionEvmType } from 'utils/cosmos'
import { getTransactionEvmType } from 'utils/cosmos'
import { getFromToEvmTxFromCosmosEntry } from 'utils/evm'
import { caculateAmount } from '../utils'

export default function useTransaction() {
const [_items, _setTransactionItem] = useState<TransactionItemModified[]>()
Expand Down Expand Up @@ -32,7 +33,7 @@ export default function useTransaction() {

const convertData = (transactionResult: TransactionItem[]) => {
return transactionResult.map((item: TransactionItem) => {
const totalFee = caculateCosmosAmount(item.fee)
const totalFee = caculateAmount(item.fee)
const evmType = getTransactionEvmType(item.messages)
const { from, to, evmHash } = getFromToEvmTxFromCosmosEntry(item.messages[0])
return {
Expand Down
49 changes: 42 additions & 7 deletions views/transactions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import API_LIST from 'api/api_list'
import { BigNumber } from 'ethers'
import { formatEther, formatUnits } from 'ethers/lib/utils'
import numeral from 'numeral'
import { caculateCosmosAmount, convertMessageToTransfer, getTransactionType } from 'utils/cosmos'
import { convertMessageToTransfer, getTransactionType } from 'utils/cosmos'
import { TransacionTypeEnum } from 'utils/enum'
import { evmConvertTokenTransferToTransactionRow, evmTransactionType, isEmptyRawInput } from 'utils/evm'
import { TransactionRowProps } from './TransactionRow'
Expand Down Expand Up @@ -91,7 +91,7 @@ export const evmTransactionDetail = async (evmHash?: string, cosmosHash?: string
data.from = result.from
data.to = result.to
data.createdContractAddressHash = result.createdContractAddressHash
data.value = (result.value ? formatEther(result.value) : caculateTxAmount(result.messages)) || '0'
data.value = (result.value ? formatEther(result.value) : caculateEthereumTxAmount(result.messages)) || '0'
data.fee = result.fee && result.fee.length > 0 ? formatEther(result.fee[0].amount) : ''
data.gasPrice = result.gasPrice ? formatUnits(result.gasPrice, 9) + ' NanoAstra' : ''
data.gasLimit = result.gasLimit ? formatNumber(result.gasLimit, 0) : ''
Expand Down Expand Up @@ -141,7 +141,7 @@ export const evmTransactionDetail = async (evmHash?: string, cosmosHash?: string
data.from = result.from
data.to = result.to
data.createdContractAddressHash = result.createdContractAddressHash
data.value = (result.value ? formatEther(result.value) : caculateTxAmount(result.messages)) || '0'
data.value = (result.value ? formatEther(result.value) : caculateEthereumTxAmount(result.messages)) || '0'
data.fee = result.fee && result.fee.length > 0 ? formatEther(result.fee[0].amount) : ''
data.gasPrice = result.gasPrice ? formatUnits(result.gasPrice, 9) + ' NanoAstra' : ''
data.gasLimit = result.gasLimit ? formatNumber(result.gasLimit, 0) : ''
Expand Down Expand Up @@ -182,7 +182,7 @@ export const evmTransactionDetail = async (evmHash?: string, cosmosHash?: string

export const cosmsTransactionDetail = async (result: TransactionItem): Promise<TransactionDetail> => {
const data: TransactionDetail = {}
const fee = caculateCosmosAmount(result.fee)
const fee = caculateAmount(result.fee)
data.type = 'cosmos'
data.pageTitle = getTransactionType(result?.messages[0]?.type)
data.evmHash = ''
Expand All @@ -192,7 +192,7 @@ export const cosmsTransactionDetail = async (result: TransactionItem): Promise<T
data.blockHeight = `${result.blockHeight}`
data.time = result.blockTime
data.failLog = !result.success ? result.log : undefined
data.value = caculateTxAmount(result.messages) || '0'
data.value = caculateCosmosTxAmount(result.messages) || '0'
// data.from = result.from
// data.to = result.to
// data.tokenTransfer = []
Expand All @@ -219,11 +219,11 @@ export const cosmsTransactionDetail = async (result: TransactionItem): Promise<T
}

/**
* return tx amount
* return ethereumtx amount
* @param tx messages
* @returns amount in string
*/
export const caculateTxAmount = (messages: TransactionMessage[]): string => {
export const caculateEthereumTxAmount = (messages: TransactionMessage[]): string => {
if (messages && messages.length > 0) {
let totalAmount = BigNumber.from('0')
for (let message of messages) {
Expand All @@ -234,6 +234,41 @@ export const caculateTxAmount = (messages: TransactionMessage[]): string => {
return '0'
}

/**
* return cosmos amount
* @param tx messages
* @returns amount in string
*/
export const caculateCosmosTxAmount = (messages: TransactionMessage[]): string => {
if (messages && messages.length > 0) {
let totalAmount = BigNumber.from('0')
for (let message of messages) {
totalAmount = totalAmount.add(BigNumber.from(caculateAmount(message.content?.amount).amount || '0'))
}
return formatEther(totalAmount)
}
return '0'
}

/**
* return amount with format
* @param amounts
* @returns TokenAmount
*/
export const caculateAmount = (amounts: TokenAmount[]): TokenAmount => {
if (!amounts || amounts.length == 0) {
return { amount: '0', denom: 'aastra' }
}
let totalAmount = BigNumber.from('0')
for (let amount of amounts) {
totalAmount = totalAmount.add(BigNumber.from(amount.amount))
}
return {
amount: totalAmount.toBigInt().toString(),
denom: amounts[0].denom
}
}

const _convertTransfer = (
data: TransactionDetail,
messages: TransactionMessage[],
Expand Down

0 comments on commit 28a3aa3

Please sign in to comment.