Skip to content

Commit

Permalink
fix-v4: Fix invalid nonce error on the EVM chain (#3748)
Browse files Browse the repository at this point in the history
* fix: only show pending txs count when the impl is evm

* fix: throw error when get on-chain nonce failed
  • Loading branch information
weatherstar authored Nov 19, 2023
1 parent 7a5c826 commit 81c2cbb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
33 changes: 20 additions & 13 deletions packages/engine/src/vaults/VaultBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
InvalidAddress,
InvalidTokenAddress,
NotImplemented,
OneKeyError,
PendingQueueTooLong,
} from '../errors';
import { getAccountNameInfoByImpl } from '../managers/impl';
Expand Down Expand Up @@ -721,12 +722,15 @@ export abstract class VaultBase extends VaultBaseChainOnly {

async getNextNonce(networkId: string, dbAccount: DBAccount): Promise<number> {
// 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.
Expand All @@ -746,25 +750,28 @@ 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;
}
}
}

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);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/engine/src/vaults/impl/evm/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const settings: IVaultSettings = Object.freeze({
sendNFTEnable: true,
hexDataEditable: true,

showPendingTxsWarning: true,

accountNameInfo: {
default: {
prefix: 'EVM',
Expand Down
1 change: 1 addition & 0 deletions packages/engine/src/vaults/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export type IVaultSettings = {
exportCredentialInfo?: AccountCredential[];
txExtraInfo?: TxExtraInfo[];
enabledInDevModeOnly?: boolean;
showPendingTxsWarning?: boolean;

minTransferAmount?: string;
allowZeroFee?: boolean;
Expand Down
11 changes: 9 additions & 2 deletions packages/kit/src/views/Send/components/BaseSendConfirmModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 81c2cbb

Please sign in to comment.