From 81c2cbbad7244cb408fd06d086a44ba8ebf6123b Mon Sep 17 00:00:00 2001 From: weatherstar Date: Sun, 19 Nov 2023 21:38:38 +0800 Subject: [PATCH] fix-v4: Fix invalid nonce error on the EVM chain (#3748) * fix: only show pending txs count when the impl is evm * fix: throw error when get on-chain nonce failed --- packages/engine/src/vaults/VaultBase.ts | 33 +++++++++++-------- .../engine/src/vaults/impl/evm/settings.ts | 2 ++ packages/engine/src/vaults/types.ts | 1 + .../Send/components/BaseSendConfirmModal.tsx | 11 +++++-- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/packages/engine/src/vaults/VaultBase.ts b/packages/engine/src/vaults/VaultBase.ts index b2aeead8a12..700b87b9863 100644 --- a/packages/engine/src/vaults/VaultBase.ts +++ b/packages/engine/src/vaults/VaultBase.ts @@ -24,6 +24,7 @@ import { InvalidAddress, InvalidTokenAddress, NotImplemented, + OneKeyError, PendingQueueTooLong, } from '../errors'; import { getAccountNameInfoByImpl } from '../managers/impl'; @@ -721,12 +722,15 @@ export abstract class VaultBase extends VaultBaseChainOnly { async getNextNonce(networkId: string, dbAccount: DBAccount): Promise { // TODO move to Vault.getOnChainNextNonce - const onChainNonce = - ( - await this.engine.providerManager.getAddresses(networkId, [ - dbAccount.address, - ]) - )[0]?.nonce ?? 0; + const resp = await this.engine.providerManager.getAddresses(networkId, [ + dbAccount.address, + ]); + + const onChainNextNonce = resp[0]?.nonce; + + if (isNil(onChainNextNonce)) { + throw new OneKeyError('Get on-chain nonce failed.'); + } // TODO: Although 100 history items should be enough to cover all the // pending transactions, we need to find a more reliable way. @@ -746,13 +750,13 @@ export abstract class VaultBase extends VaultBaseChainOnly { }); let nextNonce = Math.max( isNil(maxPendingNonce) ? 0 : maxPendingNonce + 1, - onChainNonce, + onChainNextNonce, ); if (Number.isNaN(nextNonce)) { - nextNonce = onChainNonce; + nextNonce = onChainNextNonce; } - if (nextNonce > onChainNonce) { - for (let i = onChainNonce; i < nextNonce; i += 1) { + if (nextNonce > onChainNextNonce) { + for (let i = onChainNextNonce; i < nextNonce; i += 1) { if (!pendingNonceList.includes(i)) { nextNonce = i; break; @@ -760,11 +764,14 @@ export abstract class VaultBase extends VaultBaseChainOnly { } } - if (nextNonce < onChainNonce) { - nextNonce = onChainNonce; + if (nextNonce < onChainNextNonce) { + nextNonce = onChainNextNonce; } - if (nextNonce - onChainNonce >= HISTORY_CONSTS.PENDING_QUEUE_MAX_LENGTH) { + if ( + nextNonce - onChainNextNonce >= + HISTORY_CONSTS.PENDING_QUEUE_MAX_LENGTH + ) { throw new PendingQueueTooLong(HISTORY_CONSTS.PENDING_QUEUE_MAX_LENGTH); } diff --git a/packages/engine/src/vaults/impl/evm/settings.ts b/packages/engine/src/vaults/impl/evm/settings.ts index 562fa0dabf1..481ed6837c5 100644 --- a/packages/engine/src/vaults/impl/evm/settings.ts +++ b/packages/engine/src/vaults/impl/evm/settings.ts @@ -43,6 +43,8 @@ const settings: IVaultSettings = Object.freeze({ sendNFTEnable: true, hexDataEditable: true, + showPendingTxsWarning: true, + accountNameInfo: { default: { prefix: 'EVM', diff --git a/packages/engine/src/vaults/types.ts b/packages/engine/src/vaults/types.ts index 5d104533825..d5150399492 100644 --- a/packages/engine/src/vaults/types.ts +++ b/packages/engine/src/vaults/types.ts @@ -91,6 +91,7 @@ export type IVaultSettings = { exportCredentialInfo?: AccountCredential[]; txExtraInfo?: TxExtraInfo[]; enabledInDevModeOnly?: boolean; + showPendingTxsWarning?: boolean; minTransferAmount?: string; allowZeroFee?: boolean; diff --git a/packages/kit/src/views/Send/components/BaseSendConfirmModal.tsx b/packages/kit/src/views/Send/components/BaseSendConfirmModal.tsx index 99674cfb344..43876adee51 100644 --- a/packages/kit/src/views/Send/components/BaseSendConfirmModal.tsx +++ b/packages/kit/src/views/Send/components/BaseSendConfirmModal.tsx @@ -260,8 +260,15 @@ export function BaseSendConfirmModal(props: ITxConfirmViewProps) { setPendingTxCount(count); }; - getPendingTxCount(); - }, [accountId, advancedSettings?.currentNonce, networkId]); + if (network?.settings.showPendingTxsWarning) { + getPendingTxCount(); + } + }, [ + accountId, + advancedSettings?.currentNonce, + network?.settings.showPendingTxsWarning, + networkId, + ]); useEffect(() => { const checkPendingTxWithSameNonce = async () => {