UNPKG

@symbioticfi/relay-stats-ts

Version:

TypeScript library for deriving validator sets from Symbiotic network contracts

38 lines 1.61 kB
export const blockTagFromFinality = (finalized) => finalized ? 'finalized' : 'latest'; export const bigintToBytes = (value, size) => { const hex = value.toString(16).padStart(size * 2, '0'); return Uint8Array.from(Buffer.from(hex, 'hex')); }; export const bigintToBytes32 = (value) => bigintToBytes(value, 32); export const bytesToBigint = (bytes) => { let hex = ''; for (const byte of bytes) { hex += byte.toString(16).padStart(2, '0'); } return BigInt(`0x${hex || '0'}`); }; export const bytesToLimbs = (bytes, limbSize) => { if (limbSize <= 0) return []; const result = []; for (let offset = bytes.length - limbSize; offset >= 0; offset -= limbSize) { result.push(bytesToBigint(bytes.slice(offset, offset + limbSize))); } return result; }; export const sortHexAsc = (items) => [...items].sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0)); export const clampWithinBuffer = (value, buffer, lowerBound, upperBound) => { const min = value > buffer ? value - buffer : lowerBound; const max = value + buffer > upperBound ? upperBound : value + buffer; return max < min ? min : max; }; export const toBlockRange = (fromEstimate, toEstimate, buffer, highestBlock) => { const bufferedFrom = fromEstimate > buffer ? fromEstimate - buffer : 0n; let bufferedTo = toEstimate + buffer; if (bufferedTo > highestBlock) bufferedTo = highestBlock; if (bufferedTo < bufferedFrom) bufferedTo = bufferedFrom; return { fromBlock: bufferedFrom, toBlock: bufferedTo }; }; //# sourceMappingURL=utils.js.map