diff --git a/package-lock.json b/package-lock.json index 5290ffc..9aa4f17 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,8 @@ "dependencies": { "@solana/web3.js": "^1.10.1", "assert": "^2.0.0", - "buffer": "^6.0.1" + "buffer": "^6.0.1", + "jsbi": "^3.1.5" }, "devDependencies": { "@types/jest": "^26.0.23", @@ -4223,6 +4224,11 @@ "js-yaml": "bin/js-yaml.js" } }, + "node_modules/jsbi": { + "version": "3.1.5", + "resolved": "https://registry.npmjs.org/jsbi/-/jsbi-3.1.5.tgz", + "integrity": "sha512-w2BY0VOYC1ahe+w6Qhl4SFoPvPsZ9NPHY4bwass+LCgU7RK3PBoVQlQ3G1s7vI8W3CYyJiEXcbKF7FIM/L8q3Q==" + }, "node_modules/jsdom": { "version": "16.6.0", "resolved": "https://registry.npmjs.org/jsdom/-/jsdom-16.6.0.tgz", @@ -10458,6 +10464,11 @@ "esprima": "^4.0.0" } }, + "jsbi": { + "version": "3.1.5", + "resolved": "https://registry.npmjs.org/jsbi/-/jsbi-3.1.5.tgz", + "integrity": "sha512-w2BY0VOYC1ahe+w6Qhl4SFoPvPsZ9NPHY4bwass+LCgU7RK3PBoVQlQ3G1s7vI8W3CYyJiEXcbKF7FIM/L8q3Q==" + }, "jsdom": { "version": "16.6.0", "resolved": "https://registry.npmjs.org/jsdom/-/jsdom-16.6.0.tgz", diff --git a/package.json b/package.json index 7771bfd..d11574d 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "dependencies": { "@solana/web3.js": "^1.10.1", "assert": "^2.0.0", - "buffer": "^6.0.1" + "buffer": "^6.0.1", + "jsbi": "^3.1.5" } } diff --git a/src/index.ts b/src/index.ts index 03d86f8..2ddf2ea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import { PublicKey } from '@solana/web3.js' import { Buffer } from 'buffer' +import JSBI from 'jsbi' import { readBigInt64LE, readBigUInt64LE } from './readBig' export const Magic = 0xa1b2c3d4 @@ -39,13 +40,13 @@ export interface ProductData extends Base { } export interface Price { - priceComponent: bigint + priceComponent: JSBI price: number - confidenceComponent: bigint + confidenceComponent: JSBI confidence: number status: number corporateAction: number - publishSlot: bigint + publishSlot: JSBI } export interface PriceComponent { @@ -58,23 +59,23 @@ export interface PriceData extends Base, Price { priceType: number exponent: number numComponentPrices: number - currentSlot: bigint - validSlot: bigint - twapComponent: bigint + currentSlot: JSBI + validSlot: JSBI + twapComponent: JSBI twap: number - avolComponent: bigint + avolComponent: JSBI avol: number - drv0Component: bigint + drv0Component: JSBI drv0: number - drv1Component: bigint + drv1Component: JSBI drv1: number - drv2Component: bigint + drv2Component: JSBI drv2: number - drv3Component: bigint + drv3Component: JSBI drv3: number - drv4Component: bigint + drv4Component: JSBI drv4: number - drv5Component: bigint + drv5Component: JSBI drv5: number productAccountKey: PublicKey nextPriceAccountKey: PublicKey | null diff --git a/src/readBig.ts b/src/readBig.ts index 5ec754f..a409801 100644 --- a/src/readBig.ts +++ b/src/readBig.ts @@ -1,4 +1,4 @@ -import { Buffer } from 'buffer' +import JSBI from 'jsbi' // https://github.com/nodejs/node/blob/v14.17.0/lib/internal/errors.js#L758 const ERR_BUFFER_OUT_OF_BOUNDS = () => new Error('Attempt to access memory outside buffer bounds') @@ -29,21 +29,21 @@ function boundsError(value: number, length: number) { } // https://github.com/nodejs/node/blob/v14.17.0/lib/internal/buffer.js#L129-L145 -export function readBigInt64LE(buffer: Buffer, offset = 0): bigint { +export function readBigInt64LE(buffer: Buffer, offset = 0): JSBI { validateNumber(offset, 'offset') const first = buffer[offset] const last = buffer[offset + 7] if (first === undefined || last === undefined) boundsError(offset, buffer.length - 8) // tslint:disable-next-line:no-bitwise const val = buffer[offset + 4] + buffer[offset + 5] * 2 ** 8 + buffer[offset + 6] * 2 ** 16 + (last << 24) // Overflow - return ( - (BigInt(val) << BigInt(32)) + // tslint:disable-line:no-bitwise - BigInt(first + buffer[++offset] * 2 ** 8 + buffer[++offset] * 2 ** 16 + buffer[++offset] * 2 ** 24) + return JSBI.add( + JSBI.leftShift(JSBI.BigInt(val), JSBI.BigInt(32)), // tslint:disable-line:no-bitwise + JSBI.BigInt(first + buffer[++offset] * 2 ** 8 + buffer[++offset] * 2 ** 16 + buffer[++offset] * 2 ** 24), ) } // https://github.com/nodejs/node/blob/v14.17.0/lib/internal/buffer.js#L89-L107 -export function readBigUInt64LE(buffer: Buffer, offset = 0): bigint { +export function readBigUInt64LE(buffer: Buffer, offset = 0): JSBI { validateNumber(offset, 'offset') const first = buffer[offset] const last = buffer[offset + 7] @@ -53,5 +53,5 @@ export function readBigUInt64LE(buffer: Buffer, offset = 0): bigint { const hi = buffer[++offset] + buffer[++offset] * 2 ** 8 + buffer[++offset] * 2 ** 16 + last * 2 ** 24 - return BigInt(lo) + (BigInt(hi) << BigInt(32)) // tslint:disable-line:no-bitwise + return JSBI.add(JSBI.BigInt(lo), JSBI.leftShift(JSBI.BigInt(hi), JSBI.BigInt(32))) // tslint:disable-line:no-bitwise } diff --git a/tsconfig.json b/tsconfig.json index e305280..1e8cfdd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,8 +4,9 @@ "module": "commonjs", "declaration": true, "outDir": "./lib", - "strict": true + "strict": true, + "esModuleInterop": true }, "include": ["src"], "exclude": ["node_modules", "**/__tests__/*"] -} \ No newline at end of file +}