harb/kraiken-lib/src/ids.ts
johba 09c36f2c87 lint/lib (#49)
resolves #42

Co-authored-by: johba <johba@harb.eth>
Reviewed-on: https://codeberg.org/johba/harb/pulls/49
2025-10-03 11:57:01 +02:00

18 lines
601 B
TypeScript

import { bytesToUint256LittleEndian } from './subgraph.js';
export function toBigIntId(id: string | Uint8Array | number | bigint): bigint {
if (typeof id === 'bigint') return id;
if (typeof id === 'number') return BigInt(id);
if (typeof id === 'string') {
const trimmed = id.startsWith('0x') ? id : `0x${id}`;
return BigInt(trimmed);
}
if (id instanceof Uint8Array) {
return bytesToUint256LittleEndian(id);
}
throw new Error('Unsupported position id type');
}
export function decodePositionId(id: string | Uint8Array | number | bigint): bigint {
return toBigIntId(id);
}