Skip to content

Commit af95b18

Browse files
committed
v0.18.2: added num to buffer conversions for program ixs.
1 parent c8e1682 commit af95b18

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

README.md

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
# Tensor Common package
1+
# Tensor Common
22

3-
We place all things that needs to be shared b/w front-end & backend.
4-
5-
This means we should be very choosey what goes here and only include stuff that makes sense to include everywhere.
6-
7-
## Development
8-
9-
Make sure to ugprade version in `package.json` for new changes that may be breaking.
10-
11-
### Add to another project
3+
## Installation
124

135
```sh
14-
yarn add tensor-hq/tensor-common
6+
npm i @tensor-hq/tensor-common
7+
yarn add @tensor-hq/tensor-common
158
```

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tensor-hq/tensor-common",
3-
"version": "0.18.1",
3+
"version": "0.18.2",
44
"description": "Common utility methods used by Tensor.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/solana_contrib/utils.ts

+37
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,40 @@ export const getAccountRent = (
1919
// based on https://docs.solana.com/developing/programming-model/accounts#:~:text=The%20current%20maximum%20size%20of,per%20account%20and%20per%20instruction.
2020
export const getAccountRentSync = (dataSize: number) =>
2121
Math.trunc(19.055441478439427 * (128 + dataSize) * 365.25);
22+
23+
export const numToi32Bytes = (num: number) => {
24+
// Ensure number is a 32-bit signed integer
25+
if (num > 2147483647 || num < -2147483648) {
26+
throw new Error('Number out of range for i32');
27+
}
28+
29+
const bytes = new Uint8Array(4);
30+
for (let i = 0; i < 4; i++) {
31+
// Take the lowest 8 bits of the number and store them in the current byte
32+
bytes[i] = num & 0xff;
33+
// Right-shift the number by 8 bits to prepare for the next byte extraction
34+
num >>= 8;
35+
}
36+
return bytes;
37+
};
38+
39+
export const numToU64Bytes = (num: number) => {
40+
const buffer = new ArrayBuffer(8);
41+
const view = new DataView(buffer);
42+
view.setBigUint64(0, BigInt(num), true); // true for little endian
43+
return new Uint8Array(buffer);
44+
};
45+
46+
export const numToU32Bytes = (num: number) => {
47+
const buffer = new ArrayBuffer(4);
48+
const view = new DataView(buffer);
49+
view.setUint32(0, num, true); // true for little endian
50+
return new Uint8Array(buffer);
51+
};
52+
53+
export const numToU8Bytes = (num: number) => {
54+
const buffer = new ArrayBuffer(1);
55+
const view = new DataView(buffer);
56+
view.setUint8(0, num);
57+
return new Uint8Array(buffer);
58+
};

0 commit comments

Comments
 (0)