Skip to content

fetchNonce: prefer /v2/accounts over extended API for nonce accuracy #1833

@whoabuddy

Description

@whoabuddy

Summary

When making rapid sequential transactions, we observed that /v2/accounts/{address} returns more up-to-date nonce values than /extended/v1/address/{address}/nonces. Preferring /v2/accounts in fetchNonce would improve reliability.

Observed Behavior

# Extended API - returned possible_next_nonce: 4636
curl "https://api.mainnet.hiro.so/extended/v1/address/SPWD5Y6Z7SKANZH6C7BK6A81CRKD6B4G2CG4SQ8W/nonces"

# v2/accounts - returned nonce: 4637 (correct)
curl "https://api.mainnet.hiro.so/v2/accounts/SPWD5Y6Z7SKANZH6C7BK6A81CRKD6B4G2CG4SQ8W"

# Nonce 4636 was already used in a confirmed transaction

Current Code

fetchNonce tries the extended API first:

export async function fetchNonce(opts) {
    try {
        return await _getNonceApi(opts);  // /extended/v1/.../nonces
    } catch (e) {}
    // Falls back to /v2/accounts
}

Suggestion

Swap the order to prefer /v2/accounts:

export async function fetchNonce(opts) {
    try {
        const url = `${client.baseUrl}/v2/accounts/${opts.address}?proof=0`;
        const response = await client.fetch(url);
        const json = await response.json();
        return BigInt(json.nonce);
    } catch (e) {}
    return _getNonceApi(opts);  // fallback
}

Environment

  • @stacks/transactions: 7.x
  • Observed on mainnet

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions