Skip to content

Commit

Permalink
fix: broadcastTransactionOnly
Browse files Browse the repository at this point in the history
  • Loading branch information
devchenyan committed Jan 13, 2024
1 parent 994dc94 commit b245751
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 28 deletions.
45 changes: 25 additions & 20 deletions packages/neuron-ui/src/components/BroadcastTransaction/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -19,7 +19,7 @@ const BroadcastTransaction = () => {
} = useGlobalState()

const [isBroadcasting, setIsBroadcasting] = useState(false)
const [broadcastedTxHash, setBroadCastedTxHash] = useState('')
const [broadcastedTxHash, setBroadCastedTxHash] = useState<string | null>('')
const [t] = useTranslation()
const dispatch = useDispatch()
const [errMsg, setErrMsg] = useState('')
Expand All @@ -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])

Expand Down
11 changes: 10 additions & 1 deletion packages/neuron-ui/src/services/remote/offline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,23 @@ export interface OfflineSignJSON {
multisig_configs?: MultisigConfigs
}

export interface SignedTransaction {
transaction: State.GeneratedTx
status: OfflineSignStatus.Signed
type: OfflineSignType
description?: string
asset_account?: Pick<Controller.SUDTAccount, 'symbol' | 'tokenName' | 'accountName' | 'decimal' | 'tokenID'>
multisig_configs?: MultisigConfigs
}

export type SignProps = OfflineSignJSON & { walletID: string; password: string; multisigConfig?: MultisigEntity }

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')
export const broadcastTransactionOnly = remoteApi<OfflineSignJSON, string>('broadcast-transaction-only')
export const broadcastTransactionOnly = remoteApi<SignedTransaction, string>('broadcast-transaction-only')
export const signAndExportTransaction = remoteApi<SignProps, { filePath: string; json: OfflineSignJSON }>(
'sign-and-export-transaction'
)
Expand Down
46 changes: 40 additions & 6 deletions packages/neuron-wallet/src/controllers/offline-sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
)
}
}
}
}
9 changes: 9 additions & 0 deletions packages/neuron-wallet/src/models/offline-sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

1 comment on commit b245751

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

Please sign in to comment.