Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove BigInt and replace with JSBI #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
27 changes: 14 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/readBig.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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]
Expand All @@ -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
}
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"module": "commonjs",
"declaration": true,
"outDir": "./lib",
"strict": true
"strict": true,
"esModuleInterop": true
},
"include": ["src"],
"exclude": ["node_modules", "**/__tests__/*"]
}
}