Skip to content

Commit

Permalink
fix: Replace Asset to Amount and better 'Asset' data format (#3151)
Browse files Browse the repository at this point in the history
fix: Replace `Asset` to `Amount`
  • Loading branch information
yanguoyu authored May 24, 2024
1 parent a544080 commit bed901b
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 29 deletions.
24 changes: 15 additions & 9 deletions packages/neuron-ui/src/components/FormattedTokenAmount/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type FormattedTokenAmountProps = { item: State.Transaction; show: boolean; symbo
type AmountProps = Omit<FormattedTokenAmountProps, 'isNeedCopy'> & {
sudtAmount?: string
isReceive: boolean
amount: string
amount?: string
symbolClassName?: string
}

Expand All @@ -30,18 +30,18 @@ const Amount = ({ sudtAmount, show, item, isReceive, amount, symbolClassName, sy
</div>
) : (
<div>
<span className={show ? styles.amount : ''} data-direction={isReceive ? 'receive' : 'send'}>
{amount}
<span className={show ? styles.amount : ''} data-direction={amount && (isReceive ? 'receive' : 'send')}>
{amount ?? '--'}
</span>
&nbsp;{symbol}
&nbsp;{amount ? symbol : ''}
</div>
)
}

export const FormattedTokenAmount = ({ item, show, symbolClassName }: FormattedTokenAmountProps) => {
let amount = '--'
let amount: string | undefined
let sudtAmount = ''
let copyText = amount
let copyText: string | undefined = amount
let isReceive = false
let symbol = ''

Expand All @@ -61,16 +61,22 @@ export const FormattedTokenAmount = ({ item, show, symbolClassName }: FormattedT
isReceive = !sudtAmount.includes('-')
}
} else {
amount = show ? `${shannonToCKBFormatter(item.value, true)}` : `${HIDE_BALANCE}`
isReceive = !amount.includes('-')
amount = show
? `${shannonToCKBFormatter(item.nervosDao ? item.daoCapacity ?? '--' : item.value, true)}`
: `${HIDE_BALANCE}`
isReceive = !amount?.includes('-')
copyText = `${amount} CKB`
symbol = 'CKB'
if (item.nervosDao && item.daoCapacity === undefined) {
amount = undefined
copyText = undefined
}
}
}

const props = { sudtAmount, show, item, isReceive, amount, symbolClassName, symbol }

return show ? (
return show && copyText ? (
<CopyZone content={copyText}>
<Amount {...props} />
</CopyZone>
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-ui/src/components/History/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const History = () => {
sortable: true,
},
{
title: t('history.table.amount'),
title: t('history.table.asset'),
dataIndex: 'amount',
align: 'left',
isBalance: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-ui/src/components/Overview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ const Overview = () => {
},
},
{
title: t('overview.amount'),
title: t('overview.asset'),
dataIndex: 'amount',
align: 'left',
isBalance: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-ui/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
"activity": "Activity",
"datetime": "Date & Time",
"status": "Status",
"amount": "Amount",
"asset": "Asset",
"address": "Address",
"sent": "Sent",
"sending": "Sending",
Expand Down Expand Up @@ -313,7 +313,7 @@
"name": "Wallet Name",
"type": "Type",
"balance": "Balance",
"amount": "Amount",
"asset": "Asset",
"timestamp": "Time",
"status": "Status",
"operation": "Operation"
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-ui/src/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
"activity": "Actividad",
"datetime": "Fecha y Hora",
"status": "Estado",
"amount": "Cantidad",
"asset": "Activos",
"address": "Dirección",
"sent": "Enviado",
"sending": "Enviando",
Expand Down Expand Up @@ -305,7 +305,7 @@
"name": "Nombre de la Billetera",
"type": "Tipo",
"balance": "Saldo",
"amount": "Cantidad",
"asset": "activos",
"timestamp": "Tiempo",
"status": "Estado",
"operation": "Operación"
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-ui/src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
"activity": "Activité",
"datetime": "Date et heure",
"status": "Statut",
"amount": "Montant",
"asset": "actifs",
"address": "Adresse",
"sent": "Envoyé",
"sending": "Envoi",
Expand Down Expand Up @@ -312,7 +312,7 @@
"name": "Nom du Wallet",
"type": "Type",
"balance": "Solde",
"amount": "Montant",
"asset": "actifs",
"timestamp": "Heure",
"status": "Statut",
"operation": "Opération"
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-ui/src/locales/zh-tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"activity": "收支活動",
"datetime": "時間",
"status": "狀態",
"amount": "金額",
"asset": "資產",
"address": "地址",
"sent": "已發送",
"sending": "正在發送",
Expand Down Expand Up @@ -308,7 +308,7 @@
"name": "錢包名稱",
"type": "類型",
"balance": "余額",
"amount": "金額",
"asset": "資產",
"timestamp": "時間",
"status": "狀態",
"operation": "操作"
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-ui/src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
"activity": "收支活动",
"datetime": "时间",
"status": "状态",
"amount": "金额",
"asset": "资产",
"address": "地址",
"sent": "已发送",
"sending": "正在发送",
Expand Down Expand Up @@ -306,7 +306,7 @@
"name": "钱包名称",
"type": "类型",
"balance": "余额",
"amount": "金额",
"asset": "资产",
"timestamp": "时间",
"status": "状态",
"operation": "操作"
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-ui/src/types/App/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ declare namespace State {
data: string
}
assetAccountType?: 'CKB' | 'sUDT' | string
daoCapacity?: string
}

interface DetailedInput {
Expand Down
11 changes: 9 additions & 2 deletions packages/neuron-wallet/src/models/chain/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export default class Transaction {
public signatures: Signatures = {}
public assetAccountType?: AssetAccountType

public daoCapacity?: string

constructor(
version: string,
cellDeps: CellDep[] = [],
Expand All @@ -106,7 +108,8 @@ export default class Transaction {
sudtInfo?: SudtInfo,
nftType?: NFTInfo,
signatures: Signatures = {},
assetAccountType?: AssetAccountType
assetAccountType?: AssetAccountType,
daoCapacity?: string
) {
this.cellDeps = cellDeps
this.headerDeps = headerDeps
Expand All @@ -133,6 +136,7 @@ export default class Transaction {
this.sudtInfo = sudtInfo
this.nftInfo = nftType
this.signatures = signatures
this.daoCapacity = daoCapacity
TypeCheckerUtils.hashChecker(...this.headerDeps, this.blockHash)
TypeCheckerUtils.numberChecker(
this.version,
Expand Down Expand Up @@ -171,6 +175,7 @@ export default class Transaction {
nftInfo,
signatures = {},
assetAccountType,
daoCapacity,
}: {
version: string
cellDeps?: CellDep[]
Expand All @@ -196,6 +201,7 @@ export default class Transaction {
nftInfo?: NFTInfo
signatures?: Signatures
assetAccountType?: AssetAccountType
daoCapacity?: string
}): Transaction {
return new Transaction(
version,
Expand Down Expand Up @@ -226,7 +232,8 @@ export default class Transaction {
sudtInfo,
nftInfo,
signatures,
assetAccountType
assetAccountType,
daoCapacity
)
}

Expand Down
38 changes: 32 additions & 6 deletions packages/neuron-wallet/src/services/tx/transaction-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ export class TransactionsService {
.addSelect('input.transactionHash', 'transactionHash')
.addSelect('input.outPointTxHash', 'outPointTxHash')
.addSelect('input.outPointIndex', 'outPointIndex')
.addSelect('input.data', 'data')
.where(
`
input.transactionHash IN (:...txHashes) AND
Expand All @@ -266,7 +267,13 @@ export class TransactionsService {
walletId: params.walletID,
}
)
.getRawMany()
.getRawMany<{
capacity: string
transactionHash: string
outPointTxHash: string
outPointIndex: string
data: string
}>()

const outputs = await connection
.getRepository(OutputEntity)
Expand All @@ -284,7 +291,7 @@ export class TransactionsService {
walletId: params.walletID,
}
)
.getRawMany()
.getRawMany<{ capacity: string; transactionHash: string; daoData: string }>()

const assetAccountInputs = await connection
.getRepository(InputEntity)
Expand Down Expand Up @@ -373,13 +380,17 @@ export class TransactionsService {
).filter(o => inputPreviousTxHashes.includes(o.txHash))

const sums = new Map<string, bigint>()
const daoFlag = new Map<string, boolean>()
const daoInfo = new Map<string, { inputs: typeof inputs; outputs: typeof outputs }>()
outputs.map(o => {
const s = sums.get(o.transactionHash) || BigInt(0)
sums.set(o.transactionHash, s + BigInt(o.capacity))

if (o.daoData) {
daoFlag.set(o.transactionHash, true)
if (daoInfo.has(o.transactionHash)) {
daoInfo.get(o.transactionHash)!.outputs.push(o)
} else {
daoInfo.set(o.transactionHash, { inputs: [], outputs: [o] })
}
}
})

Expand All @@ -391,7 +402,11 @@ export class TransactionsService {
return dc.txHash === i.outPointTxHash && dc.index === i.outPointIndex
})
if (result) {
daoFlag.set(i.transactionHash, true)
if (daoInfo.has(i.transactionHash)) {
daoInfo.get(i.transactionHash)!.inputs.push(i)
} else {
daoInfo.set(i.transactionHash, { inputs: [i], outputs: [] })
}
}
})

Expand Down Expand Up @@ -477,21 +492,32 @@ export class TransactionsService {
nftInfo = { type: NFTType.Receive, data: receiveNFTCell.typeArgs! }
}

const txDaoInfo = daoInfo.get(tx.hash)
let daoCapacity: string | undefined
if (txDaoInfo) {
if (txDaoInfo.inputs.length && !txDaoInfo.outputs.length) {
daoCapacity = txDaoInfo.inputs.reduce((pre, cur) => BigInt(cur.capacity) + pre, BigInt(value)).toString()
} else if (!txDaoInfo.inputs.length && txDaoInfo.outputs.length) {
daoCapacity = `-${txDaoInfo.outputs.reduce((pre, cur) => BigInt(cur.capacity) + pre, BigInt(0)).toString()}`
}
}

return Transaction.fromObject({
timestamp: tx.timestamp,
value: value.toString(),
hash: tx.hash,
version: tx.version,
type: txType,
assetAccountType: assetAccountType,
nervosDao: daoFlag.get(tx.hash!),
nervosDao: !!txDaoInfo,
status: tx.status,
description: tx.description,
createdAt: tx.createdAt,
updatedAt: tx.updatedAt,
blockNumber: tx.blockNumber,
sudtInfo: sudtInfo,
nftInfo: nftInfo,
daoCapacity,
})
})
)
Expand Down

2 comments on commit bed901b

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 9220228007

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 9220227046

Please sign in to comment.