UNPKG

@d3or/slotseek

Version:

A library for finding the storage slots on an ERC20 token for balances and approvals, which can be used to mock the balances and approvals of an address when estimating gas costs of transactions that would fail if the address did not have the required bal

64 lines 2.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.balanceCache = exports.approvalCache = void 0; // check every minute const CACHE_INTERVAL = 60 * 1000; exports.approvalCache = new Map(); exports.balanceCache = new Map(); const clearCacheJob = (type) => { // 1mb per map const totalMaxSize = 1000000; const cache = type === 'balance' ? exports.balanceCache : exports.approvalCache; let cacheSize = getMapSizeInBytes(cache); const diff = cacheSize - totalMaxSize; if (diff < 0) return; // Convert to array and sort in one pass const sortedEntries = Array.from(cache.entries()) .sort((a, b) => a[1].ts - b[1].ts); let index = 0; while (cacheSize > totalMaxSize && index < sortedEntries.length) { const [key, value] = sortedEntries[index]; const entrySize = getObjectSize(key) + getObjectSize(value); cache.delete(key); cacheSize -= entrySize; index++; } }; const getMapSizeInBytes = (map) => { let totalSize = 0; for (const [key, value] of map) { totalSize += getObjectSize(key); totalSize += getObjectSize(value); } // Add overhead for the Map structure itself totalSize += 8 * map.size; // Assuming 8 bytes per entry for internal structure return totalSize; }; const getObjectSize = (obj) => { const type = typeof obj; switch (type) { case 'number': return 8; case 'string': return obj.length * 2; case 'boolean': return 4; case 'object': if (obj === null) { return 0; } let size = 0; for (const key in obj) { if (obj.hasOwnProperty(key)) { size += getObjectSize(key); size += getObjectSize(obj[key]); } } return size; default: return 0; } }; setInterval(clearCacheJob, CACHE_INTERVAL); //# sourceMappingURL=cache.js.map