Skip to content

Commit

Permalink
fix: broadcastTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
devchenyan committed Jan 1, 2024
1 parent 3bb57b7 commit 12c6c7c
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 18 deletions.
43 changes: 36 additions & 7 deletions packages/neuron-ui/src/components/BroadcastTransaction/index.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
import React, { useCallback, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useNavigate } from 'react-router-dom'
import { isSuccessResponse, RoutePath, useDidMount, useGoBack } from 'utils'
import { isSuccessResponse, RoutePath, isMainnet as isMainnetUtil, useDidMount, useGoBack, getExplorerUrl } from 'utils'
import Dialog from 'widgets/Dialog'
import AlertDialog from 'widgets/AlertDialog'
import { useDispatch, useState as useGlobalState } from 'states'
import { broadcastTransaction, getCurrentWallet, OfflineSignStatus } from 'services/remote'
import {
broadcastTransactionOnly,
getCurrentWallet,
OfflineSignStatus,
openExternal,
getLiveCells,
} from 'services/remote'
import { ReactComponent as HardWalletIcon } from 'widgets/Icons/HardWallet.svg'

import styles from './broadcastTransaction.module.scss'

const BroadcastTransaction = () => {
const {
app: { loadedTransaction = {} },
chain: { networkID },
settings: { networks },
} = useGlobalState()

const [wallet, setWallet] = useState<State.Wallet | null>(null)
const [isBroadCasting, setIsBroadcasting] = useState(false)
const [broadCastedTxHash, setBroadCastedTxHash] = useState('')
const [t] = useTranslation()
const dispatch = useDispatch()
const [errMsg, setErrMsg] = useState('')
const isMainnet = isMainnetUtil(networks, networkID)

const { filePath, json } = loadedTransaction

Expand All @@ -35,21 +45,38 @@ const BroadcastTransaction = () => {

const navigate = useNavigate()
const onBroadcast = useCallback(async () => {
if (broadCastedTxHash) {
openExternal(`${getExplorerUrl(isMainnet)}/transaction/${broadCastedTxHash}`)
return
}

setIsBroadcasting(true)

try {
const res = await broadcastTransaction({
const res = await broadcastTransactionOnly({
...json,
walletID: wallet!.id,
})
if (isSuccessResponse(res)) {
navigate(RoutePath.History)
getLiveCells().then(cellRes => {
if (isSuccessResponse(cellRes) && cellRes.result) {
const cellWithTransaction = cellRes.result.find(item => item.outPoint.txHash === res.result)

if (cellWithTransaction) {
navigate(RoutePath.History)
}
}

if (res.result) {
setBroadCastedTxHash(res.result)
}
})
} else {
setErrMsg(typeof res.message === 'string' ? res.message : res.message.content || '')
}
} finally {
setIsBroadcasting(false)
}
}, [wallet, json, navigate, dispatch])
}, [wallet, json, navigate, dispatch, broadCastedTxHash, setBroadCastedTxHash])

useDidMount(() => {
getCurrentWallet().then(res => {
Expand All @@ -66,7 +93,9 @@ const BroadcastTransaction = () => {
title={t('offline-sign.broadcast-transaction')}
cancelText={t('offline-sign.actions.cancel')}
onCancel={onBack}
confirmText={t('offline-sign.actions.broadcast')}
confirmText={
broadCastedTxHash ? t('offline-sign.actions.view-in-explorer') : t('offline-sign.actions.broadcast')
}
isLoading={isBroadCasting}
onConfirm={onBroadcast}
>
Expand Down
9 changes: 5 additions & 4 deletions packages/neuron-ui/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
"partially-signed": "Partially Signed",
"signed": "Signed"
},
"import-signed-transaction": "Import signed transaction",
"import-signed-transaction": "Import a signed transaction",
"import-signed-transaction-detail": "You have imported a signed transaction, please confirm and try again.",
"wallet": "Wallet:",
"content": "Content:",
Expand All @@ -122,11 +122,12 @@
"actions": {
"broadcast": "Broadcast",
"sign": "Sign and export",
"cancel": "Cancel"
"cancel": "Cancel",
"view-in-explorer": "View in Explorer"
},
"broadcast-transaction": "Broadcast Transaction",
"import-unsigned-transaction": "Import unsigned transaction",
"import-unsigned-transaction-detail": "You have imported a unsigned transaction, please confirm and try again."
"import-unsigned-transaction": "Import an unsigned transaction",
"import-unsigned-transaction-detail": "You have imported an unsigned transaction, please confirm and try again."
},
"overview": {
"date": "Date",
Expand Down
3 changes: 2 additions & 1 deletion packages/neuron-ui/src/locales/zh-tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@
"actions": {
"broadcast": "廣播交易",
"sign": "簽名並導出",
"cancel": "取消"
"cancel": "取消",
"view-in-explorer": "在瀏覽器中查看"
},
"broadcast-transaction": "廣播交易",
"import-unsigned-transaction": "導入未簽名交易",
Expand Down
3 changes: 2 additions & 1 deletion packages/neuron-ui/src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@
"actions": {
"broadcast": "广播交易",
"sign": "签名并导出",
"cancel": "取消"
"cancel": "取消",
"view-in-explorer": "在浏览器中查看"
},
"broadcast-transaction": "广播交易",
"import-unsigned-transaction": "导入未签名",
Expand Down
3 changes: 2 additions & 1 deletion packages/neuron-ui/src/services/remote/offline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export type BroadcastProps = OfflineSignJSON & { walletID: string }

export const exportTransactionAsJSON = remoteApi<OfflineSignJSON, void>('export-transaction-as-json')
export const signTransactionOnly = remoteApi<OfflineSignJSON, void>('sign-transaction-only')
export const broadcastTransaction = remoteApi<BroadcastProps, void>('broadcast-transaction-only')
export const broadcastTransaction = remoteApi<BroadcastProps, void>('broadcast-transaction')
export const broadcastTransactionOnly = remoteApi<OfflineSignJSON, string>('broadcast-transaction-only')
export const signAndExportTransaction = remoteApi<SignProps, { filePath: string; json: OfflineSignJSON }>(
'sign-and-export-transaction'
)
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-ui/src/services/remote/remoteApiWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ type Action =
// offline-signature
| 'export-transaction-as-json'
| 'sign-transaction-only'
| 'broadcast-transaction'
| 'broadcast-transaction-only'
| 'sign-and-export-transaction'
| 'sign-and-broadcast-transaction'
Expand Down
6 changes: 5 additions & 1 deletion packages/neuron-wallet/src/controllers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,10 +764,14 @@ export default class ApiController {
return this.#offlineSignController.signTransaction(params)
})

handle('broadcast-transaction-only', async (_, params) => {
handle('broadcast-transaction', async (_, params) => {
return this.#offlineSignController.broadcastTransaction(params)
})

handle('broadcast-transaction-only', async (_, params) => {
return this.#offlineSignController.broadcastTransactionOnly(params)
})

handle('sign-and-export-transaction', async (_, params) => {
return this.#offlineSignController.signAndExportTransaction({
...params,
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/controllers/app/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ const updateApplicationMenu = (mainWindow: BrowserWindow | null) => {
},
{
label: t('application-menu.tools.broadcast-transaction'),
enabled: hasCurrentWallet && !isXpubWallet,
enabled: hasCurrentWallet,
click: async () => {
const result = await OfflineSignService.loadTransactionJSON()
if (!result) {
Expand Down
9 changes: 9 additions & 0 deletions packages/neuron-wallet/src/controllers/offline-sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,13 @@ export default class OfflineSignController {
}
}
}

public async broadcastTransactionOnly({ transaction }: OfflineSignJSON) {
const tx = Transaction.fromObject(transaction)
const hash = await new TransactionSender().broadcastTx('', tx)
return {
status: ResponseCode.Success,
result: hash,
}
}
}
6 changes: 4 additions & 2 deletions packages/neuron-wallet/src/services/transaction-sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ export default class TransactionSender {
await TransactionPersistor.saveSentTx(tx, txHash)
await MultisigService.saveSentMultisigOutput(tx)

const wallet = WalletService.getInstance().get(walletID)
await wallet.checkAndGenerateAddresses()
if (walletID) {
const wallet = WalletService.getInstance().get(walletID)
await wallet.checkAndGenerateAddresses()
}
return txHash
}

Expand Down

1 comment on commit 12c6c7c

@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 7378430152

Please sign in to comment.