chaingate
Version:
Multi-chain cryptocurrency SDK for TypeScript — unified API for Bitcoin, Ethereum, Litecoin, Dogecoin, Bitcoin Cash, Polygon, Arbitrum, and any EVM-compatible chain. Create wallets, query balances, send transactions, and manage tokens and NFTs across UTXO
37 lines (36 loc) • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EvmNonceCache = void 0;
/**
* In-memory cache of the next nonce to use per (chainId, address).
*
* Bridges the gap between broadcasting a transaction and the network
* reflecting it: when we send tx N, the cache records that the next nonce is
* `N + 1` so a follow-up build does not collide even if the node
* has not yet observed the broadcast.
*/
class EvmNonceCache {
constructor() {
this.nextNonce = new Map();
}
key(chainId, address) {
return `${chainId}:${address.toLowerCase()}`;
}
/** Returns the cached next nonce for an address, or `undefined` if untracked. */
get(chainId, address) {
return this.nextNonce.get(this.key(chainId, address));
}
/**
* Records that `usedNonce` was just consumed by a broadcast. The cache
* advances to `usedNonce + 1` but never goes backwards.
*/
recordUsed(chainId, address, usedNonce) {
const k = this.key(chainId, address);
const next = usedNonce + 1n;
const prev = this.nextNonce.get(k);
if (prev === undefined || next > prev) {
this.nextNonce.set(k, next);
}
}
}
exports.EvmNonceCache = EvmNonceCache;