Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-v4: Fix invalid nonce error on the EVM chain OK-24664 OK-24663 #3748

Merged
merged 3 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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