Skip to content

Commit

Permalink
feat: Support XUDT
Browse files Browse the repository at this point in the history
  • Loading branch information
yanguoyu committed Jul 9, 2024
1 parent fe2082c commit 523e0fd
Show file tree
Hide file tree
Showing 51 changed files with 882 additions and 440 deletions.
14 changes: 7 additions & 7 deletions packages/neuron-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
"last 2 chrome versions"
],
"dependencies": {
"@ckb-lumos/bi": "0.21.1",
"@ckb-lumos/rpc": "0.21.1",
"@ckb-lumos/base": "0.21.1",
"@ckb-lumos/codec": "0.21.1",
"@ckb-lumos/helpers": "0.21.1",
"@ckb-lumos/config-manager": "0.21.1",
"@ckb-lumos/common-scripts": "0.21.1",
"@ckb-lumos/bi": "0.23.0",
"@ckb-lumos/rpc": "0.23.0",
"@ckb-lumos/base": "0.23.0",
"@ckb-lumos/codec": "0.23.0",
"@ckb-lumos/helpers": "0.23.0",
"@ckb-lumos/config-manager": "0.23.0",
"@ckb-lumos/common-scripts": "0.23.0",
"canvg": "2.0.0",
"i18next": "23.7.11",
"immer": "9.0.21",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
.actions {
display: flex;
gap: 16px;
& > svg {
& svg {
cursor: pointer;
&[data-disabled='true'] {
cursor: not-allowed;
Expand Down
7 changes: 6 additions & 1 deletion packages/neuron-ui/src/components/CellManagement/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { ErrorCode, LockScriptCategory, RoutePath, TypeScriptCategory, isSuccess
import { SortType } from 'widgets/Table'

const cellTypeOrder: Record<string, number> = {
[TypeScriptCategory.SUDT]: 1,
[TypeScriptCategory.SUDT]: 0,
[TypeScriptCategory.XUDT]: 1,
[TypeScriptCategory.NFT]: 2,
[TypeScriptCategory.Spore]: 3,
[TypeScriptCategory.Unknown]: 4,
Expand Down Expand Up @@ -47,6 +48,9 @@ const getLockStatusAndReason = (item: State.LiveCellWithLocalInfo) => {
case TypeScriptCategory.DAO:
lockedReason = { key: 'cell-manage.locked-reason.NFT-SUDT-DAO', params: { type: 'Nervos DAO' } }
break
case TypeScriptCategory.XUDT:
lockedReason = { key: 'cell-manage.locked-reason.NFT-SUDT-DAO', params: { type: 'XUDT' } }
break
case TypeScriptCategory.Unknown:
lockedReason = { key: 'cell-manage.locked-reason.Unknown' }
break
Expand Down Expand Up @@ -82,6 +86,7 @@ const getCellType = (item: State.LiveCellWithLocalInfo) => {
switch (item.typeScriptType) {
case TypeScriptCategory.NFT:
case TypeScriptCategory.SUDT:
case TypeScriptCategory.XUDT:
case TypeScriptCategory.Spore:
case TypeScriptCategory.Unknown:
return item.typeScriptType
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-ui/src/components/CellManagement/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ const getColumns = ({
<Tooltip tip={t('cell-manage.unlock')} showTriangle placement="top">
<UnLock
data-disabled={!!lockedReason}
onClick={onAction}
onClick={lockedReason ? undefined : onAction}
data-action={Actions.Unlock}
data-index={index}
/>
Expand All @@ -202,7 +202,7 @@ const getColumns = ({
<Tooltip tip={t('cell-manage.lock')} showTriangle placement="top">
<LockCell
data-disabled={!!lockedReason}
onClick={onAction}
onClick={lockedReason ? undefined : onAction}
data-action={Actions.Lock}
data-index={index}
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-ui/src/components/NervosDAO/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export const useUpdateDepositEpochList = ({
useEffect(() => {
if (connectionStatus === 'online') {
getBlockHashes(records.map(v => v.depositOutPoint?.txHash).filter(v => !!v) as string[]).then(
(depositBlockHashes: { txHash: string; blockHash: string | null }[]) => {
(depositBlockHashes: { txHash: string; blockHash: string | undefined }[]) => {
const recordKeyIdx: string[] = []
const batchParams: ['getHeader', string][] = []
records.forEach(record => {
Expand Down
10 changes: 8 additions & 2 deletions packages/neuron-ui/src/components/SUDTAccountList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
isSuccessResponse,
useIsInsufficientToCreateSUDTAccount,
useOnGenerateNewAccountTransaction,
UDTType,
} from 'utils'

import { getSUDTAccountList, updateSUDTAccount } from 'services/remote'
Expand All @@ -48,7 +49,11 @@ const SUDTAccountList = () => {
const [keyword, setKeyword] = useState('')
const [dialog, setDialog] = useState<{ id: string; action: 'create' | 'update' } | null>(null)
const [isLoaded, setIsLoaded] = useState(false)
const [insufficient, setInsufficient] = useState({ [AccountType.CKB]: false, [AccountType.SUDT]: false })
const [insufficient, setInsufficient] = useState({
[AccountType.CKB]: false,
[AccountType.SUDT]: false,
[AccountType.XUDT]: false,
})

const isMainnet = isMainnetUtil(networks, networkID)
const [receiveData, setReceiveData] = useState<DataProps | null>(null)
Expand Down Expand Up @@ -178,6 +183,7 @@ const SUDTAccountList = () => {
tokenName: accountToUpdate.tokenName || DEFAULT_SUDT_FIELDS.tokenName,
symbol: accountToUpdate.symbol || DEFAULT_SUDT_FIELDS.symbol,
isCKB: accountToUpdate.tokenId === DEFAULT_SUDT_FIELDS.CKBTokenId,
udtType: accountToUpdate.udtType,
onSubmit: (info: Omit<TokenInfo, 'isCKB'>) => {
const params: any = { id: accountToUpdate.accountId }
Object.keys(info).forEach(key => {
Expand Down Expand Up @@ -208,7 +214,7 @@ const SUDTAccountList = () => {
: undefined

const handleCreateAccount = useCallback(
(info: TokenInfo) => {
(info: TokenInfo & { udtType?: UDTType }) => {
createAccount(info, () => {
setNotice(t('s-udt.create-account-success'))
})
Expand Down
35 changes: 24 additions & 11 deletions packages/neuron-ui/src/components/SUDTCreateDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import {
isSuccessResponse,
useSUDTAccountInfoErrors,
useFetchTokenInfoList,
useOpenSUDTTokenUrl,
useOpenUDTTokenUrl,
UDTType,
} from 'utils'
import { DEFAULT_SUDT_FIELDS } from 'utils/const'
import styles from './sUDTCreateDialog.module.scss'

export enum AccountType {
SUDT = 'sudt',
XUDT = 'xudt',
CKB = 'ckb',
}

Expand All @@ -33,10 +35,10 @@ export interface TokenInfo extends BasicInfo {

export interface SUDTCreateDialogProps extends TokenInfo {
isMainnet: boolean
onSubmit: (info: TokenInfo) => void
onSubmit: (info: TokenInfo & { udtType?: UDTType }) => void
onCancel: () => void
existingAccountNames?: string[]
insufficient?: { [AccountType.CKB]: boolean; [AccountType.SUDT]: boolean }
insufficient?: { [P in AccountType]: boolean }
}

enum DialogSection {
Expand All @@ -49,6 +51,10 @@ const accountTypes: { key: AccountType; label: string }[] = [
key: AccountType.SUDT,
label: 's-udt.create-dialog.sudt-account',
},
{
key: AccountType.XUDT,
label: 's-udt.create-dialog.xudt-account',
},
{
key: AccountType.CKB,
label: 's-udt.create-dialog.ckb-account',
Expand Down Expand Up @@ -121,21 +127,26 @@ const SUDTCreateDialog = ({
onSubmit,
onCancel,
existingAccountNames = [],
insufficient = { [AccountType.CKB]: false, [AccountType.SUDT]: false },
insufficient = { [AccountType.CKB]: false, [AccountType.SUDT]: false, [AccountType.XUDT]: false },
isMainnet,
}: Partial<Omit<SUDTCreateDialogProps, 'onSubmit' | 'onCancel'>> &
Pick<SUDTCreateDialogProps, 'onSubmit' | 'onCancel' | 'insufficient'>) => {
const [t] = useTranslation()
const [info, dispatch] = useReducer(reducer, { accountName, tokenId, tokenName, symbol, decimal })
const [accountType, setAccountType] = useState([AccountType.SUDT, AccountType.CKB].find(at => !insufficient[at]))
const [accountType, setAccountType] = useState(
[AccountType.SUDT, AccountType.CKB, AccountType.XUDT].find(at => !insufficient[at])
)
const isUDT = accountType === AccountType.SUDT || accountType === AccountType.XUDT
// eslint-disable-next-line no-nested-ternary
const udtType = isUDT ? (accountType === AccountType.SUDT ? UDTType.SUDT : UDTType.XUDT) : undefined
const [step, setStep] = useState(DialogSection.Account)
const tokenInfoList = useFetchTokenInfoList()

const tokenInfoFields: (keyof TokenInfo)[] = ['tokenId', 'tokenName', 'symbol', 'decimal']

const tokenErrors = useSUDTAccountInfoErrors({
info,
isCKB: AccountType.CKB === accountType,
isCKB: !isUDT,
existingAccountNames,
t,
})
Expand Down Expand Up @@ -190,7 +201,7 @@ const SUDTCreateDialog = ({
}
case DialogSection.Token: {
if (isTokenReady) {
onSubmit({ ...info, accountName: info.accountName.trim(), tokenName: info.tokenName.trim() })
onSubmit({ ...info, udtType, accountName: info.accountName.trim(), tokenName: info.tokenName.trim() })
}
break
}
Expand All @@ -216,7 +227,7 @@ const SUDTCreateDialog = ({
}
}
}
const openSUDTTokenUrl = useOpenSUDTTokenUrl(info.tokenId, isMainnet)
const openSUDTTokenUrl = useOpenUDTTokenUrl(info.tokenId, udtType, isMainnet)
return (
<Dialog
show
Expand Down Expand Up @@ -285,14 +296,16 @@ const SUDTCreateDialog = ({
className={styles.settingField}
placeholder={t(`s-udt.create-dialog.input.${field.label}`)}
hint={
!tokenErrors[field.key] && field.key === 'tokenId' && accountType === AccountType.SUDT
? t(`s-udt.create-dialog.placeholder.${field.label}`)
!tokenErrors[field.key] && field.key === 'tokenId' && isUDT
? t(`s-udt.create-dialog.placeholder.${field.label}`, {
udtType: AccountType.SUDT === accountType ? 'sUDT' : 'xUDT',
})
: undefined
}
/>
))}
</div>
{accountType === AccountType.SUDT && !tokenErrors.tokenId && info.tokenId && (
{isUDT && !tokenErrors.tokenId && info.tokenId && (
<button
type="button"
className={styles.explorerNavButton}
Expand Down
8 changes: 5 additions & 3 deletions packages/neuron-ui/src/components/SUDTMigrateDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useTranslation } from 'react-i18next'
import { SpecialAssetCell } from 'components/SpecialAssetList/hooks'
import { MIN_CKB_REQUIRED_BY_NORMAL_SUDT, SHANNON_CKB_RATIO } from 'utils/const'
import Dialog from 'widgets/Dialog'
import { PresetScript, UDTType } from 'utils'
import styles from './sUDTMigrateDialog.module.scss'

const items = [
Expand Down Expand Up @@ -38,12 +39,13 @@ const SUDTMigrateDialog = ({
setType('')
onCancel()
}
const udtType = cell.customizedAssetInfo.type === PresetScript.SUDT ? UDTType.SUDT : UDTType.XUDT

return (
<Dialog
className={styles.container}
show
title={t('migrate-sudt.title')}
title={t('migrate-sudt.title', { udtType })}
onCancel={handleCancel}
cancelText={t('migrate-sudt.cancel')}
confirmText={t('migrate-sudt.next')}
Expand All @@ -64,8 +66,8 @@ const SUDTMigrateDialog = ({
role="button"
tabIndex={idx}
>
<div className={styles.title}>{t(v.title)}</div>
<div className={styles.subTitle}>{t(v.subTitle)}</div>
<div className={styles.title}>{t(v.title, { udtType })}</div>
<div className={styles.subTitle}>{t(v.subTitle, { udtType })}</div>
</div>
))}
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTranslation } from 'react-i18next'
import { SpecialAssetCell } from 'components/SpecialAssetList/hooks'
import TextField from 'widgets/TextField'
import Dialog from 'widgets/Dialog'
import { getSUDTAmount, isSuccessResponse } from 'utils'
import { getSUDTAmount, getUdtType, isSuccessResponse } from 'utils'
import { generateSudtMigrateAcpTx } from 'services/remote'
import { AppActions, showGlobalAlertDialog, useDispatch } from 'states'
import { useTokenInfo, TokenInfoType } from './hooks'
Expand Down Expand Up @@ -43,6 +43,7 @@ const SUDTMigrateToNewAccountDialog = ({
tokenId: cell.type?.args,
sUDTAccounts,
})
const udtType = getUdtType(cell.type)

const confirmDisabled = useMemo(
() => fields.some(v => (tokenInfoErrors[v.key] || !tokenInfo[v.key]) && v.key !== 'balance'),
Expand Down Expand Up @@ -121,7 +122,7 @@ const SUDTMigrateToNewAccountDialog = ({
<Dialog
className={styles.container}
show
title={t('migrate-sudt.turn-into-new-account.title')}
title={t('migrate-sudt.turn-into-new-account.title', { udtType })}
onCancel={onBack}
cancelText={t('migrate-sudt.cancel')}
confirmText={t('migrate-sudt.confirm')}
Expand Down
10 changes: 6 additions & 4 deletions packages/neuron-ui/src/components/SUDTUpdateDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React, { useReducer, useMemo, useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import { destroyAssetAccount } from 'services/remote'
import { useState as useGlobalState, useDispatch, AppActions } from 'states'
import { isSuccessResponse } from 'utils'
import { UDTType, isSuccessResponse } from 'utils'
import TextField from 'widgets/TextField'
import Dialog from 'widgets/Dialog'
import Tooltip from 'widgets/Tooltip'
import { ReactComponent as Delete } from 'widgets/Icons/Delete.svg'
import { ReactComponent as ExplorerIcon } from 'widgets/Icons/ExplorerIcon.svg'
import { useSUDTAccountInfoErrors, useOpenSUDTTokenUrl } from 'utils/hooks'
import { useSUDTAccountInfoErrors, useOpenUDTTokenUrl } from 'utils/hooks'
import styles from './sUDTUpdateDialog.module.scss'

export interface BasicInfo {
Expand All @@ -30,6 +30,7 @@ export interface SUDTUpdateDialogProps extends TokenInfo {
onCancel: () => void
existingAccountNames: string[]
balance: string
udtType?: UDTType
}

const fields: { key: keyof Omit<TokenInfo, 'accountId' | 'isCKB'>; label: string }[] = [
Expand Down Expand Up @@ -78,6 +79,7 @@ const SUDTUpdateDialog = ({
isCKB = false,
existingAccountNames = [],
balance,
udtType,
}: SUDTUpdateDialogProps) => {
const {
wallet: { id: walletId },
Expand Down Expand Up @@ -106,7 +108,7 @@ const SUDTUpdateDialog = ({
onSubmit({ ...info, accountName: info.accountName.trim(), tokenName: info.tokenName.trim(), accountId })
}
}
const openSUDTTokenUrl = useOpenSUDTTokenUrl(info.tokenId, isMainnet)
const openUDTTokenUrl = useOpenUDTTokenUrl(info.tokenId, udtType, isMainnet)

const onDestroy = useCallback(() => {
destroyAssetAccount({ walletID: walletId, id: accountId! }).then(res => {
Expand Down Expand Up @@ -184,7 +186,7 @@ const SUDTUpdateDialog = ({
})}
</div>
{!isCKB && !tokenErrors.tokenId && info.tokenId && (
<button type="button" className={styles.explorerNavButton} onClick={openSUDTTokenUrl}>
<button type="button" className={styles.explorerNavButton} onClick={openUDTTokenUrl}>
<ExplorerIcon /> {t('history.view-in-explorer-button-title')}
</button>
)}
Expand Down
16 changes: 9 additions & 7 deletions packages/neuron-ui/src/components/SpecialAssetList/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,6 @@ export const useSpecialAssetColumnInfo = ({
}
break
}
case PresetScript.SUDT: {
status = 'user-defined-token'
const tokenInfo = tokenInfoList.find(info => info.tokenID === type?.args)
const amountInfo = getSUDTAmount({ tokenInfo, data })
amount = amountInfo.amount
break
}
default: {
// ignore
}
Expand Down Expand Up @@ -220,6 +213,14 @@ export const useSpecialAssetColumnInfo = ({
amount = t('special-assets.unknown-asset')
break
}
case PresetScript.XUDT:
case PresetScript.SUDT: {
status = 'user-defined-token'
const tokenInfo = tokenInfoList.find(info => info.tokenID === type?.args)
const amountInfo = getSUDTAmount({ tokenInfo, data })
amount = amountInfo.amount
break
}
default: {
break
}
Expand All @@ -235,6 +236,7 @@ export const useSpecialAssetColumnInfo = ({
epochsInfo,
isSpore,
sporeClusterInfo,
udtType: assetInfo.type,
}
},
[epoch, bestKnownBlockTimestamp, tokenInfoList, t]
Expand Down
Loading

0 comments on commit 523e0fd

Please sign in to comment.