Skip to content

Commit

Permalink
fix: throw error when get on-chain nonce failed
Browse files Browse the repository at this point in the history
  • Loading branch information
weatherstar committed Nov 1, 2023
1 parent df35f30 commit 260a1bb
Showing 1 changed file with 20 additions and 13 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

0 comments on commit 260a1bb

Please sign in to comment.