diff --git a/packages/neuron-ui/src/components/BroadcastTransaction/index.tsx b/packages/neuron-ui/src/components/BroadcastTransaction/index.tsx index bafa4640a3..383d25efb8 100644 --- a/packages/neuron-ui/src/components/BroadcastTransaction/index.tsx +++ b/packages/neuron-ui/src/components/BroadcastTransaction/index.tsx @@ -5,7 +5,7 @@ import { isSuccessResponse, RoutePath, isMainnet as isMainnetUtil, useGoBack, ge import Dialog from 'widgets/Dialog' import AlertDialog from 'widgets/AlertDialog' import { useDispatch, useState as useGlobalState } from 'states' -import { broadcastTransactionOnly, OfflineSignStatus, openExternal, getLiveCells } from 'services/remote' +import { broadcastTransactionOnly, OfflineSignStatus, openExternal, getTransactionList } from 'services/remote' import { ReactComponent as HardWalletIcon } from 'widgets/Icons/HardWallet.svg' import styles from './broadcastTransaction.module.scss' @@ -19,7 +19,7 @@ const BroadcastTransaction = () => { } = useGlobalState() const [isBroadcasting, setIsBroadcasting] = useState(false) - const [broadcastedTxHash, setBroadCastedTxHash] = useState('') + const [broadcastedTxHash, setBroadCastedTxHash] = useState('') const [t] = useTranslation() const dispatch = useDispatch() const [errMsg, setErrMsg] = useState('') @@ -46,29 +46,34 @@ const BroadcastTransaction = () => { setIsBroadcasting(true) - try { - const res = await broadcastTransactionOnly({ - ...json, - }) - if (isSuccessResponse(res)) { - getLiveCells().then(cellRes => { - if (isSuccessResponse(cellRes) && cellRes.result) { - const cellWithTransaction = cellRes.result.find(item => item.outPoint.txHash === res.result) - - if (cellWithTransaction) { - navigate(RoutePath.History) - } - } + const res = await broadcastTransactionOnly({ + ...json, + }) + + setIsBroadcasting(false) + + if (isSuccessResponse(res)) { + if (!wallet?.id) { + setBroadCastedTxHash(res.result) + return + } - if (res.result) { + if (res.result) { + getTransactionList({ + walletID: wallet.id, + pageNo: 1, + pageSize: 1, + keywords: res.result, + }).then(txRes => { + if (isSuccessResponse(txRes) && txRes.result.items.length) { + navigate(RoutePath.History) + } else { setBroadCastedTxHash(res.result) } }) - } else { - setErrMsg(typeof res.message === 'string' ? res.message : res.message.content || '') } - } finally { - setIsBroadcasting(false) + } else { + setErrMsg(typeof res.message === 'string' ? res.message : res.message.content || '') } }, [wallet, json, navigate, dispatch, broadcastedTxHash, setBroadCastedTxHash]) diff --git a/packages/neuron-ui/src/services/remote/offline.ts b/packages/neuron-ui/src/services/remote/offline.ts index 7239493fdd..5ef264228c 100644 --- a/packages/neuron-ui/src/services/remote/offline.ts +++ b/packages/neuron-ui/src/services/remote/offline.ts @@ -34,6 +34,15 @@ export interface OfflineSignJSON { multisig_configs?: MultisigConfigs } +export interface SignedTransaction { + transaction: State.GeneratedTx + status: OfflineSignStatus.Signed + type: OfflineSignType + description?: string + asset_account?: Pick + multisig_configs?: MultisigConfigs +} + export type SignProps = OfflineSignJSON & { walletID: string; password: string; multisigConfig?: MultisigEntity } export type BroadcastProps = OfflineSignJSON & { walletID: string } @@ -41,7 +50,7 @@ export type BroadcastProps = OfflineSignJSON & { walletID: string } export const exportTransactionAsJSON = remoteApi('export-transaction-as-json') export const signTransactionOnly = remoteApi('sign-transaction-only') export const broadcastTransaction = remoteApi('broadcast-transaction') -export const broadcastTransactionOnly = remoteApi('broadcast-transaction-only') +export const broadcastTransactionOnly = remoteApi('broadcast-transaction-only') export const signAndExportTransaction = remoteApi( 'sign-and-export-transaction' ) diff --git a/packages/neuron-wallet/src/controllers/offline-sign.ts b/packages/neuron-wallet/src/controllers/offline-sign.ts index 1ef3b361f0..50756bb8da 100644 --- a/packages/neuron-wallet/src/controllers/offline-sign.ts +++ b/packages/neuron-wallet/src/controllers/offline-sign.ts @@ -3,7 +3,7 @@ import path from 'path' import { dialog } from 'electron' import { t } from 'i18next' import { ResponseCode } from '../utils/const' -import OfflineSign, { SignType, OfflineSignJSON, SignStatus } from '../models/offline-sign' +import OfflineSign, { SignType, OfflineSignJSON, SignStatus, SignedTransaction } from '../models/offline-sign' import TransactionSender from '../services/transaction-sender' import Transaction from '../models/chain/transaction' import AssetAccountController from './asset-account' @@ -197,12 +197,46 @@ export default class OfflineSignController { } } - public async broadcastTransactionOnly({ transaction }: OfflineSignJSON) { + public async broadcastTransactionOnly({ + transaction, + type, + asset_account: assetAccount, + description, + }: SignedTransaction) { const tx = Transaction.fromObject(transaction) - const hash = await new TransactionSender().broadcastTx('', tx) - return { - status: ResponseCode.Success, - result: hash, + switch (type) { + case SignType.CreateSUDTAccount: { + return new AssetAccountController().sendCreateTx( + { + walletID: '', + assetAccount: assetAccount!, + tx, + password: '', + }, + true + ) + } + case SignType.SendSUDT: { + return new AnyoneCanPayController().sendTx( + { + walletID: '', + tx, + password: '', + }, + true + ) + } + default: { + return new WalletsController().sendTx( + { + walletID: '', + tx, + password: '', + description, + }, + true + ) + } } } } diff --git a/packages/neuron-wallet/src/models/offline-sign.ts b/packages/neuron-wallet/src/models/offline-sign.ts index 2fd73a0d93..4bb4946ae8 100644 --- a/packages/neuron-wallet/src/models/offline-sign.ts +++ b/packages/neuron-wallet/src/models/offline-sign.ts @@ -46,6 +46,15 @@ export interface OfflineSignJSON { multisig_configs?: MultisigConfigs } +export interface SignedTransaction { + transaction: Transaction + status: SignStatus.Signed + type: SignType + description?: string + asset_account?: AssetAccount + multisig_configs?: MultisigConfigs +} + export default class OfflineSign implements OfflineSignProps { public transaction: Transaction public assetAccount?: AssetAccount diff --git a/packages/neuron-wallet/src/services/asset-account-service.ts b/packages/neuron-wallet/src/services/asset-account-service.ts index 7e076a4310..1fa719a0c9 100644 --- a/packages/neuron-wallet/src/services/asset-account-service.ts +++ b/packages/neuron-wallet/src/services/asset-account-service.ts @@ -341,7 +341,7 @@ export default class AssetAccountService { [assetAccount.tokenID, assetAccount.blake160] ) - if (exists[0].exist === 1) { + if (exists[0].exist === 1 && walletID) { // For hardware wallet in ckb asset account: // 1. If a ckb account has been created, another one cannot be created; // 2. If a ckb account has been destroyed, ckb account can be created.